Tonmoy
Tonmoy

Reputation: 2038

Webpack warning on bundling ASP.NET core - Angular 4 application : Critical dependency: the request of a dependency is an expression

Recently when I am running dotnet run or dotnet watch run, I'm getting below warnings. Everything else works fine.

> dotnet watch run
watch : Started
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
      User profile is available. Using 'C:\Users\tonmoy\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
Hosting environment: Development
Content root path: c:\SOME_DIRECTORY\PROJECT_NAME
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
info: Microsoft.AspNetCore.NodeServices[0]
      webpack built ab029819bd659146ff3f in 14017ms
fail: Microsoft.AspNetCore.NodeServices[0]
      Hash: ab029819bd659146ff3f
      Version: webpack 2.5.1
      Time: 14017ms
                   Asset     Size  Chunks                    Chunk Names
          main-client.js  2.91 MB       0  [emitted]  [big]  main-client
      main-client.js.map   3.5 MB       0  [emitted]         main-client
      chunk    {0} main-client.js, main-client.js.map (main-client) 2.77 MB [entry] [rendered]
          [0] ./~/@angular/core/@angular/core.es5.js 489 kB {0} [built]
         [45] ./ClientApp/boot.browser.ts 989 bytes {0} [built]
         [46] (webpack)-hot-middleware/client.js?path=__webpack_hmr&dynamicPublicPath=true 6.68 kB {0} [built]
         [47] delegated ./node_modules/event-source-polyfill/eventsource.js from dll-reference vendor_b56098dd79ef0a035831 42 bytes {0} [not cacheable] [built]
         [50] ./~/@angular/platform-browser-dynamic/@angular/platform-browser-dynamic.es5.js 5.88 kB {0} [built]
         [54] ./ClientApp/app/app.browser.module.ts 1.35 kB {0} [built]
         [94] ./~/querystring-es3/index.js 127 bytes {0} [built]
         [95] ./~/reflect-metadata/Reflect.js 48 kB {0} [built]
        [125] ./~/strip-ansi/index.js 161 bytes {0} [built]
        [138] (webpack)-hot-middleware/client-overlay.js 1.82 kB {0} [built]
        [139] (webpack)-hot-middleware/process-update.js 3.88 kB {0} [built]
        [140] (webpack)/buildin/module.js 517 bytes {0} [built]
        [141] ./~/zone.js/dist/zone.js 96 kB {0} [built]
        [143] delegated ./node_modules/bootstrap/dist/js/npm.js from dll-reference vendor_b56098dd79ef0a035831 42 bytes {0} [not cacheable] [built]
        [144] multi event-source-polyfill webpack-hot-middleware/client?path=__webpack_hmr&dynamicPublicPath=true ./ClientApp/boot.browser.ts 52 bytes {0} [built]
           + 130 hidden modules

      WARNING in ./~/@angular/core/@angular/core.es5.js
      5704:15-36 Critical dependency: the request of a dependency is an expression

      WARNING in ./~/@angular/core/@angular/core.es5.js
      5720:15-102 Critical dependency: the request of a dependency is an expression

I've found webpack: Critical dependency message and https://github.com/angular/angular/issues/11580. But I have no similar ContextReplacementPlugin() to replace on my webpack config. Can anybody help please?

Here is my webpack.config.js file:

const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const AotPlugin = require('@ngtools/webpack').AotPlugin;
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;

module.exports = (env) => {
    // Configuration in common to both client-side and server-side bundles
    const isDevBuild = !(env && env.prod);
    const sharedConfig = {
        stats: { modules: false },
        context: __dirname,
        resolve: { extensions: [ '.js', '.ts' ] },
        output: {
            filename: '[name].js',
            publicPath: 'dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
        },
        module: {
            rules: [
                { test: /\.ts$/, use: isDevBuild ? ['awesome-typescript-loader?silent=true', 'angular2-template-loader', 'angular2-router-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|svg)$/, use: 'url-loader?limit=25000' }
            ]
        },
        plugins: [new CheckerPlugin()]
    };

    // 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 AotPlugin({
                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 AotPlugin({
                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];
};

enter image description here

tonmoy@DESKTOP c:\SOME_DIRECTORY\PROJECT_NAME
> npm -v
5.8.0

tonmoy@DESKTOP c:\SOME_DIRECTORY\PROJECT_NAME
> node -v
v8.11.1

tonmoy@DESKTOP c:\SOME_DIRECTORY\PROJECT_NAME
> ng -v

    _                      _                 ____ _     ___
   / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
  / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
 / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
/_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
               |___/

Angular CLI: 1.7.3
Node: 8.11.1
OS: win32 x64
Angular: 4.2.5
... animations, common, compiler, compiler-cli, core, forms
... http, platform-browser, platform-browser-dynamic
... platform-server, router, tsc-wrapped

@angular/cli: 1.7.3
@angular-devkit/build-optimizer: 0.3.2
@angular-devkit/core: 0.3.2
@angular-devkit/schematics: 0.3.2
@ngtools/json-schema: 1.2.0
@ngtools/webpack: 1.5.0
@schematics/angular: 0.3.2
@schematics/package-update: 0.3.2
typescript: 2.4.1
webpack-hot-middleware: 2.18.2
webpack-merge: 4.1.0
webpack: 2.5.1

Upvotes: 0

Views: 493

Answers (1)

Tonmoy
Tonmoy

Reputation: 2038

I found a fix on https://github.com/angular/angular/issues/14898

In my webpack.config.js file, previously the plugins array was like this:

plugins: [new CheckerPlugin()]

Now, I've updated the array as below:

plugins: [
            new CheckerPlugin(),
            new webpack.ContextReplacementPlugin(
                // The (\\|\/) piece accounts for path separators in *nix and Windows
                /angular(\\|\/)core(\\|\/)(@angular|esm5)/,
                path.resolve(__dirname, '../src')
            )
        ]

If I run the app now, webpack builds without any warning and works just fine. Hope this answer will help people like me out there.

Upvotes: 1

Related Questions