【发布时间】:2018-05-03 05:04:34
【问题描述】:
我正在尝试将 ASP.NET Core + Angular 4 SPA 项目从 .NET Core 2.0.0/Angular4 升级到 .NET Core 2.0.3/Angular5。除了生产环境中的服务器端渲染外,我设法让一切正常工作,即当我发布应用程序时:
发生未处理的异常:未找到“AppModule”的 NgModule 元数据。
错误:找不到“AppModule”的 NgModule 元数据。
只有同时满足这两个条件时才会出现问题:
- Webpack 使用
--env.prod开关构建包 -
Index.cshtml视图文件包含asp-prerender-module参数,如下例所示:<app asp-prerender-module="ClientApp/dist/main-server">Loading...</app>
如果我移除开关和/或参数,问题就会消失(连同 SSR)。
我可以提供很多其他信息:
- 这与 IIS 无关,它发生在 Kestrel 级别。
- 它与 Web 服务器计算机无关,因为我什至可以通过在 Debug 或 Release 运行之前手动启动 Webpack 并使用
--end.prod开关在本地复制它. - 它似乎与源代码没有任何关系,因为即使使用带有非常基本的 AppModule 文件和琐碎代码的单组件示例应用程序,我也可以重现它。
- 该项目在 .NET Core 2.0.0 和 Angular 4.3.x 上运行良好。
- 我在
webpack.config.js文件中更改的唯一主要内容是将AotPlugin替换为Angular5-specific 由@ngtools/webpack 包提供的新AngularCompilerPlugin:我认为这也可能是原因,因为--env.prod开关使用该 AOT 编译器而不是 JIT 编译器。那,或者与.NET SpaServices package 相关的东西——可能与新的 Angular5 和/或新的 AoT 编译器不相称?
遗憾的是,我无法恢复到以前的 AotPlugin,因为它也会引发错误 - 这是完全可以理解的,因为它不适合与 Angular5 一起使用。
软件版本
- .NET Core 2.0.3
- Angular 5.0.2
- @ngtools/webpack 1.8.2(也尝试了 1.8.1 - 结果相同)
- WebPack 2.6.1(也尝试使用 2.5.6 - 结果相同)
webpack.config.js
const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const AotPlugin = require('@ngtools/webpack').AngularCompilerPlugin;
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: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/, include: /ClientApp/, 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()]
};
// 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];
};
【问题讨论】:
-
你能分享你的 webpack.config.js 文件吗? TIA
-
@GeekHour 刚刚做了:谢谢。
标签: asp.net angular asp.net-core