Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metadata not handled properly when merging webpack.common.js and webpack.<env>.js files #1175

Closed
duderdice opened this Issue Nov 14, 2016 · 9 comments

Comments

Projects
None yet
8 participants
@duderdice
Copy link

duderdice commented Nov 14, 2016

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

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..js file.

  • 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)
@duderdice

This comment has been minimized.

Copy link
Author

duderdice commented Nov 16, 2016

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.

@yuki-takei

This comment has been minimized.

Copy link

yuki-takei commented Nov 29, 2016

+1

@echo-sean

This comment has been minimized.

Copy link

echo-sean commented Dec 22, 2016

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?

@shusugmt

This comment has been minimized.

Copy link

shusugmt commented Jan 6, 2017

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: 7bfba74

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?

@shusugmt

This comment has been minimized.

Copy link

shusugmt commented Jan 10, 2017

Here's the actual working code: 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.

@lexzhukov

This comment has been minimized.

Copy link

lexzhukov commented Mar 7, 2017

same issue

@kub1x

This comment has been minimized.

Copy link

kub1x commented Jun 28, 2017

@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.

@PatrickJS

This comment has been minimized.

Copy link
Owner

PatrickJS commented Jun 28, 2017

can someone create a PR

@shusugmt

This comment has been minimized.

Copy link

shusugmt commented Jun 29, 2017

@kub1x Here: master...shusugmt:improve-webpack-config-metadata-handling

(also fixed the link in previous comment)

@PatrickJS PatrickJS closed this Sep 2, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.