Skip to content

Commit

Permalink
feat: support babel default config
Browse files Browse the repository at this point in the history
  • Loading branch information
hubcarl committed Oct 19, 2018
1 parent 1bce900 commit df9bb78
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
17 changes: 17 additions & 0 deletions config/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"presets": [
["env", {
"modules": false,
"targets": {
"browsers": ["last 2 versions", "safari >= 7"]
}
}]
],
"plugins": [
"transform-object-rest-spread",
"syntax-dynamic-import",
"transform-object-assign",
"transform-runtime"
],
"comments": false
}
18 changes: 0 additions & 18 deletions config/babelrc.js

This file was deleted.

14 changes: 8 additions & 6 deletions lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ class WebpackBaseBuilder extends Config {
if (this.entryLoader) {
const mergeLoader = this.merge(entryLoader, this.entryLoader);
const entryLoaderOptions = mergeLoader.options || mergeLoader;
const { match, loader, babel = 'babel-loader' } = entryLoaderOptions;
const { match, loader } = entryLoaderOptions;
let templateFile = entryLoaderOptions.templateFile || '';
// 零配置:设置默认 entry 模板代码路径配置, TODO entry exclude 实现
if (templateFile) {
Expand All @@ -299,14 +299,17 @@ class WebpackBaseBuilder extends Config {
}

if (match && loader) {
const babel = this.createBabelLoader();
const babelOptions = this.merge(babel.options, { babelrc: this.babelrc });
const babelLoaderString = `${babel.loader}?${JSON.stringify(babelOptions)}`;
const entryLoaderString = this.utils.getLoaderOptionString(loader, { templateFile });
const regMatch = match instanceof RegExp ? match : new RegExp(match);
Object.keys(entries).forEach(entryName => {
const entryFile = entries[entryName];
if (this.utils.isString(entryFile)) {
const fileExt = path.extname(entryFile);
if (regMatch.test(fileExt)) {
entries[entryName] = [babel, entryLoaderString, entryFile].join('!');
entries[entryName] = [babelLoaderString, entryLoaderString, entryFile].join('!');
}
}
});
Expand Down Expand Up @@ -592,8 +595,7 @@ class WebpackBaseBuilder extends Config {
return config;
}
// use default babelrc
const options = require(this.babelrc);
return this.merge(config, { options });
return this.merge(config, { options : this.babelConfig });
}

createCacheLoader(loaderOptions, name) {
Expand Down Expand Up @@ -658,8 +660,8 @@ class WebpackBaseBuilder extends Config {
setBabelENV(value) {
const key = value || this.webpackConfig.target;
try {
const json = require(this.babelrc);
if (json && json.env && json.env[key]) {
const { env } = this.babelConfig;
if (env && env[key]) {
process.env.BABEL_ENV = key;
}
} catch (e) {
Expand Down
8 changes: 8 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ class Config {
return hotPlugin && this.isEnable(hotPlugin.enable);
}

get babelConfig() {
if (this._babelConfig) {
return this._babelConfig;
}
this._babelConfig = this.utils.readFile(this.babelrc) || {};
return this._babelConfig;
}

initZero(config) {
if (this.egg) {
zero.initEggDefault(config);
Expand Down

0 comments on commit df9bb78

Please sign in to comment.