Reputation: 12511
Using Webpack's DefinePlugin, is it possible to change the value of a key based on the Webpack build context? Such as entry name, output details, etc.
For example:
module.exports = {
entry: {
main: "main.js",
"main-free": "main.js",
},
plugins: [
new webpack.DefinePlugin({
FREE_VERSION: (ctx) => ctx.entryName.endsWith("-free"),
}),
],
output: {
filename: `[name].js`,
},
};
// main.js
if (FREE_VERSION) {
alert("You're using a free version.");
} else {
alert("Thank you for purchasing.");
}
Upvotes: 0
Views: 219
Reputation: 12071
I don't think you can do this with the DefinePlugin, but you might be able to get around it by exporting an array of configs:
function getConfig(entryKey, entryValue, isFree) {
return {
entry: {
[entryKey]: entryValue
},
plugins: [
new webpack.DefinePlugin({
FREE_VERSION: isFree,
}),
],
output: {
filename: `[name].js`,
},
}
};
module.exports = [
getConfig("main", "main.js", false),
getConfig("main-free", "main.js", true),
];
Upvotes: 1