加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
rewire-scss.js 1.71 KB
一键复制 编辑 原始数据 按行查看 历史
MrStack 提交于 2018-05-16 12:11 . change the directory
const getRules = (config) =>
config.module.rules.find((rule) => Object.keys(rule).includes('oneOf')).oneOf;
const findFileLoaderRuleFn = (rule) =>
typeof rule.loader === 'string' && rule.loader.includes('file-loader');
const findStyleLoaderRuleFn = (rule) =>
rule.test.toString() === /\.css$/.toString();
function rewireSass(config, env, sassOptions = {}) {
// find the non-javascript ruleset in the webpack config
const rules = getRules(config);
// find the file-loader and add a rule excluding sass files from being loaded as text
config.module.rules[1].oneOf
.find(findFileLoaderRuleFn)
.exclude.push(/\.scss$/);
// find the current rule for loading css files
const styleLoaderRule = rules.find(findStyleLoaderRuleFn);
// allows the test to be pre-defined by react-scripts as an array or a single regex
const currentTests = Array.isArray(styleLoaderRule.test)
? [...styleLoaderRule.test]
: [styleLoaderRule.test];
// add regexes for scss files
styleLoaderRule.test = [...currentTests, /\.scss$/, /\.sass$/];
if (styleLoaderRule.loader) {
styleLoaderRule.loader.push({
loader: require.resolve('sass-loader'),
options: sassOptions,
});
styleLoaderRule.loader.push({
loader: require.resolve('ice-skin-loader'),
options: {
themeFile: require.resolve('@icedesign/skin'),
},
});
}
if (styleLoaderRule.use) {
styleLoaderRule.use.push({
loader: require.resolve('sass-loader'),
options: sassOptions,
});
styleLoaderRule.use.push({
loader: require.resolve('ice-skin-loader'),
options: {
themeFile: require.resolve('@icedesign/skin'),
},
});
}
return config;
}
module.exports = rewireSass;
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化