From a39e9f98fc85a0341f667e7422599ac695473a2b Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Thu, 30 Aug 2018 20:57:49 +0300 Subject: [PATCH] Move uglify into own worker for every chunk Moved uglify call into worker which may significantly reduce build time in projects with code splitting. This required to remove `minifier` option. Prefer using `terser` plugin instead. --- index.js | 29 +++++++++++++++--------- package.json | 2 +- test/__snapshots__/test.js.snap | 40 +++++++++++++++++++++++++++++++++ test/fixtures/chunk-1.js | 2 ++ test/fixtures/chunk-2.js | 2 ++ test/test.js | 39 ++++++++++++-------------------- transform.js | 12 ++++++++++ yarn.lock | 6 ++--- 8 files changed, 92 insertions(+), 40 deletions(-) create mode 100644 test/__snapshots__/test.js.snap create mode 100644 test/fixtures/chunk-1.js create mode 100644 test/fixtures/chunk-2.js create mode 100644 transform.js diff --git a/index.js b/index.js index e204fea..f2793cb 100644 --- a/index.js +++ b/index.js @@ -1,22 +1,29 @@ const { codeFrameColumns } = require("@babel/code-frame"); -const { minify } = require("uglify-js"); +const Worker = require("jest-worker").default; -function uglify(userOptions, minifier = minify) { +function uglify(userOptions) { const options = Object.assign({ sourceMap: true }, userOptions); return { name: "uglify", transformBundle(code) { - const result = minifier(code, options); - if (result.error) { - const { message, line, col: column } = result.error; - console.error( - codeFrameColumns(code, { start: { line, column } }, { message }) - ); - throw result.error; - } - return result; + const worker = new Worker(require.resolve("./transform.js")); + + return worker + .transform(code, options) + .then(result => { + worker.end(); + return result; + }) + .catch(error => { + worker.end(); + const { message, line, col: column } = error; + console.error( + codeFrameColumns(code, { start: { line, column } }, { message }) + ); + throw error; + }); } }; } diff --git a/package.json b/package.json index 9943b6b..348fe3a 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,6 @@ "babel-jest": "^22.4.4", "jest": "^22.4.4", "prettier": "^1.12.1", - "rollup": "^0.59.1" + "rollup": "^0.65.0" } } diff --git a/test/__snapshots__/test.js.snap b/test/__snapshots__/test.js.snap new file mode 100644 index 0000000..18c7de3 --- /dev/null +++ b/test/__snapshots__/test.js.snap @@ -0,0 +1,40 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`works with code splitting 1`] = ` +Object { + "chunk-1.js": Object { + "code": "var chunk1=\\"chunk-1\\";console.log(chunk1); +", + "exports": Array [], + "fileName": "chunk-1.js", + "imports": Array [], + "isEntry": true, + "map": null, + "modules": Object { + "/Users/trysound/host/rollup-plugin-uglify/test/fixtures/chunk-1.js": Object { + "originalLength": 44, + "removedExports": Array [], + "renderedExports": Array [], + "renderedLength": 44, + }, + }, + }, + "chunk-2.js": Object { + "code": "var chunk2=\\"chunk-2\\";console.log(chunk2); +", + "exports": Array [], + "fileName": "chunk-2.js", + "imports": Array [], + "isEntry": true, + "map": null, + "modules": Object { + "/Users/trysound/host/rollup-plugin-uglify/test/fixtures/chunk-2.js": Object { + "originalLength": 44, + "removedExports": Array [], + "renderedExports": Array [], + "renderedLength": 44, + }, + }, + }, +} +`; diff --git a/test/fixtures/chunk-1.js b/test/fixtures/chunk-1.js new file mode 100644 index 0000000..14f3da7 --- /dev/null +++ b/test/fixtures/chunk-1.js @@ -0,0 +1,2 @@ +var chunk1 = 'chunk-1'; +console.log(chunk1) diff --git a/test/fixtures/chunk-2.js b/test/fixtures/chunk-2.js new file mode 100644 index 0000000..0ae296f --- /dev/null +++ b/test/fixtures/chunk-2.js @@ -0,0 +1,2 @@ +var chunk2 = 'chunk-2'; +console.log(chunk2) diff --git a/test/test.js b/test/test.js index 202ac0f..244c3f9 100644 --- a/test/test.js +++ b/test/test.js @@ -9,7 +9,9 @@ test("minify", async () => { plugins: [uglify()] }); const result = await bundle.generate({ format: "cjs" }); - expect(result.code).toEqual('"use strict";window.a=5,window.a<3&&console.log(4);\n'); + expect(result.code).toEqual( + '"use strict";window.a=5,window.a<3&&console.log(4);\n' + ); expect(result.map).toBeFalsy(); }); @@ -35,29 +37,6 @@ test("minify with sourcemaps", async () => { expect(result.map).toBeTruthy(); }); -test("allow passing minifier", async () => { - const expectedCode = readFile("test/fixtures/plain-file.js", "utf-8"); - const testOptions = { - foo: "bar" - }; - - const bundle = await rollup({ - input: "test/fixtures/plain-file.js", - plugins: [ - uglify(testOptions, (code, options) => { - expect(code.trim()).toEqual(expectedCode.trim()); - expect(options).toEqual({ - foo: "bar", - sourceMap: true - }); - return { code }; - }) - ] - }); - const result = await bundle.generate({ format: "es" }); - expect(result.code.trim()).toEqual(expectedCode.trim()); -}); - test("throw error on uglify fail", async () => { try { const bundle = await rollup({ @@ -69,9 +48,19 @@ test("throw error on uglify fail", async () => { uglify() ] }); - await bundle.generate({ format: "es" }); + await 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"], + experimentalCodeSplitting: true, + plugins: [uglify()] + }); + const { output } = await bundle.generate({ format: "esm" }); + expect(output).toMatchSnapshot(); +}); diff --git a/transform.js b/transform.js new file mode 100644 index 0000000..3d55371 --- /dev/null +++ b/transform.js @@ -0,0 +1,12 @@ +const { minify } = require("uglify-js"); + +const transform = (code, options) => { + const result = minify(code, options); + if (result.error) { + throw result.error; + } else { + return result; + } +}; + +exports.transform = transform; diff --git a/yarn.lock b/yarn.lock index 04fbdd6..a369011 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2857,9 +2857,9 @@ rimraf@^2.6.1: dependencies: glob "^7.0.5" -rollup@^0.59.1: - version "0.59.1" - resolved "https://registry.yarnpkg.com/rollup/-/rollup-0.59.1.tgz#86cbceaecd861df1317a0aa29207173de23e6a5d" +rollup@^0.65.0: + version "0.65.0" + resolved "https://registry.yarnpkg.com/rollup/-/rollup-0.65.0.tgz#280db1252169b68fc3043028346b337dde453fba" dependencies: "@types/estree" "0.0.39" "@types/node" "*"