Javascriptservices: Webpack 4 issue with Visual Studio 2017 .net core 2.0.7

Created on 11 May 2018  路  7Comments  路  Source: aspnet/JavaScriptServices

My project is based on the Angular template for VS 2017 .net core 2.0 (not the new 2.1 templates, yet).

I've upgraded my project to webpack 4. I have update the webpack.config files (main and vendor) to handle the changes and have tweaked the csproj file to use the new --mode parameter required by webpack 4. I can build and publish ok and can run both webpack configs (main and vendor) without problems.

However, when I launch the app in dev mode (CTRL-F5) and VS automatically re-runs webpack, I get a console warning that 'mode' option has not been set and it falls back to Production mode.

How to I make VS call the hot middleware webpack rebuild with the new --mode=development parameter?

Is it an option in startup.cs? Maybe an additional option here?

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

All 7 comments

Webpack V4 is currently unsupported as far as I'm aware.

Here's the still open issue about it.

OK, then. I'll have to roll back. Thanks.

@AlejandroFlorin,
As far as I know, HMR middleware just calls 'webpack' command, without any parameters.
So to provide a mode option to HMR you need to specify it in your webpack config file:

module.exports = (env) => {
    const isDevBuild = !(env && env.prod);
    const mode = isDevBuild ? 'development' : 'production';
    const config = {
        mode: mode
    };
    return [config];
};

You could found more details in the official docs: https://webpack.js.org/concepts/mode

Thanks for answering, @zhaparoff!

Also note that we just released an updated aspnet-webpack 3.0.0 that adds Webpack 4 support.

I have upgraded to aspnet-webpack 3.0.0 as well as used @zhaparoff recommended settings. I still get the warning but I assume that is innocuous as setting the mode inside the webpack.config.js directly covers it?

This is still no good. The middleware is doing a full webpack rebuild with every TS change. Here is my webpack.config.js

//PROD: webpack --env.prod"
//PROD: webpack --mode=production"
const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const AngularCompilerPlugin = require('@ngtools/webpack').AngularCompilerPlugin;

module.exports = (env) => {
    // Configuration in common to both client-side and server-side bundles
    const isDevBuild = !(env && env.prod);
    const mode = isDevBuild ? 'development' : 'production';
    const config = {
        mode: mode
    };
    const sharedConfig = {
        stats: { modules: false },
        context: __dirname,
        resolve: {
            extensions: ['.js', '.ts'],
            modules: [
                path.resolve('./ClientApp'),
                path.resolve('./node_modules')]
        },
        output: {
            filename: '[name].js',
            publicPath: 'dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
        },
        module: {
            rules: [
                {
                    test: /\.ts$/,
                    include: /ClientApp/,
                    use: isDevBuild ? ['ts-loader', 'angular-router-loader', 'angular2-template-loader'] : '@ngtools/webpack'
                },
                { test: /\.html$/, use: 'html-loader?minimize=false' },
                { test: /\.css$/, use: ['to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize'] },
                { test: /\.(png|jpg|jpeg|gif|woff|woff2|eot|ttf|svg)$/, use: 'url-loader?limit=25000' },
                {
                    test: /\.(scss)$/,
                    use: [{
                        loader: 'style-loader' // inject CSS to page
                    },
                    {
                        loader: 'css-loader' // translates CSS into CommonJS modules
                    },
                    {
                        loader: 'postcss-loader', // Run post css actions
                        options: {
                            plugins: function () { // post css plugins, can be exported to postcss.config.js
                                return [
                                    //require('precss'),
                                    require('autoprefixer')
                                ];
                            }
                        }
                    },
                    {
                        loader: 'resolve-url-loader', //handles url pathing in scss
                    },
                    {
                        loader: 'sass-loader?sourceMap' // compiles SASS to CSS
                    }]
                },
            ]
        }
    };

    // Configuration for client-side bundle suitable for running in browsers
    const clientBundleOutputDir = './wwwroot/dist';
    const clientBundleConfig = merge(sharedConfig, {
        entry: { 'main-client': './ClientApp/boot.browser.ts' },
        output: { path: path.join(__dirname, clientBundleOutputDir) },
        plugins: [
            new webpack.DllReferencePlugin({
                context: __dirname,
                manifest: require('./wwwroot/dist/vendor-manifest.json')
            })
        ].concat(isDevBuild ? [
            // Plugins that apply in development builds only
            new webpack.SourceMapDevToolPlugin({
                filename: '[file].map', // Remove this line if you prefer inline source maps
                moduleFilenameTemplate: path.relative(clientBundleOutputDir, '[resourcePath]') // Point sourcemap entries to the original file locations on disk
            })
        ] : [
                // Plugins that apply in production builds only
                //new webpack.optimize.UglifyJsPlugin(),
                //new AngularCompilerPlugin({
                //    tsConfigPath: './tsconfig.json',
                //    entryModule: path.join(__dirname, 'ClientApp/app/app-browser.module#AppModule'),
                //    exclude: ['./**/*.server.ts']
                //})
            ])
    });

    // Configuration for server-side (prerendering) bundle suitable for running in Node
    const serverBundleConfig = merge(sharedConfig, {
        resolve: { mainFields: ['main'] },
        entry: { 'main-server': './ClientApp/boot.server.ts' },
        plugins: [
            new webpack.DllReferencePlugin({
                context: __dirname,
                manifest: require('./ClientApp/dist/vendor-manifest.json'),
                sourceType: 'commonjs2',
                name: './vendor'
            })
        ].concat(isDevBuild ? [] : [
            // Plugins that apply in production builds only
            new AngularCompilerPlugin({
                tsConfigPath: './tsconfig.json',
                entryModule: path.join(__dirname, 'ClientApp/app/app-server.module#AppModule'),
                exclude: ['./**/*.browser.ts']
            })
        ]),
        output: {
            libraryTarget: 'commonjs',
            path: path.join(__dirname, './ClientApp/dist')
        },
        target: 'node',
        devtool: 'inline-source-map'
    });

    return [clientBundleConfig, serverBundleConfig];
};

@AlejandroFlorin,
Looking at your current config, mode isn't included in any output.
You should add mode setting into sharedConfig directly:

const sharedConfig = {
        mode: mode,
        stats: { modules: false },
        context: __dirname,
        ...
}

OR
merge 'config' whith 'sharedConfig':

const sharedConfig = merge(config, {
        stats: { modules: false },
        context: __dirname,
        ...
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

justinyoo picture justinyoo  路  3Comments

DanHarman picture DanHarman  路  4Comments

tmedanovic picture tmedanovic  路  4Comments

benaadams picture benaadams  路  3Comments

LovedByTheLord picture LovedByTheLord  路  3Comments