Skip to content
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

Closed
urbanhusky opened this issue Mar 28, 2017 · 1 comment

Comments

@urbanhusky
Copy link

Hi,

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)");
        throw new Error("Environment is not set up. Webpack needs --env.dev or --env.prod arguments, passed to module.exports function(env)");
    }
    if (env.prod) {
        return require('./config/webpack.prod.js');
    } else {
        return require('./config/webpack.dev.js');
    }
}

However, when I try to run the application with hot module replacement (i.e. in development), env is undefined.

The Code in Startup.cs looks like this:

if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();

               app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions { HotModuleReplacement = true });
            }

How can I fix this? (process.argv.indexOf is no longer supported in webpack 2)

@SteveSandersonMS
Copy link
Member

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.

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 webpack on the command line: no env value 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 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!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants