You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 8, 2020. It is now read-only.
Repository navigation
This repository was archived by the owner on Apr 8, 2020. It is now read-only.
Pass webpack 2 build environment arguments, e.g. --env.production, in webpack dev middleware #816
I've got the following webpack.config.js file in which I've added a guard clause to check that the build environment is properly injected (as it should be for webpack 2).
module.exports=function(env){if(env==null){console.log("Environment is not set up. Webpack needs --env.dev or --env.prod arguments, passed to module.exports function(env)");thrownewError("Environment is not set up. Webpack needs --env.dev or --env.prod arguments, passed to module.exports function(env)");}if(env.prod){returnrequire('./config/webpack.prod.js');}else{returnrequire('./config/webpack.dev.js');}}
However, when I try to run the application with hot module replacement (i.e. in development), env is undefined.
check that the build environment is properly injected (as it should be for webpack 2)
I'm not sure it's really correct to say "should be for webpack 2". The env param is best understood as optional, since invoking webpack on the command line (on its own, with no args), will result in your function being invoked with no env value.
However, I understand that you want some way of forcing WebpackDevMiddleware to pass some env value. There isn't currently a good way of doing that. But if you want to implement support for it, you could add a new property, EnvParameter (type: object) on WebpackDevMiddlewareOptions.cs, so that it gets serialized and included in the CreateDevServerOptions received by WebpackDevMiddleware.ts. Then you could simply pass that object to webpackConfigExport on WebpackDevMiddleware.ts line 222. If you're interested in submitting a pull request along those lines, I'd be happy to merge it!
Closing this as by design, but as described above, we'd be happy to add a new feature to make this more flexible if you want, and if you're able to do a PR for it. Thanks!
Activity
SteveSandersonMS commentedon Mar 29, 2017
I'm not sure it's really correct to say "should be for webpack 2". The
envparam is best understood as optional, since invokingwebpackon the command line (on its own, with no args), will result in your function being invoked with noenvvalue.The current design for how WebpackDevMiddleware reflects this is described in the comment here: https://github.com/aspnet/JavaScriptServices/blob/dev/src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/src/WebpackDevMiddleware.ts#L217-L221. In summary, we do the same thing as happens if you call
webpackon the command line: noenvvalue is passed, and it's up to your webpack config to define its own default behaviour.However, I understand that you want some way of forcing WebpackDevMiddleware to pass some
envvalue. There isn't currently a good way of doing that. But if you want to implement support for it, you could add a new property,EnvParameter(type:object) onWebpackDevMiddlewareOptions.cs, so that it gets serialized and included in theCreateDevServerOptionsreceived byWebpackDevMiddleware.ts. Then you could simply pass that object towebpackConfigExportonWebpackDevMiddleware.tsline 222. If you're interested in submitting a pull request along those lines, I'd be happy to merge it!Closing this as by design, but as described above, we'd be happy to add a new feature to make this more flexible if you want, and if you're able to do a PR for it. Thanks!