Skip to content

Commit

Permalink
fix: babel7 not support forceEnv, use envName replace
Browse files Browse the repository at this point in the history
  • Loading branch information
hubcarl committed Nov 7, 2018
1 parent d42c93a commit 5e6f49d
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 10 deletions.
19 changes: 14 additions & 5 deletions lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ class WebpackBaseBuilder extends Config {

if (match && loader) {
const babel = this.createBabelLoader();
const babelOptions = this.merge(babel.options, { babelrc: this.babelrc });
const babelrc = this.projectBabelrc ? undefined : this.babelrc;
const babelOptions = this.merge(babel.options, { babelrc });
const babelLoaderString = `${babel.loader}?${JSON.stringify(babelOptions)}`;
const entryLoaderString = this.utils.getLoaderOptionString(loader, { templateFile });
const regMatch = match instanceof RegExp ? match : new RegExp(match);
Expand Down Expand Up @@ -593,17 +594,25 @@ class WebpackBaseBuilder extends Config {

createBabelLoader() {
const loaderName = 'babel-loader';
const options = process.env.BABEL_ENV ? { forceEnv: process.env.BABEL_ENV } : {};
const babelOptions = {};
const babelEnv = process.env.BABEL_ENV;
if (babelEnv) {
if (this.isBabel7) { // @babel/core use envName key
babelOptions.envName = babelEnv;
} else { // babel-core use forceEnv key
babelOptions.forceEnv = babelEnv;
}
}
const compile = this.config.compile || {};
const configCacheOptions = this.utils.isObject(compile.cache) ? compile.cache : {};
const cacheOptions = this.merge(options, configCacheOptions);
const config = compile.cache ? this.createCacheLoader(cacheOptions, loaderName) : { loader: loaderName, options };
const cacheOptions = this.merge(babelOptions, configCacheOptions);
const config = compile.cache ? this.createCacheLoader(cacheOptions, loaderName) : { loader: loaderName, babelOptions };
// use project .babelrc
if (fs.existsSync(this.projectBabelrc)) {
return config;
}
// use default babelrc
return this.merge(config, this.merge(options, this.babelConfig));
return this.merge(config, this.merge(babelOptions, this.babelConfig));
}

createCacheLoader(loaderOptions, name) {
Expand Down
2 changes: 1 addition & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class WebpackClientBuilder extends WebpackBaseBuilder {
this.setPack(config.packs);
this.setTarget(WebpackClientBuilder.TARGET);
this.setCommonsChunkLib();
this.setCreateQueue(this.setBabelENV);
this.setStartCreateQueue(this.setBabelENV);
this.setCreateQueue(this.createDllAssetPlugin);
this.setCreateQueue(this.createDllReferencePlugin);
this.setCreateQueue(this.createHTML);
Expand Down
2 changes: 2 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ class Config {
} else {
this.babelrc = path.resolve(__dirname, '../config/babelrc');
}
const filepath = path.resolve(this.baseDir, 'node_modules', '@babel/core');
this.isBabel7 = fs.existsSync(filepath);
}

initialize(config) {
Expand Down
4 changes: 2 additions & 2 deletions lib/dll.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ class WebpackDllBuilder extends WebpackBaseBuilder {
constructor(config) {
super(config);
this.type = WebpackDllBuilder.TYPE;
this.setBabelENV('web');
this.setTarget(WebpackDllBuilder.TARGET);
this.mergeConfig(Config.dllConfig);
this.setBuildPath(this.utils.getDllCompileFileDir(this.env), true);
this.setDevTool(config.devtool);
this.setDllEntry(config.dll);
this.setTarget(WebpackDllBuilder.TARGET);
this.setLibrary('[name]');
this.setStartCreateQueue(this.setBabelENV);
this.setCreateQueue(this.createDllPlugin);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class WebpackServerBuilder extends WebpackBaseBuilder {
};
this.setExternals([nodeExternals(this.merge(defaultNodeExternals, this.config.nodeExternals))]);
this.createFileName();
this.setCreateQueue(this.setBabelENV);
this.setStartCreateQueue(this.setBabelENV);
}

createFileName() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "easywebpack",
"version": "4.9.3",
"version": "4.9.4",
"description": "基于 Webpack 的前端构建工程化解决方案",
"keywords": [
"webpack",
Expand Down

0 comments on commit 5e6f49d

Please sign in to comment.