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

Commit 4b84e2c

Browse files
author
Dimitar Kerezov
committed
Enable plugin to run through hooks
Instead of relying on npm scripts, rely on hooks so that certain parts of the prepare chain can be overriden. Do not skip moving `App_Resources` directory, as CLI expects it to be present in platforms and will take care of the resources inside.
1 parent d17ea4d commit 4b84e2c

9 files changed

+125
-11
lines changed

lib/after-prepare.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const snapshotGenerator = require("../snapshot/android/project-snapshot-generator");
2+
const utils = require("./utils");
3+
4+
module.exports = function ($mobileHelper, $projectData, hookArgs) {
5+
if (utils.shouldSnapshot($mobileHelper, hookArgs.platform, hookArgs.appFilesUpdaterOptions.bundle)) {
6+
snapshotGenerator.installSnapshotArtefacts($projectData.projectDir);
7+
}
8+
}

lib/before-prepareJS.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
const utils = require("./utils");
2+
const { spawn } = require("child_process");
3+
const { join } = require("path");
4+
let hasBeenInvoked = false;
5+
6+
function escapeWithQuotes(arg) {
7+
return `"${arg}"`;
8+
}
9+
10+
function spawnChildProcess(projectDir, command, ...args) {
11+
return new Promise((resolve, reject) => {
12+
const escapedArgs = args.map(escapeWithQuotes)
13+
14+
const childProcess = spawn(command, escapedArgs, {
15+
stdio: "inherit",
16+
pwd: projectDir,
17+
shell: true,
18+
});
19+
20+
childProcess.on("close", code => {
21+
if (code === 0) {
22+
resolve();
23+
} else {
24+
reject({
25+
code,
26+
message: `child process exited with code ${code}`,
27+
});
28+
}
29+
});
30+
});
31+
}
32+
33+
function throwError(error) {
34+
console.error(error.message);
35+
process.exit(error.code || 1);
36+
}
37+
38+
function prepareJSWebpack(config, $mobileHelper, $projectData, originalArgs, originalMethod) {
39+
if (config.bundle && config.release) {
40+
return new Promise(function (resolve, reject) {
41+
console.log(`Running webpack for ${config.platform}...`);
42+
const envFlagNames = Object.keys(config.env).concat([config.platform.toLowerCase()]);
43+
if (utils.shouldSnapshot($mobileHelper, config.platform, config.bundle)) {
44+
envFlagNames.push("snapshot");
45+
}
46+
47+
const args = [
48+
$projectData.projectDir,
49+
"node",
50+
"--preserve-symlinks",
51+
join($projectData.projectDir, "node_modules", "webpack", "bin", "webpack.js"),
52+
"--config=webpack.config.js",
53+
"--progress",
54+
...envFlagNames.map(item => `--env.${item}`)
55+
].filter(a => !!a);
56+
57+
// TODO: require webpack instead of spawning
58+
spawnChildProcess(...args)
59+
.then(resolve)
60+
.catch(throwError);
61+
});
62+
}
63+
}
64+
65+
module.exports = function ($mobileHelper, $projectData, hookArgs) {
66+
const env = hookArgs.config.env || {};
67+
const platform = hookArgs.config.platform;
68+
const appFilesUpdaterOptions = hookArgs.config.appFilesUpdaterOptions;
69+
const config = {
70+
env,
71+
platform,
72+
release: appFilesUpdaterOptions.release,
73+
bundle: appFilesUpdaterOptions.bundle
74+
};
75+
76+
return config.release && config.bundle && prepareJSWebpack.bind(prepareJSWebpack, config, $mobileHelper, $projectData);
77+
}

lib/utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const os = require("os");
2+
3+
function shouldSnapshot($mobileHelper, platform, bundle) {
4+
const platformSupportsSnapshot = $mobileHelper.isAndroidPlatform(platform);
5+
const osSupportsSnapshot = os.type() !== "Windows_NT";
6+
return bundle && platformSupportsSnapshot && osSupportsSnapshot;
7+
}
8+
9+
module.exports.shouldSnapshot = shouldSnapshot;

