Skip to content

Commit 5516dcf

Browse files
committed
Update webpack
1 parent 5e9d1e9 commit 5516dcf

File tree

2 files changed

+55
-17
lines changed

2 files changed

+55
-17
lines changed

demo/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@
2424
"karma-nativescript-launcher": "^0.4.0",
2525
"lazy": "1.0.11",
2626
"nativescript-css-loader": "~0.26.1",
27-
"nativescript-dev-webpack": "^0.9.0",
27+
"nativescript-dev-webpack": "~0.10.0",
2828
"nativescript-worker-loader": "~0.8.1",
2929
"raw-loader": "~0.5.1",
3030
"resolve-url-loader": "~2.1.0",
3131
"webpack": "~3.8.1",
3232
"webpack-bundle-analyzer": "^2.8.2",
3333
"webpack-sources": "~1.0.1",
34-
"uglifyjs-webpack-plugin": "~1.1.6"
34+
"uglifyjs-webpack-plugin": "~1.1.6",
35+
"clean-webpack-plugin": "~0.1.19"
3536
},
3637
"nativescript": {
3738
"id": "org.nativescript.demo.javascript",
@@ -51,4 +52,4 @@
5152
"publish-ios-bundle": "npm run ns-bundle --ios --publish-app",
5253
"generate-android-snapshot": "generate-android-snapshot --targetArchs arm,arm64,ia32 --install"
5354
}
54-
}
55+
}

demo/webpack.config.js

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
const { resolve, join } = require("path");
1+
const { relative, resolve, join } = require("path");
22

33
const webpack = require("webpack");
44
const nsWebpack = require("nativescript-dev-webpack");
55
const nativescriptTarget = require("nativescript-dev-webpack/nativescript-target");
6+
const CleanWebpackPlugin = require("clean-webpack-plugin");
67
const CopyWebpackPlugin = require("copy-webpack-plugin");
78
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
89
const { 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

Comments
 (0)