angular - Webpack错误: angular_compiler_plugin - Cannot read property 'environment' of undefined

标签 angular webpack

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

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

我收到以下错误:

angular_compiler_plugin.ts:580 compiler.hooks.environment.tap('angular-compiler', () => {
                                                                            ^
TypeError: Cannot read property 'environment' of undefined

按照 this article 的“调整”部分的建议,我已将 AoTPlugin 替换为 AngularCompilerPlugin - 我在整个第一个应用程序创建过程中都遵循了这一点 - 因为 Aot 由于它的 Angular 5 不兼容而给我带来了同样的错误,但现在我遇到了这个错误,而且我真的在解决它时迷失了方向。

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

<小时/>

下面是我的代码

这是我的 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"
  }
}

最佳答案

问题出在这种依赖性上 "@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

关于angular - Webpack错误: angular_compiler_plugin - Cannot read property 'environment' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49945544/

相关文章:

angular - Docker组成了Angular 502 Bad Gateway

Angular Mentions - 保存所选用户的 ID 值

javascript - Webpack babel-loader runtime : Module build failed: TypeError: this. setDynamic 不是函数

javascript - { React jsx babel es6 webpack } 如何在 render 中评论 ( return (//||/**/) )?

javascript - Webpack 从 .jsx 文件中删除 css?

typescript - 如何让 webpack 和 babel 处理 private typescript 关键字?

angular - ngx-select-dropdown 示例值未绑定(bind)

具有异步管道多个条件的 Angular *ngIf 变量

typescript - angular2 类型 'Subject<{}>' 不可分配给类型 '() => Subject<number>'

debugging - 如何调试 webpack bundle 文件错误并在包含数千个的 bundle 中找到确切的错误。 ReactJs 中的行数