Skip to content

Commit

Permalink
Minify prelude.js (#431)
Browse files Browse the repository at this point in the history
* run prettier

* minify

* fix linting issues
  • Loading branch information
Jasper De Moor authored and devongovett committed Dec 31, 2017
1 parent d11a15e commit a0ede06
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 9 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ lib
dist
/test/dist
/test/input
minified

coverage
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ lib
!test/**/node_modules
.vscode/
.idea/
minified/
41 changes: 41 additions & 0 deletions minify.js
Original file line number Diff line number Diff line change
@@ -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();
})();
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
1 change: 1 addition & 0 deletions src/builtins/prelude.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 18 additions & 8 deletions src/packagers/JSPackager.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
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 {
async start() {
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) {
Expand All @@ -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));
}
}

Expand Down

0 comments on commit a0ede06

Please sign in to comment.