1- const { resolve, join } = require ( "path" ) ;
1+ const { relative , resolve, join } = require ( "path" ) ;
22
33const webpack = require ( "webpack" ) ;
44const nsWebpack = require ( "nativescript-dev-webpack" ) ;
55const nativescriptTarget = require ( "nativescript-dev-webpack/nativescript-target" ) ;
6+ const CleanWebpackPlugin = require ( "clean-webpack-plugin" ) ;
67const CopyWebpackPlugin = require ( "copy-webpack-plugin" ) ;
7- const ExtractTextPlugin = require ( "extract-text-webpack-plugin" ) ;
88const { BundleAnalyzerPlugin } = require ( "webpack-bundle-analyzer" ) ;
99const { NativeScriptWorkerPlugin } = require ( "nativescript-worker-loader/NativeScriptWorkerPlugin" ) ;
1010const UglifyJsPlugin = require ( "uglifyjs-webpack-plugin" ) ;
@@ -14,23 +14,60 @@ module.exports = env => {
1414 if ( ! platform ) {
1515 throw new Error ( "You need to provide a target platform!" ) ;
1616 }
17+
1718 const platforms = [ "ios" , "android" ] ;
18- const { snapshot, uglify, report, aot } = env ;
19- const ngToolsWebpackOptions = { tsConfigPath : "tsconfig.json" } ;
19+ const projectRoot = __dirname ;
20+ // Default destination inside platforms/<platform>/...
21+ const dist = resolve ( projectRoot , nsWebpack . getAppPath ( platform ) ) ;
22+ const appResourcesPlatformDir = platform === "android" ? "Android" : "iOS" ;
23+
24+ const {
25+ // The 'appPath' and 'appResourcesPath' values are fetched from
26+ // the nsconfig.json configuration file
27+ // when bundling with `tns run android|ios --bundle`.
28+ appPath = "app" ,
29+ appResourcesPath = "app/App_Resources" ,
30+
31+ // Aot, snapshot, uglify and report can be enabled by providing
32+ // the `--env.snapshot`, `--env.uglify` or `--env.report` flags
33+ // when running 'tns run android|ios'
34+ aot,
35+ snapshot,
36+ uglify,
37+ report,
38+ } = env ;
39+ const ngToolsWebpackOptions = { tsConfigPath : join ( __dirname , "tsconfig.json" ) } ;
40+
41+ const appFullPath = resolve ( projectRoot , appPath ) ;
42+ const appResourcesFullPath = resolve ( projectRoot , appResourcesPath ) ;
43+
44+ const entryModule = aot ?
45+ nsWebpack . getAotEntryModule ( appFullPath ) :
46+ `${ nsWebpack . getEntryModule ( appFullPath ) } .ts` ;
47+ const entryPath = `./${ entryModule } ` ;
48+ const vendorPath = `./vendor.ts` ;
2049
2150 const config = {
22- context : resolve ( "./app" ) ,
51+ mode : "development" ,
52+ context : appFullPath ,
53+ watchOptions : {
54+ ignored : [
55+ appResourcesFullPath ,
56+ // Don't watch hidden files
57+ "**/.*" ,
58+ ]
59+ } ,
2360 target : nativescriptTarget ,
2461 entry : {
25- bundle : aot ? "./main.aot.ts" : "./main.ts" ,
26- vendor : "./vendor" ,
62+ bundle : entryPath ,
63+ vendor : vendorPath ,
2764 } ,
2865 output : {
2966 pathinfo : true ,
30- // Default destination inside platforms/<platform>/...
31- path : resolve ( nsWebpack . getAppPath ( platform ) ) ,
67+ path : dist ,
3268 libraryTarget : "commonjs2" ,
3369 filename : "[name].js" ,
70+ globalObject : "global" ,
3471 } ,
3572 resolve : {
3673 extensions : [ ".ts" , ".js" , ".scss" , ".css" ] ,
@@ -40,7 +77,7 @@ module.exports = env => {
4077 "node_modules" ,
4178 ] ,
4279 alias : {
43- '~' : resolve ( "./app" )
80+ '~' : appFullPath
4481 } ,
4582 // don't resolve symlinks to symlinked modules
4683 symlinks : false
@@ -55,6 +92,30 @@ module.exports = env => {
5592 "timers" : false ,
5693 "setImmediate" : false ,
5794 "fs" : "empty" ,
95+ "__dirname" : false ,
96+ } ,
97+ devtool : "none" ,
98+ optimization : {
99+ runtimeChunk : { name : "vendor" } ,
100+ splitChunks : {
101+ cacheGroups : {
102+ common : {
103+ name : "common" ,
104+ chunks : "all" ,
105+ test : / v e n d o r / ,
106+ enforce : true ,
107+ } ,
108+ }
109+ } ,
110+ minimize : ! ! uglify ,
111+ minimizer : [
112+ // Override default minimizer to work around an Android issue by setting compress = false
113+ new UglifyJsPlugin ( {
114+ uglifyOptions : {
115+ compress : platform !== "android"
116+ }
117+ } )
118+ ] ,
58119 } ,
59120 module : {
60121 rules : [
@@ -81,32 +142,40 @@ module.exports = env => {
81142 { test : / \. s c s s $ / , exclude : / [ \/ | \\ ] a p p \. s c s s $ / , use : [ "raw-loader" , "resolve-url-loader" , "sass-loader" ] } ,
82143
83144 // Compile TypeScript files with ahead-of-time compiler.
84- { test : / .t s $ / , use : [
85- "nativescript-dev-webpack/moduleid-compat-loader" ,
86- { loader : "@ngtools/webpack" , options : ngToolsWebpackOptions } ,
87- ] } ,
145+ {
146+ test : / .t s $ / , use : [
147+ "nativescript-dev-webpack/moduleid-compat-loader" ,
148+ { loader : "@ngtools/webpack" , options : ngToolsWebpackOptions } ,
149+ ]
150+ } ,
88151 ] ,
89152 } ,
90153 plugins : [
91- // Vendor libs go to the vendor.js chunk
92- new webpack . optimize . CommonsChunkPlugin ( {
93- name : [ "vendor" ] ,
94- } ) ,
95154 // Define useful constants like TNS_WEBPACK
96155 new webpack . DefinePlugin ( {
97156 "global.TNS_WEBPACK" : "true" ,
98157 } ) ,
158+ // Remove all files from the out dir.
159+ new CleanWebpackPlugin ( [ `${ dist } /**/*` ] ) ,
160+ // Copy native app resources to out dir.
161+ new CopyWebpackPlugin ( [
162+ {
163+ from : `${ appResourcesFullPath } /${ appResourcesPlatformDir } ` ,
164+ to : `${ dist } /App_Resources/${ appResourcesPlatformDir } ` ,
165+ context : projectRoot
166+ } ,
167+ ] ) ,
99168 // Copy assets to out dir. Add your own globs as needed.
100169 new CopyWebpackPlugin ( [
101- { from : "App_Resources/**" } ,
102170 { from : "fonts/**" } ,
103171 { from : "**/*.jpg" } ,
104172 { from : "**/*.png" } ,
105173 { from : "**/*.xml" } ,
106- ] ) ,
174+ ] , { ignore : [ ` ${ relative ( appPath , appResourcesFullPath ) } /**` ] } ) ,
107175 // Generate a bundle starter script and activate it in package.json
108176 new nsWebpack . GenerateBundleStarterPlugin ( [
109177 "./vendor" ,
178+ "./common" ,
110179 "./bundle" ,
111180 ] ) ,
112181 // Support for web workers since v3.2
@@ -119,45 +188,52 @@ module.exports = env => {
119188 platformOptions : {
120189 platform,
121190 platforms,
122- // ignore: ["App_Resources"]
123191 } ,
124192 } , ngToolsWebpackOptions )
125193 ) ,
126194 // Does IPC communication with the {N} CLI to notify events when running in watch mode.
127195 new nsWebpack . WatchStateLoggerPlugin ( ) ,
128196 ] ,
129197 } ;
198+
199+ if ( platform === "android" ) {
200+ // Add your custom Activities, Services and other android app components here.
201+ const appComponents = [
202+ "tns-core-modules/ui/frame" ,
203+ "tns-core-modules/ui/frame/activity" ,
204+ ] ;
205+
206+ // Require all Android app components
207+ // in the entry module (bundle.ts) and the vendor module (vendor.ts).
208+ config . module . rules . unshift ( {
209+ test : new RegExp ( `${ entryPath } |${ vendorPath } ` ) ,
210+ use : {
211+ loader : "nativescript-dev-webpack/android-app-components-loader" ,
212+ options : { modules : appComponents }
213+ }
214+ } ) ;
215+ }
216+
130217 if ( report ) {
131218 // Generate report files for bundles content
132219 config . plugins . push ( new BundleAnalyzerPlugin ( {
133220 analyzerMode : "static" ,
134221 openAnalyzer : false ,
135222 generateStatsFile : true ,
136- reportFilename : join ( __dirname , "report" , `report.html` ) ,
137- statsFilename : join ( __dirname , "report" , `stats.json` ) ,
223+ reportFilename : resolve ( projectRoot , "report" , `report.html` ) ,
224+ statsFilename : resolve ( projectRoot , "report" , `stats.json` ) ,
138225 } ) ) ;
139226 }
227+
140228 if ( snapshot ) {
141229 config . plugins . push ( new nsWebpack . NativeScriptSnapshotPlugin ( {
142- chunk : "vendor" ,
143- projectRoot : __dirname ,
230+ chunks : [ "vendor" , "common" ] ,
231+ projectRoot,
144232 webpackConfig : config ,
145233 targetArchs : [ "arm" , "arm64" , "ia32" ] ,
146- tnsJavaClassesOptions : { packages : [ "tns-core-modules" ] } ,
147234 useLibs : false
148235 } ) ) ;
149236 }
150- if ( uglify ) {
151- config . plugins . push ( new webpack . LoaderOptionsPlugin ( { minimize : true } ) ) ;
152-
153- // Work around an Android issue by setting compress = false
154- const compress = platform !== "android" ;
155- config . plugins . push ( new UglifyJsPlugin ( {
156- uglifyOptions : {
157- mangle : { reserved : nsWebpack . uglifyMangleExcludes } ,
158- compress,
159- }
160- } ) ) ;
161- }
237+
162238 return config ;
163239} ;
0 commit comments