-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
支持对依赖的内容进行转换 #109
支持对依赖的内容进行转换 #109
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,10 +36,16 @@ const makeBabelPlugin = plugin => ( | |
: [adaptBabelPluginName(plugin[0]), ...plugin.slice(1)] | ||
) | ||
|
||
// 修改 babel-loader 的配置以适配 webpack2 (enable tree-shaking,由 webpack 来做 module 格式的转换) | ||
// 添加 preset-env 的 targets | ||
// 找到 env 这个 preset,添加 { "modules": false, targets } | ||
// 注意后续可能要修改这边逻辑,考虑会对 import / export 进行转换的不一定只有 env 这个 preset | ||
/** | ||
* @desc 修改 babel-loader 的配置以适配 webpack2 (enable tree-shaking,由 webpack 来做 module 格式的转换) | ||
* 添加 preset-env 的 targets | ||
* 找到 env 这个 preset,添加 { "modules": false, targets } | ||
* 注意后续可能要修改这边逻辑,考虑会对 import / export 进行转换的不一定只有 env 这个 preset | ||
* @param {object} options babel options | ||
* @param {object} targets babel env targets: https://babeljs.io/docs/en/babel-preset-env#targets | ||
* @return {object} | ||
* TODO: 检查现在 webpack4 是不是还需要 modules: false 的逻辑 | ||
*/ | ||
const makeBabelLoaderOptions = (options, targets) => { | ||
if (!options) { | ||
return options | ||
|
@@ -87,7 +93,7 @@ const adaptLoader = ({ loader, options }) => { | |
return loaderObj | ||
} | ||
|
||
const makeRule = (extension, context, ...loaderList) => { | ||
const makeRule = (extension, context, exclude, ...loaderList) => { | ||
const rule = { | ||
test: makeExtensionPattern(extension), | ||
use: loaderList.map(adaptLoader) | ||
|
@@ -104,10 +110,10 @@ const makeRule = (extension, context, ...loaderList) => { | |
rule.issuer = makeExtensionPattern(context) | ||
} | ||
|
||
// 针对后缀为 js 的 transform,控制范围(不对依赖做转换) | ||
if (extension === 'js') { | ||
rule.exclude = /(node_modules)/ | ||
if (exclude != null) { | ||
rule.exclude = exclude | ||
} | ||
|
||
return rule | ||
} | ||
|
||
|
@@ -145,12 +151,24 @@ function makePostcssOptions({ autoprefixerOptions }) { | |
} | ||
} | ||
|
||
/** | ||
* @desc 构造处理 Javascript 内容时排除依赖用的正则 | ||
* @param {boolean|string[]} transformDeps 是否排除依赖,或需要被处理(不应该被排除)的依赖包名 | ||
* @return {RegExp} | ||
*/ | ||
function makeJsExcludePattern(transformDeps) { | ||
if (Array.isArray(transformDeps)) { | ||
return new RegExp(`node_modules/(?!(${transformDeps.join('|')})/).*`) | ||
} | ||
return transformDeps ? null : /node_modules\// | ||
} | ||
|
||
module.exports = (config, key, transform, buildConfig, post) => { | ||
if (!key || typeof key !== 'string') { | ||
throw new TypeError(`Invalid transform key: ${JSON.stringify(key)}`) | ||
} | ||
|
||
const { targets } = buildConfig | ||
const { targets, optimization } = buildConfig | ||
const [extension, context] = key.split('@') | ||
|
||
const isTesting = buildEnv.get() === buildEnv.test | ||
|
@@ -171,6 +189,13 @@ module.exports = (config, key, transform, buildConfig, post) => { | |
return config | ||
} | ||
|
||
// 针对后缀为 js 的 transform,控制范围(不对依赖做转换) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 为啥特意限制一下呀,这个是不是太 hack 了。。毕竟从表面上看并看不出会有这个限制。。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @codingstar 你指哪个限制? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 就是不 transform 后缀为 js 的文件。。。0 0 @nighca There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @codingstar 是工程上的常见优化手段,基于两个前提:
|
||
const exclude = ( | ||
extension === 'js' | ||
? makeJsExcludePattern(optimization.transformDeps) | ||
: null | ||
) | ||
|
||
switch(transform.transformer) { | ||
case transforms.css: | ||
case transforms.less: | ||
|
@@ -212,7 +237,7 @@ module.exports = (config, key, transform, buildConfig, post) => { | |
|
||
config = update(config, { | ||
module: { rules: { | ||
$push: [makeRule(extension, context, ...loaders)] | ||
$push: [makeRule(extension, context, exclude, ...loaders)] | ||
} } | ||
}) | ||
break | ||
|
@@ -223,7 +248,7 @@ module.exports = (config, key, transform, buildConfig, post) => { | |
config = addDefaultExtension(config, extension) | ||
config = update(config, { | ||
module: { rules: { | ||
$push: [makeRule(extension, context, { loader: 'babel', options: babelLoaderOptions })] | ||
$push: [makeRule(extension, context, exclude, { loader: 'babel', options: babelLoaderOptions })] | ||
} } | ||
}) | ||
break | ||
|
@@ -237,7 +262,7 @@ module.exports = (config, key, transform, buildConfig, post) => { | |
config = addDefaultExtension(config, extension) | ||
config = update(config, { | ||
module: { rules: { | ||
$push: [makeRule(extension, context, { | ||
$push: [makeRule(extension, context, exclude, { | ||
loader: 'babel', | ||
options: makeBabelLoaderOptions( | ||
makeReactBabelOptions(transformConfig.babelOptions), | ||
|
@@ -276,6 +301,7 @@ module.exports = (config, key, transform, buildConfig, post) => { | |
$push: [makeRule( | ||
extension, | ||
context, | ||
exclude, | ||
{ loader: 'babel', options: makeBabelLoaderOptions(babelOptions, targets) }, | ||
{ loader: 'ts', options: tsLoaderOptions } | ||
)] | ||
|
@@ -289,7 +315,7 @@ module.exports = (config, key, transform, buildConfig, post) => { | |
config = addDefaultExtension(config, extension) | ||
config = update(config, { | ||
module: { rules: { | ||
$push: [makeRule(extension, context, { loader: transform.transformer, options: transform.config })] | ||
$push: [makeRule(extension, context, exclude, { loader: transform.transformer, options: transform.config })] | ||
} } | ||
}) | ||
break | ||
|
@@ -299,7 +325,7 @@ module.exports = (config, key, transform, buildConfig, post) => { | |
config = update(config, { | ||
module: { rules: { | ||
$push: [ | ||
makeRule(extension, context, { | ||
makeRule(extension, context, exclude, { | ||
loader: 'file', | ||
options: { name: 'static/[name]-[hash].[ext]' } | ||
}) | ||
|
@@ -342,7 +368,7 @@ module.exports = (config, key, transform, buildConfig, post) => { | |
config = update(config, { | ||
module: { rules: { | ||
$push: [ | ||
makeRule(extension, context, { | ||
makeRule(extension, context, exclude, { | ||
loader: 'vue', | ||
options | ||
}) | ||
|
@@ -356,7 +382,7 @@ module.exports = (config, key, transform, buildConfig, post) => { | |
config = update(config, { | ||
module: { rules: { | ||
$push: [ | ||
makeRule(extension, context, { | ||
makeRule(extension, context, exclude, { | ||
loader: 'svg-sprite', | ||
options: { | ||
// TODO: | ||
|
@@ -375,7 +401,7 @@ module.exports = (config, key, transform, buildConfig, post) => { | |
default: { | ||
config = update(config, { | ||
module: { rules: { | ||
$push: [makeRule(extension, context, { loader: transform.transformer, options: transform.config })] | ||
$push: [makeRule(extension, context, exclude, { loader: transform.transformer, options: transform.config })] | ||
} } | ||
}) | ||
} | ||
|
+3 −0 | transform-deps/.gitignore | |
+25 −0 | transform-deps/build-config.json | |
+7 −0 | transform-deps/fec-test.sh | |
+3 −0 | transform-deps/node_modules/foo/index.js | |
+798 −0 | transform-deps/package-lock.json | |
+10 −0 | transform-deps/package.json | |
+12 −0 | transform-deps/src/index.html | |
+5 −0 | transform-deps/src/index.js |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
会不会这样比较好懂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里主要是考虑了下值为
undefined
等的边际情况