feat(webpack): disable aot flag, optional angular dep and tsconfig utils (#9711)

Co-authored-by: Igor Randjelovic <rigor789@gmail.com>
This commit is contained in:
Eduardo Speroni
2021-12-22 16:54:17 -03:00
committed by GitHub
parent 9c6c84b107
commit 0df5aa9712
5 changed files with 68 additions and 19 deletions

View File

@ -400,7 +400,8 @@ exports[`angular configuration for android 1`] = `
new AngularWebpackPlugin( new AngularWebpackPlugin(
{ {
tsconfig: '__jest__/tsconfig.json', tsconfig: '__jest__/tsconfig.json',
directTemplateLoading: false directTemplateLoading: false,
jitMode: false
} }
) )
], ],
@ -816,7 +817,8 @@ exports[`angular configuration for ios 1`] = `
new AngularWebpackPlugin( new AngularWebpackPlugin(
{ {
tsconfig: '__jest__/tsconfig.json', tsconfig: '__jest__/tsconfig.json',
directTemplateLoading: false directTemplateLoading: false,
jitMode: false
} }
) )
], ],

View File

@ -54,6 +54,7 @@
"webpack-virtual-modules": "^0.4.0" "webpack-virtual-modules": "^0.4.0"
}, },
"devDependencies": { "devDependencies": {
"@angular-devkit/build-angular": "^13.1.2",
"@types/css": "0.0.33", "@types/css": "0.0.33",
"@types/jest": "27.0.1", "@types/jest": "27.0.1",
"@types/loader-utils": "2.0.3", "@types/loader-utils": "2.0.3",

View File

@ -3,8 +3,11 @@ import { extname, resolve } from 'path';
import Config from 'webpack-chain'; import Config from 'webpack-chain';
import { existsSync } from 'fs'; import { existsSync } from 'fs';
import { getDependencyPath } from '../helpers/dependencies';
import { getProjectFilePath } from '../helpers/project'; import { getProjectFilePath } from '../helpers/project';
import { env as _env, IWebpackEnv } from '../index'; import { env as _env, IWebpackEnv } from '../index';
import { readTsConfig } from '../helpers/tsconfig';
import { warnOnce } from '../helpers/log';
import { import {
getEntryDirPath, getEntryDirPath,
getEntryPath, getEntryPath,
@ -22,6 +25,8 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
getProjectFilePath('tsconfig.json'), getProjectFilePath('tsconfig.json'),
].find((path) => existsSync(path)); ].find((path) => existsSync(path));
const disableAOT = !!env.disableAOT;
// remove default ts rule // remove default ts rule
config.module.rules.delete('ts'); config.module.rules.delete('ts');
@ -158,6 +163,7 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
{ {
tsconfig: tsConfigPath, tsconfig: tsConfigPath,
directTemplateLoading: false, directTemplateLoading: false,
jitMode: disableAOT,
}, },
]); ]);
@ -169,6 +175,18 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
.use('angular-hot-loader') .use('angular-hot-loader')
.loader('angular-hot-loader'); .loader('angular-hot-loader');
}); });
const buildAngularPath = getDependencyPath('@angular-devkit/build-angular');
if (buildAngularPath) {
const tsConfig = readTsConfig(tsConfigPath);
const scriptTarget = tsConfig.options.target ?? ScriptTarget.ESNext;
const buildAngularOptions: any = {
scriptTarget,
aot: !disableAOT,
};
if (disableAOT) {
buildAngularOptions.optimize = false;
}
// zone + async/await // zone + async/await
config.module config.module
.rule('angular-webpack-loader') .rule('angular-webpack-loader')
@ -182,10 +200,15 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
.before('angular') .before('angular')
.use('webpack-loader') .use('webpack-loader')
.loader('@angular-devkit/build-angular/src/babel/webpack-loader') .loader('@angular-devkit/build-angular/src/babel/webpack-loader')
.options({ .options(buildAngularOptions);
scriptTarget: ScriptTarget.ESNext, } else {
aot: true, warnOnce(
}); 'build-angular-missing',
`
@angular-devkit/build-angular is missing! Some features may not work as expected. Please install it manually to get rid of this warning.
`
);
}
} }
// look for platform specific polyfills first // look for platform specific polyfills first

View File

@ -26,6 +26,7 @@ import {
getPlatform, getPlatform,
getPlatformName, getPlatformName,
} from './platform'; } from './platform';
import { readTsConfig } from './tsconfig';
// intentionally populated manually // intentionally populated manually
// as this generates nicer typings // as this generates nicer typings
@ -75,4 +76,7 @@ export default {
addVirtualEntry, addVirtualEntry,
addVirtualModule, addVirtualModule,
}, },
tsconfig: {
readTsConfig,
},
}; };

View File

@ -0,0 +1,19 @@
import { readConfigFile, parseJsonConfigFileContent, sys } from 'typescript';
import { dirname } from 'path';
export function readTsConfig(path: string) {
const f = readConfigFile(path, sys.readFile);
const parsed = parseJsonConfigFileContent(
f.config,
{
fileExists: sys.fileExists,
readFile: sys.readFile,
readDirectory: sys.readDirectory,
useCaseSensitiveFileNames: true,
},
dirname(path)
);
return parsed;
}