craco
覆盖webpack
配置craco.config.js
文件// 先解除限制src/以外的文件资源
const definePlugin = webpackConfig.plugins.find(({ constructor }) => {
return constructor && constructor.name === 'DefinePlugin';
});
const scopePluginIndex = webpackConfig.resolve.plugins.findIndex(({ constructor }) => {
return constructor && constructor.name === 'ModuleScopePlugin';
});
definePlugin.definitions['process.env'] = {
...definePlugin.definitions['process.env']
};
webpackConfig.resolve.plugins.splice(scopePluginIndex, 1);
// 添加loader以支持引用src/外资源
const { isFound, match } = getLoader(
webpackConfig,
loaderByName("babel-loader")
);
if (isFound) {
const include = Array.isArray(match.loader.include)
? match.loader.include
: [match.loader.include];
match.loader.include = include.concat(packages);
}
craco.config.js
文件const path = require('path');
const { getLoader, loaderByName } = require("@craco/craco");
const packages = [];
packages.push(path.join(__dirname, "./@types"));
module.exports = {
webpack: {
alias: {
'@': path.resolve(__dirname, 'src/'),
types: path.resolve(__dirname, '@types/'),
dist: path.resolve(__dirname, 'dist/')
},
configure: webpackConfig => {
const definePlugin = webpackConfig.plugins.find(({ constructor }) => {
return constructor && constructor.name === 'DefinePlugin';
});
const scopePluginIndex = webpackConfig.resolve.plugins.findIndex(({ constructor }) => {
return constructor && constructor.name === 'ModuleScopePlugin';
});
definePlugin.definitions['process.env'] = {
...definePlugin.definitions['process.env']
};
webpackConfig.resolve.plugins.splice(scopePluginIndex, 1);
const { isFound, match } = getLoader(
webpackConfig,
loaderByName("babel-loader")
);
if (isFound) {
const include = Array.isArray(match.loader.include)
? match.loader.include
: [match.loader.include];
match.loader.include = include.concat(packages);
}
return webpackConfig;
}
}
};
本文链接:https://blog.crazylei.com/art/fb22efb5
版权声明: 本博客所有文章除特别声明外,均采用CC BY 4.0 CN协议 许可协议。转载请注明出处!
正在加载评论...