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,25 +14,52 @@ 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" ) } ;
2040
21- const nativeClassExtenders = [
22- join ( __dirname , "app/main-activity.android.ts" ) ,
23- ] ;
41+ const appFullPath = resolve ( projectRoot , appPath ) ;
42+ const appResourcesFullPath = resolve ( projectRoot , appResourcesPath ) ;
2443
2544 const config = {
26- context : resolve ( "./app" ) ,
45+ context : appFullPath ,
46+ watchOptions : {
47+ ignored : [
48+ appResourcesFullPath ,
49+ // Don't watch hidden files
50+ "**/.*" ,
51+ ]
52+ } ,
2753 target : nativescriptTarget ,
2854 entry : {
29- bundle : aot ? "./main.aot.ts" : "./main.ts" ,
55+ bundle : aot ?
56+ `./${ nsWebpack . getAotEntryModule ( appFullPath ) } ` :
57+ `./${ nsWebpack . getEntryModule ( appFullPath ) } ` ,
3058 vendor : "./vendor" ,
3159 } ,
3260 output : {
3361 pathinfo : true ,
34- // Default destination inside platforms/<platform>/...
35- path : resolve ( nsWebpack . getAppPath ( platform ) ) ,
62+ path : dist ,
3663 libraryTarget : "commonjs2" ,
3764 filename : "[name].js" ,
3865 } ,
@@ -44,7 +71,7 @@ module.exports = env => {
4471 "node_modules" ,
4572 ] ,
4673 alias : {
47- '~' : resolve ( "./app" )
74+ '~' : appFullPath
4875 } ,
4976 // don't resolve symlinks to symlinked modules
5077 symlinks : false
@@ -65,15 +92,30 @@ module.exports = env => {
6592 { test : / \. h t m l $ | \. x m l $ / , use : "raw-loader" } ,
6693
6794 // tns-core-modules reads the app.css and its imports using css-loader
68- { test : / \/ a p p \. c s s $ / , use : "css-loader?url=false" } ,
69- { test : / \/ a p p \. s c s s $ / , use : [ "css-loader?url=false" , "sass-loader" ] } ,
95+ {
96+ test : / [ \/ | \\ ] a p p \. c s s $ / ,
97+ use : {
98+ loader : "css-loader" ,
99+ options : { minimize : false , url : false } ,
100+ }
101+ } ,
102+ {
103+ test : / [ \/ | \\ ] a p p \. s c s s $ / ,
104+ use : [
105+ { loader : "css-loader" , options : { minimize : false , url : false } } ,
106+ "sass-loader"
107+ ]
108+ } ,
70109
71110 // Angular components reference css files and their imports using raw-loader
72- { test : / \. c s s $ / , exclude : / \/ a p p \. c s s $ / , use : "raw-loader" } ,
73- { test : / \. s c s s $ / , exclude : / \/ a p p \. s c s s $ / , use : [ "raw-loader" , "resolve-url-loader" , "sass-loader" ] } ,
111+ { test : / \. c s s $ / , exclude : / [ \/ | \\ ] a p p \. c s s $ / , use : "raw-loader" } ,
112+ { test : / \. s c s s $ / , exclude : / [ \/ | \\ ] a p p \. s c s s $ / , use : [ "raw-loader" , "resolve-url-loader" , "sass-loader" ] } ,
74113
75114 // Compile TypeScript files with ahead-of-time compiler.
76- { test : / .t s $ / , use : [ "nativescript-dev-webpack/moduleid-compat-loader" , "@ngtools/webpack" ] } ,
115+ { test : / .t s $ / , use : [
116+ "nativescript-dev-webpack/moduleid-compat-loader" ,
117+ { loader : "@ngtools/webpack" , options : ngToolsWebpackOptions } ,
118+ ] } ,
77119 ] ,
78120 } ,
79121 plugins : [
@@ -85,16 +127,23 @@ module.exports = env => {
85127 new webpack . DefinePlugin ( {
86128 "global.TNS_WEBPACK" : "true" ,
87129 } ) ,
130+ // Remove all files from the out dir.
131+ new CleanWebpackPlugin ( [ `${ dist } /**/*` ] ) ,
132+ // Copy native app resources to out dir.
133+ new CopyWebpackPlugin ( [
134+ {
135+ from : `${ appResourcesFullPath } /${ appResourcesPlatformDir } ` ,
136+ to : `${ dist } /App_Resources/${ appResourcesPlatformDir } ` ,
137+ context : projectRoot
138+ } ,
139+ ] ) ,
88140 // Copy assets to out dir. Add your own globs as needed.
89141 new CopyWebpackPlugin ( [
90- { from : "App_Resources/**" } ,
91142 { from : "fonts/**" } ,
92143 { from : "**/*.jpg" } ,
93144 { from : "**/*.png" } ,
94145 { from : "**/*.xml" } ,
95- { from : "**/person-model.json" } ,
96- { from : "**/person-metadata.json" } ,
97- ] ) ,
146+ ] , { ignore : [ `${ relative ( appPath , appResourcesFullPath ) } /**` ] } ) ,
98147 // Generate a bundle starter script and activate it in package.json
99148 new nsWebpack . GenerateBundleStarterPlugin ( [
100149 "./vendor" ,
@@ -105,7 +154,7 @@ module.exports = env => {
105154 // AngularCompilerPlugin with augmented NativeScript filesystem to handle platform specific resource resolution.
106155 new nsWebpack . NativeScriptAngularCompilerPlugin (
107156 Object . assign ( {
108- entryModule : resolve ( __dirname , "app/ app.module#AppModule" ) ,
157+ entryModule : resolve ( appPath , "app.module#AppModule" ) ,
109158 skipCodeGeneration : ! aot ,
110159 platformOptions : {
111160 platform,
@@ -124,20 +173,17 @@ module.exports = env => {
124173 analyzerMode : "static" ,
125174 openAnalyzer : false ,
126175 generateStatsFile : true ,
127- reportFilename : join ( __dirname , "report" , `report.html` ) ,
128- statsFilename : join ( __dirname , "report" , `stats.json` ) ,
176+ reportFilename : resolve ( projectRoot , "report" , `report.html` ) ,
177+ statsFilename : resolve ( projectRoot , "report" , `stats.json` ) ,
129178 } ) ) ;
130179 }
131180 if ( snapshot ) {
132181 config . plugins . push ( new nsWebpack . NativeScriptSnapshotPlugin ( {
133182 chunk : "vendor" ,
134- projectRoot : __dirname ,
183+ projectRoot,
135184 webpackConfig : config ,
136185 targetArchs : [ "arm" , "arm64" , "ia32" ] ,
137- tnsJavaClassesOptions : {
138- packages : [ "tns-core-modules" ] ,
139- modules : nativeClassExtenders ,
140- } ,
186+ tnsJavaClassesOptions : { packages : [ "tns-core-modules" ] } ,
141187 useLibs : false
142188 } ) ) ;
143189 }
@@ -148,17 +194,10 @@ module.exports = env => {
148194 const compress = platform !== "android" ;
149195 config . plugins . push ( new UglifyJsPlugin ( {
150196 uglifyOptions : {
151- mangle : {
152- reserved : [
153- ...nsWebpack . uglifyMangleExcludes ,
154- "org.nativescript.sdkAngular.MainActivity" ,
155- "AutoCompleteAdapter" ,
156- ] ,
157- } ,
197+ mangle : { reserved : nsWebpack . uglifyMangleExcludes } , // Deprecated. Remove if using {N} 4+.
158198 compress,
159199 }
160200 } ) ) ;
161201 }
162-
163202 return config ;
164203} ;
0 commit comments