From 0feb14e51cf46469325f7658605dc1a19b18e9c4 Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Tue, 6 Mar 2018 08:56:58 +0200 Subject: [PATCH 01/12] feat: add 'getWebpackConfig' helper function --- projectHelpers.js | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/projectHelpers.js b/projectHelpers.js index fb1b69bf..bfca4642 100644 --- a/projectHelpers.js +++ b/projectHelpers.js @@ -44,6 +44,28 @@ const getAndroidRuntimeVersion = (projectDir) => { } } +const getWebpackConfig = (projectDir, configPath = "webpack.config.js") => { + const configAbsolutePath = path.resolve(projectDir, configPath); + let config; + try { + config = require(configAbsolutePath); + } catch (e) { + throw new Error( + `Couldn't load webpack config from ${configAbsolutePath}. ` + + `Original error:\n${e}` + ); + } + if (typeof config === "function") { + config = config(); + } + + if (!config) { + throw new Error(`Webpack config from ${configAbsolutePath} is empty!`); + } + + return config; +}; + const getPackageJson = projectDir => { const packageJsonPath = getPackageJsonPath(projectDir); return JSON.parse(fs.readFileSync(packageJsonPath, "utf8")); @@ -115,14 +137,15 @@ const resolveAndroidConfigurationsPath = projectDir => { const getPackageJsonPath = projectDir => path.resolve(projectDir, "package.json"); module.exports = { - isTypeScript, - isAngular, - isSass, - writePackageJson, + getAndroidProjectPath, + getAndroidRuntimeVersion, getPackageJson, getProjectDir, - getAndroidRuntimeVersion, - getAndroidProjectPath, + getWebpackConfig, + isAngular, + isSass, + isTypeScript, resolveAndroidAppPath, resolveAndroidConfigurationsPath, + writePackageJson, }; From 47bb3e6222a7b51e1d10e155f99943e564d25b29 Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Tue, 6 Mar 2018 09:09:33 +0200 Subject: [PATCH 02/12] feat: add 'getCompilationContext' helper function --- lib/utils.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 648133c8..596aa9c8 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,9 +1,23 @@ const os = require("os"); +const { getProjectDir, getWebpackConfig } = require("../projectHelpers"); + function shouldSnapshot($mobileHelper, config) { - const platformSupportsSnapshot = $mobileHelper.isAndroidPlatform(config.platform); - const osSupportsSnapshot = os.type() !== "Windows_NT"; - return config.bundle && config.release && platformSupportsSnapshot && osSupportsSnapshot; + const platformSupportsSnapshot = $mobileHelper.isAndroidPlatform(config.platform); + const osSupportsSnapshot = os.type() !== "Windows_NT"; + + return config.bundle && config.release && platformSupportsSnapshot && osSupportsSnapshot; +} + +function getCompilationContext() { + const projectDir = getProjectDir(); + const config = getWebpackConfig(projectDir); + const context = config.context || projectDir; + + return context; } -module.exports.shouldSnapshot = shouldSnapshot; +module.exports = { + shouldSnapshot, + getCompilationContext, +}; From 644eb432ddb1b7a57b51609993b4ab420ba96d2f Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Tue, 6 Mar 2018 15:09:47 +0200 Subject: [PATCH 03/12] refactor: simplify getProjectDir helper --- bin/ns-verify-bundle | 2 +- index.js | 2 +- installer.js | 2 +- projectHelpers.js | 6 +++--- verify/update.js | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/bin/ns-verify-bundle b/bin/ns-verify-bundle index f6981e83..d7de29e2 100755 --- a/bin/ns-verify-bundle +++ b/bin/ns-verify-bundle @@ -7,7 +7,7 @@ const fs = require("fs"); const { getProjectDir } = require("../projectHelpers"); -const PROJECT_DIR = getProjectDir({ nestingLvl: 2 }); +const PROJECT_DIR = getProjectDir(); const APP_ID = require(path.resolve(PROJECT_DIR, "./package.json")).nativescript.id; const APP_NAME = APP_ID.substring(APP_ID.lastIndexOf(".") + 1); const PROJECT_PATHS = { diff --git a/index.js b/index.js index 4bdf487c..119c629d 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,7 @@ const { existsSync } = require("fs"); const { getPackageJson, getProjectDir, isAngular, resolveAndroidAppPath } = require("./projectHelpers"); -const PROJECT_DIR = getProjectDir({ nestingLvl: 2 }); +const PROJECT_DIR = getProjectDir(); const APP_DIR = path.join(PROJECT_DIR, "app"); Object.assign(exports, require('./plugins')); diff --git a/installer.js b/installer.js index 7eb168ad..94b2b7ed 100644 --- a/installer.js +++ b/installer.js @@ -5,7 +5,7 @@ const helpers = require("./projectHelpers"); const projectFilesManager = require("./projectFilesManager"); const dependencyManager = require("./dependencyManager"); -const PROJECT_DIR = helpers.getProjectDir({ nestingLvl: 2 }); +const PROJECT_DIR = helpers.getProjectDir(); const APP_DIR = path.resolve(PROJECT_DIR, "app"); function install() { diff --git a/projectHelpers.js b/projectHelpers.js index bfca4642..366fbcff 100644 --- a/projectHelpers.js +++ b/projectHelpers.js @@ -44,7 +44,7 @@ const getAndroidRuntimeVersion = (projectDir) => { } } -const getWebpackConfig = (projectDir, configPath = "webpack.config.js") => { +const getWebpackConfig = (projectDir, env, configPath = "webpack.config.js") => { const configAbsolutePath = path.resolve(projectDir, configPath); let config; try { @@ -56,7 +56,7 @@ const getWebpackConfig = (projectDir, configPath = "webpack.config.js") => { ); } if (typeof config === "function") { - config = config(); + config = config(env); } if (!config) { @@ -75,7 +75,7 @@ const writePackageJson = (content, projectDir) => { const packageJsonPath = getPackageJsonPath(projectDir); fs.writeFileSync(packageJsonPath, JSON.stringify(content, null, 2)) } -const getProjectDir = ({ nestingLvl } = { nestingLvl: 0 }) => { +const getProjectDir = ({ nestingLvl } = { nestingLvl: 2 }) => { // INIT_CWD is available since npm 5.4 const initCwd = process.env.INIT_CWD; const shouldUseInitCwd = (() => { diff --git a/verify/update.js b/verify/update.js index e928fe9e..a5eac1b7 100644 --- a/verify/update.js +++ b/verify/update.js @@ -6,7 +6,7 @@ const { forceUpdateProjectFiles } = require("../projectFilesManager"); const { forceUpdateProjectDeps } = require("../dependencyManager"); const PLUGIN_NAME = "nativescript-dev-webpack"; -const PROJECT_DIR = getProjectDir({ nestingLvl: 2 }); +const PROJECT_DIR = getProjectDir(); function update({ deps: shouldUpdateDeps, From 223c2b3286e1a1641f0a67dff3a08528f74ce8e3 Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Tue, 6 Mar 2018 15:10:33 +0200 Subject: [PATCH 04/12] style: indent with spaces --- lib/before-prepareJS.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/before-prepareJS.js b/lib/before-prepareJS.js index a8a0eca1..bb2ea12c 100644 --- a/lib/before-prepareJS.js +++ b/lib/before-prepareJS.js @@ -1,15 +1,15 @@ const { runWebpackCompiler } = require("./compiler"); module.exports = function ($mobileHelper, $projectData, $logger, hookArgs) { - const env = hookArgs.config.env || {}; - const platform = hookArgs.config.platform; - const appFilesUpdaterOptions = hookArgs.config.appFilesUpdaterOptions; - const config = { - env, - platform, - bundle: appFilesUpdaterOptions.bundle, - release: appFilesUpdaterOptions.release, - }; - const result = config.bundle && runWebpackCompiler.bind(runWebpackCompiler, config, $mobileHelper, $projectData, $logger, hookArgs); - return result; + const env = hookArgs.config.env || {}; + const platform = hookArgs.config.platform; + const appFilesUpdaterOptions = hookArgs.config.appFilesUpdaterOptions; + const config = { + env, + platform, + bundle: appFilesUpdaterOptions.bundle, + release: appFilesUpdaterOptions.release, + }; + const result = config.bundle && runWebpackCompiler.bind(runWebpackCompiler, config, $mobileHelper, $projectData, $logger, hookArgs); + return result; } From e2cc38a0fc088b528d3d230b12bccc8fa93d7bbc Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Tue, 6 Mar 2018 15:11:17 +0200 Subject: [PATCH 05/12] refactor: ignore compilation contexts from CLI watcher --- lib/before-watchPatterns.js | 41 ++++++++++++++++++++++++++----------- lib/compiler.js | 5 ++++- lib/utils.js | 24 ++++++++++++++-------- 3 files changed, 49 insertions(+), 21 deletions(-) diff --git a/lib/before-watchPatterns.js b/lib/before-watchPatterns.js index e40b2b7e..77f92923 100644 --- a/lib/before-watchPatterns.js +++ b/lib/before-watchPatterns.js @@ -1,16 +1,33 @@ -const { AppDirectoryLocation } = require("./constants"); +const { basename } = require("path"); +const { buildEnvData, getCompilationContext } = require("./utils"); module.exports = function (hookArgs) { - if (hookArgs.liveSyncData && hookArgs.liveSyncData.bundle) { - return (args, originalMethod) => { - return originalMethod(...args).then(originalPatterns => { - const appDirectoryLocationIndex = originalPatterns.indexOf(AppDirectoryLocation); - if (appDirectoryLocationIndex !== -1) { - originalPatterns.splice(appDirectoryLocationIndex, 1); - } + const { liveSyncData } = hookArgs; + if (!liveSyncData || !liveSyncData.bundle) { + return; + } - return originalPatterns; - }); - }; - } + const { platforms } = hookArgs; + const { env } = liveSyncData; + return (args, originalMethod) => { + return originalMethod(...args).then(originalPatterns => { + if (!platforms.length) { + throw new Error("Target platform should be specified!"); + } + + const compilationContexts = platforms.map(platform => + getContext(platform, env)); + + const ignorePatterns = compilationContexts.map( + context => `!${context}` + ); + + return [...originalPatterns, ...ignorePatterns]; + }); + }; +} + +function getContext(platform, env) { + const fullEnvData = buildEnvData(platform, env); + return getCompilationContext(fullEnvData); } diff --git a/lib/compiler.js b/lib/compiler.js index 8dbf022d..be484e76 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -4,6 +4,7 @@ const { join, resolve: pathResolve } = require("path"); const { existsSync } = require("fs"); const readline = require("readline"); const { messages } = require("../plugins/WatchStateLoggerPlugin"); +const { buildEnvData } = require("./utils"); const { AppDirectoryLocation } = require("./constants"); let hasBeenInvoked = false; @@ -41,7 +42,9 @@ exports.runWebpackCompiler = function runWebpackCompiler(config, $mobileHelper, } console.log(`Running webpack for ${config.platform}...`); - const envFlagNames = Object.keys(config.env).concat([config.platform.toLowerCase()]); + + const envData = buildEnvData(config.platform, config.env); + const envFlagNames = Object.keys(envData); const snapshotEnvIndex = envFlagNames.indexOf("snapshot"); if (snapshotEnvIndex !== -1 && !utils.shouldSnapshot($mobileHelper, config)) { diff --git a/lib/utils.js b/lib/utils.js index 596aa9c8..cb591402 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -2,22 +2,30 @@ const os = require("os"); const { getProjectDir, getWebpackConfig } = require("../projectHelpers"); -function shouldSnapshot($mobileHelper, config) { - const platformSupportsSnapshot = $mobileHelper.isAndroidPlatform(config.platform); - const osSupportsSnapshot = os.type() !== "Windows_NT"; - - return config.bundle && config.release && platformSupportsSnapshot && osSupportsSnapshot; +function buildEnvData(platform, env) { + return { + ...env, + [platform.toLowerCase()]: true, + }; } -function getCompilationContext() { +function getCompilationContext(env) { const projectDir = getProjectDir(); - const config = getWebpackConfig(projectDir); + const config = getWebpackConfig(projectDir, env); const context = config.context || projectDir; return context; } +function shouldSnapshot($mobileHelper, config) { + const platformSupportsSnapshot = $mobileHelper.isAndroidPlatform(config.platform); + const osSupportsSnapshot = os.type() !== "Windows_NT"; + + return config.bundle && config.release && platformSupportsSnapshot && osSupportsSnapshot; +} + module.exports = { - shouldSnapshot, + buildEnvData, getCompilationContext, + shouldSnapshot, }; From 6b9b0effa85a73ec9403fddba7cc8c7858c578f0 Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Tue, 6 Mar 2018 15:19:44 +0200 Subject: [PATCH 06/12] refactor: get absolute path for context --- lib/utils.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/utils.js b/lib/utils.js index cb591402..30ca6640 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,4 +1,5 @@ const os = require("os"); +const path = require("path"); const { getProjectDir, getWebpackConfig } = require("../projectHelpers"); @@ -13,8 +14,9 @@ function getCompilationContext(env) { const projectDir = getProjectDir(); const config = getWebpackConfig(projectDir, env); const context = config.context || projectDir; + const absolutePathToContext = path.resolve(context); - return context; + return absolutePathToContext; } function shouldSnapshot($mobileHelper, config) { From aae5baa873da13b5e01f8823206e6198c20b2f20 Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Tue, 6 Mar 2018 15:38:43 +0200 Subject: [PATCH 07/12] refactor: use webpack context when notifying CLI for changed files --- lib/compiler.js | 10 +++++++--- lib/constants.js | 3 --- 2 files changed, 7 insertions(+), 6 deletions(-) delete mode 100644 lib/constants.js diff --git a/lib/compiler.js b/lib/compiler.js index be484e76..49a32aac 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -4,8 +4,7 @@ const { join, resolve: pathResolve } = require("path"); const { existsSync } = require("fs"); const readline = require("readline"); const { messages } = require("../plugins/WatchStateLoggerPlugin"); -const { buildEnvData } = require("./utils"); -const { AppDirectoryLocation } = require("./constants"); +const { buildEnvData, getCompilationContext } = require("./utils"); let hasBeenInvoked = false; @@ -95,7 +94,12 @@ exports.runWebpackCompiler = function runWebpackCompiler(config, $mobileHelper, } if (hookArgs.filesToSync && hookArgs.startSyncFilesTimeout) { - hookArgs.filesToSync.push(...message.emittedFiles.map(emittedFile => join($projectData.projectDir, AppDirectoryLocation, emittedFile))); + const compilationContext = getCompilationContext(envData); + hookArgs.filesToSync.push( + ...message.emittedFiles.map( + emittedFile => join(compilationContext, emittedFile) + ) + ); hookArgs.startSyncFilesTimeout(); } } diff --git a/lib/constants.js b/lib/constants.js deleted file mode 100644 index 3efcf965..00000000 --- a/lib/constants.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = { - AppDirectoryLocation: "app" -}; \ No newline at end of file From e6bffcf6608dd48e4006fb5d2082bf37136c4456 Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Tue, 6 Mar 2018 16:04:46 +0200 Subject: [PATCH 08/12] refactor: throw if no 'platforms' --- lib/before-watchPatterns.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/before-watchPatterns.js b/lib/before-watchPatterns.js index 77f92923..0e2703a7 100644 --- a/lib/before-watchPatterns.js +++ b/lib/before-watchPatterns.js @@ -11,7 +11,7 @@ module.exports = function (hookArgs) { const { env } = liveSyncData; return (args, originalMethod) => { return originalMethod(...args).then(originalPatterns => { - if (!platforms.length) { + if (!platforms || !platforms.length) { throw new Error("Target platform should be specified!"); } From 11aed4d7db639e71ab3e7a272967c16b9996bd16 Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Tue, 6 Mar 2018 18:22:01 +0200 Subject: [PATCH 09/12] fix: resolve context and ignored files relative to __dirname --- templates/webpack.angular.js | 4 ++-- templates/webpack.javascript.js | 4 ++-- templates/webpack.typescript.js | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/templates/webpack.angular.js b/templates/webpack.angular.js index 2352afb2..11bb5c8b 100644 --- a/templates/webpack.angular.js +++ b/templates/webpack.angular.js @@ -19,10 +19,10 @@ module.exports = env => { const ngToolsWebpackOptions = { tsConfigPath: "tsconfig.json" }; const config = { - context: resolve("./app"), + context: resolve(__dirname, "app"), watchOptions: { ignored: [ - resolve("./app/App_Resources"), + resolve(__dirname, "./app/App_Resources"), // Don't watch hidden files "**/.*", ] diff --git a/templates/webpack.javascript.js b/templates/webpack.javascript.js index 5635bb99..6307e4f3 100644 --- a/templates/webpack.javascript.js +++ b/templates/webpack.javascript.js @@ -17,10 +17,10 @@ module.exports = env => { const { snapshot, uglify, report } = env; const config = { - context: resolve("./app"), + context: resolve(__dirname, "app"), watchOptions: { ignored: [ - resolve("./app/App_Resources"), + resolve(__dirname, "./app/App_Resources"), // Don't watch hidden files "**/.*", ] diff --git a/templates/webpack.typescript.js b/templates/webpack.typescript.js index d4728967..d1538ea3 100644 --- a/templates/webpack.typescript.js +++ b/templates/webpack.typescript.js @@ -17,10 +17,10 @@ module.exports = env => { const { snapshot, uglify, report } = env; const config = { - context: resolve("./app"), + context: resolve(__dirname, "app"), watchOptions: { ignored: [ - resolve("./app/App_Resources"), + resolve(__dirname, "./app/App_Resources"), // Don't watch hidden files "**/.*", ] From 875fd3e6ff7b663c0bff2fadd165d253d6e73b3c Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Tue, 6 Mar 2018 18:43:47 +0200 Subject: [PATCH 10/12] fix: use relative path for context --- lib/compiler.js | 2 +- lib/utils.js | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/compiler.js b/lib/compiler.js index 49a32aac..5a5b571d 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -97,7 +97,7 @@ exports.runWebpackCompiler = function runWebpackCompiler(config, $mobileHelper, const compilationContext = getCompilationContext(envData); hookArgs.filesToSync.push( ...message.emittedFiles.map( - emittedFile => join(compilationContext, emittedFile) + emittedFile => join($projectData.projectDir, compilationContext, emittedFile) ) ); hookArgs.startSyncFilesTimeout(); diff --git a/lib/utils.js b/lib/utils.js index 30ca6640..33468526 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -13,10 +13,11 @@ function buildEnvData(platform, env) { function getCompilationContext(env) { const projectDir = getProjectDir(); const config = getWebpackConfig(projectDir, env); - const context = config.context || projectDir; - const absolutePathToContext = path.resolve(context); + const { context } = config; - return absolutePathToContext; + return context ? + path.relative(projectDir, context) : + "."; } function shouldSnapshot($mobileHelper, config) { From 2d61a70bb077eea7eef6f10d60f09b964405bcec Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Tue, 6 Mar 2018 19:01:22 +0200 Subject: [PATCH 11/12] style: replace destructuring with Object assign --- lib/utils.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 33468526..3a4329d3 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -4,10 +4,10 @@ const path = require("path"); const { getProjectDir, getWebpackConfig } = require("../projectHelpers"); function buildEnvData(platform, env) { - return { - ...env, - [platform.toLowerCase()]: true, - }; + return Object.assign({}, + env, + { [platform.toLowerCase()]: true } + ); } function getCompilationContext(env) { From 04a31cc05be7eb1ad9ce06e9310726635eb48d60 Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Tue, 6 Mar 2018 19:06:00 +0200 Subject: [PATCH 12/12] style: replace \n with os.EOL --- projectHelpers.js | 3 ++- snapshot/android/utils.js | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/projectHelpers.js b/projectHelpers.js index 366fbcff..cabeb51c 100644 --- a/projectHelpers.js +++ b/projectHelpers.js @@ -1,6 +1,7 @@ const path = require("path"); const fs = require("fs"); const semver = require("semver"); +const { EOL } = require("os"); const isTypeScript = ({ projectDir, packageJson } = {}) => { packageJson = packageJson || getPackageJson(projectDir); @@ -52,7 +53,7 @@ const getWebpackConfig = (projectDir, env, configPath = "webpack.config.js") => } catch (e) { throw new Error( `Couldn't load webpack config from ${configAbsolutePath}. ` + - `Original error:\n${e}` + `Original error:${EOL}${e}` ); } if (typeof config === "function") { diff --git a/snapshot/android/utils.js b/snapshot/android/utils.js index 7cc5676a..bf59ddd2 100644 --- a/snapshot/android/utils.js +++ b/snapshot/android/utils.js @@ -1,5 +1,5 @@ const { chmodSync, createWriteStream, existsSync } = require("fs"); -const { tmpdir } = require("os"); +const { tmpdir, EOL } = require("os"); const { dirname, join } = require("path"); const { mkdir } = require("shelljs"); @@ -36,14 +36,14 @@ const getJsonFile = url => } if (!response || response.statusCode !== 200) { - return reject(`Couldn't fetch ${url}! Response:\n${response}`); + return reject(`Couldn't fetch ${url}! Response:${EOL}${response}`); } try { const data = JSON.parse(body); resolve(data); } catch (error) { - reject(`Couldn't parse json data! Original error:\n${error}`); + reject(`Couldn't parse json data! Original error:${EOL}${error}`); } }) ).catch(reject); @@ -58,7 +58,7 @@ const getRequestOptions = (url) => resolve(allOptions); }) .catch(error => - reject(`Couldn't get proxy settings! Original error:\n${error}`)); + reject(`Couldn't get proxy settings! Original error:${EOL}${error}`)); }); module.exports = {