Webpack错误:angular_compiler_plugin - 无法读取未定义

时间:2018-04-20 15:55:22

标签: angular webpack

我正在尝试部署我的应用,但是当它执行命令时:

webpack.js --config webpack.config.vendor.js --env.prod

我收到以下错误:

  

angular_compiler_plugin.ts:580   compiler.hooks.environment.tap('angular-compiler',()=> {   
^   
TypeError:无法读取未定义的属性“environment”

我已根据this article的“调整”部分的建议将AngoCompilerPlugin替换为AoTPlugin - 这是我在创建第一个应用程序时所遵循的 - 因为AotCaused我同样的错误,因为它的角度不相容但是现在我遇到了这个错误,我真的迷失了解决它。

有谁知道如何解决这个问题?

我的代码

这是我的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;
var nodeExternals = require('webpack-node-externals');

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', 'angular2-router-loader'] : '@ngtools/webpack' },
                { test: /\.html$/, use: 'html-loader?minimize=false' },
                { test: /\.css$/, use: [ 'to-string-loader', 'style-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize' ] },
                { test: /\.(png|jpg|jpeg|gif|svg)$/, use: 'url-loader?limit=25000' },
                //font management
                {
                    test: /\.(svg|eot|ttf|woff|woff2)$/,
                    use: [{
                        loader: 'file-loader',
                        options: {
                            name: 'images/[name].[hash].[ext]'
                        }
                    }]
                }
            ]
        },
        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')
        },

        //esclusione moduli node
        target: 'node',
        externals: [nodeExternals()], // in order to ignore all modules in node_modules folder
        devtool: 'inline-source-map'
    });

    return [clientBundleConfig, serverBundleConfig];
};

我的package.json内容:

{
  "name": "AngularHeroes",
  "private": true,
  "version": "0.0.0",
  "scripts": {
    "test": "karma start ClientApp/test/karma.conf.js",
    "postinstall": "npm run webpack:vendor",
    "webpack": "webpack --config webpack.config.js",
    "webpack:vendor": "webpack --config webpack.config.vendor.js"
  },
  "devDependencies": {
    "@angular/animations": "^5.2.9",
    "@angular/cdk": "^5.2.4",
    "@angular/common": "^5.2.9",
    "@angular/compiler": "^5.2.9",
    "@angular/compiler-cli": "^5.2.9",
    "@angular/core": "^5.2.9",
    "@angular/forms": "^5.2.9",
    "@angular/http": "^5.2.9",
    "@angular/material": "^5.2.4",
    "@angular/platform-browser": "^5.2.9",
    "@angular/platform-browser-dynamic": "^5.2.9",
    "@angular/platform-server": "^5.2.9",
    "@angular/router": "^5.2.9",
    "@ngtools/webpack": "^6.0.0-rc.2.4",
    "@types/chai": "^4.1.2",
    "@types/jasmine": "^2.8.6",
    "@types/webpack-env": "^1.13.5",
    "angular2-router-loader": "0.3.5",
    "angular2-template-loader": "0.6.2",
    "aspnet-prerendering": "^3.0.1",
    "aspnet-webpack": "^2.0.3",
    "awesome-typescript-loader": "^4.0.1",
    "bootstrap": "^4.0.0",
    "cdk": "0.0.0",
    "chai": "^4.1.2",
    "css": "2.2.1",
    "css-loader": "^0.28.11",
    "devextreme": "^17.2.7",
    "devextreme-angular": "^17.2.7",
    "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",
    "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.0",
    "karma-chai": "0.1.0",
    "karma-chrome-launcher": "2.2.0",
    "karma-cli": "1.0.1",
    "karma-jasmine": "^1.1.1",
    "karma-webpack": "^3.0.0",
    "popper.js": "^1.14.3",
    "preboot": "^6.0.0-beta.3",
    "raw-loader": "0.5.1",
    "reflect-metadata": "0.1.12",
    "requirejs": "^2.3.5",
    "rxjs": "5.5.8",
    "style-loader": "^0.20.3",
    "to-string-loader": "1.1.5",
    "typescript": "^2.8.1",
    "url-loader": "^1.0.1",
    "webpack": "^3.11.0",
    "webpack-hot-middleware": "^2.22.0",
    "webpack-merge": "^4.1.2",
    "webpack-node-externals": "^1.7.2",
    "file-saver": "^1.3.8",
    "zone.js": "^0.8.26",
    "angular-jwt": "^0.1.9",
    "jsonwebtoken": "^8.2.1"
  },
  "dependencies": {
    "angular-jwt": "^0.1.9",
    "file-saver": "^1.3.8",
    "jsonwebtoken": "^8.2.1",
    "webpack": "^3.11.0"
  }
}

1 个答案:

答案 0 :(得分:5)

问题来自这种依赖性     " @ ngtools / webpack":" ^ 6.0.0-rc.2.4", 你的解决方案在这里 https://github.com/angular/angular-cli/issues/9793 基本上这样做 npm install @ngtools/webpack@1.10.2 --only=dev