1
- const { join, relative, resolve, sep } = require ( "path" ) ;
1
+ const { join, relative, resolve, sep, dirname } = require ( "path" ) ;
2
2
3
3
const webpack = require ( "webpack" ) ;
4
4
const nsWebpack = require ( "nativescript-dev-webpack" ) ;
5
5
const nativescriptTarget = require ( "nativescript-dev-webpack/nativescript-target" ) ;
6
6
const { nsReplaceBootstrap } = require ( "nativescript-dev-webpack/transformers/ns-replace-bootstrap" ) ;
7
+ const { nsReplaceLazyLoader } = require ( "nativescript-dev-webpack/transformers/ns-replace-lazy-loader" ) ;
8
+ const { nsSupportHmrNg } = require ( "nativescript-dev-webpack/transformers/ns-support-hmr-ng" ) ;
9
+ const { getMainModulePath } = require ( "nativescript-dev-webpack/utils/ast-utils" ) ;
7
10
const CleanWebpackPlugin = require ( "clean-webpack-plugin" ) ;
8
11
const CopyWebpackPlugin = require ( "copy-webpack-plugin" ) ;
9
12
const { BundleAnalyzerPlugin } = require ( "webpack-bundle-analyzer" ) ;
@@ -33,8 +36,8 @@ module.exports = env => {
33
36
// The 'appPath' and 'appResourcesPath' values are fetched from
34
37
// the nsconfig.json configuration file
35
38
// when bundling with `tns run android|ios --bundle`.
36
- appPath = "app " ,
37
- appResourcesPath = "app/ App_Resources" ,
39
+ appPath = "src " ,
40
+ appResourcesPath = "App_Resources" ,
38
41
39
42
// You can provide the following flags when running 'tns run android|ios'
40
43
aot, // --env.aot
@@ -44,23 +47,45 @@ module.exports = env => {
44
47
sourceMap, // --env.sourceMap
45
48
hmr, // --env.hmr,
46
49
} = env ;
47
- const externals = ( env . externals || [ ] ) . map ( ( e ) => { // --env.externals
48
- return new RegExp ( e + ".*" ) ;
49
- } ) ;
50
50
51
+ const externals = nsWebpack . getConvertedExternals ( env . externals ) ;
51
52
const appFullPath = resolve ( projectRoot , appPath ) ;
52
53
const appResourcesFullPath = resolve ( projectRoot , appResourcesPath ) ;
53
-
54
+ const tsConfigName = "tsconfig.tns.json" ;
54
55
const entryModule = `${ nsWebpack . getEntryModule ( appFullPath ) } .ts` ;
55
56
const entryPath = `.${ sep } ${ entryModule } ` ;
57
+ const ngCompilerTransformers = [ ] ;
58
+ const additionalLazyModuleResources = [ ] ;
59
+ if ( aot ) {
60
+ ngCompilerTransformers . push ( nsReplaceBootstrap ) ;
61
+ }
62
+
63
+ if ( hmr ) {
64
+ ngCompilerTransformers . push ( nsSupportHmrNg ) ;
65
+ }
66
+
67
+ // when "@angular/core" is external, it's not included in the bundles. In this way, it will be used
68
+ // directly from node_modules and the Angular modules loader won't be able to resolve the lazy routes
69
+ // fixes https://github.com/NativeScript/nativescript-cli/issues/4024
70
+ if ( env . externals && env . externals . indexOf ( "@angular/core" ) > - 1 ) {
71
+ const appModuleRelativePath = getMainModulePath ( resolve ( appFullPath , entryModule ) , tsConfigName ) ;
72
+ if ( appModuleRelativePath ) {
73
+ const appModuleFolderPath = dirname ( resolve ( appFullPath , appModuleRelativePath ) ) ;
74
+ // include the lazy loader inside app module
75
+ ngCompilerTransformers . push ( nsReplaceLazyLoader ) ;
76
+ // include the new lazy loader path in the allowed ones
77
+ additionalLazyModuleResources . push ( appModuleFolderPath ) ;
78
+ }
79
+ }
56
80
57
81
const ngCompilerPlugin = new AngularCompilerPlugin ( {
58
82
hostReplacementPaths : nsWebpack . getResolver ( [ platform , "tns" ] ) ,
59
- platformTransformers : aot ? [ nsReplaceBootstrap ( ( ) => ngCompilerPlugin ) ] : null ,
83
+ platformTransformers : ngCompilerTransformers . map ( t => t ( ( ) => ngCompilerPlugin , resolve ( appFullPath , entryModule ) ) ) ,
60
84
mainPath : resolve ( appPath , entryModule ) ,
61
- tsConfigPath : join ( __dirname , "tsconfig.tns.json" ) ,
85
+ tsConfigPath : join ( __dirname , tsConfigName ) ,
62
86
skipCodeGeneration : ! aot ,
63
87
sourceMap : ! ! sourceMap ,
88
+ additionalLazyModuleResources : additionalLazyModuleResources
64
89
} ) ;
65
90
66
91
const config = {
@@ -171,14 +196,15 @@ module.exports = env => {
171
196
// tns-core-modules reads the app.css and its imports using css-loader
172
197
{
173
198
test : / [ \/ | \\ ] a p p \. c s s $ / ,
174
- use : {
175
- loader : "css -loader",
176
- options : { minimize : false , url : false } ,
177
- }
199
+ use : [
200
+ "nativescript-dev-webpack/style-hot -loader",
201
+ { loader : "css-loader" , options : { minimize : false , url : false } }
202
+ ]
178
203
} ,
179
204
{
180
205
test : / [ \/ | \\ ] a p p \. s c s s $ / ,
181
206
use : [
207
+ "nativescript-dev-webpack/style-hot-loader" ,
182
208
{ loader : "css-loader" , options : { minimize : false , url : false } } ,
183
209
"sass-loader"
184
210
]
@@ -192,6 +218,7 @@ module.exports = env => {
192
218
test : / (?: \. n g f a c t o r y \. j s | \. n g s t y l e \. j s | \. t s ) $ / ,
193
219
use : [
194
220
"nativescript-dev-webpack/moduleid-compat-loader" ,
221
+ "nativescript-dev-webpack/lazy-ngmodule-hot-loader" ,
195
222
"@ngtools/webpack" ,
196
223
]
197
224
} ,
0 commit comments