Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit db588f3

Browse files
committed
fix(prepare): clean platforms/.../app/ when running webpack
Every time we are running a new build with webpack the platforms/.../app/ dir should be deleted as there may be old assets left. Ex. When the app bundled with snapshot enabled: ``` tns build android --bundle --env.snapshot ``` this produces some assets: ``` platforms/android/.../app/vendor.js platforms/android/.../app/_embedded_script.js // ... ``` Then, if the project is bundled without snapshot: ``` tns build android --bundle ``` the produced assets will override the ones that are already in `platforms/android/.../app`. However, since the build is without snapshot, an `_embedded_script.js` won't be generated to override the one that's left in `platforms/android/.../app` from the previous build. We'll be using `CleanWebpackPlugin` to clean the dist folder. ** Important **: Currently we're running two webpack builds when doing `tns run android|ios` - one on prepare and one when the watcher starts. This means that the dist folder will be cleaned two times. This will be resolved when NativeScript/nativescript-cli#3404 is implemented. fixes #463
1 parent 916d8f9 commit db588f3

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

Diff for: dependencyManager.js

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ function getRequiredDeps(packageJson) {
6161
"webpack": "~3.10.0",
6262
"webpack-bundle-analyzer": "^2.9.1",
6363
"webpack-sources": "~1.1.0",
64+
"clean-webpack-plugin": "~0.1.19",
6465
"copy-webpack-plugin": "~4.3.0",
6566
"raw-loader": "~0.5.1",
6667
"css-loader": "~0.28.7",

Diff for: templates/webpack.angular.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { resolve, join } = require("path");
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,6 +14,11 @@ module.exports = env => {
1314
if (!platform) {
1415
throw new Error("You need to provide a target platform!");
1516
}
17+
18+
const projectRoot = __dirname;
19+
// Default destination inside platforms/<platform>/...
20+
const dist = resolve(projectRoot, nsWebpack.getAppPath(platform));
21+
1622
const platforms = ["ios", "android"];
1723
const {
1824
// The 'appPath' and 'appResourcesDir' values are fetched from
@@ -31,7 +37,6 @@ module.exports = env => {
3137
} = env;
3238
const ngToolsWebpackOptions = { tsConfigPath: join(__dirname, "tsconfig.json") };
3339

34-
const projectRoot = __dirname;
3540
const appFullPath = resolve(projectRoot, appPath);
3641
const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
3742

@@ -53,8 +58,7 @@ module.exports = env => {
5358
},
5459
output: {
5560
pathinfo: true,
56-
// Default destination inside platforms/<platform>/...
57-
path: resolve(nsWebpack.getAppPath(platform)),
61+
path: dist,
5862
libraryTarget: "commonjs2",
5963
filename: "[name].js",
6064
},
@@ -122,6 +126,8 @@ module.exports = env => {
122126
new webpack.DefinePlugin({
123127
"global.TNS_WEBPACK": "true",
124128
}),
129+
// Remove all files from the out dir.
130+
new CleanWebpackPlugin([ `${dist}/**/*` ]),
125131
// Copy assets to out dir. Add your own globs as needed.
126132
new CopyWebpackPlugin([
127133
{ from: `${appResourcesFullPath}/**`, context: projectRoot },

Diff for: templates/webpack.javascript.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { resolve, join } = require("path");
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,6 +14,11 @@ module.exports = env => {
1314
if (!platform) {
1415
throw new Error("You need to provide a target platform!");
1516
}
17+
18+
const projectRoot = __dirname;
19+
// Default destination inside platforms/<platform>/...
20+
const dist = resolve(projectRoot, nsWebpack.getAppPath(platform));
21+
1622
const platforms = ["ios", "android"];
1723
const {
1824
// The 'appPath' and 'appResourcesPath' values are fetched from
@@ -29,7 +35,6 @@ module.exports = env => {
2935
report,
3036
} = env;
3137

32-
const projectRoot = __dirname;
3338
const appFullPath = resolve(projectRoot, appPath);
3439
const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
3540

@@ -49,8 +54,7 @@ module.exports = env => {
4954
},
5055
output: {
5156
pathinfo: true,
52-
// Default destination inside platforms/<platform>/...
53-
path: resolve(nsWebpack.getAppPath(platform)),
57+
path: dist,
5458
libraryTarget: "commonjs2",
5559
filename: "[name].js",
5660
},
@@ -105,6 +109,8 @@ module.exports = env => {
105109
new webpack.DefinePlugin({
106110
"global.TNS_WEBPACK": "true",
107111
}),
112+
// Remove all files from the out dir.
113+
new CleanWebpackPlugin([ `${dist}/**/*` ]),
108114
// Copy assets to out dir. Add your own globs as needed.
109115
new CopyWebpackPlugin([
110116
{ from: `${appResourcesFullPath}/**`, context: projectRoot },

Diff for: templates/webpack.typescript.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { resolve, join } = require("path");
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,6 +14,11 @@ module.exports = env => {
1314
if (!platform) {
1415
throw new Error("You need to provide a target platform!");
1516
}
17+
18+
const projectRoot = __dirname;
19+
// Default destination inside platforms/<platform>/...
20+
const dist = resolve(projectRoot, nsWebpack.getAppPath(platform));
21+
1622
const platforms = ["ios", "android"];
1723
const {
1824
// The 'appPath' and 'appResourcesDir' values are fetched from
@@ -29,7 +35,6 @@ module.exports = env => {
2935
report,
3036
} = env;
3137

32-
const projectRoot = __dirname;
3338
const appFullPath = resolve(projectRoot, appPath);
3439
const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
3540

@@ -49,8 +54,7 @@ module.exports = env => {
4954
},
5055
output: {
5156
pathinfo: true,
52-
// Default destination inside platforms/<platform>/...
53-
path: resolve(nsWebpack.getAppPath(platform)),
57+
path: dist,
5458
libraryTarget: "commonjs2",
5559
filename: "[name].js",
5660
},
@@ -107,6 +111,8 @@ module.exports = env => {
107111
new webpack.DefinePlugin({
108112
"global.TNS_WEBPACK": "true",
109113
}),
114+
// Remove all files from the out dir.
115+
new CleanWebpackPlugin([ `${dist}/**/*` ]),
110116
// Copy assets to out dir. Add your own globs as needed.
111117
new CopyWebpackPlugin([
112118
{ from: `${appResourcesFullPath}/**`, context: projectRoot },

0 commit comments

Comments
 (0)