1- const { relative, resolve, join } = require ( "path" ) ;
1+ const { join , relative, resolve, sep } = require ( "path" ) ;
22
33const webpack = require ( "webpack" ) ;
44const nsWebpack = require ( "nativescript-dev-webpack" ) ;
@@ -10,14 +10,19 @@ const { NativeScriptWorkerPlugin } = require("nativescript-worker-loader/NativeS
1010const UglifyJsPlugin = require ( "uglifyjs-webpack-plugin" ) ;
1111
1212module . exports = env => {
13+ // Add your custom Activities, Services and other Android app components here.
14+ const appComponents = [
15+ "tns-core-modules/ui/frame" ,
16+ "tns-core-modules/ui/frame/activity" ,
17+ ] ;
18+
1319 const platform = env && ( env . android && "android" || env . ios && "ios" ) ;
1420 if ( ! platform ) {
1521 throw new Error ( "You need to provide a target platform!" ) ;
1622 }
1723
1824 const platforms = [ "ios" , "android" ] ;
1925 const projectRoot = __dirname ;
20- nsWebpack . loadAdditionalPlugins ( { projectDir : projectRoot } ) ;
2126
2227 // Default destination inside platforms/<platform>/...
2328 const dist = resolve ( projectRoot , nsWebpack . getAppPath ( platform , projectRoot ) ) ;
@@ -30,19 +35,27 @@ module.exports = env => {
3035 appPath = "app" ,
3136 appResourcesPath = "app/App_Resources" ,
3237
33- // Snapshot, uglify and report can be enabled by providing
34- // the `--env. snapshot`, `--env.uglify` or ` --env.report` flags
35- // when running 'tns run android|ios'
36- snapshot ,
37- uglify ,
38- report ,
38+ // You can provide the following flags when running 'tns run android|ios'
39+ snapshot, // --env.snapshot
40+ uglify , // --env.uglify
41+ report , // --env.report
42+ sourceMap , // --env.sourceMap
43+ hmr , // --env.hmr ,
3944 } = env ;
45+ const externals = ( env . externals || [ ] ) . map ( ( e ) => { // --env.externals
46+ return new RegExp ( e + ".*" ) ;
47+ } ) ;
4048
4149 const appFullPath = resolve ( projectRoot , appPath ) ;
4250 const appResourcesFullPath = resolve ( projectRoot , appResourcesPath ) ;
4351
52+ const entryModule = nsWebpack . getEntryModule ( appFullPath ) ;
53+ const entryPath = `.${ sep } ${ entryModule } .ts` ;
54+
4455 const config = {
56+ mode : uglify ? "production" : "development" ,
4557 context : appFullPath ,
58+ externals,
4659 watchOptions : {
4760 ignored : [
4861 appResourcesFullPath ,
@@ -52,27 +65,29 @@ module.exports = env => {
5265 } ,
5366 target : nativescriptTarget ,
5467 entry : {
55- bundle : `./${ nsWebpack . getEntryModule ( appFullPath ) } ` ,
56- vendor : "./vendor"
68+ bundle : entryPath ,
5769 } ,
5870 output : {
59- pathinfo : true ,
71+ pathinfo : false ,
6072 path : dist ,
6173 libraryTarget : "commonjs2" ,
6274 filename : "[name].js" ,
75+ globalObject : "global" ,
6376 } ,
6477 resolve : {
6578 extensions : [ ".ts" , ".js" , ".scss" , ".css" ] ,
6679 // Resolve {N} system modules from tns-core-modules
6780 modules : [
81+ resolve ( __dirname , "node_modules/tns-core-modules" ) ,
82+ resolve ( __dirname , "node_modules" ) ,
6883 "node_modules/tns-core-modules" ,
6984 "node_modules" ,
7085 ] ,
7186 alias : {
72- '~' : resolve ( "./app" )
87+ '~' : appFullPath
7388 } ,
74- // don't resolve symlinks to symlinked modules
75- symlinks : false
89+ // resolve symlinks to symlinked modules
90+ symlinks : true
7691 } ,
7792 resolveLoader : {
7893 // don't resolve symlinks to symlinked loaders
@@ -84,10 +99,80 @@ module.exports = env => {
8499 "timers" : false ,
85100 "setImmediate" : false ,
86101 "fs" : "empty" ,
102+ "__dirname" : false ,
103+ } ,
104+ devtool : sourceMap ? "inline-source-map" : "none" ,
105+ optimization : {
106+ splitChunks : {
107+ cacheGroups : {
108+ vendor : {
109+ name : "vendor" ,
110+ chunks : "all" ,
111+ test : ( module , chunks ) => {
112+ const moduleName = module . nameForCondition ? module . nameForCondition ( ) : '' ;
113+ return / [ \\ / ] n o d e _ m o d u l e s [ \\ / ] / . test ( moduleName ) ||
114+ appComponents . some ( comp => comp === moduleName ) ;
115+
116+ } ,
117+ enforce : true ,
118+ } ,
119+ }
120+ } ,
121+ minimize : ! ! uglify ,
122+ minimizer : [
123+ new UglifyJsPlugin ( {
124+ parallel : true ,
125+ cache : true ,
126+ uglifyOptions : {
127+ output : {
128+ comments : false ,
129+ } ,
130+ compress : {
131+ // The Android SBG has problems parsing the output
132+ // when these options are enabled
133+ 'collapse_vars' : platform !== "android" ,
134+ sequences : platform !== "android" ,
135+ }
136+ }
137+ } )
138+ ] ,
87139 } ,
88140 module : {
89141 rules : [
90- { test : / \. ( h t m l | x m l ) $ / , use : "raw-loader" } ,
142+ {
143+ test : new RegExp ( entryPath ) ,
144+ use : [
145+ // Require all Android app components
146+ platform === "android" && {
147+ loader : "nativescript-dev-webpack/android-app-components-loader" ,
148+ options : { modules : appComponents }
149+ } ,
150+
151+ {
152+ loader : "nativescript-dev-webpack/bundle-config-loader" ,
153+ options : {
154+ loadCss : ! snapshot , // load the application css if in debug mode
155+ }
156+ } ,
157+ ] . filter ( loader => ! ! loader )
158+ } ,
159+
160+ {
161+ test : / - p a g e \. t s $ / ,
162+ use : "nativescript-dev-webpack/script-hot-loader"
163+ } ,
164+
165+ {
166+ test : / \. ( c s s | s c s s ) $ / ,
167+ use : "nativescript-dev-webpack/style-hot-loader"
168+ } ,
169+
170+ {
171+ test : / \. ( h t m l | x m l ) $ / ,
172+ use : "nativescript-dev-webpack/markup-hot-loader"
173+ } ,
174+
175+ { test : / \. ( h t m l | x m l ) $ / , use : "nativescript-dev-webpack/xml-namespace-loader" } ,
91176
92177 {
93178 test : / \. c s s $ / ,
@@ -102,17 +187,23 @@ module.exports = env => {
102187 ]
103188 } ,
104189
105- { test : / \. t s $ / , use : "awesome-typescript-loader" }
190+ {
191+ test : / \. t s $ / ,
192+ use : {
193+ loader : "ts-loader" ,
194+ options : {
195+ configFile : "tsconfig.tns.json" ,
196+ allowTsInNodeModules : true ,
197+ } ,
198+ }
199+ } ,
106200 ]
107201 } ,
108202 plugins : [
109- // Vendor libs go to the vendor.js chunk
110- new webpack . optimize . CommonsChunkPlugin ( {
111- name : [ "vendor" ] ,
112- } ) ,
113203 // Define useful constants like TNS_WEBPACK
114204 new webpack . DefinePlugin ( {
115205 "global.TNS_WEBPACK" : "true" ,
206+ "process" : undefined ,
116207 } ) ,
117208 // Remove all files from the out dir.
118209 new CleanWebpackPlugin ( [ `${ dist } /**/*` ] ) ,
@@ -126,17 +217,17 @@ module.exports = env => {
126217 ] ) ,
127218 // Copy assets to out dir. Add your own globs as needed.
128219 new CopyWebpackPlugin ( [
129- { from : "fonts/**" } ,
130- { from : "**/*.jpg" } ,
131- { from : "**/*.png" } ,
132- { from : "**/*.xml" } ,
220+ { from : { glob : "fonts/**" } } ,
221+ { from : { glob : "**/*.jpg" } } ,
222+ { from : { glob : "**/*.png" } } ,
133223 ] , { ignore : [ `${ relative ( appPath , appResourcesFullPath ) } /**` ] } ) ,
134224 // Generate a bundle starter script and activate it in package.json
135225 new nsWebpack . GenerateBundleStarterPlugin ( [
136226 "./vendor" ,
137227 "./bundle" ,
138228 ] ) ,
139- // Support for web workers since v3.2
229+ // For instructions on how to set up workers with webpack
230+ // check out https://github.com/nativescript/worker-loader
140231 new NativeScriptWorkerPlugin ( ) ,
141232 new nsWebpack . PlatformFSPlugin ( {
142233 platform,
@@ -146,6 +237,7 @@ module.exports = env => {
146237 new nsWebpack . WatchStateLoggerPlugin ( ) ,
147238 ] ,
148239 } ;
240+
149241 if ( report ) {
150242 // Generate report files for bundles content
151243 config . plugins . push ( new BundleAnalyzerPlugin ( {
@@ -156,26 +248,22 @@ module.exports = env => {
156248 statsFilename : resolve ( projectRoot , "report" , `stats.json` ) ,
157249 } ) ) ;
158250 }
251+
159252 if ( snapshot ) {
160253 config . plugins . push ( new nsWebpack . NativeScriptSnapshotPlugin ( {
161254 chunk : "vendor" ,
255+ requireModules : [
256+ "tns-core-modules/bundle-entry-points" ,
257+ ] ,
162258 projectRoot,
163259 webpackConfig : config ,
164- targetArchs : [ "arm" , "arm64" , "ia32" ] ,
165- tnsJavaClassesOptions : { packages : [ "tns-core-modules" ] } ,
166- useLibs : false
167260 } ) ) ;
168261 }
169- if ( uglify ) {
170- config . plugins . push ( new webpack . LoaderOptionsPlugin ( { minimize : true } ) ) ;
171-
172- // Work around an Android issue by setting compress = false
173- const compress = platform !== "android" ;
174- config . plugins . push ( new UglifyJsPlugin ( {
175- uglifyOptions : {
176- compress,
177- }
178- } ) ) ;
262+
263+ if ( hmr ) {
264+ config . plugins . push ( new webpack . HotModuleReplacementPlugin ( ) ) ;
179265 }
266+
267+
180268 return config ;
181269} ;
0 commit comments