.Net Core 2.0 Angular SPA Template VS 2017 Azure publish fails on lazy loaded module #343

Closed
opened 2025-08-09 17:15:55 +00:00 by fergalmoran · 0 comments
Owner

Originally created by @RichDef77 on 10/26/2017

Hello,

I have a project I created with the latest angular .net Core 2.0 SPA template in Visual Studio 2017 v. 15.4.1. I upgraded the template to use the latest version of Angular and Webpack.

Everything builds and runs correctly when I run webpack commands from the command line however the production commands fail when trying to publish to Azure from Visual Studio with the following error below.

Note: When I remove the lazy loaded module the VS publish executes successfully. Also, unfortunately we could not leverage the server side pre-rendering offered by .Net Core as some components we use reference the DOM directly. So our index.cshtml looks like this:

<app>Loading</app>

Also, boot.server.ts and boot.browser.ts files remain unchanged from template.

I'm trying to publish the app to Azure with AOT compilation enabled and functioning, server side pre-rendering disabled, and with lazy loaded angular modules. What am I doing wrong here?

Any help is greatly appreciated!

=============================================
Publish Error:

ERROR in ./ClientApp/app/app-routing.module.ts
Module not found(0,0): Error : Can't resolve '.\features\admin-console\admin-console.module.ngfactory' in 'D:\GIT\EMS\Ems.Solution\Ems.UI\ClientApp\app'
    resolve '.\features\admin-console\admin-console.module.ngfactory' in 'D:\GIT\EMS\Ems.Solution\Ems.UI\ClientApp\app'
      using description file: D:\GIT\EMS\Ems.Solution\Ems.UI\package.json (relative path: ./ClientApp/app)
      after using description file: D:\GIT\EMS\Ems.Solution\Ems.UI\package.json (relative path: ./ClientApp/app)
        using description file: D:\GIT\EMS\Ems.Solution\Ems.UI\package.json (relative path: ./ClientApp/app/features/admin-console/admin-console.module.ngfactory)
          no extension
            D:\GIT\EMS\Ems.Solution\Ems.UI\ClientApp\app\features\admin-console\admin-console.module.ngfactory doesn't exist
          .js
            D:\GIT\EMS\Ems.Solution\Ems.UI\ClientApp\app\features\admin-console\admin-console.module.ngfactory.js doesn't exist
          .ts
            D:\GIT\EMS\Ems.Solution\Ems.UI\ClientApp\app\features\admin-console\admin-console.module.ngfactory.ts doesn't exist
          as directory
            D:\GIT\EMS\Ems.Solution\Ems.UI\ClientApp\app\features\admin-console\admin-console.module.ngfactory doesn't exist
    [D:\GIT\EMS\Ems.Solution\Ems.UI\ClientApp\app\features\admin-console\admin-console.module.ngfactory]
    [D:\GIT\EMS\Ems.Solution\Ems.UI\ClientApp\app\features\admin-console\admin-console.module.ngfactory.js]
    [D:\GIT\EMS\Ems.Solution\Ems.UI\ClientApp\app\features\admin-console\admin-console.module.ngfactory.ts]
    [D:\GIT\EMS\Ems.Solution\Ems.UI\ClientApp\app\features\admin-console\admin-console.module.ngfactory]
     @ ./ClientApp/app/app-routing.module.ts 6:137-206
     @ ./$$_gendir/ClientApp/app/app.module.ngfactory.ts
     @ ./ClientApp/boot.server.ts

=================================================
Here is my package.json:

