Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathwebpack.config.js
More file actions
Latest commit
298 lines (270 loc) · 11.2 KB
/
Copy pathwebpack.config.js
File metadata and controls
298 lines (270 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
const{ join, relative, resolve, sep }=require("path");
constfs=require('fs');
constwebpack=require("webpack");
constnsWebpack=require("@nativescript/webpack");
constnativescriptTarget=require("@nativescript/webpack/nativescript-target");
const{ CleanWebpackPlugin }=require("clean-webpack-plugin");
constCopyWebpackPlugin=require("copy-webpack-plugin");
const{ BundleAnalyzerPlugin }=require("webpack-bundle-analyzer");
const{ NativeScriptWorkerPlugin }=require("nativescript-worker-loader/NativeScriptWorkerPlugin");
constTerserPlugin=require("terser-webpack-plugin");
consthashSalt=Date.now().toString();
module.exports=env=>{
// Add your custom Activities, Services and other android app components here.
constappComponents=env.appComponents||[];
appComponents.push(...[
"@nativescript/core/ui/frame",
"@nativescript/core/ui/frame/activity",
]);
constplatform=env&&(env.android&&"android"||env.ios&&"ios"||env.platform);
if(!platform){
thrownewError("You need to provide a target platform!");
}
constplatforms=["ios","android"];
constprojectRoot=__dirname;
if(env.platform){
platforms.push(env.platform);
}
// Default destination inside platforms/<platform>/...
constdist=resolve(projectRoot,nsWebpack.getAppPath(platform,projectRoot));
const{
// The 'appPath' and 'appResourcesPath' values are fetched from
// the nsconfig.json configuration file.
appPath ="src",
appResourcesPath ="App_Resources",
// You can provide the following flags when running 'tns run android|ios'
snapshot,// --env.snapshot
production,// --env.production
uglify,// --env.uglify
report,// --env.report
sourceMap,// --env.sourceMap
hiddenSourceMap,// --env.hiddenSourceMap
hmr,// --env.hmr,
unitTesting,// --env.unitTesting,
testing,// --env.testing
verbose,// --env.verbose
snapshotInDocker,// --env.snapshotInDocker
skipSnapshotTools,// --env.skipSnapshotTools
compileSnapshot // --env.compileSnapshot
}=env;
constuseLibs=compileSnapshot;
constisAnySourceMapEnabled=!!sourceMap||!!hiddenSourceMap;
constexternals=nsWebpack.getConvertedExternals(env.externals);
letappFullPath=resolve(projectRoot,appPath);
if(!fs.existsSync(appFullPath)){
// some apps use 'app' directory
appFullPath=resolve(projectRoot,'app');
}
consthasRootLevelScopedModules=nsWebpack.hasRootLevelScopedModules({projectDir: projectRoot});
letcoreModulesPackageName="tns-core-modules";
constalias=env.alias||{};
alias['~']=appFullPath;
if(hasRootLevelScopedModules){
coreModulesPackageName="@nativescript/core";
alias["tns-core-modules"]=coreModulesPackageName;
}
constappResourcesFullPath=resolve(projectRoot,appResourcesPath);
constcopyIgnore={ignore: [`${relative(appPath,appResourcesFullPath)}/**`]};
constentryModule=nsWebpack.getEntryModule(appFullPath,platform);
constentryPath=`.${sep}${entryModule}.js`;
constentries=env.entries||{};
entries.bundle=entryPath;
constareCoreModulesExternal=Array.isArray(env.externals)&&env.externals.some(e=>e.indexOf("@nativescript")>-1);
if(platform==="ios"&&!areCoreModulesExternal&&!testing){
entries["tns_modules/@nativescript/core/inspector_modules"]="inspector_modules";
};
letsourceMapFilename=nsWebpack.getSourceMapFilename(hiddenSourceMap,__dirname,dist);
constitemsToClean=[`${dist}/**/*`];
if(platform==="android"){
itemsToClean.push(`${join(projectRoot,"platforms","android","app","src","main","assets","snapshots")}`);
itemsToClean.push(`${join(projectRoot,"platforms","android","app","build","configurations","nativescript-android-snapshot")}`);
}
nsWebpack.processAppComponents(appComponents,platform);
constconfig={
mode: production ? "production" : "development",
context: appFullPath,
externals,
watchOptions: {
ignored: [
appResourcesFullPath,
// Don't watch hidden files
"**/.*",
]
},
target: nativescriptTarget,
entry: entries,
output: {
pathinfo: false,
path: dist,
sourceMapFilename,
libraryTarget: "commonjs2",
filename: "[name].js",
globalObject: "global",
hashSalt
},
resolve: {
extensions: [".js",".scss",".css"],
// Resolve {N} system modules from @nativescript/core
modules: [
resolve(__dirname,`node_modules/${coreModulesPackageName}`),
resolve(__dirname,"node_modules"),
`node_modules/${coreModulesPackageName}`,
"node_modules",
],
alias,
// resolve symlinks to symlinked modules
symlinks: true
},
resolveLoader: {
// don't resolve symlinks to symlinked loaders
symlinks: false
},
node: {
// Disable node shims that conflict with NativeScript
"http": false,
"timers": false,
"setImmediate": false,
"fs": "empty",
"__dirname": false,
},
devtool: hiddenSourceMap ? "hidden-source-map" : (sourceMap ? "inline-source-map" : "none"),
optimization: {
runtimeChunk: "single",
noEmitOnErrors: true,
splitChunks: {
cacheGroups: {
vendor: {
name: "vendor",
chunks: "all",
test: (module,chunks)=>{
constmoduleName=module.nameForCondition ? module.nameForCondition() : '';
return/[\\/]node_modules[\\/]/.test(moduleName)||
appComponents.some(comp=>comp===moduleName);
},
enforce: true,
},
}
},
minimize: !!uglify,
minimizer: [
newTerserPlugin({
parallel: true,
cache: true,
sourceMap: isAnySourceMapEnabled,
terserOptions: {
output: {
comments: false,
semicolons: !isAnySourceMapEnabled
},
compress: {
// The Android SBG has problems parsing the output
// when these options are enabled
'collapse_vars': platform!=="android",
sequences: platform!=="android",
}
}
})
],
},
module: {
rules: [
{
include: join(appFullPath,entryPath),
use: [
// Require all Android app components
platform==="android"&&{
loader: "@nativescript/webpack/helpers/android-app-components-loader",
options: {modules: appComponents}
},
{
loader: "@nativescript/webpack/bundle-config-loader",
options: {
loadCss: !snapshot,// load the application css if in debug mode
unitTesting,
appFullPath,
projectRoot,
ignoredFiles: nsWebpack.getUserDefinedEntries(entries,platform)
}
},
].filter(loader=>!!loader)
},
{
test: /\.(js|css|scss|html|xml)$/,
use: "@nativescript/webpack/hmr/hot-loader"
},
{test: /\.(html|xml)$/,use: "@nativescript/webpack/helpers/xml-namespace-loader"},
{
test: /\.css$/,
use: "@nativescript/webpack/helpers/css2json-loader"
},
{
test: /\.scss$/,
use: [
"@nativescript/webpack/helpers/css2json-loader",
"sass-loader"
]
},
]
},
plugins: [
// Define useful constants like TNS_WEBPACK
newwebpack.DefinePlugin({
"global.TNS_WEBPACK": "true",
"global.isAndroid": platform==='android',
"global.isIOS": platform==='ios',
"process": "global.process",
}),
// Remove all files from the out dir.
newCleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: itemsToClean,
verbose: !!verbose
}),
// Copy assets
newCopyWebpackPlugin({
patterns: [
{from: 'assets/**',noErrorOnMissing: true,globOptions: {dot: false, ...copyIgnore}},
{from: 'fonts/**',noErrorOnMissing: true,globOptions: {dot: false, ...copyIgnore}},
{from: '**/*.jpg',noErrorOnMissing: true,globOptions: {dot: false, ...copyIgnore}},
{from: '**/*.png',noErrorOnMissing: true,globOptions: {dot: false, ...copyIgnore}},
],
}),
newnsWebpack.GenerateNativeScriptEntryPointsPlugin("bundle"),
// For instructions on how to set up workers with webpack
// check out https://github.com/nativescript/worker-loader
newNativeScriptWorkerPlugin(),
newnsWebpack.PlatformFSPlugin({
platform,
platforms,
}),
// Does IPC communication with the {N} CLI to notify events when running in watch mode.
newnsWebpack.WatchStateLoggerPlugin()
],
};
if(report){
// Generate report files for bundles content
config.plugins.push(newBundleAnalyzerPlugin({
analyzerMode: "static",
openAnalyzer: false,
generateStatsFile: true,
reportFilename: resolve(projectRoot,"report",`report.html`),
statsFilename: resolve(projectRoot,"report",`stats.json`),
}));
}
if(snapshot){
config.plugins.push(newnsWebpack.NativeScriptSnapshotPlugin({
chunk: "vendor",
requireModules: [
"@nativescript/core/bundle-entry-points",
],
projectRoot,
webpackConfig: config,
snapshotInDocker,
skipSnapshotTools,
useLibs
}));
}
if(hmr){
config.plugins.push(newwebpack.HotModuleReplacementPlugin());
}
returnconfig;
};