使用@ngtools/Webpack在angular 11+Webpack 4中实现AOT



我正在尝试在我的angular项目中实现AOT,该项目不支持cli。我搜索了一下,发现@ngtools/webpack是一条路。我的angular是11.0.4,Typescript是4.0.5,webpack是4.44.2。我安装了@ngtools/webpack-11.0.4。

我的tsconfig.aot.json-

{
"angularCompilerOptions": {
"skipMetadataEmit" : true,
"trace": true,
"genDir": "aot/"
},
"angularOptions": {
"annotationsAs": "decorators"
},
"compilerOptions": {
"target": "es2015",
"module": "es2020",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [ "es2015", "dom", "es7", "es2017", "es2018", "es2019" ],
// "noEmit": true,
"noImplicitAny": false,
"suppressImplicitAnyIndexErrors": true,
"baseUrl": ".",
"allowJs": true,
"allowUnreachableCode": false,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"incremental": false,
"resolveJsonModule": true,
"outDir": "./out-tsc/app",
},
"exclude": [
"node_modules",
"aot/",
"app/Content/js/embed/appBootstrap.ts"
]

}

我的Webpack.config-

//dependencies
const path = require('path');
const webpack = require('webpack');
const { AngularCompilerPlugin } = require('@ngtools/webpack');
//paths
const portalPath = path.join(__dirname, 'app');
const portalContentPath = path.join(portalPath, 'Content');
const webpackScriptPath = path.join(portalContentPath, 'js');
const nodeModulesPath = path.join(__dirname, 'node_modules');
const vbNodeModulesPath = path.join(nodeModulesPath, '@vb');
const outputPath = path.join(portalPath, 'dist');
//entry points
const entries = {
embed: path.join(webpackScriptPath, 'embed', 'EmbedBootstrap.ts')
};
module.exports = {
mode: 'production',
entry: entries,
output: {
path: outputPath,
filename: '[name].js',
publicPath: '/dist/',
sourceMapFilename: '[file].map'
},
module: {
rules: [
{
test: /(.ngfactory.js|.ngstyle.js|.ts)$/,
use: [
'@ngtools/webpack'
]
},
{
test: /.html$/,
use: [
{
loader:'html-loader',
options: {
minimize: {
caseSensitive: true,
keepClosingSlash: true,
removeAttributeQuotes: false
}
}
}
]
}
]
},
plugins: [
new AngularCompilerPlugin({
tsConfigPath: path.join(__dirname, 'tsconfig.aot.json'),
sourceMap: true,
skipCodeGeneration: true, // AOT toggle
mainPath: path.join(webpackScriptPath, 'embed', 'appBootstrap.ts'),
entryModule: path.join(webpackScriptPath, 'embed', 'App.Module#AppModule')
}),
...Object.values(entries).map(entryPath => new webpack.ContextReplacementPlugin(/@angular(\|/)core(\|/)fesm2015/, entryPath))
],
resolve: {
modules: [
'node_modules',
vbNodeModulesPath,
path.join(vbNodeModulesPath, 'vb-player', 'node_modules', '@vb')
],
extensions: ['.tsx', '.ts', '.js']
}
};

appBootstrap.ts


import { enableProdMode } from '@angular/core';
import { platformBrowser } from '@angular/platform-browser';
// This file not getting generated and giving compilation error
import { AppModuleNgFactory } from './App.Module.ngfactory';
enableProdMode();
platformBrowser().bootstrapModuleFactory(AppModuleNgFactory);

App.Module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './App.Component';
@NgModule({
bootstrap: [AppComponent],
declarations: [
AppComponent
],
imports: [
CommonModule,
FormsModule
]
})
export class AppModule {}

我正在使用以下命令运行webpack.min.config(我将其添加到我的package.json中,并使用npm运行构建(

node --max_old_space_size=4096 node_modules/webpack/bin/webpack --config ./webpack.config.min.js

我得到的错误:-

Module not found: Error: Can't resolve './App.Module.ngfactory'.

我不确定工厂文件将在哪个文件夹中生成。我找不到任何关于@ngtools/webpack最新版本的可靠实现文档。

我还尝试使用platformBrowserDynamic((而不是ngFactory引导我的AppModule。在这种情况下,我会错误地说

Module parse failed: Unexpected character '@' (41:0)
> @NgModule({
|       bootstrap: [AppComponent],
|       declarations: [
@ ./App.Module.ts

我试着在tsconfig中使用一些选项,但最终没有成功。如有任何帮助,我们将不胜感激。提前非常感谢。

如果将"enableIvy": false置于";angularCompilerOptions;?

最新更新