diff --git a/.eslintignore b/.eslintignore index 3762b202a24..77c718ad660 100644 --- a/.eslintignore +++ b/.eslintignore @@ -16,5 +16,6 @@ lib dist /test/dist /test/input +minified coverage \ No newline at end of file diff --git a/.gitignore b/.gitignore index 89594f2b7c7..e0a5e959985 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ lib !test/**/node_modules .vscode/ .idea/ +minified/ \ No newline at end of file diff --git a/minify.js b/minify.js new file mode 100644 index 00000000000..011bc4c0f0d --- /dev/null +++ b/minify.js @@ -0,0 +1,41 @@ +const uglify = require('uglify-es'); +const fs = require('./src/utils/fs'); +const path = require('path'); + +async function minify(inputPath, outputPath) { + let input = (await fs.readFile(inputPath)).toString(); + + // Minify input + let options = { + mangle: { + toplevel: true + }, + output: { + ecma: 3, + semicolons: false + } + }; + let res = uglify.minify(input, options); + + // Write output + await fs.mkdirp(path.dirname(outputPath)); + await fs.writeFile(outputPath, res.code); +} + +async function runMinifier() { + let files = ['prelude.js']; + + files.forEach(async file => { + await minify( + path.join('./src/builtins/', file), + path.join('./minified/builtins/', file) + ); + // eslint-disable-next-line no-console + console.log('minified: ' + file); + return; + }); +} + +(async () => { + await runMinifier(); +})(); diff --git a/package.json b/package.json index ce599fc3cea..a838a01dfc6 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,8 @@ "test": "cross-env NODE_ENV=test mocha", "format": "prettier --write './{src,bin,test}/**/*.{js,json,md}'", "build": "babel src -d lib", - "prepublish": "yarn build", + "prepublish": "yarn build && npm run minify", + "minify": "node ./minify.js", "precommit": "npm run lint && lint-staged", "lint": "eslint .", "postinstall": diff --git a/src/builtins/prelude.js b/src/builtins/prelude.js index 305a9f0e2a0..2d2f9d5b8e7 100644 --- a/src/builtins/prelude.js +++ b/src/builtins/prelude.js @@ -6,6 +6,7 @@ // anything defined in a previous bundle is accessed via the // orig method which is the require for previous bundles +// eslint-disable-next-line no-global-assign require = (function (modules, cache, entry) { // Save the require from previous bundle to this closure if any var previousRequire = typeof require === "function" && require; diff --git a/src/packagers/JSPackager.js b/src/packagers/JSPackager.js index 924cc1a9cd3..b7c404a9e60 100644 --- a/src/packagers/JSPackager.js +++ b/src/packagers/JSPackager.js @@ -1,12 +1,21 @@ const fs = require('fs'); -const {basename} = require('path'); +const path = require('path'); const Packager = require('./Packager'); -const prelude = fs - .readFileSync(__dirname + '/../builtins/prelude.js', 'utf8') - .trim(); +const prelude = { + source: fs + .readFileSync(path.join(__dirname, '../builtins/prelude.js'), 'utf8') + .trim(), + minified: fs + .readFileSync( + path.join(__dirname, '../../minified/builtins/prelude.js'), + 'utf8' + ) + .trim() +}; + const hmr = fs - .readFileSync(__dirname + '/../builtins/hmr-runtime.js', 'utf8') + .readFileSync(path.join(__dirname, '../builtins/hmr-runtime.js'), 'utf8') .trim(); class JSPackager extends Packager { @@ -14,7 +23,8 @@ class JSPackager extends Packager { this.first = true; this.dedupe = new Map(); - await this.dest.write(prelude + '({'); + let preludeCode = this.options.minify ? prelude.minified : prelude.source; + await this.dest.write(preludeCode + '({'); } async addAsset(asset) { @@ -33,10 +43,10 @@ class JSPackager extends Packager { // For dynamic dependencies, list the child bundles to load along with the module id if (dep.dynamic && this.bundle.childBundles.has(mod.parentBundle)) { - let bundles = [basename(mod.parentBundle.name)]; + let bundles = [path.basename(mod.parentBundle.name)]; for (let child of mod.parentBundle.siblingBundles.values()) { if (!child.isEmpty) { - bundles.push(basename(child.name)); + bundles.push(path.basename(child.name)); } }