package.json

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
{
22
"name": "nativescript-dev-webpack",
3-
"version": "0.8.0",
3+
"version": "0.9.0",
44
"main": "index",
55
"description": "",
66
"homepage": "http://www.telerik.com",
77
"bugs": "http://www.telerik.com",
88
"contributors": [
99
"Hristo Deshev <[email protected]>"
1010
],
11+
"nativescript": {
12+
"hooks": [
13+
{
14+
"type": "before-prepareJSApp",
15+
"script": "lib/before-prepareJS.js",
16+
"inject": true
17+
},
18+
{
19+
"type": "after-prepare",
20+
"script": "lib/after-prepare.js",
21+
"inject": true
22+
}
23+
]
24+
},
1125
"license": "Apache-2.0",
1226
"repository": {
1327
"type": "git",
@@ -27,7 +41,8 @@
2741
},
2842
"dependencies": {
2943
"semver": "^5.4.1",
30-
"shelljs": "^0.6.0"
44+
"shelljs": "^0.6.0",
45+
"nativescript-hook": "0.2.1"
3146
},
3247
"devDependencies": {}
33-
}
48+
}

postinstall.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1+
"use strict";
2+
3+
const hook = require("nativescript-hook")(__dirname);
4+
hook.postinstall();
5+
16
const installer = require("./installer");
27
installer.install();

prepublish/common/plugins.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ module.exports = `
1919
{ from: "**/*.jpg" },
2020
{ from: "**/*.png" },
2121
{ from: "**/*.xml" },
22-
], { ignore: ["App_Resources/**"] }),
22+
]),
2323
2424
// Generate a bundle starter script and activate it in package.json
2525
new nsWebpack.GenerateBundleStarterPlugin([
2626
"./vendor",
2727
"./bundle",
2828
]),
29-
29+
3030
// Support for web workers since v3.2
3131
new NativeScriptWorkerPlugin(),
3232

templates/webpack.angular.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,14 @@ function getPlugins(platform, env) {
173173
{ from: "**/*.jpg" },
174174
{ from: "**/*.png" },
175175
{ from: "**/*.xml" },
176-
], { ignore: ["App_Resources/**"] }),
176+
]),
177177

178178
// Generate a bundle starter script and activate it in package.json
179179
new nsWebpack.GenerateBundleStarterPlugin([
180180
"./vendor",
181181
"./bundle",
182182
]),
183-
183+
184184
// Support for web workers since v3.2
185185
new NativeScriptWorkerPlugin(),
186186

templates/webpack.javascript.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,14 @@ function getPlugins(platform, env) {
157157
{ from: "**/*.jpg" },
158158
{ from: "**/*.png" },
159159
{ from: "**/*.xml" },
160-
], { ignore: ["App_Resources/**"] }),
160+
]),
161161

162162
// Generate a bundle starter script and activate it in package.json
163163
new nsWebpack.GenerateBundleStarterPlugin([
164164
"./vendor",
165165
"./bundle",
166166
]),
167-
167+
168168
// Support for web workers since v3.2
169169
new NativeScriptWorkerPlugin(),
170170

templates/webpack.typescript.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,14 @@ function getPlugins(platform, env) {
166166
{ from: "**/*.jpg" },
167167
{ from: "**/*.png" },
168168
{ from: "**/*.xml" },
169-
], { ignore: ["App_Resources/**"] }),
169+
]),
170170

171171
// Generate a bundle starter script and activate it in package.json
172172
new nsWebpack.GenerateBundleStarterPlugin([
173173
"./vendor",
174174
"./bundle",
175175
]),
176-
176+
177177
// Support for web workers since v3.2
178178
new NativeScriptWorkerPlugin(),
179179

0 commit comments

Comments
 (0)