Skip to content

Commit d309bdc

Browse files
legendecasczy88840616
authored andcommitted
feat(build): options to minify all products (#389)
(cherry picked from commit 86d5279)
1 parent 9c90eb1 commit d309bdc

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

packages/midway-bin/lib/cmd/build.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
const Command = require('egg-bin').Command;
44
const path = require('path');
55
const fs = require('fs');
6+
const fsPromise = fs.promises;
67
const rimraf = require('mz-modules/rimraf');
78
const fse = require('fs-extra');
89
const globby = require('globby');
910
const ncc = require('@midwayjs/ncc');
11+
const typescript = require('typescript');
12+
const terser = require('terser');
1013

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

1317
class BuildCommand extends Command {
1418
constructor(rawArgv) {
@@ -37,6 +41,9 @@ class BuildCommand extends Command {
3741
type: 'string',
3842
default: '',
3943
},
44+
minify: {
45+
type: 'boolean',
46+
},
4047
mode: {
4148
description: 'bundle mode, "debug" or "release" (default)',
4249
type: 'string',
@@ -94,6 +101,10 @@ class BuildCommand extends Command {
94101
args.push(argv.project);
95102
}
96103
await this.helper.forkNode(tscCli, args, { cwd, execArgv: [] });
104+
105+
if (argv.minify) {
106+
await this.minify(tsConfig, outDirAbsolute);
107+
}
97108
}
98109

99110
async bundle(entry, outDir, { sourceMap = false, mode } = {}) {
@@ -207,6 +218,69 @@ class BuildCommand extends Command {
207218
return tsConfig.compilerOptions[optionKeyPath];
208219
}
209220
}
221+
222+
async minify(tsConfig, outDir) {
223+
const inlineSourceMap = !!tsConfig.compilerOptions.inlineSourceMap;
224+
const sourceMap = inlineSourceMap || tsConfig.compilerOptions.sourceMap;
225+
if (!sourceMap) {
226+
return;
227+
}
228+
229+
let files;
230+
if (outDir) {
231+
files = globby.sync([ '**/*.js' ], {
232+
cwd: outDir,
233+
ignore: [ '**/node_modules' ],
234+
});
235+
files = files.map(it => path.join(outDir, it));
236+
} else {
237+
const host = typescript.createCompilerHost(tsConfig.compilerOptions);
238+
files = host.readDirectory(__dirname, [ '.ts' ], tsConfig.exclude, tsConfig.include);
239+
files = files.map(it => {
240+
return path.join(path.dirname(it), path.basename(it, '.js'));
241+
});
242+
}
243+
244+
for (const file of files) {
245+
let code = await fsPromise.readFile(file, 'utf8');
246+
let map;
247+
if (inlineSourceMap) {
248+
map = this.parseInlineSourceMap(code);
249+
} else {
250+
map = await fsPromise.readFile(file + '.map', 'utf8');
251+
}
252+
map = JSON.parse(map);
253+
const result = terser.minify(code, {
254+
compress: false,
255+
mangle: {
256+
keep_classnames: true,
257+
keep_fnames: true,
258+
},
259+
sourceMap: {
260+
content: map,
261+
filename: path.basename(file),
262+
url: inlineSourceMap ? 'inline' : `${path.basename(file)}.map`,
263+
},
264+
});
265+
({ code, map } = result);
266+
if (code == null) {
267+
break;
268+
}
269+
if (!inlineSourceMap) {
270+
await fsPromise.writeFile(file + '.map', map, 'utf8');
271+
}
272+
await fsPromise.writeFile(file, code, 'utf8');
273+
}
274+
}
275+
276+
parseInlineSourceMap(code) {
277+
const match = inlineSourceMapRegEx.exec(code);
278+
if (match == null) {
279+
return;
280+
}
281+
const map = Buffer.from(match[1], 'base64').toString('utf8');
282+
return map;
283+
}
210284
}
211285

212286
module.exports = BuildCommand;

packages/midway-bin/test/lib/cmd/build.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,20 @@ describe('test/lib/cmd/build.test.js - bundling', () => {
170170
await rimraf(path.join(cwd, 'dist'));
171171
});
172172
});
173+
174+
describe('test/lib/cmd/build.test.js - minify', () => {
175+
const midwayBin = require.resolve('../../../bin/midway-bin.js');
176+
const argv = [ 'build', '-c', '--minify'];
177+
178+
afterEach(mm.restore);
179+
180+
it('should build success', async () => {
181+
const cwd = path.join(__dirname, '../../fixtures/ts-dir');
182+
await rimraf(path.join(cwd, 'dist'));
183+
const child = coffee.fork(midwayBin, argv, { cwd });
184+
await child.expect('code', 0).end();
185+
assert(fs.existsSync(path.join(cwd, 'dist/a.js')));
186+
assert(/\/\/# sourceMappingURL/.exec(fs.readFileSync(path.join(cwd, 'dist/a.js'), 'utf8')));
187+
await rimraf(path.join(cwd, 'dist'));
188+
});
189+
});

0 commit comments

Comments
 (0)