angular - 使用 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];
};
解决方案
推荐阅读
- java - 属性中的 Spring Boot 和 Keycloak 配置引发 NullPointerException 然后 500 状态代码
- spring - 当来自同一主机的相同 url 的 curl 工作正常时,为什么 Spring RestTemplate.getForObject() 到 localhost 会得到 404?
- django - 如何根据条件增加和减少 django Templete 中的值
- flutter - Flutter:使用 Azure AD B2C 进行身份验证返回结果,但访问令牌为空
- javascript - JS:如何在特定单词和一定数量的字符之后提取子字符串?
- ios - 构建中的“不支持的 swift 版本”(ARKit + Reality Composer)
- javascript - 如何转换 x,y 坐标以使用绝对显示?
- c# - 如何更改 mp3 的播放速度?
- plugins - 在 Grafana 中,如何使用普通数据库表显示系统状态表并根据文本值突出显示?
- node.js - 在 Cloud Functions 中执行复合 Firestore 查询