{
  "dependencies": {
    "@angular/animations": "4.4.6",
    "@angular/common": "4.4.6",
    "@angular/compiler": "4.4.6",
    "@angular/compiler-cli": "4.4.6",
    "@angular/core": "4.4.6",
    "@angular/forms": "4.4.6",
    "@angular/http": "4.4.6",
    "@angular/platform-browser": "4.4.6",
    "@angular/platform-browser-dynamic": "4.4.6",
    "@angular/platform-server": "4.4.6",
    "@angular/router": "4.4.6",
    "@ngtools/webpack": "1.7.4",
    "@types/webpack-env": "1.13.2",
    "angular-router-loader": "^0.6.0",
    "angular2-template-loader": "0.6.2",
    "aspnet-prerendering": "^3.0.1",
    "aspnet-webpack": "^2.0.1",
    "awesome-typescript-loader": "3.2.3",
    "bootstrap": "3.3.7",    
    "copy-webpack-plugin": "^4.2.0",
    "css": "2.2.1",
    "css-loader": "0.28.7",
    "es6-shim": "0.35.3",
    "event-source-polyfill": "0.0.11",
    "expose-loader": "0.7.3",
    "extract-text-webpack-plugin": "3.0.1",
    "file-loader": "1.1.5",
    "html-loader": "0.5.1",
    "isomorphic-fetch": "2.2.1",
    "jquery": "3.2.1",
    "json-loader": "0.5.7",
    "preboot": "5.1.5",
    "raw-loader": "0.5.1",
    "reflect-metadata": "0.1.10",
    "rxjs": "5.5.0",
    "style-loader": "0.19.0",
    "to-string-loader": "1.1.5",
    "typescript": "2.5.3",
    "url-loader": "0.6.2",
    "webpack": "3.8.1",
    "webpack-externals-plugin": "^1.0.0",
    "webpack-hot-middleware": "2.20.0",
    "webpack-merge": "4.1.0",
    "zone.js": "0.8.18"
  },
  "devDependencies": {
    "@types/chai": "4.0.4",
    "@types/jasmine": "2.6.0",
    "chai": "4.1.2",
    "jasmine-core": "2.8.0",
    "karma": "1.7.1",
    "karma-chai": "0.1.0",
    "karma-chrome-launcher": "2.2.0",
    "karma-cli": "1.0.1",
    "karma-jasmine": "1.1.0",
    "karma-webpack": "2.0.5"
  },
  "name": "xmserviceui",
  "private": true,
  "scripts": {
    "test": "karma start ClientApp/test/karma.conf.js"
  },
  "version": "1.0.0"
}

==========================================
Here is webpack.config.js:

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;
const CopyWebpackPlugin = require('copy-webpack-plugin');

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$/, include: /ClientApp/, use: isDevBuild ? ['awesome-typescript-loader?silent=true', 'angular2-template-loader', 'angular-router-loader'] : ['@ngtools/webpack', 'angular-router-loader?aot=true'] },
                { 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(),
            // NOTE: We can remove this dependency by simply moving the assets folder to the wwwroot folder.
            new CopyWebpackPlugin([{
                from: './ClientApp/app/assets',
                to: './assets'
            }])
        ]
    };

    // 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.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.module#AppModule'),
                exclude: ['./**/*.browser.ts']
            })
        ]),
        output: {
            libraryTarget: 'commonjs',
            path: path.join(__dirname, './ClientApp/dist')
        },
        target: 'node',
        devtool: 'inline-source-map'
    });

    return [clientBundleConfig, serverBundleConfig];
};

================================================
Here is main app routing module (app-routing.module.ts):


import { HomeComponent } from './features/publish/home/home.component';
import { PublishFinishComponent } from './features/publish/publish-finish.component';
import { CanActivateIfEntitled } from './common/can-activate-if-entitled';
import { EntitlementsService } from './common/services/entitlements.service';

 specific routes.
const appRoutes: Routes = [
    { path: 'ops/home', component: HomeComponent }, // Not block when not entitled
    { path: 'ops/publish-finish', component: PublishFinishComponent }, 
    
    // Admin-Console - lazy loaded.
    { path: 'admin', loadChildren: './features/admin-console/admin-console.module#AdminConsoleModule' },
    { path: '', redirectTo: 'admin', pathMatch: 'full' },
    { path: '**', redirectTo: 'admin' }
];

