Skip to content

Commit

Permalink
Move uglify into own worker for every chunk
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
TrySound committed Aug 30, 2018
1 parent 94aa350 commit a39e9f9
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 40 deletions.
29 changes: 18 additions & 11 deletions 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;
});
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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"
}
}
40 changes: 40 additions & 0 deletions 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,
},
},
},
}
`;
2 changes: 2 additions & 0 deletions test/fixtures/chunk-1.js
@@ -0,0 +1,2 @@
var chunk1 = 'chunk-1';
console.log(chunk1)
2 changes: 2 additions & 0 deletions test/fixtures/chunk-2.js
@@ -0,0 +1,2 @@
var chunk2 = 'chunk-2';
console.log(chunk2)
39 changes: 14 additions & 25 deletions test/test.js
Expand Up @@ -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();
});

Expand All @@ -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({
Expand All @@ -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();
});
12 changes: 12 additions & 0 deletions 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;
6 changes: 3 additions & 3 deletions yarn.lock
Expand Up @@ -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" "*"
Expand Down

0 comments on commit a39e9f9

Please sign in to comment.