Angular 5 TypeError: AngularCompilerPlugin不是一个构造函数。

3

我的环境是Angular 5和aspnetcore 2.0。

我将我的包更新到了Angular 5,并在webpack.config.js中将AotPlugin替换为AngularCompilerPlugin。但在发布项目时,我遇到了以下错误:

 TypeError: AngularCompilerPlugin is not a constructor
2017-12-07T06:25:19.8388691Z       at module.exports (d:\a\1\s\web\source\webpack.config.js:48:13)
2017-12-07T06:25:19.8388916Z       at requireConfig (d:\a\1\s\web\source\node_modules\webpack\bin\convert-argv.js:102:15)
2017-12-07T06:25:19.8389187Z       at d:\a\1\s\web\source\node_modules\webpack\bin\convert-argv.js:109:17
2017-12-07T06:25:19.8389479Z       at Array.forEach (native)
2017-12-07T06:25:19.8389687Z       at module.exports (d:\a\1\s\web\source\node_modules\webpack\bin\convert-argv.js:107:15)
2017-12-07T06:25:19.8389914Z       at Object.<anonymous> (d:\a\1\s\web\source\node_modules\webpack\bin\webpack.js:153:40)
2017-12-07T06:25:19.8390154Z       at Module._compile (module.js:570:32)
2017-12-07T06:25:19.8390340Z       at Object.Module._extensions..js (module.js:579:10)
2017-12-07T06:25:19.8390524Z       at Module.load (module.js:487:32)
2017-12-07T06:25:19.8390881Z       at tryModuleLoad (module.js:446:12)
2017-12-07T06:25:19.8569143Z d:\a\1\s\web\source\CA.IDM.PAdmin.Web.csproj(41,5): error MSB3073: The command "node node_modules/webpack/bin/webpack.js --env.prod" exited with code 1.
2017-12-07T06:25:19.9453100Z ##[error]Error: C:\Program Files\dotnet\dotnet.exe failed with return code: 1
2017-12-07T06:25:19.9454881Z ##[error]Dotnet command failed with non-zero exit code on the following projects : d:\a\1\s\web\source\CA.IDM.PAdmin.Web.csproj

因此,我撤销了这些更改并在webpack.config.js中恢复了AotPlugin,现在我的发布成功了。

实际上,对于angular 5,使用的是AngularCompilerPlugin而不是AotPlugin。所以我的问题是,我错过了什么?我在这里做错了什么?

webpack.config.js

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

package.json

{
  "name": "Web5",
  "private": true,
  "version": "0.0.0",
  "scripts": {
    "test": "karma start ClientApp/test/karma.conf.js"
  },
  "devDependencies": {
    "@angular/animations": "5.0.5",
    "@angular/common": "5.0.5",
    "@angular/compiler": "5.0.5",
    "@angular/compiler-cli": "5.0.5",
    "@angular/core": "5.0.5",
    "@angular/forms": "5.0.5",
    "@angular/http": "5.0.5",
    "@angular/platform-browser": "5.0.5",
    "@angular/platform-browser-dynamic": "5.0.5",
    "@angular/platform-server": "5.0.5",
    "@angular/router": "5.0.5",
    "@ngtools/webpack": "1.8.5",
    "@types/chai": "4.0.1",
    "@types/jasmine": "2.5.53",
    "@types/webpack-env": "1.13.0",
    "angular2-router-loader": "0.3.5",
    "angular2-template-loader": "0.6.2",
    "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": "2.1.2",
    "file-loader": "0.11.2",
    "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": "2.0.3",
    "preboot": "4.5.2",
    "raw-loader": "0.5.1",
    "reflect-metadata": "0.1.10",
    "rxjs": "5.5.4",
    "style-loader": "0.18.2",
    "to-string-loader": "1.1.5",
    "typescript": "2.6.2",
    "url-loader": "0.5.9",
    "webpack": "2.5.1",
    "webpack-hot-middleware": "2.18.2",
    "webpack-merge": "4.1.0",
    "zone.js": "0.8.12"
  }
}

我检查了一下,在发布时它没有采用更新的版本。这是什么原因?

12-07T06:23:03.8757490Z   Web5 -> d:\a\1\s\web5.dll
2017-12-07T06:24:43.8922441Z   Web5@0.0.0 d:\a\1\s\web\
2017-12-07T06:24:43.8923186Z   +-- @angular/animations@4.2.5 
2017-12-07T06:24:43.8923751Z   +-- @angular/common@4.2.5 
2017-12-07T06:24:43.8925363Z   +-- @angular/compiler@4.2.5 
2017-12-07T06:24:43.8935332Z   +-- @angular/compiler-cli@4.2.5 
2017-12-07T06:24:43.8936589Z   | `-- minimist@1.2.0 
2017-12-07T06:24:43.8936969Z   +-- @angular/core@4.2.5 
2017-12-07T06:24:43.8937290Z   +-- @angular/forms@4.2.5 
2017-12-07T06:24:43.8937696Z   +-- @angular/http@4.2.5 
2017-12-07T06:24:43.8938023Z   +-- @angular/platform-browser@4.2.5 
2017-12-07T06:24:43.8938348Z   +-- @angular/platform-browser-dynamic@4.2.5 
2017-12-07T06:24:43.8938687Z   +-- @angular/platform-server@4.2.5 
2017-12-07T06:24:43.8939007Z   +-- @angular/router@4.2.5 
2017-12-07T06:24:43.8939341Z   +-- @angular/tsc-wrapped@4.2.5 
2个回答

4

没有看到您的webpack.config很难确定问题所在。

以下是我的首选方式:

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

plugins: [
        new AotPlugin({
            tsConfigPath: './tsconfig.json',
            entryModule: path.join(__dirname, 'src/app/app.module#AppModule')
        })
]

没什么可展示的,我只是在 webpack.config 中将所有的 AotPlugin 替换为 AngularCompilerPlugin,其余都保持不变。 - k11k2
很难猜测原文是什么样的 :) - pixelbits
我尝试了上面的方法,但仍然没有运气。抛出相同的错误。 - k11k2
@angular/compiler-cli的版本是多少? - pixelbits
刚刚我发现了一个奇怪的问题:我的npm文件夹中的依赖项都回退到了4.2.5版本。这是怎么发生的?最初,我更新了所有的包并运行了应用程序,检查它们的版本为5.0.5。后来,在webpack更改之后,又回退到了旧版本。 - k11k2
显示剩余2条评论

1
我已经手动将版本号从4.2.x更改为5.0.x在package.json中,因此它没有反映在package-lock.json中。在发布时,考虑到package-lock.json,我遇到了上述错误,因为angular 4.2不支持AngularCompilerPlugin。npm > 5版本允许我同时更新package.json和package-lock.json。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接