@NgModule({
    imports: [RouterModule.forRoot(appRoutes, { initialNavigation: 'enabled' })],
    exports: [RouterModule],
    providers: [ CanActivateIfEntitled, EntitlementsService ]
})
export class AppRoutingModule { }

*Originally created by @RichDef77 on 10/26/2017* Hello, I have a project I created with the latest angular .net Core 2.0 SPA template in Visual Studio 2017 v. 15.4.1. I upgraded the template to use the latest version of Angular and Webpack. Everything builds and runs correctly when I run webpack commands from the command line however the production commands fail when trying to publish to Azure from Visual Studio with the following error below. Note: When I remove the lazy loaded module the VS publish executes successfully. Also, unfortunately we could not leverage the server side pre-rendering offered by .Net Core as some components we use reference the DOM directly. So our index.cshtml looks like this: ``` <app>Loading</app> ``` Also, boot.server.ts and boot.browser.ts files remain unchanged from template. I'm trying to publish the app to Azure with AOT compilation enabled and functioning, server side pre-rendering disabled, and with lazy loaded angular modules. What am I doing wrong here? Any help is greatly appreciated! ============================================= Publish Error: ``` ERROR in ./ClientApp/app/app-routing.module.ts Module not found(0,0): Error : Can't resolve '.\features\admin-console\admin-console.module.ngfactory' in 'D:\GIT\EMS\Ems.Solution\Ems.UI\ClientApp\app' resolve '.\features\admin-console\admin-console.module.ngfactory' in 'D:\GIT\EMS\Ems.Solution\Ems.UI\ClientApp\app' using description file: D:\GIT\EMS\Ems.Solution\Ems.UI\package.json (relative path: ./ClientApp/app) after using description file: D:\GIT\EMS\Ems.Solution\Ems.UI\package.json (relative path: ./ClientApp/app) using description file: D:\GIT\EMS\Ems.Solution\Ems.UI\package.json (relative path: ./ClientApp/app/features/admin-console/admin-console.module.ngfactory) no extension D:\GIT\EMS\Ems.Solution\Ems.UI\ClientApp\app\features\admin-console\admin-console.module.ngfactory doesn't exist .js D:\GIT\EMS\Ems.Solution\Ems.UI\ClientApp\app\features\admin-console\admin-console.module.ngfactory.js doesn't exist .ts D:\GIT\EMS\Ems.Solution\Ems.UI\ClientApp\app\features\admin-console\admin-console.module.ngfactory.ts doesn't exist as directory D:\GIT\EMS\Ems.Solution\Ems.UI\ClientApp\app\features\admin-console\admin-console.module.ngfactory doesn't exist [D:\GIT\EMS\Ems.Solution\Ems.UI\ClientApp\app\features\admin-console\admin-console.module.ngfactory] [D:\GIT\EMS\Ems.Solution\Ems.UI\ClientApp\app\features\admin-console\admin-console.module.ngfactory.js] [D:\GIT\EMS\Ems.Solution\Ems.UI\ClientApp\app\features\admin-console\admin-console.module.ngfactory.ts] [D:\GIT\EMS\Ems.Solution\Ems.UI\ClientApp\app\features\admin-console\admin-console.module.ngfactory] @ ./ClientApp/app/app-routing.module.ts 6:137-206 @ ./$$_gendir/ClientApp/app/app.module.ngfactory.ts @ ./ClientApp/boot.server.ts ``` ================================================= Here is my package.json: ``` { "dependencies": { "@angular/animations": "4.4.6", "@angular/common": "4.4.6", "@angular/compiler": "4.4.6", "@angular/compiler-cli": "4.4.6", "@angular/core": "4.4.6", "@angular/forms": "4.4.6", "@angular/http": "4.4.6", "@angular/platform-browser": "4.4.6", "@angular/platform-browser-dynamic": "4.4.6", "@angular/platform-server": "4.4.6", "@angular/router": "4.4.6", "@ngtools/webpack": "1.7.4", "@types/webpack-env": "1.13.2", "angular-router-loader": "^0.6.0", "angular2-template-loader": "0.6.2", "aspnet-prerendering": "^3.0.1", "aspnet-webpack": "^2.0.1", "awesome-typescript-loader": "3.2.3", "bootstrap": "3.3.7", "copy-webpack-plugin": "^4.2.0", "css": "2.2.1", "css-loader": "0.28.7", "es6-shim": "0.35.3", "event-source-polyfill": "0.0.11", "expose-loader": "0.7.3", "extract-text-webpack-plugin": "3.0.1", "file-loader": "1.1.5", "html-loader": "0.5.1", "isomorphic-fetch": "2.2.1", "jquery": "3.2.1", "json-loader": "0.5.7", "preboot": "5.1.5", "raw-loader": "0.5.1", "reflect-metadata": "0.1.10", "rxjs": "5.5.0", "style-loader": "0.19.0", "to-string-loader": "1.1.5", "typescript": "2.5.3", "url-loader": "0.6.2", "webpack": "3.8.1", "webpack-externals-plugin": "^1.0.0", "webpack-hot-middleware": "2.20.0", "webpack-merge": "4.1.0", "zone.js": "0.8.18" }, "devDependencies": { "@types/chai": "4.0.4", "@types/jasmine": "2.6.0", "chai": "4.1.2", "jasmine-core": "2.8.0", "karma": "1.7.1", "karma-chai": "0.1.0", "karma-chrome-launcher": "2.2.0", "karma-cli": "1.0.1", "karma-jasmine": "1.1.0", "karma-webpack": "2.0.5" }, "name": "xmserviceui", "private": true, "scripts": { "test": "karma start ClientApp/test/karma.conf.js" }, "version": "1.0.0" } ``` ========================================== Here is webpack.config.js: ``` 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; const CopyWebpackPlugin = require('copy-webpack-plugin'); 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$/, include: /ClientApp/, use: isDevBuild ? ['awesome-typescript-loader?silent=true', 'angular2-template-loader', 'angular-router-loader'] : ['@ngtools/webpack', 'angular-router-loader?aot=true'] }, { 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(), // NOTE: We can remove this dependency by simply moving the assets folder to the wwwroot folder. new CopyWebpackPlugin([{ from: './ClientApp/app/assets', to: './assets' }]) ] }; // 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.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.module#AppModule'), exclude: ['./**/*.browser.ts'] }) ]), output: { libraryTarget: 'commonjs', path: path.join(__dirname, './ClientApp/dist') }, target: 'node', devtool: 'inline-source-map' }); return [clientBundleConfig, serverBundleConfig]; }; ``` ================================================ Here is main app routing module (app-routing.module.ts): ``` import { HomeComponent } from './features/publish/home/home.component'; import { PublishFinishComponent } from './features/publish/publish-finish.component'; import { CanActivateIfEntitled } from './common/can-activate-if-entitled'; import { EntitlementsService } from './common/services/entitlements.service'; specific routes. const appRoutes: Routes = [ { path: 'ops/home', component: HomeComponent }, // Not block when not entitled { path: 'ops/publish-finish', component: PublishFinishComponent }, // Admin-Console - lazy loaded. { path: 'admin', loadChildren: './features/admin-console/admin-console.module#AdminConsoleModule' }, { path: '', redirectTo: 'admin', pathMatch: 'full' }, { path: '**', redirectTo: 'admin' } ]; @NgModule({ imports: [RouterModule.forRoot(appRoutes, { initialNavigation: 'enabled' })], exports: [RouterModule], providers: [ CanActivateIfEntitled, EntitlementsService ] }) export class AppRoutingModule { } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/JavaScriptServices#343
No description provided.