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 { getMainModulePath } = require ( "nativescript-dev-webpack/utils/ast-utils" ) ;
7
9
const CleanWebpackPlugin = require ( "clean-webpack-plugin" ) ;
8
10
const CopyWebpackPlugin = require ( "copy-webpack-plugin" ) ;
9
11
const { BundleAnalyzerPlugin } = require ( "webpack-bundle-analyzer" ) ;
@@ -34,8 +36,8 @@ module.exports = env => {
34
36
// The 'appPath' and 'appResourcesPath' values are fetched from
35
37
// the nsconfig.json configuration file
36
38
// when bundling with `tns run android|ios --bundle`.
37
- appPath = "app " ,
38
- appResourcesPath = "app/ App_Resources" ,
39
+ appPath = "src " ,
40
+ appResourcesPath = "App_Resources" ,
39
41
40
42
// You can provide the following flags when running 'tns run android|ios'
41
43
aot, // --env.aot
@@ -45,7 +47,8 @@ module.exports = env => {
45
47
sourceMap, // --env.sourceMap
46
48
hmr, // --env.hmr,
47
49
} = env ;
48
- const externals = ( env . externals || [ ] ) . map ( ( e ) => { // --env.externals
50
+ env . externals = env . externals || [ ] ;
51
+ const externals = ( env . externals ) . map ( ( e ) => { // --env.externals
49
52
return new RegExp ( e + ".*" ) ;
50
53
} ) ;
51
54
@@ -54,14 +57,34 @@ module.exports = env => {
54
57
55
58
const entryModule = `${ nsWebpack . getEntryModule ( appFullPath ) } .ts` ;
56
59
const entryPath = `.${ sep } ${ entryModule } ` ;
60
+ const ngCompilerTransformers = [ ] ;
61
+ const additionalLazyModuleResources = [ ] ;
62
+ if ( aot ) {
63
+ ngCompilerTransformers . push ( nsReplaceBootstrap ) ;
64
+ }
65
+
66
+ // when "@angular/core" is external, it's not included in the bundles. In this way, it will be used
67
+ // directly from node_modules and the Angular modules loader won't be able to resolve the lazy routes
68
+ // fixes https://github.com/NativeScript/nativescript-cli/issues/4024
69
+ if ( env . externals . indexOf ( "@angular/core" ) > - 1 ) {
70
+ const appModuleRelativePath = getMainModulePath ( resolve ( appFullPath , entryModule ) ) ;
71
+ if ( appModuleRelativePath ) {
72
+ const appModuleFolderPath = dirname ( resolve ( appFullPath , appModuleRelativePath ) ) ;
73
+ // include the lazy loader inside app module
74
+ ngCompilerTransformers . push ( nsReplaceLazyLoader ) ;
75
+ // include the new lazy loader path in the allowed ones
76
+ additionalLazyModuleResources . push ( appModuleFolderPath ) ;
77
+ }
78
+ }
57
79
58
80
const ngCompilerPlugin = new AngularCompilerPlugin ( {
59
81
hostReplacementPaths : nsWebpack . getResolver ( [ platform , "tns" ] ) ,
60
- platformTransformers : aot ? [ nsReplaceBootstrap ( ( ) => ngCompilerPlugin ) ] : null ,
82
+ platformTransformers : ngCompilerTransformers . map ( t => t ( ( ) => ngCompilerPlugin ) ) ,
61
83
mainPath : resolve ( appPath , entryModule ) ,
62
84
tsConfigPath : join ( __dirname , "tsconfig.tns.json" ) ,
63
85
skipCodeGeneration : ! aot ,
64
86
sourceMap : ! ! sourceMap ,
87
+ additionalLazyModuleResources : additionalLazyModuleResources
65
88
} ) ;
66
89
67
90
const config = {
@@ -78,7 +101,6 @@ module.exports = env => {
78
101
target : nativescriptTarget ,
79
102
entry : {
80
103
bundle : entryPath ,
81
- application : "./application.android" ,
82
104
} ,
83
105
output : {
84
106
pathinfo : false ,
@@ -195,6 +217,7 @@ module.exports = env => {
195
217
test : / (?: \. n g f a c t o r y \. j s | \. n g s t y l e \. j s | \. t s ) $ / ,
196
218
use : [
197
219
"nativescript-dev-webpack/moduleid-compat-loader" ,
220
+ "nativescript-dev-webpack/lazy-ngmodule-hot-loader" ,
198
221
"@ngtools/webpack" ,
199
222
]
200
223
} ,
@@ -225,9 +248,9 @@ module.exports = env => {
225
248
] ) ,
226
249
// Copy assets to out dir. Add your own globs as needed.
227
250
new CopyWebpackPlugin ( [
228
- { from : "fonts/**" } ,
229
- { from : "**/*.jpg" } ,
230
- { from : "**/*.png" } ,
251
+ { from : { glob : "fonts/**" } } ,
252
+ { from : { glob : "**/*.jpg" } } ,
253
+ { from : { glob : "**/*.png" } } ,
231
254
] , { ignore : [ `${ relative ( appPath , appResourcesFullPath ) } /**` ] } ) ,
232
255
// Generate a bundle starter script and activate it in package.json
233
256
new nsWebpack . GenerateBundleStarterPlugin ( [
0 commit comments