metadata not handled properly when merging webpack.common.js and webpack.<env>.js files
Note: for support questions, please use one of these channels: Chat: AngularClass.slack or Twitter: @AngularClass
-
I'm submitting a ... [x] bug report [ ] feature request [ ] question about the decisions made in the repository
-
Do you want to request a feature or report a bug? Report a bug
-
What is the current behavior? Using the following expression within a webpack.
.js file returns undefined because "metadata" is not a valid property of the object returned by the module.exports function in webpack.common.js:
commonConfig({ env: ENV }).metadata
- If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://plnkr.co or similar (you can use this template as a starting point: http://plnkr.co/edit/tpl:AvJOMERrnz94ekVua0u5).
Insert the following code at webpack.dev.js::23 of the current project:
const superMETADATA = commonConfig({ env: ENV }).metadata;
console.log('superMETADATA inside webpack.dev.js, showing exported commonConfig =>');
console.log(superMETADATA);
then run "npm run build" and you will see the following output:
superMETADATA inside webpack.dev.js, showing exported commonConfig =>
undefined
where "undefined" is the metadata from webpack.common.js, and therefore is not able to merge properly with any webpack.
-
What is the expected behavior? Somehow metadata should be on the object. A valid metadata object should be returned which can be merged with the env-specific webpack config
-
What is the motivation / use case for changing the behavior?
-
Please tell us about your environment:
- Angular version: 2.1.2
- Browser: [Chrome XX | Firefox XX ]
- Other information (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. stackoverflow, gitter, etc)
As a suggestion, I fixed this locally by creating a "webpack.metadata.js" file using a similar structure as the "webpack.common.js" file and requiring/calling the metadata file from within the separate env-specific webpack files, as follows:
const metadata = require('./webpack.metadata.js');
const METADATA = webpackMerge(metadata(), {
HMR: HMR,
host: HOST,
port: PORT,
isWebpackDevServer,
});
Gets around the core issue (that webpack cannot export properties other than the ones explicitly allowed) and does so in a manner consistent with the way these files are managed.
+1
I found the same issue. "commonConfig({env: ENV}).metadata" is always undefined.
We need to get the metadata from LoaderOptionsPlugin, this worked for me but feels like a hack.
let commonOptions = { metadata: {} };
if ( commonConfig({ env: ENV }).plugins ) {
commonConfig({ env: ENV }).plugins.forEach( plugin => {
if (plugin instanceof LoaderOptionsPlugin && plugin.options ) {
commonOptions = plugin.options.options;
}
});
}
const METADATA = webpackMerge(commonOptions.metadata, {
host: HOST,
port: PORT,
ENV: ENV,
HMR: HMR
});
Related issue - the options in "LoaderOptionsPlugin" are not merged correctly either.
@gdi2290 is this an issue wth webpack-merge then?
I'm just facing the exact same problem and after digging deep into this for a couple of days, finally I figured out the cause. As @duderdice already mentioned, webpack config now cannot have arbitrary key including metadata. I guess that change has introduced sometime between webpack 2.1.0 beta 23 and beta 25 according to this commit: https://github.com/AngularClass/angular2-webpack-starter/commit/7bfba744ebfa77c5dfb30524e639e394a1d92c0e
So, the below code still exists in config/webpack.env.js does not work as intended.
const METADATA = webpackMerge(commonConfig({env: ENV}).metadata, {
host: HOST,
port: PORT,
ENV: ENV,
HMR: HMR
});
Indeed, config/webpack.github-deplooy.js which is actively used, does not have any similar code fragments anymore.
So I might guess the suggested method by @duderdice may be the solution for now?
Here's the actual working code: https://github.com/AngularClass/angular2-webpack-starter/compare/master...shusugmt:improve-webpack-config-metadata-handling
My original purpose is to switch baseUrl passed to HtmlWebpackPlugin, based on webpack.env.js and now I can achieve that using the above approach.
same issue
@shusugmt Could you please pass a snippet here? I'm facing the same issue in a different project and would like to inspire on your solution.
can someone create a PR
@kub1x Here: https://github.com/AngularClass/angular-starter/compare/master...shusugmt:improve-webpack-config-metadata-handling
(also fixed the link in previous comment)