8000 feat(webpack): disable aot flag, optional angular dep and tsconfig ut… · NativeScript/NativeScript@0df5aa9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0df5aa9

Browse files
edusperonirigor789
andauthored
feat(webpack): disable aot flag, optional angular dep and tsconfig utils (#9711)
Co-authored-by: Igor Randjelovic <rigor789@gmail.com>
1 parent 9c6c84b commit 0df5aa9

File tree

5 files changed

+68
-19
lines changed

5 files changed

+68
-19
lines changed

packages/webpack5/__tests__/configuration/__snapshots__/angular.spec.ts.snap

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,8 @@ exports[`angular configuration for android 1`] = `
400400
new AngularWebpackPlugin(
401401
{
402402
tsconfig: '__jest__/tsconfig.json',
403-
directTemplateLoading: false
403+
directTemplateLoading: false,
404+
jitMode: false
404405
}
405406
)
406407
],
@@ -816,7 +817,8 @@ exports[`angular configuration for ios 1`] = `
816817
new AngularWebpackPlugin(
817818
{
818819
tsconfig: '__jest__/tsconfig.json',
819-
directTemplateLoading: false
820+
directTemplateLoading: false,
821+
jitMode: false
820822
}
821823
)
822824
],

packages/webpack5/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"webpack-virtual-modules": "^0.4.0"
5555
},
5656
"devDependencies": {
57+
"@angular-devkit/build-angular": "^13.1.2",
5758
"@types/css": "0.0.33",
5859
"@types/jest": "27.0.1",
5960
"@types/loader-utils": "2.0.3",

packages/webpack5/src/configuration/angular.ts

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import { extname, resolve } from 'path';
33
import Config from 'webpack-chain';
44
import { existsSync } from 'fs';
55

6+
import { getDependencyPath } from '../helpers/dependencies';
67
import { getProjectFilePath } from '../helpers/project';
78
import { env as _env, IWebpackEnv } from '../index';
9+
import { readTsConfig } from '../helpers/tsconfig';
10+
import { warnOnce } from '../helpers/log';
811
import {
912
getEntryDirPath,
1013
getEntryPath,
@@ -22,6 +25,8 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
2225
getProjectFilePath('tsconfig.json'),
2326
].find((path) => existsSync(path));
2427

28+
const disableAOT = !!env.disableAOT;
29+
2530
// remove default ts rule
2631
config.module.rules.delete('ts');
2732

@@ -158,6 +163,7 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
158163
{
159164
tsconfig: tsConfigPath,
160165
directTemplateLoading: false,
166+
jitMode: disableAOT,
161167
},
162168
]);
163169

@@ -169,23 +175,40 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
169175
.use('angular-hot-loader')
170176
.loader('angular-hot-loader');
171177
});
172-
// zone + async/await
173-
config.module
174-
.rule('angular-webpack-loader')
175-
.test(/\.[cm]?[tj]sx?$/)
176-
.exclude.add(
177-
/[/\\](?:core-js|@babel|tslib|web-animations-js|web-streams-polyfill)[/\\]/
178-
)
179-
.end()
180-
.resolve.set('fullySpecified', false)
181-
.end()
182-
.before('angular')
183-
.use('webpack-loader')
184-
.loader('@angular-devkit/build-angular/src/babel/webpack-loader')
185-
.options({
186-
scriptTarget: ScriptTarget.ESNext,
187-
aot: true,
188-
});
178+
179+
const buildAngularPath = getDependencyPath('@angular-devkit/build-angular');
180+
if (buildAngularPath) {
181+
const tsConfig = readTsConfig(tsConfigPath);
182+
const scriptTarget = tsConfig.options.target ?? ScriptTarget.ESNext;
183+
const buildAngularOptions: any = {
184+
scriptTarget,
185+
aot: !disableAOT,
186+
};
187+
if (disableAOT) {
188+
buildAngularOptions.optimize = false;
189+
}
190+
// zone + async/await
191+
config.module
192+
.rule('angular-webpack-loader')< F438 /span>
193+
.test(/\.[cm]?[tj]sx?$/)
194+
.exclude.add(
195+
/[/\\](?:core-js|@babel|tslib|web-animations-js|web-streams-polyfill)[/\\]/
196+
)
197+
.end()
198+
.resolve.set('fullySpecified', false)
199+
.end()
200+
.before('angular')
201+
.use('webpack-loader')
202+
.loader('@angular-devkit/build-angular/src/babel/webpack-loader')
203+
.options(buildAngularOptions);
204+
} else {
205+
warnOnce(
206+
'build-angular-missing',
207+
`
208+
@angular-devkit/build-angular is missing! Some features may not work as expected. Please install it manually to get rid of this warning.
209+
`
210+
);
211+
}
189212
}
190213

191214
// look for platform specific polyfills first

packages/webpack5/src/helpers/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
getPlatform,
2727
getPlatformName,
2828
} from './platform';
29+
import { readTsConfig } from './tsconfig';
2930

3031
// intentionally populated manually
3132
// as this generates nicer typings
@@ -75,4 +76,7 @@ export default {
7576
addVirtualEntry,
7677
addVirtualModule,
7778
},
79+
tsconfig: {
80+
readTsConfig,
81+
},
7882
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { readConfigFile, parseJsonConfigFileContent, sys } from 'typescript';
2+
import { dirname } from 'path';
3+
4+
export function readTsConfig(path: string) {
5+
const f = readConfigFile(path, sys.readFile);
6+
7+
const parsed = parseJsonConfigFileContent(
8+
f.config,
9+
{
10+
fileExists: sys.fileExists,
11+
readFile: sys.readFile,
12+
readDirectory: sys.readDirectory,
13+
useCaseSensitiveFileNames: true,
14+
},
15+
dirname(path)
16+
);
17+
18+
return parsed;
19+
}

0 commit comments

Comments
 (0)
0