@@ -5,13 +5,15 @@ 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
7
const { nsReplaceLazyLoader } = require ( "nativescript-dev-webpack/transformers/ns-replace-lazy-loader" ) ;
8
+ const { nsSupportHmrNg } = require ( "nativescript-dev-webpack/transformers/ns-support-hmr-ng" ) ;
8
9
const { getMainModulePath } = require ( "nativescript-dev-webpack/utils/ast-utils" ) ;
9
10
const CleanWebpackPlugin = require ( "clean-webpack-plugin" ) ;
10
11
const CopyWebpackPlugin = require ( "copy-webpack-plugin" ) ;
11
12
const { BundleAnalyzerPlugin } = require ( "webpack-bundle-analyzer" ) ;
12
13
const { NativeScriptWorkerPlugin } = require ( "nativescript-worker-loader/NativeScriptWorkerPlugin" ) ;
13
- const UglifyJsPlugin = require ( "uglifyjs-webpack-plugin" ) ;
14
- const { AngularCompilerPlugin } = require ( "@ngtools/webpack" ) ;
14
+ const TerserPlugin = require ( "terser-webpack-plugin" ) ;
15
+ const { getAngularCompilerPlugin } = require ( "nativescript-dev-webpack/plugins/NativeScriptAngularCompilerPlugin" ) ;
16
+ const hashSalt = Date . now ( ) . toString ( ) ;
15
17
16
18
module . exports = env => {
17
19
// Add your custom Activities, Services and other Android app components here.
@@ -26,6 +28,7 @@ module.exports = env => {
26
28
throw new Error ( "You need to provide a target platform!" ) ;
27
29
}
28
30
31
+ const AngularCompilerPlugin = getAngularCompilerPlugin ( platform ) ;
29
32
const projectRoot = __dirname ;
30
33
31
34
// Default destination inside platforms/<platform>/...
@@ -45,29 +48,38 @@ module.exports = env => {
45
48
uglify, // --env.uglify
46
49
report, // --env.report
47
50
sourceMap, // --env.sourceMap
51
+ hiddenSourceMap, // --env.hiddenSourceMap
48
52
hmr, // --env.hmr,
53
+ unitTesting, // --env.unitTesting
49
54
} = env ;
50
- env . externals = env . externals || [ ] ;
51
- const externals = ( env . externals ) . map ( ( e ) => { // --env.externals
52
- return new RegExp ( e + ".*" ) ;
53
- } ) ;
54
55
56
+ const isAnySourceMapEnabled = ! ! sourceMap || ! ! hiddenSourceMap ;
57
+ const externals = nsWebpack . getConvertedExternals ( env . externals ) ;
55
58
const appFullPath = resolve ( projectRoot , appPath ) ;
56
59
const appResourcesFullPath = resolve ( projectRoot , appResourcesPath ) ;
57
-
58
- const entryModule = `${ nsWebpack . getEntryModule ( appFullPath ) } .ts` ;
60
+ const tsConfigName = "tsconfig.tns.json" ;
61
+ const entryModule = `${ nsWebpack . getEntryModule ( appFullPath , platform ) } .ts` ;
59
62
const entryPath = `.${ sep } ${ entryModule } ` ;
63
+ const entries = { bundle : entryPath , application : "./application.android" } ;
64
+ if ( platform === "ios" ) {
65
+ entries [ "tns_modules/tns-core-modules/inspector_modules" ] = "inspector_modules.js" ;
66
+ } ;
67
+
60
68
const ngCompilerTransformers = [ ] ;
61
69
const additionalLazyModuleResources = [ ] ;
62
70
if ( aot ) {
63
71
ngCompilerTransformers . push ( nsReplaceBootstrap ) ;
64
72
}
65
73
74
+ if ( hmr ) {
75
+ ngCompilerTransformers . push ( nsSupportHmrNg ) ;
76
+ }
77
+
66
78
// when "@angular/core" is external, it's not included in the bundles. In this way, it will be used
67
79
// directly from node_modules and the Angular modules loader won't be able to resolve the lazy routes
68
80
// fixes https://github.com/NativeScript/nativescript-cli/issues/4024
69
- if ( env . externals . indexOf ( "@angular/core" ) > - 1 ) {
70
- const appModuleRelativePath = getMainModulePath ( resolve ( appFullPath , entryModule ) ) ;
81
+ if ( env . externals && env . externals . indexOf ( "@angular/core" ) > - 1 ) {
82
+ const appModuleRelativePath = getMainModulePath ( resolve ( appFullPath , entryModule ) , tsConfigName ) ;
71
83
if ( appModuleRelativePath ) {
72
84
const appModuleFolderPath = dirname ( resolve ( appFullPath , appModuleRelativePath ) ) ;
73
85
// include the lazy loader inside app module
@@ -79,14 +91,16 @@ module.exports = env => {
79
91
80
92
const ngCompilerPlugin = new AngularCompilerPlugin ( {
81
93
hostReplacementPaths : nsWebpack . getResolver ( [ platform , "tns" ] ) ,
82
- platformTransformers : ngCompilerTransformers . map ( t => t ( ( ) => ngCompilerPlugin ) ) ,
83
- mainPath : resolve ( appPath , entryModule ) ,
84
- tsConfigPath : join ( __dirname , "tsconfig.tns.json" ) ,
94
+ platformTransformers : ngCompilerTransformers . map ( t => t ( ( ) => ngCompilerPlugin , resolve ( appFullPath , entryModule ) ) ) ,
95
+ mainPath : join ( appFullPath , entryModule ) ,
96
+ tsConfigPath : join ( __dirname , tsConfigName ) ,
85
97
skipCodeGeneration : ! aot ,
86
- sourceMap : ! ! sourceMap ,
98
+ sourceMap : ! ! isAnySourceMapEnabled ,
87
99
additionalLazyModuleResources : additionalLazyModuleResources
88
100
} ) ;
89
101
102
+ let sourceMapFilename = nsWebpack . getSourceMapFilename ( hiddenSourceMap , __dirname , dist ) ;
103
+
90
104
const config = {
91
105
mode : uglify ? "production" : "development" ,
92
106
context : appFullPath ,
@@ -99,16 +113,15 @@ module.exports = env => {
99
113
]
100
114
} ,
101
115
target : nativescriptTarget ,
102
- entry : {
103
- bundle : entryPath ,
104
- application : "./application.android" ,
105
- } ,
116
+ entry : entries ,
106
117
output : {
107
118
pathinfo : false ,
108
119
path : dist ,
120
+ sourceMapFilename,
109
121
libraryTarget : "commonjs2" ,
110
122
filename : "[name].js" ,
111
123
globalObject : "global" ,
124
+ hashSalt
112
125
} ,
113
126
resolve : {
114
127
extensions : [ ".ts" , ".js" , ".scss" , ".css" ] ,
@@ -135,8 +148,9 @@ module.exports = env => {
135
148
"fs" : "empty" ,
136
149
"__dirname" : false ,
137
150
} ,
138
- devtool : sourceMap ? "inline-source-map" : "none" ,
151
+ devtool : hiddenSourceMap ? "hidden-source-map" : ( sourceMap ? "inline-source-map" : "none" ) ,
139
152
optimization : {
153
+ runtimeChunk : "single" ,
140
154
splitChunks : {
141
155
cacheGroups : {
142
156
vendor : {
@@ -153,12 +167,14 @@ module.exports = env => {
153
167
} ,
154
168
minimize : ! ! uglify ,
155
169
minimizer : [
156
- new UglifyJsPlugin ( {
170
+ new TerserPlugin ( {
157
171
parallel : true ,
158
172
cache : true ,
159
- uglifyOptions : {
173
+ sourceMap : isAnySourceMapEnabled ,
174
+ terserOptions : {
160
175
output : {
161
176
comments : false ,
177
+ semicolons : ! isAnySourceMapEnabled
162
178
} ,
163
179
compress : {
164
180
// The Android SBG has problems parsing the output
@@ -173,7 +189,7 @@ module.exports = env => {
173
189
module : {
174
190
rules : [
175
191
{
176
- test : new RegExp ( entryPath ) ,
192
+ test : nsWebpack . getEntryPathRegExp ( appFullPath , entryPath ) ,
177
193
use : [
178
194
// Require all Android app components
179
195
platform === "android" && {
@@ -186,6 +202,9 @@ module.exports = env => {
186
202
options : {
187
203
angular : true ,
188
204
loadCss : ! snapshot , // load the application css if in debug mode
205
+ unitTesting,
206
+ appFullPath,
207
+ projectRoot,
189
208
}
190
209
} ,
191
210
] . filter ( loader => ! ! loader )
@@ -239,25 +258,23 @@ module.exports = env => {
239
258
} ) ,
240
259
// Remove all files from the out dir.
241
260
new CleanWebpackPlugin ( [ `${ dist } /**/*` ] ) ,
242
- // Copy native app resources to out dir.
243
- new CopyWebpackPlugin ( [
244
- {
245
- from : `${ appResourcesFullPath } /${ appResourcesPlatformDir } ` ,
246
- to : `${ dist } /App_Resources/${ appResourcesPlatformDir } ` ,
247
- context : projectRoot
248
- } ,
249
- ] ) ,
250
261
// Copy assets to out dir. Add your own globs as needed.
251
262
new CopyWebpackPlugin ( [
252
263
{ from : { glob : "fonts/**" } } ,
253
264
{ from : { glob : "**/*.jpg" } } ,
254
265
{ from : { glob : "**/*.png" } } ,
255
266
] , { ignore : [ `${ relative ( appPath , appResourcesFullPath ) } /**` ] } ) ,
256
267
// Generate a bundle starter script and activate it in package.json
257
- new nsWebpack . GenerateBundleStarterPlugin ( [
258
- "./vendor" ,
259
- "./bundle" ,
260
- ] ) ,
268
+ new nsWebpack . GenerateBundleStarterPlugin (
269
+ // Don't include `runtime.js` when creating a snapshot. The plugin
270
+ // configures the WebPack runtime to be generated inside the snapshot
271
+ // module and no `runtime.js` module exist.
272
+ ( snapshot ? [ ] : [ "./runtime" ] )
273
+ . concat ( [
274
+ "./vendor" ,
275
+ "./bundle" ,
276
+ ] )
277
+ ) ,
261
278
// For instructions on how to set up workers with webpack
262
279
// check out https://github.com/nativescript/worker-loader
263
280
new NativeScriptWorkerPlugin ( ) ,
@@ -267,6 +284,18 @@ module.exports = env => {
267
284
] ,
268
285
} ;
269
286
287
+ // Copy the native app resources to the out dir
288
+ // only if doing a full build (tns run/build) and not previewing (tns preview)
289
+ if ( ! externals || externals . length === 0 ) {
290
+ config . plugins . push ( new CopyWebpackPlugin ( [
291
+ {
292
+ from : `${ appResourcesFullPath } /${ appResourcesPlatformDir } ` ,
293
+ to : `${ dist } /App_Resources/${ appResourcesPlatformDir } ` ,
294
+ context : projectRoot
295
+ } ,
296
+ ] ) ) ;
297
+ }
298
+
270
299
271
300
if ( report ) {
272
301
// Generate report files for bundles content
0 commit comments