首页 > 解决方案 > 带有 AOT 预渲染的 webpack --config --env.prod 出错

问题描述

我在使用 ASP.NET Core 2.1 获取 Angular 4 SPA 时遇到问题

当我运行 cmd 时;

webpack --config .\webpack.config.js --env.prod

我收到以下错误;

Hash: b51f3807ce83943809e7
Time: 21678ms
         Asset     Size  Chunks                    Chunk Names
main-server.js  2.08 MB       0  [emitted]  [big]  main-server

ERROR in ./ClientApp/boot.server.ts
Module not found: Error: Can't resolve './../$$_gendir/ClientApp/app/app.server.module.ngfactory' in 'C:\Users\matt\Source\Repos\lifestyleinteriors\ClientApp'
 @ ./ClientApp/boot.server.ts 8:0-94
Child ClientApp\app\components\home\home.component.html:
     1 asset
Child ClientApp\app\components\app\app.component.html:
     1 asset
Child ClientApp\app\components\about\about.component.html:
     1 asset
Child ClientApp\app\components\contact\contact.component.html:
     1 asset
Child ClientApp\app\components\terms\terms.component.html:
     1 asset
Child ClientApp\app\components\gallery\gallery.component.html:
     1 asset
Child ClientApp\app\components\shared\navmenu\navmenu.component.html:
     1 asset
Child ClientApp\app\components\privacy\privacy.component.html:
     1 asset
Child ClientApp\app\components\shared\navsocial\navsocial.component.html:
     1 asset
Child ClientApp\app\components\shared\callbackPopup\callbackPopup.component.html:
     1 asset
Child ClientApp\app\components\shared\theBurlington\theBurlington.component.html:
     1 asset
Child ClientApp\app\components\shared\contactForm\contactForm.component.html:
     1 asset
Child ClientApp\app\components\shared\quickContactForm\quickContactForm.component.html:
     1 asset
Child ClientApp\app\components\shared\testimonial\testimonial.component.html:
     1 asset
Child ClientApp\app\components\shared\servicePreview\servicePreview.component.html:
     1 asset
Child ClientApp\app\components\shared\hero\hero.component.html:
     1 asset
Child ClientApp\app\components\shared\theWallingford\theWallingford.component.html:
     1 asset
Child ClientApp\app\components\shared\footer\footer.component.html:
     1 asset
Child ClientApp\app\components\services\showHomes\showHomes.component.html:
     1 asset
Child ClientApp\app\components\services\marketingSuites\marketingSuites.component.html:
     1 asset
Child ClientApp\app\components\services\furniturePackages\furniturePackages.component.html:
     1 asset
Child ClientApp\app\components\services\privateClients\privateClients.component.html:
     1 asset
Child ClientApp\app\components\home\home.component.css:
     1 asset
Child ClientApp\app\components\about\about.component.css:
     1 asset
Child ClientApp\app\components\app\app.component.css:
     1 asset
Child ClientApp\app\components\contact\contact.component.css:
     1 asset
Child ClientApp\app\components\terms\terms.component.css:
     1 asset
Child ClientApp\app\components\gallery\gallery.component.css:
     1 asset
Child ClientApp\app\components\shared\navmenu\navmenu.component.css:
     1 asset
Child ClientApp\app\components\privacy\privacy.component.css:
     1 asset
Child ClientApp\app\components\shared\callbackPopup\callbackPopup.component.css:
     1 asset
Child ClientApp\app\components\shared\theBurlington\theBurlington.component.css:
     1 asset
Child ClientApp\app\components\shared\navsocial\navsocial.component.css:
     1 asset
Child ClientApp\app\components\shared\testimonial\testimonial.component.css:
     1 asset
Child ClientApp\app\components\shared\contactForm\contactForm.component.css:
     1 asset
Child ClientApp\app\components\shared\servicePreview\servicePreview.component.css:
     1 asset
Child ClientApp\app\components\shared\hero\hero.component.css:
     1 asset
Child ClientApp\app\components\shared\quickContactForm\quickContactForm.component.css:
     1 asset
Child ClientApp\app\components\services\marketingSuites\marketingSuites.component.css:
     1 asset
Child ClientApp\app\components\shared\footer\footer.component.css:
     1 asset
Child ClientApp\app\components\shared\theWallingford\theWallingford.component.css:
     1 asset
