Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit 9a57a53

Browse files
feat: enable plugin to run through {N} CLI hooks (#299)
* 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. * chore(deps): unpin nativescript-hook version * enable webpack builds in debug and optional snapshot
1 parent b4800c8 commit 9a57a53

File tree

9 files changed

+127
-10
lines changed

9 files changed

+127
-10
lines changed

‎lib/after-prepare.js‎

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

‎lib/before-prepareJS.js‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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) {
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+
44+
const snapshotEnvIndex = envFlagNames.indexOf("snapshot");
45+
if (snapshotEnvIndex !== -1 && !utils.shouldSnapshot($mobileHelper, config.platform, config.bundle)) {
46+
envFlagNames.splice(snapshotEnvIndex, 1);
47+
}
48+
49+
const args = [
50+
$projectData.projectDir,
51+
"node",
52+
"--preserve-symlinks",
53+
join($projectData.projectDir, "node_modules", "webpack", "bin", "webpack.js"),
54+
"--config=webpack.config.js",
55+
"--progress",
56+
...envFlagNames.map(item => `--env.${item}`)
57+
].filter(a => !!a);
58+
59+
// TODO: require webpack instead of spawning
60+
spawnChildProcess(...args)
61+
.then(resolve)
62+
.catch(throwError);
63+
});
64+
}
65+
}
66+
67+
module.exports = function ($mobileHelper, $projectData, hookArgs) {
68+
const env = hookArgs.config.env || {};
69+
const platform = hookArgs.config.platform;
70+
const appFilesUpdaterOptions = hookArgs.config.appFilesUpdaterOptions;
71+
const config = {
72+
env,
73+
platform,
74+
bundle: appFilesUpdaterOptions.bundle
75+
};
76+
77+
return config.bundle && prepareJSWebpack.bind(prepareJSWebpack, config, $mobileHelper, $projectData);
78+
}

‎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: 17 additions & 2 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 <hristo.deshev@telerik.com>"
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.2"
3247
},
3348
"devDependencies": {}
3449
}

‎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
@@ -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

‎templates/webpack.javascript.js‎

Lines changed: 2 additions & 2 deletions
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

‎templates/webpack.typescript.js‎

Lines changed: 2 additions & 2 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /