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