Skip to content

Commit

Permalink
feat: support BABEL_ENV node and web single config
Browse files Browse the repository at this point in the history
  • Loading branch information
hubcarl committed Sep 4, 2018
1 parent a0d4400 commit 61df5fb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,22 @@ class WebpackBaseBuilder extends Config {
});
}
}

// 根据 BABEL_ENV 动态获取 BABEL 配置, 支持 node 和 web 配置
setBabelENV(value) {
const babelrc = path.join(this.baseDir, '.babelrc');
if (fs.existsSync(babelrc)) {
const jsonStr = fs.readFileSync(babelrc);
try {
const json = JSON.parse(jsonStr);
if (json && json.env && json.env[value]) {
process.env.BABEL_ENV = value;
}
} catch (e) {
console.warn('setBabelENV error', e);
}
}
}
}

module.exports = WebpackBaseBuilder;
1 change: 1 addition & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class WebpackClientBuilder extends WebpackBaseBuilder {
constructor(config) {
super(config);
this.type = WebpackClientBuilder.TYPE;
this.setBabelENV('web');
this.setDevTool(config.devtool);
this.setPack(config.packs);
this.setTarget(WebpackClientBuilder.TARGET);
Expand Down
1 change: 1 addition & 0 deletions lib/dll.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class WebpackDllBuilder extends WebpackBaseBuilder {
super(merge({ useHost: false, buildDll: true }, config));
this.dll = true;
this.type = WebpackDllBuilder.TYPE;
this.setBabelENV('web');
this.mergeConfig(Config.dllConfig);
this.setBuildPath(this.utils.getDllCompileFileDir(this.env));
this.setDevTool(config.devtool);
Expand Down
1 change: 1 addition & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class WebpackServerBuilder extends WebpackBaseBuilder {
super(config);
this.ssr = true;
this.type = WebpackServerBuilder.TYPE;
this.setBabelENV('node');
this.setTarget(WebpackServerBuilder.TARGET);
this.setNode({ __filename: false, __dirname: false });
this.setLibraryTarget('commonjs');
Expand Down

0 comments on commit 61df5fb

Please sign in to comment.