From de1e9301b2dba57a380c960e04dcb5ba6f6186fe Mon Sep 17 00:00:00 2001 From: Dimitar Kerezov Date: Mon, 23 Oct 2017 09:21:42 +0300 Subject: [PATCH 1/3] 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. --- lib/after-prepare.js | 8 ++++ lib/before-prepareJS.js | 77 +++++++++++++++++++++++++++++++++ lib/utils.js | 9 ++++ package.json | 21 +++++++-- postinstall.js | 5 +++ prepublish/common/plugins.js | 4 +- templates/webpack.angular.js | 4 +- templates/webpack.javascript.js | 4 +- templates/webpack.typescript.js | 4 +- 9 files changed, 125 insertions(+), 11 deletions(-) create mode 100644 lib/after-prepare.js create mode 100644 lib/before-prepareJS.js create mode 100644 lib/utils.js diff --git a/lib/after-prepare.js b/lib/after-prepare.js new file mode 100644 index 00000000..fe2b0404 --- /dev/null +++ b/lib/after-prepare.js @@ -0,0 +1,8 @@ +const snapshotGenerator = require("../snapshot/android/project-snapshot-generator"); +const utils = require("./utils"); + +module.exports = function ($mobileHelper, $projectData, hookArgs) { + if (utils.shouldSnapshot($mobileHelper, hookArgs.platform, hookArgs.appFilesUpdaterOptions.bundle)) { + snapshotGenerator.installSnapshotArtefacts($projectData.projectDir); + } +} diff --git a/lib/before-prepareJS.js b/lib/before-prepareJS.js new file mode 100644 index 00000000..c52fd639 --- /dev/null +++ b/lib/before-prepareJS.js @@ -0,0 +1,77 @@ +const utils = require("./utils"); +const { spawn } = require("child_process"); +const { join } = require("path"); +let hasBeenInvoked = false; + +function escapeWithQuotes(arg) { + return `"${arg}"`; +} + +function spawnChildProcess(projectDir, command, ...args) { + return new Promise((resolve, reject) => { + const escapedArgs = args.map(escapeWithQuotes) + + const childProcess = spawn(command, escapedArgs, { + stdio: "inherit", + pwd: projectDir, + shell: true, + }); + + childProcess.on("close", code => { + if (code === 0) { + resolve(); + } else { + reject({ + code, + message: `child process exited with code ${code}`, + }); + } + }); + }); +} + +function throwError(error) { + console.error(error.message); + process.exit(error.code || 1); +} + +function prepareJSWebpack(config, $mobileHelper, $projectData, originalArgs, originalMethod) { + if (config.bundle && config.release) { + return new Promise(function (resolve, reject) { + console.log(`Running webpack for ${config.platform}...`); + const envFlagNames = Object.keys(config.env).concat([config.platform.toLowerCase()]); + if (utils.shouldSnapshot($mobileHelper, config.platform, config.bundle)) { + envFlagNames.push("snapshot"); + } + + const args = [ + $projectData.projectDir, + "node", + "--preserve-symlinks", + join($projectData.projectDir, "node_modules", "webpack", "bin", "webpack.js"), + "--config=webpack.config.js", + "--progress", + ...envFlagNames.map(item => `--env.${item}`) + ].filter(a => !!a); + + // TODO: require webpack instead of spawning + spawnChildProcess(...args) + .then(resolve) + .catch(throwError); + }); + } +} + +module.exports = function ($mobileHelper, $projectData, hookArgs) { + const env = hookArgs.config.env || {}; + const platform = hookArgs.config.platform; + const appFilesUpdaterOptions = hookArgs.config.appFilesUpdaterOptions; + const config = { + env, + platform, + release: appFilesUpdaterOptions.release, + bundle: appFilesUpdaterOptions.bundle + }; + + return config.release && config.bundle && prepareJSWebpack.bind(prepareJSWebpack, config, $mobileHelper, $projectData); +} diff --git a/lib/utils.js b/lib/utils.js new file mode 100644 index 00000000..6aebbdcb --- /dev/null +++ b/lib/utils.js @@ -0,0 +1,9 @@ +const os = require("os"); + +function shouldSnapshot($mobileHelper, platform, bundle) { + const platformSupportsSnapshot = $mobileHelper.isAndroidPlatform(platform); + const osSupportsSnapshot = os.type() !== "Windows_NT"; + return bundle && platformSupportsSnapshot && osSupportsSnapshot; +} + +module.exports.shouldSnapshot = shouldSnapshot; \ No newline at end of file diff --git a/package.json b/package.json index 79138140..3491b4d5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nativescript-dev-webpack", - "version": "0.8.0", + "version": "0.9.0", "main": "index", "description": "", "homepage": "http://www.telerik.com", @@ -8,6 +8,20 @@ "contributors": [ "Hristo Deshev " ], + "nativescript": { + "hooks": [ + { + "type": "before-prepareJSApp", + "script": "lib/before-prepareJS.js", + "inject": true + }, + { + "type": "after-prepare", + "script": "lib/after-prepare.js", + "inject": true + } + ] + }, "license": "Apache-2.0", "repository": { "type": "git", @@ -28,7 +42,8 @@ "dependencies": { "minimatch": "^3.0.4", "semver": "^5.4.1", - "shelljs": "^0.6.0" + "shelljs": "^0.6.0", + "nativescript-hook": "0.2.1" }, "devDependencies": {} -} +} \ No newline at end of file diff --git a/postinstall.js b/postinstall.js index 9b839ad2..11bba5d0 100644 --- a/postinstall.js +++ b/postinstall.js @@ -1,2 +1,7 @@ +"use strict"; + +const hook = require("nativescript-hook")(__dirname); +hook.postinstall(); + const installer = require("./installer"); installer.install(); diff --git a/prepublish/common/plugins.js b/prepublish/common/plugins.js index 6bd55b24..9dc694cc 100644 --- a/prepublish/common/plugins.js +++ b/prepublish/common/plugins.js @@ -19,14 +19,14 @@ module.exports = ` { from: "**/*.jpg" }, { from: "**/*.png" }, { from: "**/*.xml" }, - ], { ignore: ["App_Resources/**"] }), + ]), // Generate a bundle starter script and activate it in package.json new nsWebpack.GenerateBundleStarterPlugin([ "./vendor", "./bundle", ]), - + // Support for web workers since v3.2 new NativeScriptWorkerPlugin(), diff --git a/templates/webpack.angular.js b/templates/webpack.angular.js index 976aa8dd..3cd9829a 100644 --- a/templates/webpack.angular.js +++ b/templates/webpack.angular.js @@ -178,14 +178,14 @@ function getPlugins(platform, env) { { from: "**/*.jpg" }, { from: "**/*.png" }, { from: "**/*.xml" }, - ], { ignore: ["App_Resources/**"] }), + ]), // Generate a bundle starter script and activate it in package.json new nsWebpack.GenerateBundleStarterPlugin([ "./vendor", "./bundle", ]), - + // Support for web workers since v3.2 new NativeScriptWorkerPlugin(), diff --git a/templates/webpack.javascript.js b/templates/webpack.javascript.js index 864e25b1..45bf0bf7 100644 --- a/templates/webpack.javascript.js +++ b/templates/webpack.javascript.js @@ -162,14 +162,14 @@ function getPlugins(platform, env) { { from: "**/*.jpg" }, { from: "**/*.png" }, { from: "**/*.xml" }, - ], { ignore: ["App_Resources/**"] }), + ]), // Generate a bundle starter script and activate it in package.json new nsWebpack.GenerateBundleStarterPlugin([ "./vendor", "./bundle", ]), - + // Support for web workers since v3.2 new NativeScriptWorkerPlugin(), diff --git a/templates/webpack.typescript.js b/templates/webpack.typescript.js index c9396d31..03e77cbe 100644 --- a/templates/webpack.typescript.js +++ b/templates/webpack.typescript.js @@ -171,14 +171,14 @@ function getPlugins(platform, env) { { from: "**/*.jpg" }, { from: "**/*.png" }, { from: "**/*.xml" }, - ], { ignore: ["App_Resources/**"] }), + ]), // Generate a bundle starter script and activate it in package.json new nsWebpack.GenerateBundleStarterPlugin([ "./vendor", "./bundle", ]), - + // Support for web workers since v3.2 new NativeScriptWorkerPlugin(), From d9dc530aca02d6cd9973aa103a3bc2d19587775b Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Thu, 2 Nov 2017 20:59:58 +0200 Subject: [PATCH 2/3] chore(deps): unpin nativescript-hook version --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 3491b4d5..14ff9628 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "minimatch": "^3.0.4", "semver": "^5.4.1", "shelljs": "^0.6.0", - "nativescript-hook": "0.2.1" + "nativescript-hook": "^0.2.2" }, "devDependencies": {} -} \ No newline at end of file +} From 6150c0f080a9318df549660b3c66f2b7453df326 Mon Sep 17 00:00:00 2001 From: Martin Yankov Date: Fri, 3 Nov 2017 17:04:29 +0200 Subject: [PATCH 3/3] enable webpack builds in debug and optional snapshot --- lib/after-prepare.js | 4 +++- lib/before-prepareJS.js | 11 ++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/after-prepare.js b/lib/after-prepare.js index fe2b0404..c6f5cb59 100644 --- a/lib/after-prepare.js +++ b/lib/after-prepare.js @@ -2,7 +2,9 @@ const snapshotGenerator = require("../snapshot/android/project-snapshot-generato const utils = require("./utils"); module.exports = function ($mobileHelper, $projectData, hookArgs) { - if (utils.shouldSnapshot($mobileHelper, hookArgs.platform, hookArgs.appFilesUpdaterOptions.bundle)) { + const env = hookArgs.env || {}; + + if (env["snapshot"] && utils.shouldSnapshot($mobileHelper, hookArgs.platform, hookArgs.appFilesUpdaterOptions.bundle)) { snapshotGenerator.installSnapshotArtefacts($projectData.projectDir); } } diff --git a/lib/before-prepareJS.js b/lib/before-prepareJS.js index c52fd639..b4d5df9c 100644 --- a/lib/before-prepareJS.js +++ b/lib/before-prepareJS.js @@ -36,12 +36,14 @@ function throwError(error) { } function prepareJSWebpack(config, $mobileHelper, $projectData, originalArgs, originalMethod) { - if (config.bundle && config.release) { + if (config.bundle) { return new Promise(function (resolve, reject) { console.log(`Running webpack for ${config.platform}...`); const envFlagNames = Object.keys(config.env).concat([config.platform.toLowerCase()]); - if (utils.shouldSnapshot($mobileHelper, config.platform, config.bundle)) { - envFlagNames.push("snapshot"); + + const snapshotEnvIndex = envFlagNames.indexOf("snapshot"); + if (snapshotEnvIndex !== -1 && !utils.shouldSnapshot($mobileHelper, config.platform, config.bundle)) { + envFlagNames.splice(snapshotEnvIndex, 1); } const args = [ @@ -69,9 +71,8 @@ module.exports = function ($mobileHelper, $projectData, hookArgs) { const config = { env, platform, - release: appFilesUpdaterOptions.release, bundle: appFilesUpdaterOptions.bundle }; - return config.release && config.bundle && prepareJSWebpack.bind(prepareJSWebpack, config, $mobileHelper, $projectData); + return config.bundle && prepareJSWebpack.bind(prepareJSWebpack, config, $mobileHelper, $projectData); }