diff --git a/lib/after-prepare.js b/lib/after-prepare.js new file mode 100644 index 00000000..c6f5cb59 --- /dev/null +++ b/lib/after-prepare.js @@ -0,0 +1,10 @@ +const snapshotGenerator = require("../snapshot/android/project-snapshot-generator"); +const utils = require("./utils"); + +module.exports = function ($mobileHelper, $projectData, hookArgs) { + 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 new file mode 100644 index 00000000..b4d5df9c --- /dev/null +++ b/lib/before-prepareJS.js @@ -0,0 +1,78 @@ +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) { + return new Promise(function (resolve, reject) { + console.log(`Running webpack for ${config.platform}...`); + const envFlagNames = Object.keys(config.env).concat([config.platform.toLowerCase()]); + + const snapshotEnvIndex = envFlagNames.indexOf("snapshot"); + if (snapshotEnvIndex !== -1 && !utils.shouldSnapshot($mobileHelper, config.platform, config.bundle)) { + envFlagNames.splice(snapshotEnvIndex, 1); + } + + 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, + bundle: appFilesUpdaterOptions.bundle + }; + + return 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..14ff9628 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.2" }, "devDependencies": {} } 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(),