Angular4项目中的Webpack for Prod错误

时间:2017-12-11 16:36:41

标签: webpack .net-core

我一直在用#34; dotnet new angular"来解决很多webpack错误。项目(Angular 4,Dotnet Core 2.0.0)。

我的项目在开发中运作良好。但是,当我尝试做一个" dotnet发布"它正在使用" webpack --env.prod"这导致我的问题。我认为这与AOT有关。 最后一个问题超出了我的范围,我找不到导致此错误:

    ERROR in ./ClientApp/boot.browser.ts
Module not found : error : Can't resolve './../$$_gendir/ClientApp/app/app.module.browser.ngfactory' in '/Users/a2ron44/Projects/CryptoGoblin/ClientApp' [/Users/a2ron44/Projects/CryptoGoblin/CryptoGoblin.csproj]
       @ ./ClientApp/boot.browser.ts 5:0-95
  Child
      Hash: 0db56caee944b1d990af
      Time: 14368ms
               Asset     Size  Chunks                    Chunk Names
      main-server.js  1.81 MB       0  [emitted]  [big]  main-server

      ERROR in ./ClientApp/boot.server.ts
Module not found : error : Can't resolve './../$$_gendir/ClientApp/app/app.module.server.ngfactory' in '/Users/a2ron44/Projects/CryptoGoblin/ClientApp' [/Users/a2ron44/Projects/CryptoGoblin/CryptoGoblin.csproj]
       @ ./ClientApp/boot.server.ts 8:0-94
/Users/a2ron44/Projects/CryptoGoblin/CryptoGoblin.csproj(48,5): error MSB3073: The command "node node_modules/webpack/bin/webpack.js --env.prod" exited with code 2.

这是我的webpack.config.js文件:



const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const AotPlugin = require('@ngtools/webpack').AotPlugin;
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'] : '@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(), new webpack.DefinePlugin({ "global.GENTLY": false })]
    };

    // 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.module.browser#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.module.server#AppModule'),
                exclude: ['./**/*.browser.ts']
            })
        ]),
        output: {
            libraryTarget: 'commonjs',
            path: path.join(__dirname, './ClientApp/dist')
        },
        target: 'node',
        devtool: 'inline-source-map'
    });

    return [clientBundleConfig, serverBundleConfig];
};




还有什么我应该看的吗?

1 个答案:

答案 0 :(得分:0)

这可以通过替换

来解决
{ test: /\.ts$/, use: isDevBuild ? ['awesome-typescript-loader?silent=true', 'angular2-template-loader'] : '@ngtools/webpack' },

{ test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/, include: [/ClientApp/, /\$\$_gendir/], use: isDevBuild ? ['awesome-typescript-loader?silent=true', 'angular2-template-loader'] : '@ngtools/webpack' },

如果您一直在更新为角度5,则还需要将AotPlugin替换为AngularCompilerPlugin(这基本上是文件中的搜索和替换)