Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minification support #105

Merged
merged 3 commits into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"source-map-support": "^0.5.9",
"stripe": "^6.15.0",
"strong-globalize": "^4.1.2",
"terser": "^3.11.0",
"the-answer": "^1.0.0",
"twilio": "^3.23.2",
"vue": "^2.5.17",
Expand Down
2 changes: 1 addition & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ switch (args._[0]) {
const ncc = require("./index.js")(
eval("require.resolve")(resolve(args._[1] || ".")),
{
minify: !args["--no-minify"],
minify: !args["--no-minify"] && !run,
externals: args["--external"],
sourceMap: !args["--no-source-map"]
}
Expand Down
34 changes: 29 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const resolve = require("resolve");
const fs = require("fs");
const path = require("path");
const webpack = require("webpack");
const MemoryFS = require("memory-fs");
const WebpackParser = require('webpack/lib/Parser');
const webpackParse = WebpackParser.parse
const webpackParse = WebpackParser.parse;
const terser = require("terser");

// overload the webpack parser so that we can make
// acorn work with the node.js / commonjs semantics
Expand Down Expand Up @@ -44,7 +44,7 @@ function resolveModule(context, request, callback, forcedExternals = []) {
});
}

module.exports = async (entry, { externals = [], minify = true, sourceMap = false, filename = "index.js" } = {}) => {
module.exports = async (entry, { externals = [], minify = false, sourceMap = false, filename = "index.js" } = {}) => {
const mfs = new MemoryFS();
const assetNames = Object.create(null);
const assets = Object.create(null);
Expand Down Expand Up @@ -143,8 +143,8 @@ module.exports = async (entry, { externals = [], minify = true, sourceMap = fals
}
const assets = Object.create(null);
getFlatFiles(mfs.data, assets);
delete assets["index.js"];
delete assets["index.js.map"];
delete assets[filename];
delete assets[filename + ".map"];
const code = mfs.readFileSync("/index.js", "utf8");
const map = sourceMap ? mfs.readFileSync("/index.js.map", "utf8") : null;
resolve({
Expand All @@ -153,6 +153,30 @@ module.exports = async (entry, { externals = [], minify = true, sourceMap = fals
assets
});
});
})
.then(({ code, map, assets }) => {
if (!minify)
return { code, map, assets };
const result = terser.minify(code, {
compress: {
keep_classnames: true,
keep_fnames: true
},
mangle: {
keep_classnames: true,
keep_fnames: true
},
sourceMap: sourceMap ? {
content: map,
filename,
url: filename + ".map"
} : false
});
// For some reason, auth0 returns "undefined"!
// custom terser phase used over Webpack integration for this reason
if (result.code === undefined)
return { code, map, assets };
return { code: result.code, map: result.map, assets };
});
};

Expand Down
6 changes: 3 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ for (const unitTest of fs.readdirSync(`${__dirname}/unit`)) {
.toString().trim()
// Windows support
.replace(/\r/g, '');
await ncc(`${__dirname}/unit/${unitTest}/input.js`, { minify: false }).then(async ({ code, assets }) => {
await ncc(`${__dirname}/unit/${unitTest}/input.js`, { minify: false, sourceMap: false }).then(async ({ code, assets }) => {
// very simple asset validation in unit tests
if (unitTest.startsWith('asset-')) {
expect(Object.keys(assets).length).toBeGreaterThan(0);
Expand All @@ -32,7 +32,7 @@ for (const unitTest of fs.readdirSync(`${__dirname}/unit`)) {
}

// the twilio test can take a while (large codebase)
jest.setTimeout(30000);
jest.setTimeout(50000);

function clearDir (dir) {
try {
Expand All @@ -48,7 +48,7 @@ for (const integrationTest of fs.readdirSync(__dirname + "/integration")) {
// ignore e.g.: `.json` files
if (!integrationTest.endsWith(".js")) continue;
it(`should evaluate ${integrationTest} without errors`, async () => {
const { code, map, assets } = await ncc(__dirname + "/integration/" + integrationTest, { sourceMap: true });
const { code, map, assets } = await ncc(__dirname + "/integration/" + integrationTest, { minify: true, sourceMap: true });
const tmpDir = `${__dirname}/tmp/${integrationTest}/`;
clearDir(tmpDir);
mkdirp.sync(tmpDir);
Expand Down
115 changes: 0 additions & 115 deletions test/unit/asset-fs-inline-path-shadow/actual.js

This file was deleted.

9 changes: 9 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9791,6 +9791,15 @@ terser-webpack-plugin@^1.1.0:
webpack-sources "^1.1.0"
worker-farm "^1.5.2"

terser@^3.11.0:
version "3.11.0"
resolved "https://registry.yarnpkg.com/terser/-/terser-3.11.0.tgz#60782893e1f4d6788acc696351f40636d0e37af0"
integrity sha512-5iLMdhEPIq3zFWskpmbzmKwMQixKmTYwY3Ox9pjtSklBLnHiuQ0GKJLhL1HSYtyffHM3/lDIFBnb82m9D7ewwQ==
dependencies:
commander "~2.17.1"
source-map "~0.6.1"
source-map-support "~0.5.6"

terser@^3.8.1:
version "3.10.12"
resolved "https://registry.yarnpkg.com/terser/-/terser-3.10.12.tgz#06d40765e40b33fd97977c0896c75b2b5d42142d"
Expand Down