javascript cra antd css / less module
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript cra antd css / less module相关的知识,希望对你有一定的参考价值。
// https://github.com/timarney/react-app-rewired/issues/116
const path = require("path");
const { compose, getLoader, injectBabelPlugin } = require("react-app-rewired");
const rewireLessModules = (prefix = "", lessOptions = {}) => {
const cloneDeep = require("lodash/clonedeep");
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(`/${loader}/`) !== -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);
};
return function(config, env) {
const cssRule = findRule(config.module.rules, cssRuleMatcher);
cssRule.exclude = path.resolve(__dirname, "src");
cssRule.include = path.resolve(__dirname, "node_modules");
const lessRule = cloneDeep(cssRule);
lessRule.test = /\.less$/;
const cssModulesRule = cloneDeep(cssRule);
cssModulesRule.include = path.resolve(__dirname, "src");
cssModulesRule.exclude = path.resolve(__dirname, "node_modules");
const cssModulesRuleCssLoader = findRule(cssModulesRule, cssLoaderMatcher);
cssModulesRuleCssLoader.options = Object.assign(
{
modules: true,
localIdentName: `${env === "production"
? prefix
: "[local]"}-[hash:base64:8]`,
},
cssModulesRuleCssLoader.options,
);
addBeforeRule(config.module.rules, fileLoaderMatcher, cssModulesRule);
addAfterRule(lessRule, postcssLoaderMatcher, {
loader: require.resolve("less-loader"),
options: lessOptions,
});
addBeforeRule(config.module.rules, fileLoaderMatcher, lessRule);
const lessModulesRule = cloneDeep(cssModulesRule);
lessModulesRule.test = lessRule.test;
addAfterRule(
lessModulesRule,
postcssLoaderMatcher,
require.resolve("less-loader"),
);
addBeforeRule(config.module.rules, fileLoaderMatcher, lessModulesRule);
return config;
};
};
module.exports = function override(config, env) {
config = injectBabelPlugin(
["import", { libraryName: "antd", style: true, libraryDirectory: "es" }],
config,
);
const rewires = compose(
// rewireHTML,
rewireLessModules("comment", {
javascriptEnabled: true,
modifyVars: {
"@font-size-base": "14px",
},
}),
);
return rewires(config, env);
};
以上是关于javascript cra antd css / less module的主要内容,如果未能解决你的问题,请参考以下文章
使用 css prop 运行带有 CRA 和 Emotion 的 Quokka.js 时出现错误(运行时为经典时无法设置 importSource)