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

Commit 9d73021

Browse files
Dimitar KerezovPanayotCankov
Dimitar Kerezov
authored andcommitted
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 ea29bb6 commit 9d73021

9 files changed

+125
-11
lines changed

Diff for: lib/after-prepare.js

+8
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+
}

Diff for: lib/before-prepareJS.js

+77
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+
}

Diff for: lib/utils.js

+9
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;

Diff for: package.json

+18-3
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",
@@ -28,7 +42,8 @@
2842
"dependencies": {
2943
"minimatch": "^3.0.4",
3044
"semver": "^5.4.1",
31-
"shelljs": "^0.6.0"
45+
"shelljs": "^0.6.0",
46+
"nativescript-hook": "0.2.1"
3247
},
3348
"devDependencies": {}
34-
}
49+
}

Diff for: postinstall.js

+5
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();

Diff for: prepublish/common/plugins.js

+2-2
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

Diff for: templates/webpack.angular.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,14 @@ function getPlugins(platform, env) {
178178
{ from: "**/*.jpg" },
179179
{ from: "**/*.png" },
180180
{ from: "**/*.xml" },
181-
], { ignore: ["App_Resources/**"] }),
181+
]),
182182

183183
// Generate a bundle starter script and activate it in package.json
184184
new nsWebpack.GenerateBundleStarterPlugin([
185185
"./vendor",
186186
"./bundle",
187187
]),
188-
188+
189189
// Support for web workers since v3.2
190190
new NativeScriptWorkerPlugin(),
191191

Diff for: templates/webpack.javascript.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,14 @@ function getPlugins(platform, env) {
162162
{ from: "**/*.jpg" },
163163
{ from: "**/*.png" },
164164
{ from: "**/*.xml" },
165-
], { ignore: ["App_Resources/**"] }),
165+
]),
166166

167167
// Generate a bundle starter script and activate it in package.json
168168
new nsWebpack.GenerateBundleStarterPlugin([
169169
"./vendor",
170170
"./bundle",
171171
]),
172-
172+
173173
// Support for web workers since v3.2
174174
new NativeScriptWorkerPlugin(),
175175

Diff for: templates/webpack.typescript.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,14 @@ function getPlugins(platform, env) {
171171
{ from: "**/*.jpg" },
172172
{ from: "**/*.png" },
173173
{ from: "**/*.xml" },
174-
], { ignore: ["App_Resources/**"] }),
174+
]),
175175

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

0 commit comments

Comments
 (0)