迁移到最新版本的angular和webpack后,未发现AngularCompilerPlugin错误

时间:2018-05-22 06:12:19

标签: angular webpack

我使用Visual Studio在Angular 4.2.5中创建了一个带有前端的新项目,然后我将角度库更新到版本6.0.2,并使用npm将@ ngtools / webpack升级到最新版本6.0.3。

此外,作为迁移过程的一部分,我将AotPlugin替换为AngularCompilerPlugin。但是当我尝试运行webpack时,我收到以下错误:

./ClientApp/boot.server.ts中的错误     模块构建失败:错误:找不到AngularCompilerPlugin。 @ ngtools / webpack加载器需要插件。         在Object.ngcLoader(path \ to \ node_modules \ @ngtools \ webpack \ src \ loader.js:27:15)

我尝试清理npm缓存,尝试使用npm和yarn重新安装软件包,但在运行webpack时仍然会出现相同的错误。我错过了什么吗?

我的Package.json是:

    {
    "name": "MyOrders",
    "private": true,
    "version": "0.0.0",
    "scripts": {
        "test": "karma start ClientApp/test/karma.conf.js",
        "webpack": "webpack"
    },
    "devDependencies": {
        "@angular/animations": "6.0.2",
        "@angular/common": "6.0.2",
        "@angular/compiler": "6.0.2",
        "@angular/compiler-cli": "^6.0.2",
        "@angular/core": "6.0.2",
        "@angular/forms": "6.0.2",
        "@angular/http": "6.0.2",
        "@angular/platform-browser": "6.0.2",
        "@angular/platform-browser-dynamic": "6.0.2",
        "@angular/platform-server": "6.0.2",
        "@angular/router": "6.0.2",
        "@ngtools/webpack": "^6.0.3",
        "@types/chai": "4.1.3",
        "@types/jasmine": "2.8.7",
        "@types/webpack-env": "1.13.6",
        "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": "5.0.0",
        "bootstrap": "4.1.1",
        "chai": "4.1.2",
        "chart.js": "2.7.2",
        "css": "2.2.3",
        "css-loader": "0.28.11",
        "es6-shim": "0.35.3",
        "event-source-polyfill": "0.0.12",
        "expose-loader": "0.7.5",
        "extract-text-webpack-plugin": "3.0.2",
        "file-loader": "1.1.11",
        "font-awesome": "^4.7.0",
        "hammerjs": "^2.0.8",
        "html-loader": "0.5.5",
        "isomorphic-fetch": "2.2.1",
        "jasmine-core": "3.1.0",
        "jquery": "3.3.1",
        "json-loader": "0.5.7",
        "karma": "2.0.2",
        "karma-chai": "0.1.0",
        "karma-chrome-launcher": "2.2.0",
        "karma-cli": "1.0.1",
        "karma-jasmine": "1.1.2",
        "karma-webpack": "3.0.0",
        "popper.js": "1.14.3",
        "preboot": "6.0.0-beta.4",
        "raw-loader": "0.5.1",
        "reflect-metadata": "0.1.12",
        "rxjs": "6.1.0",
        "rxjs-compat": "^6.1.0",
        "source-map": "^0.7.3",
        "style-loader": "0.21.0",
        "to-string-loader": "1.1.5",
        "typescript": "2.7.2",
        "uglifyjs-webpack-plugin": "^1.2.5",
        "url-loader": "1.0.1",
        "webpack": "4.8.3",
        "webpack-cli": "2.1.3",
        "webpack-hot-middleware": "2.22.2",
        "webpack-merge": "4.1.2",
        "zone.js": "0.8.26"
        }
    }

Webpack.config是:

    const path = require('path');
    const webpack = require('webpack');
    const merge = require('webpack-merge');
    const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
    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 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$/, loaders: ['@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' }
                ]
            }
        };

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

        return [clientBundleConfig, serverBundleConfig];
    };

0 个答案:

没有答案