customize-cra-plugin
Version:
replace or delete webpack plugin capabilities
60 lines (59 loc) • 1.67 kB
JavaScript
const overrideEntry = (entryConfig, devEntry) => (config) => {
if (devEntry && devEntry.length == 0) {
try {
webpackDevClientEntry = require.resolve(
"react-dev-utils/webpackHotDevClient"
);
devEntry = [webpackDevClientEntry];
} catch (e) {}
}
const entry = entryConfig.reduce((cal, cur) => {
const { name, entry } = cur;
cal[name] =
config.mode === "development" && devEntry.length > 0
? [devEntry, entry]
: [entry];
return cal;
}, {});
config.entry = entry;
config.output.filename = "static/js/[name].js";
return config;
};
const replacePlugin = (name, plugin) => (config) => {
const pluginIndex = config.plugins.findIndex(
(i) => i.constructor.name === name
);
if (pluginIndex !== -1) {
if (plugin) {
const oldPlugin = config.plugins[pluginIndex];
//替换
config.plugins[pluginIndex] = plugin(oldPlugin, config);
} else {
config.plugins.splice(pluginIndex, 1);
}
}
return config;
};
const deletePlugin = (name) => {
return replacePlugin(name, null);
};
const useCustomSass = (sass) => (config) => {
const rules = config.module.rules.find((rule) => Array.isArray(rule.oneOf))
.oneOf;
// console.log(rules,'ff');
rules.forEach(
(r) =>
r.use &&
r.use.forEach((u) => {
// console.log(u.loader)
if (u.loader && u.loader.indexOf("sass-loader") > -1) {
u.options.implementation = sass;
}
})
);
return config;
};
exports.useCustomSass = useCustomSass;
exports.overrideEntry = overrideEntry;
exports.replacePlugin = replacePlugin;
exports.deletePlugin = deletePlugin;