Skip to content
This repository was archived by the owner on Dec 5, 2019. It is now read-only.

Commit 2c06f46

Browse files
evilebottnawimichael-ciniawsky
authored andcommitted
fix: pass of {Boolean|Function} parameters being ignored (options.extractComments) (#168)
1 parent 2af1e61 commit 2c06f46

File tree

4 files changed

+249
-7
lines changed

4 files changed

+249
-7
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ All comments that match the given expression (resp. are evaluated to `true` by t
209209
|Name|Type|Default|Description|
210210
|:--:|:--:|:-----:|:----------|
211211
|**`condition`**|`{Regex\|Function}`|``|Regular Expression or function (see previous point)|
212-
|**`filename`**|`{String\|Function}`|`compilation.assets[file]`|The file where the extracted comments will be stored. Can be either a `{String}` or a `{Function<(string) -> {String}>}`, which will be given the original filename. Default is to append the suffix `.LICENSE` to the original filename|
212+
|**`filename`**|`{String\|Function}`|`${file}.LICENSE`|The file where the extracted comments will be stored. Can be either a `{String}` or a `{Function<(string) -> {String}>}`, which will be given the original filename. Default is to append the suffix `.LICENSE` to the original filename|
213213
|**`banner`**|`{Boolean\|String\|Function}`|`/*! For license information please see ${filename}.js.LICENSE */`|The banner text that points to the extracted file and will be added on top of the original file. Can be `false` (no banner), a `{String}`, or a `{Function<(string) -> {String}` that will be called with the filename where extracted comments have been stored. Will be wrapped into comment|
214214

215215
### `warningsFilter`

Diff for: src/uglify/minify.js

+4
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,16 @@ const buildComments = (options, uglifyOptions, extractedComments) => {
3838
const commentsOpts = uglifyOptions.output.comments;
3939

4040
if (
41+
typeof options.extractComments === 'boolean' ||
4142
typeof options.extractComments === 'string' ||
4243
options.extractComments instanceof RegExp
4344
) {
4445
// extractComments specifies the extract condition and commentsOpts specifies the preserve condition
4546
condition.preserve = commentsOpts;
4647
condition.extract = options.extractComments;
48+
} else if (typeof options.extractComments === 'function') {
49+
condition.preserve = false;
50+
condition.extract = options.extractComments;
4751
} else if (
4852
Object.prototype.hasOwnProperty.call(options.extractComments, 'condition')
4953
) {
+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`errors 1`] = `Array []`;
4+
5+
exports[`errors 2`] = `Array []`;
6+
7+
exports[`errors 3`] = `Array []`;
8+
9+
exports[`errors 4`] = `Array []`;
10+
11+
exports[`errors 5`] = `Array []`;
12+
13+
exports[`test.js 1`] = `
14+
"/*! For license information please see test.js.LICENSE */
15+
var foo=1;"
16+
`;
17+
18+
exports[`test.js 2`] = `"var foo=1;"`;
19+
20+
exports[`test.js 3`] = `
21+
"/*! For license information please see test.js.LICENSE */
22+
var foo=1;"
23+
`;
24+
25+
exports[`test.js 4`] = `
26+
"/*! For license information please see test.js.LICENSE */
27+
var foo=1;"
28+
`;
29+
30+
exports[`test.js 5`] = `
31+
"/*! For license information please see test1.js.LICENSE */
32+
var foo=1;"
33+
`;
34+
35+
exports[`test.js 6`] = `
36+
"/*! License information can be found in test.license.js */
37+
var foo=1;"
38+
`;
39+
40+
exports[`test.js.LICENSE 1`] = `
41+
"// Comment
42+
"
43+
`;
44+
45+
exports[`test.js.LICENSE 2`] = `
46+
"// Comment
47+
"
48+
`;
49+
50+
exports[`test.license.js 1`] = `
51+
"// Comment
52+
"
53+
`;
54+
55+
exports[`test1.js 1`] = `
56+
"/*! For license information please see test1.js.LICENSE */
57+
var foo=1;"
58+
`;
59+
60+
exports[`test1.js 2`] = `
61+
"/*! For license information please see test1.js.LICENSE */
62+
var foo=1;"
63+
`;
64+
65+
exports[`test1.js 3`] = `
66+
"/*! For license information please see test1.js.LICENSE */
67+
var foo=1;"
68+
`;
69+
70+
exports[`test1.js.LICENSE 1`] = `
71+
"/* Comment */
72+
"
73+
`;
74+
75+
exports[`test1.js.LICENSE 2`] = `
76+
"// foo
77+
"
78+
`;
79+
80+
exports[`test1.js.LICENSE 3`] = `
81+
"/* Comment */
82+
"
83+
`;
84+
85+
exports[`test1.js.LICENSE 4`] = `
86+
"/* Comment */
87+
"
88+
`;
89+
90+
exports[`warnings 1`] = `Array []`;
91+
92+
exports[`warnings 2`] = `Array []`;
93+
94+
exports[`warnings 3`] = `Array []`;
95+
96+
exports[`warnings 4`] = `Array []`;
97+
98+
exports[`warnings 5`] = `Array []`;

Diff for: test/extract-comments-options.test.js

+146-6
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,48 @@ describe('when options.extractComments', () => {
7171
});
7272
});
7373

74+
it('normalizes when options.extractComments is boolean', () => {
75+
const pluginEnvironment = new PluginEnvironment();
76+
const compilerEnv = pluginEnvironment.getEnvironmentStub();
77+
compilerEnv.context = '';
78+
79+
const plugin = new UglifyJsPlugin({
80+
uglifyOptions: {
81+
output: {
82+
comments: false,
83+
},
84+
},
85+
extractComments: true,
86+
});
87+
plugin.apply(compilerEnv);
88+
const [eventBinding] = pluginEnvironment.getEventBindings();
89+
const chunkPluginEnvironment = new PluginEnvironment();
90+
const compilation2 = chunkPluginEnvironment.getEnvironmentStub();
91+
compilation2.assets = {
92+
'test.js': {
93+
source: () => '// Comment\nvar foo = 1;',
94+
},
95+
'test1.js': {
96+
source: () => '/* Comment */\nvar foo = 1;',
97+
},
98+
};
99+
compilation2.warnings = [];
100+
compilation2.errors = [];
101+
102+
eventBinding.handler(compilation2);
103+
[compilationEventBinding] = chunkPluginEnvironment.getEventBindings();
104+
105+
compilationEventBinding.handler([{
106+
files: ['test.js', 'test1.js'],
107+
}], () => {
108+
expect(compilation2.assets['test.js'].source()).toMatchSnapshot('test.js');
109+
expect(compilation2.assets['test1.js'].source()).toMatchSnapshot('test1.js');
110+
expect(compilation2.assets['test1.js.LICENSE'].source()).toMatchSnapshot('test1.js.LICENSE');
111+
expect(compilation2.errors).toMatchSnapshot('errors');
112+
expect(compilation2.warnings).toMatchSnapshot('warnings');
113+
});
114+
});
115+
74116
it('normalizes when options.extractComments is regex', () => {
75117
const pluginEnvironment = new PluginEnvironment();
76118
const compilerEnv = pluginEnvironment.getEnvironmentStub();
@@ -90,22 +132,116 @@ describe('when options.extractComments', () => {
90132
const compilation2 = chunkPluginEnvironment.getEnvironmentStub();
91133
compilation2.assets = {
92134
'test.js': {
93-
source: () => 'var foo = 1;',
135+
source: () => '// Comment\nvar foo = 1;',
136+
},
137+
'test1.js': {
138+
source: () => '// foo\nvar foo = 1;',
94139
},
95140
};
141+
compilation2.warnings = [];
96142
compilation2.errors = [];
97143

98144
eventBinding.handler(compilation2);
99145
[compilationEventBinding] = chunkPluginEnvironment.getEventBindings();
100146

101147
compilationEventBinding.handler([{
102-
files: ['test.js'],
148+
files: ['test.js', 'test1.js'],
103149
}], () => {
104-
expect(compilation2.errors.length).toBe(0);
150+
expect(compilation2.assets['test.js'].source()).toMatchSnapshot('test.js');
151+
expect(compilation2.assets['test1.js'].source()).toMatchSnapshot('test1.js');
152+
expect(compilation2.assets['test1.js.LICENSE'].source()).toMatchSnapshot('test1.js.LICENSE');
153+
expect(compilation2.errors).toMatchSnapshot('errors');
154+
expect(compilation2.warnings).toMatchSnapshot('warnings');
105155
});
106156
});
107157

108-
it('converts boolean options.extractComments.condition to function', () => {
158+
it('normalizes when options.extractComments is string', () => {
159+
const pluginEnvironment = new PluginEnvironment();
160+
const compilerEnv = pluginEnvironment.getEnvironmentStub();
161+
compilerEnv.context = '';
162+
163+
const plugin = new UglifyJsPlugin({
164+
uglifyOptions: {
165+
output: {
166+
comments: false,
167+
},
168+
},
169+
extractComments: 'all',
170+
});
171+
plugin.apply(compilerEnv);
172+
const [eventBinding] = pluginEnvironment.getEventBindings();
173+
const chunkPluginEnvironment = new PluginEnvironment();
174+
const compilation2 = chunkPluginEnvironment.getEnvironmentStub();
175+
compilation2.assets = {
176+
'test.js': {
177+
source: () => '// Comment\nvar foo = 1;',
178+
},
179+
'test1.js': {
180+
source: () => '/* Comment */\nvar foo = 1;',
181+
},
182+
};
183+
compilation2.warnings = [];
184+
compilation2.errors = [];
185+
186+
eventBinding.handler(compilation2);
187+
[compilationEventBinding] = chunkPluginEnvironment.getEventBindings();
188+
189+
compilationEventBinding.handler([{
190+
files: ['test.js', 'test1.js'],
191+
}], () => {
192+
expect(compilation2.assets['test.js'].source()).toMatchSnapshot('test.js');
193+
expect(compilation2.assets['test.js.LICENSE'].source()).toMatchSnapshot('test.js.LICENSE');
194+
expect(compilation2.assets['test1.js'].source()).toMatchSnapshot('test1.js');
195+
expect(compilation2.assets['test1.js.LICENSE'].source()).toMatchSnapshot('test1.js.LICENSE');
196+
expect(compilation2.errors).toMatchSnapshot('errors');
197+
expect(compilation2.warnings).toMatchSnapshot('warnings');
198+
});
199+
});
200+
201+
it('normalizes when options.extractComments is function', () => {
202+
const pluginEnvironment = new PluginEnvironment();
203+
const compilerEnv = pluginEnvironment.getEnvironmentStub();
204+
compilerEnv.context = '';
205+
206+
const plugin = new UglifyJsPlugin({
207+
uglifyOptions: {
208+
output: {
209+
comments: false,
210+
},
211+
},
212+
extractComments: () => true,
213+
});
214+
plugin.apply(compilerEnv);
215+
const [eventBinding] = pluginEnvironment.getEventBindings();
216+
const chunkPluginEnvironment = new PluginEnvironment();
217+
const compilation2 = chunkPluginEnvironment.getEnvironmentStub();
218+
compilation2.assets = {
219+
'test.js': {
220+
source: () => '// Comment\nvar foo = 1;',
221+
},
222+
'test1.js': {
223+
source: () => '/* Comment */\nvar foo = 1;',
224+
},
225+
};
226+
compilation2.warnings = [];
227+
compilation2.errors = [];
228+
229+
eventBinding.handler(compilation2);
230+
[compilationEventBinding] = chunkPluginEnvironment.getEventBindings();
231+
232+
compilationEventBinding.handler([{
233+
files: ['test.js', 'test1.js'],
234+
}], () => {
235+
expect(compilation2.assets['test.js'].source()).toMatchSnapshot('test.js');
236+
expect(compilation2.assets['test1.js'].source()).toMatchSnapshot('test.js');
237+
expect(compilation2.assets['test.js.LICENSE'].source()).toMatchSnapshot('test.js.LICENSE');
238+
expect(compilation2.assets['test1.js.LICENSE'].source()).toMatchSnapshot('test1.js.LICENSE');
239+
expect(compilation2.errors).toMatchSnapshot('errors');
240+
expect(compilation2.warnings).toMatchSnapshot('warnings');
241+
});
242+
});
243+
244+
it('normalizes when options.extractComments is object', () => {
109245
const pluginEnvironment = new PluginEnvironment();
110246
const compilerEnv = pluginEnvironment.getEnvironmentStub();
111247
compilerEnv.context = '';
@@ -132,9 +268,10 @@ describe('when options.extractComments', () => {
132268
const compilation2 = chunkPluginEnvironment.getEnvironmentStub();
133269
compilation2.assets = {
134270
'test.js': {
135-
source: () => 'var foo = 1;',
271+
source: () => '// Comment\nvar foo = 1;',
136272
},
137273
};
274+
compilation2.warnings = [];
138275
compilation2.errors = [];
139276

140277
eventBinding.handler(compilation2);
@@ -143,7 +280,10 @@ describe('when options.extractComments', () => {
143280
compilationEventBinding.handler([{
144281
files: ['test.js'],
145282
}], () => {
146-
expect(compilation2.errors.length).toBe(0);
283+
expect(compilation2.assets['test.js'].source()).toMatchSnapshot('test.js');
284+
expect(compilation2.assets['test.license.js'].source()).toMatchSnapshot('test.license.js');
285+
expect(compilation2.errors).toMatchSnapshot('errors');
286+
expect(compilation2.warnings).toMatchSnapshot('warnings');
147287
});
148288
});
149289
});

0 commit comments

Comments
 (0)