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" ) ;
78const { BundleAnalyzerPlugin } = require ( "webpack-bundle-analyzer" ) ;
89const { NativeScriptWorkerPlugin } = require ( "nativescript-worker-loader/NativeScriptWorkerPlugin" ) ;
@@ -13,20 +14,48 @@ module.exports = env => {
1314 if ( ! platform ) {
1415 throw new Error ( "You need to provide a target platform!" ) ;
1516 }
17+
1618 const platforms = [ "ios" , "android" ] ;
17- const { snapshot, uglify, report } = env ;
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+ // 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+ snapshot,
35+ uglify,
36+ report,
37+ } = env ;
38+
39+ const appFullPath = resolve ( projectRoot , appPath ) ;
40+ const appResourcesFullPath = resolve ( projectRoot , appResourcesPath ) ;
1841
1942 const config = {
20- context : resolve ( "./app" ) ,
43+ context : appFullPath ,
44+ watchOptions : {
45+ ignored : [
46+ appResourcesFullPath ,
47+ // Don't watch hidden files
48+ "**/.*" ,
49+ ]
50+ } ,
2151 target : nativescriptTarget ,
2252 entry : {
23- bundle : `./${ nsWebpack . getEntryModule ( ) } ` ,
53+ bundle : `./${ nsWebpack . getEntryModule ( appFullPath ) } ` ,
2454 vendor : "./vendor"
2555 } ,
2656 output : {
2757 pathinfo : true ,
28- // Default destination inside platforms/<platform>/...
29- path : resolve ( nsWebpack . getAppPath ( platform ) ) ,
58+ path : dist ,
3059 libraryTarget : "commonjs2" ,
3160 filename : "[name].js" ,
3261 } ,
@@ -38,7 +67,7 @@ module.exports = env => {
3867 "node_modules" ,
3968 ] ,
4069 alias : {
41- '~' : resolve ( "./app" )
70+ '~' : appFullPath
4271 } ,
4372 // don't resolve symlinks to symlinked modules
4473 symlinks : false
@@ -81,14 +110,23 @@ module.exports = env => {
81110 new webpack . DefinePlugin ( {
82111 "global.TNS_WEBPACK" : "true" ,
83112 } ) ,
113+ // Remove all files from the out dir.
114+ new CleanWebpackPlugin ( [ `${ dist } /**/*` ] ) ,
115+ // Copy native app resources to out dir.
116+ new CopyWebpackPlugin ( [
117+ {
118+ from : `${ appResourcesFullPath } /${ appResourcesPlatformDir } ` ,
119+ to : `${ dist } /App_Resources/${ appResourcesPlatformDir } ` ,
120+ context : projectRoot
121+ } ,
122+ ] ) ,
84123 // Copy assets to out dir. Add your own globs as needed.
85124 new CopyWebpackPlugin ( [
86- { from : "App_Resources/**" } ,
87125 { from : "fonts/**" } ,
88126 { from : "**/*.jpg" } ,
89127 { from : "**/*.png" } ,
90128 { from : "**/*.xml" } ,
91- ] ) ,
129+ ] , { ignore : [ ` ${ relative ( appPath , appResourcesFullPath ) } /**` ] } ) ,
92130 // Generate a bundle starter script and activate it in package.json
93131 new nsWebpack . GenerateBundleStarterPlugin ( [
94132 "./vendor" ,
@@ -99,7 +137,6 @@ module.exports = env => {
99137 new nsWebpack . PlatformFSPlugin ( {
100138 platform,
101139 platforms,
102- // ignore: ["App_Resources"]
103140 } ) ,
104141 // Does IPC communication with the {N} CLI to notify events when running in watch mode.
105142 new nsWebpack . WatchStateLoggerPlugin ( ) ,
@@ -111,14 +148,14 @@ module.exports = env => {
111148 analyzerMode : "static" ,
112149 openAnalyzer : false ,
113150 generateStatsFile : true ,
114- reportFilename : join ( __dirname , "report" , `report.html` ) ,
115- statsFilename : join ( __dirname , "report" , `stats.json` ) ,
151+ reportFilename : resolve ( projectRoot , "report" , `report.html` ) ,
152+ statsFilename : resolve ( projectRoot , "report" , `stats.json` ) ,
116153 } ) ) ;
117154 }
118155 if ( snapshot ) {
119156 config . plugins . push ( new nsWebpack . NativeScriptSnapshotPlugin ( {
120157 chunk : "vendor" ,
121- projectRoot : __dirname ,
158+ projectRoot,
122159 webpackConfig : config ,
123160 targetArchs : [ "arm" , "arm64" , "ia32" ] ,
124161 tnsJavaClassesOptions : { packages : [ "tns-core-modules" ] } ,
@@ -132,7 +169,7 @@ module.exports = env => {
132169 const compress = platform !== "android" ;
133170 config . plugins . push ( new UglifyJsPlugin ( {
134171 uglifyOptions : {
135- mangle : { reserved : nsWebpack . uglifyMangleExcludes } ,
172+ mangle : { reserved : nsWebpack . uglifyMangleExcludes } , // Deprecated. Remove if using {N} 4+.
136173 compress,
137174 }
138175 } ) ) ;
0 commit comments