| 1 |
const test = require('ava'); |
| 2 |
const { rollup } = require('rollup'); |
| 3 |
|
| 4 |
const terser = require('../'); |
| 5 |
|
| 6 |
test.serial('minify', async (t) => { |
| 7 |
const bundle = await rollup({ |
| 8 |
input: 'test/fixtures/unminified.js', |
| 9 |
plugins: [terser()] |
| 10 |
}); |
| 11 |
const result = await bundle.generate({ format: 'cjs' }); |
| 12 |
t.is(result.output.length, 1); |
| 13 |
const [output] = result.output; |
| 14 |
t.is(output.code, '"use strict";window.a=5,window.a<3&&console.log(4);\n'); |
| 15 |
t.falsy(output.map); |
| 16 |
}); |
| 17 |
|
| 18 |
test.serial('minify with source map', async (t) => { |
| 19 |
const bundle = await rollup({ |
| 20 |
input: 'test/fixtures/unminified.js', |
| 21 |
plugins: [terser()] |
| 22 |
}); |
| 23 |
const result = await bundle.generate({ format: 'cjs', sourcemap: true }); |
| 24 |
t.is(result.output.length, 2); |
| 25 |
const [output] = result.output; |
| 26 |
|
| 27 |
t.truthy(output.map); |
| 28 |
t.is(output.map.version, 3); |
| 29 |
t.is(output.map.file, 'unminified.js'); |
| 30 |
t.deepEqual(output.map.names, ['window', 'a', 'console', 'log']); |
| 31 |
}); |
| 32 |
|
| 33 |
test.serial('minify via terser options', async (t) => { |
| 34 |
const bundle = await rollup({ |
| 35 |
input: 'test/fixtures/empty.js', |
| 36 |
plugins: [terser({ output: { comments: 'all' } })] |
| 37 |
}); |
| 38 |
const result = await bundle.generate({ |
| 39 |
banner: '/* package name */', |
| 40 |
format: 'cjs' |
| 41 |
}); |
| 42 |
t.is(result.output.length, 1); |
| 43 |
const [output] = result.output; |
| 44 |
t.is(output.code, '/* package name */\n"use strict";\n'); |
| 45 |
t.falsy(output.map); |
| 46 |
}); |
| 47 |
|
| 48 |
test.serial('minify multiple outputs', async (t) => { |
| 49 |
let plugin; |
| 50 |
|
| 51 |
const bundle = await rollup({ |
| 52 |
input: 'test/fixtures/unminified.js', |
| 53 |
plugins: [(plugin = terser({ maxWorkers: 2 }))] |
| 54 |
}); |
| 55 |
|
| 56 |
const [bundle1, bundle2] = await Promise.all([ |
| 57 |
bundle.generate({ format: 'cjs' }), |
| 58 |
bundle.generate({ format: 'es' }) |
| 59 |
]); |
| 60 |
const [output1] = bundle1.output; |
| 61 |
const [output2] = bundle2.output; |
| 62 |
|
| 63 |
t.is(output1.code, '"use strict";window.a=5,window.a<3&&console.log(4);\n'); |
| 64 |
t.is(output2.code, 'window.a=5,window.a<3&&console.log(4);\n'); |
| 65 |
t.is(plugin.numOfWorkersUsed, 2, 'used 2 workers'); |
| 66 |
}); |
| 67 |
|
| 68 |
test.serial('minify multiple outputs with only 1 worker', async (t) => { |
| 69 |
let plugin; |
| 70 |
|
| 71 |
const bundle = await rollup({ |
| 72 |
input: 'test/fixtures/unminified.js', |
| 73 |
plugins: [(plugin = terser({ maxWorkers: 1 }))] |
| 74 |
}); |
| 75 |
|
| 76 |
await Promise.all([bundle.generate({ format: 'cjs' }), bundle.generate({ format: 'es' })]); |
| 77 |
|
| 78 |
t.is(plugin.numOfWorkersUsed, 1, 'used 1 worker'); |
| 79 |
}); |
| 80 |
|
| 81 |
test.serial('minify esm module', async (t) => { |
| 82 |
const bundle = await rollup({ |
| 83 |
input: 'test/fixtures/plain-file.js', |
| 84 |
plugins: [terser()] |
| 85 |
}); |
| 86 |
const result = await bundle.generate({ format: 'esm' }); |
| 87 |
t.is(result.output.length, 1); |
| 88 |
const [output] = result.output; |
| 89 |
t.is(output.code, 'console.log("bar");\n'); |
| 90 |
}); |
| 91 |
|
| 92 |
test.serial('minify esm module with disabled module option', async (t) => { |
| 93 |
const bundle = await rollup({ |
| 94 |
input: 'test/fixtures/plain-file.js', |
| 95 |
plugins: [terser({ module: false })] |
| 96 |
}); |
| 97 |
const result = await bundle.generate({ format: 'esm' }); |
| 98 |
t.is(result.output.length, 1); |
| 99 |
const [output] = result.output; |
| 100 |
t.is(output.code, 'const foo="bar";console.log(foo);\n'); |
| 101 |
}); |
| 102 |
|
| 103 |
test.serial('minify cjs module', async (t) => { |
| 104 |
const bundle = await rollup({ |
| 105 |
input: 'test/fixtures/plain-file.js', |
| 106 |
plugins: [terser()] |
| 107 |
}); |
| 108 |
const result = await bundle.generate({ format: 'cjs' }); |
| 109 |
t.is(result.output.length, 1); |
| 110 |
const [output] = result.output; |
| 111 |
t.is(output.code, '"use strict";console.log("bar");\n'); |
| 112 |
}); |
| 113 |
|
| 114 |
test.serial('minify cjs module with disabled toplevel option', async (t) => { |
| 115 |
const bundle = await rollup({ |
| 116 |
input: 'test/fixtures/plain-file.js', |
| 117 |
plugins: [terser({ toplevel: false })] |
| 118 |
}); |
| 119 |
const result = await bundle.generate({ format: 'cjs' }); |
| 120 |
t.is(result.output.length, 1); |
| 121 |
const [output] = result.output; |
| 122 |
t.is(output.code, '"use strict";const foo="bar";console.log(foo);\n'); |
| 123 |
}); |
| 124 |
|
| 125 |
test.serial('throw error on terser fail', async (t) => { |
| 126 |
try { |
| 127 |
const bundle = await rollup({ |
| 128 |
input: 'test/fixtures/failed.js', |
| 129 |
plugins: [ |
| 130 |
{ |
| 131 |
renderChunk: () => { |
| 132 |
return { code: 'var = 1' }; |
| 133 |
} |
| 134 |
}, |
| 135 |
terser() |
| 136 |
] |
| 137 |
}); |
| 138 |
await bundle.generate({ format: 'esm' }); |
| 139 |
t.falsy(true); |
| 140 |
} catch (error) { |
| 141 |
t.is(error.toString(), 'SyntaxError: Name expected'); |
| 142 |
} |
| 143 |
}); |
| 144 |
|
| 145 |
test.serial('throw error on terser fail with multiple outputs', async (t) => { |
| 146 |
try { |
| 147 |
const bundle = await rollup({ |
| 148 |
input: 'test/fixtures/failed.js', |
| 149 |
plugins: [ |
| 150 |
{ |
| 151 |
renderChunk: () => { |
| 152 |
return { code: 'var = 1' }; |
| 153 |
} |
| 154 |
}, |
| 155 |
terser() |
| 156 |
] |
| 157 |
}); |
| 158 |
await Promise.all([bundle.generate({ format: 'cjs' }), bundle.generate({ format: 'esm' })]); |
| 159 |
t.falsy(true); |
| 160 |
} catch (error) { |
| 161 |
t.is(error.toString(), 'SyntaxError: Name expected'); |
| 162 |
} |
| 163 |
}); |
| 164 |
|
| 165 |
test.serial('allow to pass not string values to worker', async (t) => { |
| 166 |
const bundle = await rollup({ |
| 167 |
input: 'test/fixtures/unminified.js', |
| 168 |
plugins: [terser({ mangle: { properties: { regex: /^_/ } } })] |
| 169 |
}); |
| 170 |
const result = await bundle.generate({ format: 'cjs' }); |
| 171 |
t.is(result.output.length, 1); |
| 172 |
const [output] = result.output; |
| 173 |
t.is(output.code, '"use strict";window.a=5,window.a<3&&console.log(4);\n'); |
| 174 |
}); |
| 175 |
|
| 176 |
test.serial('allow classic function definitions passing to worker', async (t) => { |
| 177 |
const bundle = await rollup({ |
| 178 |
input: 'test/fixtures/commented.js', |
| 179 |
plugins: [ |
| 180 |
terser({ |
| 181 |
mangle: { properties: { regex: /^_/ } }, |
| 182 |
output: { |
| 183 |
comments(node, comment) { |
| 184 |
if (comment.type === 'comment2') { |
| 185 |
// multiline comment |
| 186 |
return /@preserve|@license|@cc_on|^!/i.test(comment.value); |
| 187 |
} |
| 188 |
return false; |
| 189 |
} |
| 190 |
} |
| 191 |
}) |
| 192 |
] |
| 193 |
}); |
| 194 |
const result = await bundle.generate({ format: 'cjs', compact: true }); |
| 195 |
t.is(result.output.length, 1); |
| 196 |
const [output] = result.output; |
| 197 |
t.is( |
| 198 |
output.code, |
| 199 |
'"use strict";window.a=5,\n/* @preserve this comment */\nwindow.a<3&&console.log(4);' |
| 200 |
); |
| 201 |
}); |
| 202 |
|
| 203 |
test.serial('allow method shorthand definitions passing to worker #2', async (t) => { |
| 204 |
const bundle = await rollup({ |
| 205 |
input: 'test/fixtures/commented.js', |
| 206 |
plugins: [ |
| 207 |
terser({ |
| 208 |
mangle: { properties: { regex: /^_/ } }, |
| 209 |
output: { |
| 210 |
comments(node, comment) { |
| 211 |
if (comment.type === 'comment2') { |
| 212 |
// multiline comment |
| 213 |
return /@preserve|@license|@cc_on|^!/i.test(comment.value); |
| 214 |
} |
| 215 |
return false; |
| 216 |
} |
| 217 |
} |
| 218 |
}) |
| 219 |
] |
| 220 |
}); |
| 221 |
const result = await bundle.generate({ format: 'cjs', compact: true }); |
| 222 |
t.is(result.output.length, 1); |
| 223 |
const [output] = result.output; |
| 224 |
t.is( |
| 225 |
output.code, |
| 226 |
'"use strict";window.a=5,\n/* @preserve this comment */\nwindow.a<3&&console.log(4);' |
| 227 |
); |
| 228 |
}); |
| 229 |
|
| 230 |
test.serial('allow arrow function definitions passing to worker', async (t) => { |
| 231 |
const bundle = await rollup({ |
| 232 |
input: 'test/fixtures/unminified.js', |
| 233 |
plugins: [ |
| 234 |
terser({ |
| 235 |
mangle: { properties: { regex: /^_/ } }, |
| 236 |
output: { |
| 237 |
comments: (node, comment) => { |
| 238 |
if (comment.type === 'comment2') { |
| 239 |
// multiline comment |
| 240 |
return /@preserve|@license|@cc_on|^!/i.test.serial(comment.value); |
| 241 |
} |
| 242 |
return false; |
| 243 |
} |
| 244 |
} |
| 245 |
}) |
| 246 |
] |
| 247 |
}); |
| 248 |
const result = await bundle.generate({ format: 'cjs' }); |
| 249 |
t.is(result.output.length, 1); |
| 250 |
const [output] = result.output; |
| 251 |
t.is(output.code, '"use strict";window.a=5,window.a<3&&console.log(4);\n'); |
| 252 |
}); |
| 253 |
|
| 254 |
test.serial('allow to pass not string values to worker #2', async (t) => { |
| 255 |
const bundle = await rollup({ |
| 256 |
input: 'test/fixtures/unminified.js', |
| 257 |
plugins: [terser({ mangle: { properties: { regex: /^_/ } } })] |
| 258 |
}); |
| 259 |
const result = await bundle.generate({ format: 'cjs' }); |
| 260 |
t.is(result.output[0].code, '"use strict";window.a=5,window.a<3&&console.log(4);\n'); |
| 261 |
}); |
| 262 |
|
| 263 |
test.serial('terser accepts the nameCache option', async (t) => { |
| 264 |
const nameCache = { |
| 265 |
props: { |
| 266 |
props: { |
| 267 |
$_priv: 'custom' |
| 268 |
} |
| 269 |
} |
| 270 |
}; |
| 271 |
const bundle = await rollup({ |
| 272 |
input: 'test/fixtures/properties.js', |
| 273 |
plugins: [ |
| 274 |
terser({ |
| 275 |
mangle: { |
| 276 |
properties: { |
| 277 |
regex: /^_/ |
| 278 |
} |
| 279 |
}, |
| 280 |
nameCache |
| 281 |
}) |
| 282 |
] |
| 283 |
}); |
| 284 |
const result = await bundle.generate({ format: 'es' }); |
| 285 |
t.is(result.output[0].code.trim(), `console.log({foo:1,custom:2});`); |
| 286 |
}); |
| 287 |
|
| 288 |
test.serial('omits populates an empty nameCache object', async (t) => { |
| 289 |
const nameCache = {}; |
| 290 |
const bundle = await rollup({ |
| 291 |
input: 'test/fixtures/properties-and-locals.js', |
| 292 |
plugins: [ |
| 293 |
terser({ |
| 294 |
mangle: { |
| 295 |
properties: { |
| 296 |
regex: /./ |
| 297 |
} |
| 298 |
}, |
| 299 |
nameCache |
| 300 |
}) |
| 301 |
] |
| 302 |
}); |
| 303 |
const result = await bundle.generate({ format: 'es' }); |
| 304 |
t.is( |
| 305 |
result.output[0].code.trim(), |
| 306 |
`console.log({o:1,i:2},function o(n){return n>0?o(n-1):n}(10));` |
| 307 |
); |
| 308 |
t.deepEqual(nameCache, { |
| 309 |
props: { |
| 310 |
props: { |
| 311 |
$_priv: 'i', |
| 312 |
$foo: 'o' |
| 313 |
} |
| 314 |
}, |
| 315 |
vars: { |
| 316 |
props: {} |
| 317 |
} |
| 318 |
}); |
| 319 |
}); |
| 320 |
|
| 321 |
// Note: nameCache.vars never gets populated, but this is a Terser issue. |
| 322 |
// Here we're just testing that an empty vars object doesn't get added to nameCache if it wasn't there previously. |
| 323 |
test.serial('terser preserve vars in nameCache when provided', async (t) => { |
| 324 |
const nameCache = { |
| 325 |
vars: { |
| 326 |
props: {} |
| 327 |
} |
| 328 |
}; |
| 329 |
const bundle = await rollup({ |
| 330 |
input: 'test/fixtures/properties-and-locals.js', |
| 331 |
plugins: [ |
| 332 |
terser({ |
| 333 |
mangle: { |
| 334 |
properties: { |
| 335 |
regex: /./ |
| 336 |
} |
| 337 |
}, |
| 338 |
nameCache |
| 339 |
}) |
| 340 |
] |
| 341 |
}); |
| 342 |
const result = await bundle.generate({ format: 'es' }); |
| 343 |
t.is( |
| 344 |
result.output[0].code.trim(), |
| 345 |
`console.log({o:1,i:2},function o(n){return n>0?o(n-1):n}(10));` |
| 346 |
); |
| 347 |
t.deepEqual(nameCache, { |
| 348 |
props: { |
| 349 |
props: { |
| 350 |
$_priv: 'i', |
| 351 |
$foo: 'o' |
| 352 |
} |
| 353 |
}, |
| 354 |
vars: { |
| 355 |
props: {} |
| 356 |
} |
| 357 |
}); |
| 358 |
}); |