Skip to content

Commit

Permalink
feat(build): options to minify all products
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed Feb 17, 2020
1 parent 4a008af commit 72f1b36
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
74 changes: 74 additions & 0 deletions packages/midway-bin/lib/cmd/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
const Command = require('egg-bin').Command;
const path = require('path');
const fs = require('fs');
const fsPromise = fs.promises;
const rimraf = require('mz-modules/rimraf');
const fse = require('fs-extra');
const globby = require('globby');
const ncc = require('@midwayjs/ncc');
const typescript = require('typescript');
const terser = require('terser');

const shebangRegEx = /^#![^\n\r]*[\r\n]/;
const inlineSourceMapRegEx = /\/\/# sourceMappingURL=data:application\/json;base64,(.*)/;

class BuildCommand extends Command {
constructor(rawArgv) {
Expand Down Expand Up @@ -37,6 +41,9 @@ class BuildCommand extends Command {
type: 'string',
default: '',
},
minify: {
type: 'boolean',
},
mode: {
description: 'bundle mode, "debug" or "release" (default)',
type: 'string',
Expand Down Expand Up @@ -94,6 +101,10 @@ class BuildCommand extends Command {
args.push(argv.project);
}
await this.helper.forkNode(tscCli, args, { cwd, execArgv: [] });

if (argv.minify) {
await this.minify(tsConfig, outDirAbsolute);
}
}

async bundle(entry, outDir, { sourceMap = false, mode } = {}) {
Expand Down Expand Up @@ -207,6 +218,69 @@ class BuildCommand extends Command {
return tsConfig.compilerOptions[optionKeyPath];
}
}

async minify(tsConfig, outDir) {
const inlineSourceMap = !!tsConfig.compilerOptions.inlineSourceMap;
const sourceMap = inlineSourceMap || tsConfig.compilerOptions.sourceMap;
if (!sourceMap) {
return;
}

let files;
if (outDir) {
files = globby.sync([ '**/*.js' ], {
cwd: outDir,
ignore: [ '**/node_modules' ],
});
files = files.map(it => path.join(outDir, it));
} else {
const host = typescript.createCompilerHost(tsConfig.compilerOptions);
files = host.readDirectory(__dirname, [ '.ts' ], tsConfig.exclude, tsConfig.include);
files = files.map(it => {
return path.join(path.dirname(it), path.basename(it, '.js'));
});
}

for (const file of files) {
let code = await fsPromise.readFile(file, 'utf8');
let map;
if (inlineSourceMap) {
map = this.parseInlineSourceMap(code);
} else {
map = await fsPromise.readFile(file + '.map', 'utf8');
}
map = JSON.parse(map);
const result = terser.minify(code, {
compress: false,
mangle: {
keep_classnames: true,
keep_fnames: true,
},
sourceMap: {
content: map,
filename: path.basename(file),
url: inlineSourceMap ? 'inline' : `${path.basename(file)}.map`,
},
});
({ code, map } = result);
if (code == null) {
break;
}
if (!inlineSourceMap) {
await fsPromise.writeFile(file + '.map', map, 'utf8');
}
await fsPromise.writeFile(file, code, 'utf8');
}
}

parseInlineSourceMap(code) {
const match = inlineSourceMapRegEx.exec(code);
if (match == null) {
return;
}
const map = Buffer.from(match[1], 'base64').toString('utf8');
return map;
}
}

module.exports = BuildCommand;
17 changes: 17 additions & 0 deletions packages/midway-bin/test/lib/cmd/build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,20 @@ describe('test/lib/cmd/build.test.js - bundling', () => {
await rimraf(path.join(cwd, 'dist'));
});
});

describe('test/lib/cmd/build.test.js - minify', () => {
const midwayBin = require.resolve('../../../bin/midway-bin.js');
const argv = [ 'build', '-c', '--minify'];

afterEach(mm.restore);

it('should build success', async () => {
const cwd = path.join(__dirname, '../../fixtures/ts-dir');
await rimraf(path.join(cwd, 'dist'));
const child = coffee.fork(midwayBin, argv, { cwd });
await child.expect('code', 0).end();
assert(fs.existsSync(path.join(cwd, 'dist/a.js')));
assert(/\/\/# sourceMappingURL/.exec(fs.readFileSync(path.join(cwd, 'dist/a.js'), 'utf8')));
await rimraf(path.join(cwd, 'dist'));
});
});

0 comments on commit 72f1b36

Please sign in to comment.