1
- const { resolve, join } = require ( "path" ) ;
1
+ const { relative , resolve, join } = require ( "path" ) ;
2
2
3
3
const webpack = require ( "webpack" ) ;
4
4
const nsWebpack = require ( "nativescript-dev-webpack" ) ;
5
5
const nativescriptTarget = require ( "nativescript-dev-webpack/nativescript-target" ) ;
6
+ const CleanWebpackPlugin = require ( "clean-webpack-plugin" ) ;
6
7
const CopyWebpackPlugin = require ( "copy-webpack-plugin" ) ;
7
- const ExtractTextPlugin = require ( "extract-text-webpack-plugin" ) ;
8
8
const { BundleAnalyzerPlugin } = require ( "webpack-bundle-analyzer" ) ;
9
9
const { NativeScriptWorkerPlugin } = require ( "nativescript-worker-loader/NativeScriptWorkerPlugin" ) ;
10
10
const UglifyJsPlugin = require ( "uglifyjs-webpack-plugin" ) ;
@@ -14,25 +14,52 @@ module.exports = env => {
14
14
if ( ! platform ) {
15
15
throw new Error ( "You need to provide a target platform!" ) ;
16
16
}
17
+
17
18
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" ) } ;
20
40
21
- const nativeClassExtenders = [
22
- join ( __dirname , "app/main-activity.android.ts" ) ,
23
- ] ;
41
+ const appFullPath = resolve ( projectRoot , appPath ) ;
42
+ const appResourcesFullPath = resolve ( projectRoot , appResourcesPath ) ;
24
43
25
44
const config = {
26
- context : resolve ( "./app" ) ,
45
+ context : appFullPath ,
46
+ watchOptions : {
47
+ ignored : [
48
+ appResourcesFullPath ,
49
+ // Don't watch hidden files
50
+ "**/.*" ,
51
+ ]
52
+ } ,
27
53
target : nativescriptTarget ,
28
54
entry : {
29
- bundle : aot ? "./main.aot.ts" : "./main.ts" ,
55
+ bundle : aot ?
56
+ `./${ nsWebpack . getAotEntryModule ( appFullPath ) } ` :
57
+ `./${ nsWebpack . getEntryModule ( appFullPath ) } ` ,
30
58
vendor : "./vendor" ,
31
59
} ,
32
60
output : {
33
61
pathinfo : true ,
34
- // Default destination inside platforms/<platform>/...
35
- path : resolve ( nsWebpack . getAppPath ( platform ) ) ,
62
+ path : dist ,
36
63
libraryTarget : "commonjs2" ,
37
64
filename : "[name].js" ,
38
65
} ,
@@ -44,7 +71,7 @@ module.exports = env => {
44
71
"node_modules" ,
45
72
] ,
46
73
alias : {
47
- '~' : resolve ( "./app" )
74
+ '~' : appFullPath
48
75
} ,
49
76
// don't resolve symlinks to symlinked modules
50
77
symlinks : false
@@ -65,15 +92,30 @@ module.exports = env => {
65
92
{ test : / \. h t m l $ | \. x m l $ / , use : "raw-loader" } ,
66
93
67
94
// 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
+ } ,
70
109
71
110
// 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" ] } ,
74
113
75
114
// 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
+ ] } ,
77
119
] ,
78
120
} ,
79
121
plugins : [
@@ -85,16 +127,23 @@ module.exports = env => {
85
127
new webpack . DefinePlugin ( {
86
128
"global.TNS_WEBPACK" : "true" ,
87
129
} ) ,
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
+ ] ) ,
88
140
// Copy assets to out dir. Add your own globs as needed.
89
141
new CopyWebpackPlugin ( [
90
- { from : "App_Resources/**" } ,
91
142
{ from : "fonts/**" } ,
92
143
{ from : "**/*.jpg" } ,
93
144
{ from : "**/*.png" } ,
94
145
{ from : "**/*.xml" } ,
95
- { from : "**/person-model.json" } ,
96
- { from : "**/person-metadata.json" } ,
97
- ] ) ,
146
+ ] , { ignore : [ `${ relative ( appPath , appResourcesFullPath ) } /**` ] } ) ,
98
147
// Generate a bundle starter script and activate it in package.json
99
148
new nsWebpack . GenerateBundleStarterPlugin ( [
100
149
"./vendor" ,
@@ -105,7 +154,7 @@ module.exports = env => {
105
154
// AngularCompilerPlugin with augmented NativeScript filesystem to handle platform specific resource resolution.
106
155
new nsWebpack . NativeScriptAngularCompilerPlugin (
107
156
Object . assign ( {
108
- entryModule : resolve ( __dirname , "app/ app.module#AppModule" ) ,
157
+ entryModule : resolve ( appPath , "app.module#AppModule" ) ,
109
158
skipCodeGeneration : ! aot ,
110
159
platformOptions : {
111
160
platform,
@@ -124,20 +173,17 @@ module.exports = env => {
124
173
analyzerMode : "static" ,
125
174
openAnalyzer : false ,
126
175
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` ) ,
129
178
} ) ) ;
130
179
}
131
180
if ( snapshot ) {
132
181
config . plugins . push ( new nsWebpack . NativeScriptSnapshotPlugin ( {
133
182
chunk : "vendor" ,
134
- projectRoot : __dirname ,
183
+ projectRoot,
135
184
webpackConfig : config ,
136
185
targetArchs : [ "arm" , "arm64" , "ia32" ] ,
137
- tnsJavaClassesOptions : {
138
- packages : [ "tns-core-modules" ] ,
139
- modules : nativeClassExtenders ,
140
- } ,
186
+ tnsJavaClassesOptions : { packages : [ "tns-core-modules" ] } ,
141
187
useLibs : false
142
188
} ) ) ;
143
189
}
@@ -148,17 +194,10 @@ module.exports = env => {
148
194
const compress = platform !== "android" ;
149
195
config . plugins . push ( new UglifyJsPlugin ( {
150
196
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+.
158
198
compress,
159
199
}
160
200
} ) ) ;
161
201
}
162
-
163
202
return config ;
164
203
} ;
0 commit comments