Skip to content

Commit

Permalink
fix(deps): add terser to depenencies (#390)
Browse files Browse the repository at this point in the history
* fix(deps): add terser to depenencies

* fix(build): prevent from using fs.promises

Node.js v8 still need to be supported in midway v1
  • Loading branch information
legendecas authored Feb 17, 2020
1 parent ee93773 commit e6da77e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/midway-bin/lib/cmd/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
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');
let typescript;

const shebangRegEx = /^#![^\n\r]*[\r\n]/;
const inlineSourceMapRegEx = /\/\/# sourceMappingURL=data:application\/json;base64,(.*)/;
Expand Down Expand Up @@ -220,6 +219,10 @@ class BuildCommand extends Command {
}

async minify(tsConfig, outDir) {
if (typescript == null) {
typescript = require('typescript');
}

const inlineSourceMap = !!tsConfig.compilerOptions.inlineSourceMap;
const sourceMap = inlineSourceMap || tsConfig.compilerOptions.sourceMap;
if (!sourceMap) {
Expand All @@ -242,12 +245,12 @@ class BuildCommand extends Command {
}

for (const file of files) {
let code = await fsPromise.readFile(file, 'utf8');
let code = await fse.readFile(file, 'utf8');
let map;
if (inlineSourceMap) {
map = this.parseInlineSourceMap(code);
} else {
map = await fsPromise.readFile(file + '.map', 'utf8');
map = await fse.readFile(file + '.map', 'utf8');
}
map = JSON.parse(map);
const result = terser.minify(code, {
Expand All @@ -267,9 +270,9 @@ class BuildCommand extends Command {
break;
}
if (!inlineSourceMap) {
await fsPromise.writeFile(file + '.map', map, 'utf8');
await fse.writeFile(file + '.map', map, 'utf8');
}
await fsPromise.writeFile(file, code, 'utf8');
await fse.writeFile(file, code, 'utf8');
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/midway-bin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"globby": "^10.0.1",
"jest-environment-node": "^24.9.0",
"mz-modules": "^2.1.0",
"terser": "^4.6.3",
"typedoc": "^0.14.2"
},
"devDependencies": {
Expand Down

0 comments on commit e6da77e

Please sign in to comment.