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/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; } diff --git a/lib/before-watchPatterns.js b/lib/before-watchPatterns.js index e40b2b7e..0e2703a7 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 || !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..49a32aac 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -4,7 +4,7 @@ const { join, resolve: pathResolve } = require("path"); const { existsSync } = require("fs"); const readline = require("readline"); const { messages } = require("../plugins/WatchStateLoggerPlugin"); -const { AppDirectoryLocation } = require("./constants"); +const { buildEnvData, getCompilationContext } = require("./utils"); let hasBeenInvoked = false; @@ -41,7 +41,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)) { @@ -92,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 diff --git a/lib/utils.js b/lib/utils.js index 648133c8..30ca6640 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,9 +1,33 @@ const os = require("os"); +const path = require("path"); + +const { getProjectDir, getWebpackConfig } = require("../projectHelpers"); + +function buildEnvData(platform, env) { + return { + ...env, + [platform.toLowerCase()]: true, + }; +} + +function getCompilationContext(env) { + const projectDir = getProjectDir(); + const config = getWebpackConfig(projectDir, env); + const context = config.context || projectDir; + const absolutePathToContext = path.resolve(context); + + return absolutePathToContext; +} 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; } -module.exports.shouldSnapshot = shouldSnapshot; +module.exports = { + buildEnvData, + getCompilationContext, + shouldSnapshot, +}; diff --git a/projectHelpers.js b/projectHelpers.js index fb1b69bf..366fbcff 100644 --- a/projectHelpers.js +++ b/projectHelpers.js @@ -44,6 +44,28 @@ const getAndroidRuntimeVersion = (projectDir) => { } } +const getWebpackConfig = (projectDir, env, 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(env); + } + + 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")); @@ -53,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 = (() => { @@ -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, }; 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,