Looks like DefinePlugin just replaces text and does not introduce global variables.
So it needs quotes around the value so the replacement makes a correct JS syntax:
'APP_URL': `"${dotenv.APP_URL}"`,
Here's my webpack.config.js:
const webpack = require('webpack');
const dotenv = { APP_URL: 'http://localhost/app' };
module.exports = [
{
entry: './src/test.js',
mode: 'production',
plugins: [
new webpack.DefinePlugin({
'APP_URL': `"${dotenv.APP_URL}"`,
})
]
}
]
Don't forget to put quotes around the values or use JSON.stringify
And here's ./src/test.js file:
const url = APP_URL;
console.log(url);
Development build outputs eval statement with the whole code and replacement:
/***/ "./src/test.js":
/*!*********************!*\
!*** ./src/test.js ***!
\*********************/
/*! no static exports found */
/***/ (function(module, exports) {
eval("const url = \"http://localhost/app\";\nconsole.log(url);\n\n//# sourceURL=webpack:///./src/test.js?");
/***/ })
/******/ });
While production build keeps just resulting console.log call:
[function(e,t){console.log("http://localhost/app")}]