@@ -71,6 +71,48 @@ describe('when options.extractComments', () => {
71
71
} ) ;
72
72
} ) ;
73
73
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
+
74
116
it ( 'normalizes when options.extractComments is regex' , ( ) => {
75
117
const pluginEnvironment = new PluginEnvironment ( ) ;
76
118
const compilerEnv = pluginEnvironment . getEnvironmentStub ( ) ;
@@ -90,22 +132,116 @@ describe('when options.extractComments', () => {
90
132
const compilation2 = chunkPluginEnvironment . getEnvironmentStub ( ) ;
91
133
compilation2 . assets = {
92
134
'test.js' : {
93
- source : ( ) => 'var foo = 1;' ,
135
+ source : ( ) => '// Comment\nvar foo = 1;' ,
136
+ } ,
137
+ 'test1.js' : {
138
+ source : ( ) => '// foo\nvar foo = 1;' ,
94
139
} ,
95
140
} ;
141
+ compilation2 . warnings = [ ] ;
96
142
compilation2 . errors = [ ] ;
97
143
98
144
eventBinding . handler ( compilation2 ) ;
99
145
[ compilationEventBinding ] = chunkPluginEnvironment . getEventBindings ( ) ;
100
146
101
147
compilationEventBinding . handler ( [ {
102
- files : [ 'test.js' ] ,
148
+ files : [ 'test.js' , 'test1.js' ] ,
103
149
} ] , ( ) => {
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' ) ;
105
155
} ) ;
106
156
} ) ;
107
157
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' , ( ) => {
109
245
const pluginEnvironment = new PluginEnvironment ( ) ;
110
246
const compilerEnv = pluginEnvironment . getEnvironmentStub ( ) ;
111
247
compilerEnv . context = '' ;
@@ -132,9 +268,10 @@ describe('when options.extractComments', () => {
132
268
const compilation2 = chunkPluginEnvironment . getEnvironmentStub ( ) ;
133
269
compilation2 . assets = {
134
270
'test.js' : {
135
- source : ( ) => 'var foo = 1;' ,
271
+ source : ( ) => '// Comment\nvar foo = 1;' ,
136
272
} ,
137
273
} ;
274
+ compilation2 . warnings = [ ] ;
138
275
compilation2 . errors = [ ] ;
139
276
140
277
eventBinding . handler ( compilation2 ) ;
@@ -143,7 +280,10 @@ describe('when options.extractComments', () => {
143
280
compilationEventBinding . handler ( [ {
144
281
files : [ 'test.js' ] ,
145
282
} ] , ( ) => {
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' ) ;
147
287
} ) ;
148
288
} ) ;
149
289
} ) ;
0 commit comments