加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
rewire-less.js 2.84 KB
一键复制 编辑 原始数据 按行查看 历史
04D4BC2AE3759789CB2D61708E8CBCEE 提交于 2021-03-04 10:36 . Initial commit
const cloneDeep = require("lodash.clonedeep");
const path = require("path");
const ruleChildren = loader =>
loader.use ||
loader.oneOf ||
(Array.isArray(loader.loader) && loader.loader) ||
[];
const findIndexAndRules = (rulesSource, ruleMatcher) => {
let result = undefined;
const rules = Array.isArray(rulesSource)
? rulesSource
: ruleChildren(rulesSource);
rules.some(
(rule, index) =>
(result = ruleMatcher(rule)
? { index, rules }
: findIndexAndRules(ruleChildren(rule), ruleMatcher))
);
return result;
};
const findRule = (rulesSource, ruleMatcher) => {
const { index, rules } = findIndexAndRules(rulesSource, ruleMatcher);
return rules[index];
};
const cssRuleMatcher = rule =>
rule.test && String(rule.test) === String(/\.css$/);
const createLoaderMatcher = loader => rule =>
rule.loader && rule.loader.indexOf(`${path.sep}${loader}${path.sep}`) !== -1;
const cssLoaderMatcher = createLoaderMatcher("css-loader");
const postcssLoaderMatcher = createLoaderMatcher("postcss-loader");
const fileLoaderMatcher = createLoaderMatcher("file-loader");
const addAfterRule = (rulesSource, ruleMatcher, value) => {
const { index, rules } = findIndexAndRules(rulesSource, ruleMatcher);
rules.splice(index + 1, 0, value);
};
const addBeforeRule = (rulesSource, ruleMatcher, value) => {
const { index, rules } = findIndexAndRules(rulesSource, ruleMatcher);
rules.splice(index, 0, value);
};
function createRewireLess(
localIdentName = `[local]___[hash:base64:5]`,
lessLoaderOptions = {}
) {
return function(config, env) {
const cssRule = findRule(config.module.rules, cssRuleMatcher);
const lessRule = cloneDeep(cssRule);
const cssModulesRule = cloneDeep(cssRule);
cssRule.exclude = /\.module\.css$/;
cssModulesRule.test = /\.module\.css$/;
const cssModulesRuleCssLoader = findRule(cssModulesRule, cssLoaderMatcher);
cssModulesRuleCssLoader.options = Object.assign(
{
modules: true,
localIdentName
},
cssModulesRuleCssLoader.options
);
addBeforeRule(config.module.rules, fileLoaderMatcher, cssModulesRule);
lessRule.test = /\.less$/;
lessRule.exclude = /\.module\.less$/;
addAfterRule(lessRule, postcssLoaderMatcher, {
loader: require.resolve("less-loader"),
options: lessLoaderOptions
});
addBeforeRule(config.module.rules, fileLoaderMatcher, lessRule);
const lessModulesRule = cloneDeep(cssModulesRule);
lessModulesRule.test = /\.module\.less$/;
addAfterRule(lessModulesRule, postcssLoaderMatcher, {
loader: require.resolve("less-loader"),
options: lessLoaderOptions
});
addBeforeRule(config.module.rules, fileLoaderMatcher, lessModulesRule);
return config;
};
}
const rewireLess = createRewireLess();
rewireLess.withLoaderOptions = createRewireLess;
module.exports = rewireLess;
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化