-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
feat: support webpackLoaders and webpackPlugins #3938
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7ac69ff
feat: support webpackLoaders and webpackPlugins
ClarkXia c674989
feat: support before and after
ClarkXia 8ebfbc2
docs: build config of webpackPlugin and webpackLoaders
ClarkXia fbd400f
fix: oneOf rules
ClarkXia 5dccf9a
fix: resourceQuery
ClarkXia a2d376a
chore: doc
ClarkXia 7d095af
chore: typo
ClarkXia c7d4164
Merge branch 'release-next' into feat-configs
ClarkXia 0de4d98
Merge branch 'release-next' into feat-configs
ClarkXia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
packages/build-user-config/src/userConfig/webpackLoaders.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
const genRegExpRule = (value) => new RegExp(Array.isArray(value) ? value.join('|') : value); | ||
const ensureArray = (value) => Array.isArray(value) ? value : [value]; | ||
|
||
const optionAPIs = { | ||
test: (rule, value) => { | ||
rule.test(genRegExpRule(value)); | ||
}, | ||
oneOf: (rule, value) => { | ||
/** | ||
* config.module | ||
* .rule('css') | ||
* .oneOf('inline') | ||
* .resourceQuery(/inline/) | ||
* .use('url') | ||
* .loader('url-loader') | ||
* .end() | ||
* .end() | ||
* .oneOf('external') | ||
* .resourceQuery(/external/) | ||
* .use('file') | ||
* .loader('file-loader') | ||
*/ | ||
Object.keys(value).forEach((oneOfName) => { | ||
const { resourceQuery, loaders } = value[oneOfName]; | ||
const loaderRule = rule.oneOf(oneOfName); | ||
if (resourceQuery) { | ||
loaderRule.resourceQuery(genRegExpRule(resourceQuery)); | ||
} | ||
configRuleLoaders(loaderRule, loaders || {}); | ||
}); | ||
}, | ||
// clear include rules | ||
includeClear: (rule) => { | ||
rule.include.clear(); | ||
}, | ||
include: (rule, value) => { | ||
ensureArray(value).forEach((includeValue) => { | ||
rule.include.add(includeValue); | ||
}); | ||
}, | ||
// clear exclude rules | ||
excludeClear: (rule) => { | ||
rule.exclude.clear(); | ||
}, | ||
exclude: (rule, value) => { | ||
ensureArray(value).forEach((excludeValue) => { | ||
rule.exclude.add(excludeValue); | ||
}); | ||
}, | ||
pre: (rule) => { | ||
rule.pre(); | ||
}, | ||
post: (rule) => { | ||
rule.post(); | ||
}, | ||
enforce: (rule) => { | ||
rule.enforce(); | ||
}, | ||
before: (rule, value) => { | ||
rule.before(value); | ||
}, | ||
after: (rule, value) => { | ||
rule.after(value); | ||
}, | ||
}; | ||
const validRuleOption = Object.keys(optionAPIs); | ||
|
||
function configModuleRule(rule, options) { | ||
// loop validRuleOption to make sure optionAPIs excute in order | ||
validRuleOption.forEach((optionsKey) => { | ||
const optionValue = options[optionsKey]; | ||
if (optionValue) { | ||
optionAPIs[optionsKey](rule, optionValue); | ||
} | ||
}); | ||
} | ||
|
||
function configRuleLoaders(rule, loaders) { | ||
const loaderNames = Object.keys(loaders); | ||
loaderNames.forEach((loaderName) => { | ||
const { options, before, after } = loaders[loaderName]; | ||
// check if loader is exsits | ||
let loaderRule = null; | ||
if (rule.uses.has(loaderName)) { | ||
loaderRule = rule.use(loaderName).tap(opts => ({ ...opts, ...options})); | ||
} else { | ||
loaderRule = rule.use(loaderName).loader(loaderName).options(options); | ||
} | ||
if (before) loaderRule.before(before); | ||
if (after) loaderRule.after(after); | ||
}); | ||
} | ||
|
||
module.exports = (config, webpackLoaders) => { | ||
if (webpackLoaders) { | ||
const ruleNames = Object.keys(webpackLoaders); | ||
ruleNames.forEach((ruleName) => { | ||
// create new rule if module rule is not exists | ||
const rule = config.module.rule(ruleName); | ||
const ruleOptions = webpackLoaders[ruleName]; | ||
configModuleRule(rule, ruleOptions); | ||
configRuleLoaders(rule, ruleOptions.loaders || {}); | ||
}); | ||
} | ||
}; |
28 changes: 28 additions & 0 deletions
28
packages/build-user-config/src/userConfig/webpackPlugins.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module.exports = (config, webpackPlugins, context) => { | ||
if (webpackPlugins) { | ||
const pluginNames = Object.keys(webpackPlugins); | ||
pluginNames.forEach((pluginName) => { | ||
const { options, after, before } = webpackPlugins[pluginName]; | ||
let plguinRule = null; | ||
// check if plugin has been already registed | ||
if (config.plugins.has(pluginName)) { | ||
// modify plugin options | ||
plguinRule = config.plugin(pluginName).tap(([args]) => [{...args, ...options}]); | ||
} else { | ||
// add new plugin | ||
let plugin = null; | ||
if (pluginName.match(/^webpack\./)) { | ||
// webpack builtin plugins | ||
const { webpack } = context; | ||
plugin = webpack[pluginName]; | ||
} else { | ||
// eslint-disable-next-line | ||
plugin = require(pluginName); | ||
} | ||
plguinRule = config.plugin(pluginName).use(plugin, [options]); | ||
} | ||
if (before) plguinRule.before(before); | ||
if (after) plguinRule.after(after); | ||
}); | ||
} | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
pluginRule