首页 > 解决方案 > 使用 uglifyjs webpack 插件修改属性会导致 Angular 应用程序中出现 Uncaught ReferenceError

问题描述

我有一个托管 Angular 6 应用程序的 .net Core 2.1 MVC 应用程序。我正在使用 webpack 4 来捆绑它。我想丑化我的代码以获得优化的好处,并在部署时尽可能地保护它。当我尝试修改属性名称时,我的 Angular 应用程序停止工作,并且出现错误:“未捕获的 ReferenceError:vendor_8b8ec7e9d2171cd0b5b6”未定义。如果我在 mangle 属性中将“properties”参数从 true 设置为 false,则一切正常但是属性和函数名称在丑陋的代码中。我真的希望它们被破坏。

请帮忙。

我的 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;
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const extractCSS = new ExtractTextPlugin('bundle.min.css');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

module.exports = (env) =>
{
    // Configuration for client-side bundle suitable for running in browsers
    const clientBundleOutputDir = './wwwroot/dist';

    // Configuration in common to both client-side and server-side bundles
    const isDevBuild = !(env && env.prod);
    const sharedConfig = {
        mode: 'production',
        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(),
            new CopyWebpackPlugin([{ from: __dirname + '/node_modules/font-awesome/fonts', to: __dirname + '/wwwroot/dist/fonts' }], { copyUnmodified: true }),
            new CopyWebpackPlugin([{ from: __dirname + '/node_modules/font-awesome/css/font-awesome.css', to: __dirname + '/wwwroot/dist/fonts' }], { copyUnmodified: true }),
            new CopyWebpackPlugin([{ from: __dirname + '/node_modules/@progress/kendo-theme-default/dist/all.css', to: __dirname + '/wwwroot/dist/' }], { copyUnmodified: true }),
            new CopyWebpackPlugin([{ from: __dirname + '/ClientApp/assets/css/core.css', to: __dirname + '/wwwroot/dist/' }], { copyUnmodified: true }),
            new CopyWebpackPlugin([{ from: __dirname + '/ClientApp/assets/css/metro.min.css', to: __dirname + '/wwwroot/dist/' }], { copyUnmodified: true }),
            new CopyWebpackPlugin([{ from: __dirname + '/ClientApp/assets/css/metro-colors.min.css', to: __dirname + '/wwwroot/dist/' }], { copyUnmodified: true }),
            new CopyWebpackPlugin([{ from: __dirname + '/ClientApp/assets/css/metro-icons.min.css', to: __dirname + '/wwwroot/dist/' }], { copyUnmodified: true }),
            new CopyWebpackPlugin([{ from: __dirname + '/ClientApp/assets/css/metro-responsive.min.css', to: __dirname + '/wwwroot/dist/' }], { copyUnmodified: true }),
            new CopyWebpackPlugin([{ from: __dirname + '/ClientApp/assets/css/metro-rtl.min.css', to: __dirname + '/wwwroot/dist/' }], { copyUnmodified: true }),
            new CopyWebpackPlugin([{ from: __dirname + '/ClientApp/assets/css/metro-schemes.min.css', to: __dirname + '/wwwroot/dist/' }], { copyUnmodified: true }),
            new CopyWebpackPlugin([{ from: __dirname + '/ClientApp/images', to: __dirname + '/wwwroot/dist/images' }], { copyUnmodified: true }),
            new CopyWebpackPlugin([{ from: __dirname + '/ClientApp/accessoryimages', to: __dirname + '/wwwroot/dist/accessoryimages' }], { copyUnmodified: true }),
            new CopyWebpackPlugin([{ from: __dirname + '/ClientApp/layoutimages', to: __dirname + '/wwwroot/dist/layoutimages' }], { copyUnmodified: true }),
            new CopyWebpackPlugin([{ from: __dirname + '/ClientApp/documents', to: __dirname + '/wwwroot/dist/documents' }], { copyUnmodified: true }),
            new CopyWebpackPlugin([{ from: __dirname + '/ClientApp/Fonts', to: __dirname + '/wwwroot/dist/fonts' }], { copyUnmodified: true })
        ]
    };


    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')
            }),
            new webpack.ProvidePlugin({
                $: "jquery",
                jQuery: "jquery"
            })
        ].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 AngularCompilerPlugin({
                    tsConfigPath: './tsconfig.json',
                    entryModule: path.join(__dirname, 'ClientApp/app/app.browser.module#AppModule'),
                    exclude: ['./**/*.server.ts']
                })
            ]),
        optimization: {
            minimizer: [].concat(isDevBuild ? [] : [
                new UglifyJsPlugin({
                    cache: true,
                    parallel: true,
                    uglifyOptions: {
                        compress: true,
                        ecma: 6,
                        mangle: {
                            toplevel: true, properties: false
                        },
                        toplevel: true,
                        keep_classnames: false,
                        keep_fnames: false
                    },
                    sourceMap: false
                })
            ])
        }
    });

    // 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'
            }),
            new webpack.ProvidePlugin({
                $: "jquery",
                jQuery: "jquery"
            })
        ].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',
        optimization: {
            minimizer: [].concat(isDevBuild ? [] : [
                new UglifyJsPlugin({
                    cache: true,
                    parallel: true,
                    uglifyOptions: {
                        compress: true,
                        ecma: 6,
                        mangle: {
                            toplevel: true, properties: false
                        },
                        toplevel: true,
                        keep_classnames: false,
                        keep_fnames: false
                    },
                    sourceMap: false
                })
            ])
        }
    });

    return [clientBundleConfig, serverBundleConfig];
};

标签: angularwebpackbundling-and-minificationuglifyjswebpack.config.js

解决方案


推荐阅读