forked from TrySound/rollup-plugin-terser
/
test.js
288 lines (265 loc) · 8.46 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
const { rollup } = require("rollup");
const { terser } = require("../");
test("minify", async () => {
const bundle = await rollup({
input: "test/fixtures/unminified.js",
plugins: [terser()]
});
const result = await bundle.generate({ format: "cjs" });
expect(result.output).toHaveLength(1);
const [output] = result.output;
expect(output.code).toEqual(
'"use strict";window.a=5,window.a<3&&console.log(4);\n'
);
expect(output.map).toBeFalsy();
});
test("minify via terser options", async () => {
const bundle = await rollup({
input: "test/fixtures/empty.js",
plugins: [terser({ output: { comments: "all" } })]
});
const result = await bundle.generate({
banner: "/* package name */",
format: "cjs"
});
expect(result.output).toHaveLength(1);
const [output] = result.output;
expect(output.code).toEqual('/* package name */\n"use strict";\n');
expect(output.map).toBeFalsy();
});
test("minify multiple outputs", async () => {
const bundle = await rollup({
input: "test/fixtures/unminified.js",
plugins: [terser()]
});
const [bundle1, bundle2] = await Promise.all([
bundle.generate({ format: "cjs" }),
bundle.generate({ format: "es" })
]);
const [output1] = bundle1.output;
const [output2] = bundle2.output;
expect(output1.code).toEqual(
'"use strict";window.a=5,window.a<3&&console.log(4);\n'
);
expect(output2.code).toEqual("window.a=5,window.a<3&&console.log(4);\n");
});
test("minify module", async () => {
const bundle = await rollup({
input: "test/fixtures/plain-file.js",
plugins: [terser()]
});
const result = await bundle.generate({ format: "esm" });
expect(result.output).toHaveLength(1);
const [output] = result.output;
expect(output.code).toEqual('console.log("bar");\n');
});
test("minify with sourcemaps", async () => {
const bundle = await rollup({
input: "test/fixtures/sourcemap.js",
plugins: [terser()]
});
const result = await bundle.generate({ format: "cjs", sourcemap: true });
expect(result.output).toHaveLength(1);
const [output] = result.output;
expect(output.map).toBeTruthy();
});
test("allow to disable source maps", async () => {
const bundle = await rollup({
input: "test/fixtures/sourcemap.js",
plugins: [terser({ sourcemap: false })]
});
await bundle.generate({ format: "cjs" });
});
test("does not allow to pass sourceMap", async () => {
try {
const bundle = await rollup({
input: "test/fixtures/sourcemap.js",
plugins: [terser({ sourceMap: false })]
});
expect(true).toBeFalsy();
} catch (error) {
expect(error.toString()).toMatch(/sourceMap option is removed/);
}
});
test("throw error on terser fail", async () => {
try {
const bundle = await rollup({
input: "test/fixtures/failed.js",
plugins: [
{
renderChunk: () => ({ code: "var = 1" })
},
terser()
]
});
await bundle.generate({ format: "esm" });
expect(true).toBeFalsy();
} catch (error) {
expect(error.toString()).toMatch(/Name expected/);
}
});
test("throw error on terser fail with multiple outputs", async () => {
try {
const bundle = await rollup({
input: "test/fixtures/failed.js",
plugins: [
{
renderChunk: () => ({ code: "var = 1" })
},
terser()
]
});
await Promise.all([
bundle.generate({ format: "cjs" }),
bundle.generate({ format: "esm" })
]);
expect(true).toBeFalsy();
} catch (error) {
expect(error.toString()).toMatch(/Name expected/);
}
});
test("works with code splitting", async () => {
const bundle = await rollup({
input: ["test/fixtures/chunk-1.js", "test/fixtures/chunk-2.js"],
plugins: [terser()]
});
const { output } = await bundle.generate({ format: "esm" });
const newOutput = {};
output.forEach(out => {
// TODO rewrite with object rest after node 6 dropping
const value = Object.assign({}, out);
delete value.modules;
delete value.facadeModuleId;
newOutput[out.fileName] = value;
});
expect(newOutput).toMatchSnapshot();
});
test("allow to pass not string values to worker", async () => {
const bundle = await rollup({
input: "test/fixtures/unminified.js",
plugins: [terser({ mangle: { properties: { regex: /^_/ } } })]
});
const result = await bundle.generate({ format: "cjs" });
expect(result.output).toHaveLength(1);
const [output] = result.output;
expect(output.code).toEqual(
'"use strict";window.a=5,window.a<3&&console.log(4);\n'
);
});
test("allow classic function definitions passing to worker", async () => {
const bundle = await rollup({
input: "test/fixtures/unminified.js",
plugins: [
terser({
mangle: { properties: { regex: /^_/ } },
output: {
comments: function (node, comment) {
if (comment.type === "comment2") {
// multiline comment
return /@preserve|@license|@cc_on|^!/i.test(comment.value);
}
return false;
}
}
})
]
});
const result = await bundle.generate({ format: "cjs" });
expect(result.output).toHaveLength(1);
const [output] = result.output;
expect(output.code).toEqual(
'"use strict";window.a=5,window.a<3&&console.log(4);\n'
);
});
test("allow method shorthand definitions passing to worker", async () => {
const bundle = await rollup({
input: "test/fixtures/unminified.js",
plugins: [
terser({
mangle: { properties: { regex: /^_/ } },
output: {
comments(node, comment) {
if (comment.type === "comment2") {
// multiline comment
return /@preserve|@license|@cc_on|^!/i.test(comment.value);
}
return false;
}
}
})
]
});
const result = await bundle.generate({ format: "cjs" });
expect(result.output).toHaveLength(1);
const [output] = result.output;
expect(output.code).toEqual(
'"use strict";window.a=5,window.a<3&&console.log(4);\n'
);
});
test("allow arrow function definitions passing to worker", async () => {
const bundle = await rollup({
input: "test/fixtures/unminified.js",
plugins: [
terser({
mangle: { properties: { regex: /^_/ } },
output: {
comments: (node, comment) => {
if (comment.type === "comment2") {
// multiline comment
return /@preserve|@license|@cc_on|^!/i.test(comment.value);
}
return false;
}
}
})
]
});
const result = await bundle.generate({ format: "cjs" });
expect(result.output).toHaveLength(1);
const [output] = result.output;
expect(output.code).toEqual(
'"use strict";window.a=5,window.a<3&&console.log(4);\n'
);
});
test("allow to pass not string values to worker", async () => {
const bundle = await rollup({
input: "test/fixtures/unminified.js",
plugins: [terser({ mangle: { properties: { regex: /^_/ } } })]
});
const result = await bundle.generate({ format: "cjs" });
expect(result.output[0].code).toEqual(
'"use strict";window.a=5,window.a<3&&console.log(4);\n'
);
});
test("include chunk file by string name", async () => {
const bundle = await rollup({
input: "test/fixtures/unminified.js",
plugins: [ terser({ include: 'some.js' }) ]
});
const result = await bundle.generate({ format: "es", file: 'some.js' });
const { code, map } = result.output[0];
expect(code).toBe(`window.a=5,window.a<3&&console.log(4);\n`);
expect(map).toBeFalsy();
});
test("exclude chunk file pattern name by minimatch pattern", async () => {
const bundle = await rollup({
input: "test/fixtures/unminified.js",
plugins: [ terser({ exclude: '*-cjs.js' }) ]
});
const result = await bundle.generate({ format: "cjs", entryFileNames: '[name]-[format].js' });
const { code, map } = result.output[0];
expect(code).toBe(`'use strict';\n\nwindow.a = 5;\n\nif (window.a < 3) {\n console.log(4);\n}\n`);
expect(map).toBeFalsy();
});
test("include only one chunk file by regex", async () => {
const bundle = await rollup({
input: [ "test/fixtures/chunk-1.js", "test/fixtures/chunk-2.js" ],
plugins: [ terser({ include: /.+-1\.\w+/ }) ]
});
const result = await bundle.generate({ format: "es" });
const { 0: chunk1, 1: chunk2 } = result.output;
expect(chunk1.code).toBe(`console.log("chunk-1");\n`);
expect(chunk1.map).toBeFalsy();
expect(chunk2.code).toBe(`var chunk2 = 'chunk-2';\nconsole.log(chunk2);\n`);
expect(chunk2.map).toBeFalsy();
});