diff --git a/config/.babelrc b/config/.babelrc new file mode 100755 index 0000000..b8f987f --- /dev/null +++ b/config/.babelrc @@ -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 +} \ No newline at end of file diff --git a/config/babelrc.js b/config/babelrc.js deleted file mode 100755 index 7b32462..0000000 --- a/config/babelrc.js +++ /dev/null @@ -1,18 +0,0 @@ -'use strict'; -module.exports = { - 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 -}; \ No newline at end of file diff --git a/lib/base.js b/lib/base.js index 43b2e55..e4c03f4 100644 --- a/lib/base.js +++ b/lib/base.js @@ -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) { @@ -299,6 +299,9 @@ 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 => { @@ -306,7 +309,7 @@ class WebpackBaseBuilder extends Config { 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('!'); } } }); @@ -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) { @@ -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) { diff --git a/lib/config.js b/lib/config.js index 741bc27..f47739e 100644 --- a/lib/config.js +++ b/lib/config.js @@ -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);