Child ClientApp\app\components\services\showHomes\showHomes.component.css:
     1 asset
Child ClientApp\app\components\services\furniturePackages\furniturePackages.component.css:
     1 asset
Child ClientApp\app\components\services\privateClients\privateClients.component.css:
     1 asset

package.json的如下;

{
  "name": "spa",
  "private": true,
  "version": "0.0.0",
  "scripts": {
    "test": "karma start ClientApp/test/karma.conf.js"
  },
  "devDependencies": {
    "@angular/animations": "^4.4.7",
    "@angular/common": "^4.4.7",
    "@angular/compiler": "^4.4.7",
    "@angular/compiler-cli": "^4.2.6",
    "@angular/core": "^4.4.7",
    "@angular/forms": "^4.4.7",
    "@angular/http": "^4.4.7",
    "@angular/platform-browser": "^4.4.7",
    "@angular/platform-browser-dynamic": "^4.4.7",
    "@angular/platform-server": "^4.4.7",
    "@angular/router": "^4.4.7",
    "@ngtools/webpack": "1.5.0",
    "@types/chai": "4.0.1",
    "@types/hammerjs": "2.0.35",
    "@types/jasmine": "2.5.53",
    "@types/mousetrap": "1.6.0",
    "@types/webpack-env": "1.13.0",
    "agm-core": "1.0.0-beta.5",
    "angular-modal-gallery": "5.7.1",
    "angular2-router-loader": "0.3.5",
    "angular2-template-loader": "0.6.2",
    "angular2-useful-swiper": "5.0.1",
    "aspnet-prerendering": "^3.0.1",
    "aspnet-webpack": "^2.0.1",
    "awesome-typescript-loader": "3.2.1",
    "bootstrap": "3.3.7",
    "chai": "4.0.2",
    "css": "2.2.1",
    "css-loader": "0.28.4",
    "es6-shim": "0.35.3",
    "event-source-polyfill": "0.0.9",
    "expose-loader": "0.7.3",
    "extract-text-webpack-plugin": "3.0.2",
    "file-loader": "0.11.2",
    "font-awesome": "4.7.0",
    "hammerjs": "2.0.8",
    "html-loader": "0.4.5",
    "isomorphic-fetch": "2.2.1",
    "jasmine-core": "2.6.4",
    "jquery": "3.2.1",
    "json-loader": "0.5.4",
    "karma": "1.7.0",
    "karma-chai": "0.1.0",
    "karma-chrome-launcher": "2.2.0",
    "karma-cli": "1.0.1",
    "karma-jasmine": "1.1.0",
    "karma-webpack": "3.0.0",
    "mousetrap": "1.6.2",
    "ngx-bootstrap": "^2.0.5",
    "preboot": "4.5.2",
    "raw-loader": "0.5.1",
    "reflect-metadata": "0.1.10",
    "rxjs": "^5.0.1",
    "style-loader": "0.18.2",
    "swiper": "3.4.2",
    "to-string-loader": "1.1.5",
    "typescript": "^2.9.2",
    "url-loader": "0.5.9",
    "webpack": "3.11.0",
    "webpack-hot-middleware": "2.21.0",
    "webpack-merge": "4.1.1",
    "zone.js": "0.8.12"
  },
  "dependencies": {
    "latest-version": "^4.0.0"
  }
}

我已经浏览了以下链接;

Github问题链接

我尝试替换 webpack.config.js 中提到的行;

const AotPlugin = require('@ngtools/webpack').AotPlugin;

const AotPlugin = require('@ngtools/webpack').AngularCompilerPlugin;

但是,然后我得到了错误;

new AotPlugin({
            ^
TypeError: AotPlugin is not a constructor

谷歌搜索上述建议我应该运行角度 5?(我正在运行 4.4.7)

为了完整,我webpack.config.js是;

const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const AotPlugin = require('@ngtools/webpack').AotPlugin;
///const AotPlugin = require('@ngtools/webpack').AngularCompilerPlugin;
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];
};

如您所见,这不包含include: /ClientApp/以下链接建议删除的文本。

AOT 修复 github

有谁知道我如何深入挖掘以尝试找到实际导致错误的原因?

标签: webpackasp.net-coreasp.net-core-2.0angular2-aot

解决方案


推荐阅读