- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试将 ASP.NET Core + Angular 4 SPA 项目从 .NET Core 2.0.0/Angular4 升级到 .NET Core 2.0.3/Angular5。除了生产环境中的服务器端渲染外,我设法让一切正常工作,即当我发布应用程序时:
An unhandled exception has occurred: No NgModule metadata found for 'AppModule'.
Error: No NgModule metadata found for 'AppModule'.
只有同时满足这两个条件时才会出现问题:
--env.prod
开关构建包Index.cshtml
View 文件包含 asp-prerender-module
参数,如下例所示:
如果我移除开关和/或参数,问题就会消失(连同 SSR)。
我可以提供很多其他信息:
--end.prod
开关手动启动 Webpack 来在本地复制它发布运行。 webpack.config.js
文件中唯一更改的主要内容是将 AotPlugin
替换为新的 Angular5-specific AngularCompilerPlugin
由@ngtools/webpack 提供包:我认为这也可能是原因,因为 --env.prod
开关使用该 AOT 编译器而不是 JIT 编译器。那个,或者与 .NET SpaServices package 相关的东西- 可能与新的 Angular5 和/或新的 AoT 编译器不相上下? 遗憾的是,我无法恢复到以前的 AotPlugin
,因为它也会引发错误 - 这是完全可以理解的,因为它不适合与 Angular5 一起使用。
软件版本
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];
};
最佳答案
@ngtool/webpack
configuration Angular 5 与 Angular 2/4 略有不同。因此,我将 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;
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'] : '@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' },
{
test: /(?:\.ngfactory\.js|\.ngstyle\.js)$/,
loader: '@ngtools/webpack',
options: {
tsConfigPath: '/tsconfig.json',
}
}
]
},
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.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 AngularCompilerPlugin({
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];
};
这是我的 package.json
文件,
{
"dependencies": {
"@angular/animations": "^5.0.2",
"@angular/cdk": "^5.0.0-rc0",
"@angular/cli": "^1.6.0-beta.2",
"@angular/common": "^5.0.2",
"@angular/compiler": "^5.0.2",
"@angular/compiler-cli": "^5.0.2",
"@angular/core": "^5.0.2",
"@angular/forms": "^5.0.2",
"@angular/http": "^5.0.2",
"@angular/material": "^5.0.0-rc0",
"@angular/platform-browser": "^5.0.2",
"@angular/platform-browser-dynamic": "^5.0.2",
"@angular/platform-server": "^5.0.2",
"@angular/router": "^5.0.2",
"@ngtools/webpack": "^1.8.3",
"@types/webpack-env": "^1.13.2",
"angular2-template-loader": "0.6.2",
"aspnet-prerendering": "^3.0.1",
"aspnet-webpack": "^2.0.1",
"awesome-typescript-loader": "^3.4.0",
"bootstrap": "3.3.7",
"css": "2.2.1",
"css-loader": "^0.28.7",
"es6-shim": "0.35.3",
"event-source-polyfill": "0.0.12",
"expose-loader": "0.7.4",
"extract-text-webpack-plugin": "^3.0.2",
"file-loader": "^1.1.5",
"html-loader": "^0.5.1",
"isomorphic-fetch": "2.2.1",
"jquery": "3.2.1",
"json-loader": "^0.5.7",
"preboot": "^5.1.7",
"raw-loader": "0.5.1",
"reflect-metadata": "0.1.10",
"request": "^2.83.0",
"rxjs": "^5.5.2",
"style-loader": "^0.19.0",
"to-string-loader": "1.1.5",
"typescript": "^2.6.1",
"url-loader": "^0.6.2",
"webpack": "^3.8.1",
"webpack-hot-middleware": "^2.20.0",
"webpack-merge": "^4.1.1",
"zone.js": "^0.8.18"
},
"devDependencies": {
"@types/chai": "^4.0.5",
"@types/jasmine": "^2.8.2",
"@types/node": "^8.0.53",
"chai": "^4.1.2",
"jasmine-core": "2.8.0",
"karma": "1.7.1",
"karma-chai": "0.1.0",
"karma-chrome-launcher": "2.2.0",
"karma-cli": "1.0.1",
"karma-jasmine": "1.1.0",
"karma-webpack": "2.0.6"
},
"name": "aspnetcoreangularspa",
"private": true,
"scripts": {
"test": "karma start ClientApp/test/karma.conf.js"
},
"version": "0.0.0"
}
如果您在运行解决方案时仍然遇到问题,请查看 repository一个可行的解决方案。
希望对你有帮助:)
关于asp.net - No NgModule metadata found for 'AppModule' Error in Angular 5 server-side rendering with --env.prod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47384209/
这个问题已经有答案了: env-cmd error failed to locate ./.env file in gatsby? (14 个回答) 已关闭 3 年前。 我正在学习 Gatsby 教程
我正在尝试使用 mgcv-package,但 mgcv::gam-function 只返回一条错误消息 Tmean = c(1,3,10,5,7) prec = c(300,500,700,600,5
在 Laravel 6 中 Documentation Application Key的第二段是这样写的 Typically, this string should be 32 characters
我试图在创建消息时传递 sns 主题。但根据创建的变量,arn 可能会有所不同。 例如,在环境变量中,我将有 3 个人及其主题。 john = arn:xxxxxxxx sally= arn:xxxx
我开始学习 Angular2,我发现在示例中它们覆盖了配置文件中的 process.env.NODE_ENV 和 process.env.ENV 变量。此代码来自 this example 的 web
我从头开始创建了一个Kotlin原生应用程序,我得到了上面的错误。我怎么才能解决这个问题呢?这是一款简单的“Hello World”应用程序。我有其他计算机,但该错误没有出现在那里。我认为这与科南没有
我有一个 Dockerfile,我在其中设置了一些环境变量(在我运行容器时使用)。一些环境变量依赖于以前的环境变量。当我可以替换为一个变量时,我想保持干爽并避免多次对环境变量的值进行硬编码。 在这个简
我正在学习 Docker 并面临将一个 env 变量的值替换为另一个 env 变量的问题。 这是我的 Dockerfile FROM ubuntu ENV var_env=Tarun ENV comm
我正在将一个项目从 Symfony 2.3 升级到 Symfony 3.4,我对 .env 和 .env.dist 文件之间的关系有点困惑。据我了解,它们之间的关系与Symfony2中paramete
在 python 中,有没有办法从特定的 .env 文件中检索 env 变量的值?例如,我有多个 .env 文件,如下所示: .env.a .env.a ... 我在 .env.b 中有一个名为 IN
我假设我的身份验证服务无法加载配置文件,从而导致错误。我正在关注 AUTH0 上的教程。这是链接 https://auth0.com/blog/real-world-angular-series-pa
我正在使用 base.env 作为我的几个 docker 服务的 env_file。在这个 base.env 中,我有几个部分的环境变量在整个文件中重复。例如,对于三个不同的环境变量,port 和 i
我正在尝试使用 values.SecretValue() 设置我的 aws 存储桶名称。要设置 Django 的媒体 url,我过去使用过: AWS_STORAGE_BUCKET_NAME = val
我通过从PowerShell运行$env:path ="$($env:path);."来“解决”一个问题。显然,它将当前目录添加到了我的路径。请添加到哪个路径变量?在我的环境变量对话框中,我会在哪里看
我有一个很长的自定义应用程序配置文件,用于在工作站上运行的应用程序,其中一些文本元素必须在系统之间手动更改。 这个脚本是为了让我的生活更轻松 - $content = Get-Content("C:\
因为我使用docker和docker-compose而不是Homestead和任何基于Vargrant的开发解决方案,所以我想避免与laravel在.env文件的使用上发生冲突。因此,我希望larav
当我尝试最后一个例子时 perlfaq5: How-do-I-count-the-number-of-lines-in-a-file?我收到一条错误消息。我应该怎样做才能使脚本正常工作? #!/usr
我试图理解为什么当我从我的 .env 文件中明确删除或注释掉它们时,我的 Docker 容器中的 env 变量仍然出现。我是 Docker 的新手,不知道这是预期的行为还是异常。 我的系统设置方式是,
我正在使用 PHP 和 Laravel 框架,我使用 .env 来设置我的环境变量。然后我从我的 PHP 环境调用 python 脚本,并将结果返回给 PHP。我的问题是,在 Python 中设置的默
我安装了 Anaconda 并创建了一个新环境(“dell_proj”)。然后我用我的新环境在 Pycharm 中创建了一个新项目。我的期望是我只能在这个项目中使用我通过 Conda 在特定环境中安装
我是一名优秀的程序员,十分优秀!