Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit 9342eef

Browse files
authored
refactor: use webpack context when notifying CLI for changed files (#455)
1 parent f55cfb4 commit 9342eef

14 files changed

+124
-54
lines changed

bin/ns-verify-bundle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const fs = require("fs");
77

88
const { getProjectDir } = require("../projectHelpers");
99

10-
const PROJECT_DIR = getProjectDir({ nestingLvl: 2 });
10+
const PROJECT_DIR = getProjectDir();
1111
const APP_ID = require(path.resolve(PROJECT_DIR, "./package.json")).nativescript.id;
1212
const APP_NAME = APP_ID.substring(APP_ID.lastIndexOf(".") + 1);
1313
const PROJECT_PATHS = {

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { existsSync } = require("fs");
33

44
const { getPackageJson, getProjectDir, isAngular, resolveAndroidAppPath } = require("./projectHelpers");
55

6-
const PROJECT_DIR = getProjectDir({ nestingLvl: 2 });
6+
const PROJECT_DIR = getProjectDir();
77
const APP_DIR = path.join(PROJECT_DIR, "app");
88

99
Object.assign(exports, require('./plugins'));

installer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const helpers = require("./projectHelpers");
55
const projectFilesManager = require("./projectFilesManager");
66
const dependencyManager = require("./dependencyManager");
77

8-
const PROJECT_DIR = helpers.getProjectDir({ nestingLvl: 2 });
8+
const PROJECT_DIR = helpers.getProjectDir();
99
const APP_DIR = path.resolve(PROJECT_DIR, "app");
1010

1111
function install() {

lib/before-prepareJS.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
const { runWebpackCompiler } = require("./compiler");
22

33
module.exports = function ($mobileHelper, $projectData, $logger, hookArgs) {
4-
const env = hookArgs.config.env || {};
5-
const platform = hookArgs.config.platform;
6-
const appFilesUpdaterOptions = hookArgs.config.appFilesUpdaterOptions;
7-
const config = {
8-
env,
9-
platform,
10-
bundle: appFilesUpdaterOptions.bundle,
11-
release: appFilesUpdaterOptions.release,
12-
};
13-
const result = config.bundle && runWebpackCompiler.bind(runWebpackCompiler, config, $mobileHelper, $projectData, $logger, hookArgs);
14-
return result;
4+
const env = hookArgs.config.env || {};
5+
const platform = hookArgs.config.platform;
6+
const appFilesUpdaterOptions = hookArgs.config.appFilesUpdaterOptions;
7+
const config = {
8+
env,
9+
platform,
10+
bundle: appFilesUpdaterOptions.bundle,
11+
release: appFilesUpdaterOptions.release,
12+
};
13+
const result = config.bundle && runWebpackCompiler.bind(runWebpackCompiler, config, $mobileHelper, $projectData, $logger, hookArgs);
14+
return result;
1515
}

lib/before-watchPatterns.js

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
1-
const { AppDirectoryLocation } = require("./constants");
1+
const { basename } = require("path");
2+
const { buildEnvData, getCompilationContext } = require("./utils");
23

34
module.exports = function (hookArgs) {
4-
if (hookArgs.liveSyncData && hookArgs.liveSyncData.bundle) {
5-
return (args, originalMethod) => {
6-
return originalMethod(...args).then(originalPatterns => {
7-
const appDirectoryLocationIndex = originalPatterns.indexOf(AppDirectoryLocation);
8-
if (appDirectoryLocationIndex !== -1) {
9-
originalPatterns.splice(appDirectoryLocationIndex, 1);
10-
}
5+
const { liveSyncData } = hookArgs;
6+
if (!liveSyncData || !liveSyncData.bundle) {
7+
return;
8+
}
119

12-
return originalPatterns;
13-
});
14-
};
15-
}
10+
const { platforms } = hookArgs;
11+
const { env } = liveSyncData;
12+
return (args, originalMethod) => {
13+
return originalMethod(...args).then(originalPatterns => {
14+
if (!platforms || !platforms.length) {
15+
throw new Error("Target platform should be specified!");
16+
}
17+
18+
const compilationContexts = platforms.map(platform =>
19+
getContext(platform, env));
20+
21+
const ignorePatterns = compilationContexts.map(
22+
context => `!${context}`
23+
);
24+
25+
return [...originalPatterns, ...ignorePatterns];
26+
});
27+
};
28+
}
29+
30+
function getContext(platform, env) {
31+
const fullEnvData = buildEnvData(platform, env);
32+
return getCompilationContext(fullEnvData);
1633
}

lib/compiler.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { join, resolve: pathResolve } = require("path");
44
const { existsSync } = require("fs");
55
const readline = require("readline");
66
const { messages } = require("../plugins/WatchStateLoggerPlugin");
7-
const { AppDirectoryLocation } = require("./constants");
7+
const { buildEnvData, getCompilationContext } = require("./utils");
88

99
let hasBeenInvoked = false;
1010

@@ -41,7 +41,9 @@ exports.runWebpackCompiler = function runWebpackCompiler(config, $mobileHelper,
4141
}
4242

4343
console.log(`Running webpack for ${config.platform}...`);
44-
const envFlagNames = Object.keys(config.env).concat([config.platform.toLowerCase()]);
44+
45+
const envData = buildEnvData(config.platform, config.env);
46+
const envFlagNames = Object.keys(envData);
4547

4648
const snapshotEnvIndex = envFlagNames.indexOf("snapshot");
4749
if (snapshotEnvIndex !== -1 && !utils.shouldSnapshot($mobileHelper, config)) {
@@ -92,7 +94,12 @@ exports.runWebpackCompiler = function runWebpackCompiler(config, $mobileHelper,
9294
}
9395

9496
if (hookArgs.filesToSync && hookArgs.startSyncFilesTimeout) {
95-
hookArgs.filesToSync.push(...message.emittedFiles.map(emittedFile => join($projectData.projectDir, AppDirectoryLocation, emittedFile)));
97+
const compilationContext = getCompilationContext(envData);
98+
hookArgs.filesToSync.push(
99+
...message.emittedFiles.map(
100+
emittedFile => join($projectData.projectDir, compilationContext, emittedFile)
101+
)
102+
);
96103
hookArgs.startSyncFilesTimeout();
97104
}
98105
}

lib/constants.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

lib/utils.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,34 @@
11
const os = require("os");
2+
const path = require("path");
3+
4+
const { getProjectDir, getWebpackConfig } = require("../projectHelpers");
5+
6+
function buildEnvData(platform, env) {
7+
return Object.assign({},
8+
env,
9+
{ [platform.toLowerCase()]: true }
10+
);
11+
}
12+
13+
function getCompilationContext(env) {
14+
const projectDir = getProjectDir();
15+
const config = getWebpackConfig(projectDir, env);
16+
const { context } = config;
17+
18+
return context ?
19+
path.relative(projectDir, context) :
20+
".";
21+
}
222

323
function shouldSnapshot($mobileHelper, config) {
4-
const platformSupportsSnapshot = $mobileHelper.isAndroidPlatform(config.platform);
5-
const osSupportsSnapshot = os.type() !== "Windows_NT";
6-
return config.bundle && config.release && platformSupportsSnapshot && osSupportsSnapshot;
24+
const platformSupportsSnapshot = $mobileHelper.isAndroidPlatform(config.platform);
25+
const osSupportsSnapshot = os.type() !== "Windows_NT";
26+
27+
return config.bundle && config.release && platformSupportsSnapshot && osSupportsSnapshot;
728
}
829

9-
module.exports.shouldSnapshot = shouldSnapshot;
30+
module.exports = {
31+
buildEnvData,
32+
getCompilationContext,
33+
shouldSnapshot,
34+
};

projectHelpers.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const path = require("path");
22
const fs = require("fs");
33
const semver = require("semver");
4+
const { EOL } = require("os");
45

56
const isTypeScript = ({ projectDir, packageJson } = {}) => {
67
packageJson = packageJson || getPackageJson(projectDir);
@@ -44,6 +45,28 @@ const getAndroidRuntimeVersion = (projectDir) => {
4445
}
4546
}
4647

48+
const getWebpackConfig = (projectDir, env, configPath = "webpack.config.js") => {
49+
const configAbsolutePath = path.resolve(projectDir, configPath);
50+
let config;
51+
try {
52+
config = require(configAbsolutePath);
53+
} catch (e) {
54+
throw new Error(
55+
`Couldn't load webpack config from ${configAbsolutePath}. ` +
56+
`Original error:${EOL}${e}`
57+
);
58+
}
59+
if (typeof config === "function") {
60+
config = config(env);
61+
}
62+
63+
if (!config) {
64+
throw new Error(`Webpack config from ${configAbsolutePath} is empty!`);
65+
}
66+
67+
return config;
68+
};
69+
4770
const getPackageJson = projectDir => {
4871
const packageJsonPath = getPackageJsonPath(projectDir);
4972
return JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
@@ -53,7 +76,7 @@ const writePackageJson = (content, projectDir) => {
5376
const packageJsonPath = getPackageJsonPath(projectDir);
5477
fs.writeFileSync(packageJsonPath, JSON.stringify(content, null, 2))
5578
}
56-
const getProjectDir = ({ nestingLvl } = { nestingLvl: 0 }) => {
79+
const getProjectDir = ({ nestingLvl } = { nestingLvl: 2 }) => {
5780
// INIT_CWD is available since npm 5.4
5881
const initCwd = process.env.INIT_CWD;
5982
const shouldUseInitCwd = (() => {
@@ -115,14 +138,15 @@ const resolveAndroidConfigurationsPath = projectDir => {
115138
const getPackageJsonPath = projectDir => path.resolve(projectDir, "package.json");
116139

117140
module.exports = {
118-
isTypeScript,
119-
isAngular,
120-
isSass,
121-
writePackageJson,
141+
getAndroidProjectPath,
142+
getAndroidRuntimeVersion,
122143
getPackageJson,
123144
getProjectDir,
124-
getAndroidRuntimeVersion,
125-
getAndroidProjectPath,
145+
getWebpackConfig,
146+
isAngular,
147+
isSass,
148+
isTypeScript,
126149
resolveAndroidAppPath,
127150
resolveAndroidConfigurationsPath,
151+
writePackageJson,
128152
};

snapshot/android/utils.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { chmodSync, createWriteStream, existsSync } = require("fs");
2-
const { tmpdir } = require("os");
2+
const { tmpdir, EOL } = require("os");
33
const { dirname, join } = require("path");
44

55
const { mkdir } = require("shelljs");
@@ -36,14 +36,14 @@ const getJsonFile = url =>
3636
}
3737

3838
if (!response || response.statusCode !== 200) {
39-
return reject(`Couldn't fetch ${url}! Response:\n${response}`);
39+
return reject(`Couldn't fetch ${url}! Response:${EOL}${response}`);
4040
}
4141

4242
try {
4343
const data = JSON.parse(body);
4444
resolve(data);
4545
} catch (error) {
46-
reject(`Couldn't parse json data! Original error:\n${error}`);
46+
reject(`Couldn't parse json data! Original error:${EOL}${error}`);
4747
}
4848
})
4949
).catch(reject);
@@ -58,7 +58,7 @@ const getRequestOptions = (url) =>
5858
resolve(allOptions);
5959
})
6060
.catch(error =>
61-
reject(`Couldn't get proxy settings! Original error:\n${error}`));
61+
reject(`Couldn't get proxy settings! Original error:${EOL}${error}`));
6262
});
6363

6464
module.exports = {

templates/webpack.angular.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ module.exports = env => {
1919
const ngToolsWebpackOptions = { tsConfigPath: "tsconfig.json" };
2020

2121
const config = {
22-
context: resolve("./app"),
22+
context: resolve(__dirname, "app"),
2323
watchOptions: {
2424
ignored: [
25-
resolve("./app/App_Resources"),
25+
resolve(__dirname, "./app/App_Resources"),
2626
// Don't watch hidden files
2727
"**/.*",
2828
]

templates/webpack.javascript.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ module.exports = env => {
1717
const { snapshot, uglify, report } = env;
1818

1919
const config = {
20-
context: resolve("./app"),
20+
context: resolve(__dirname, "app"),
2121
watchOptions: {
2222
ignored: [
23-
resolve("./app/App_Resources"),
23+
resolve(__dirname, "./app/App_Resources"),
2424
// Don't watch hidden files
2525
"**/.*",
2626
]

templates/webpack.typescript.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ module.exports = env => {
1717
const { snapshot, uglify, report } = env;
1818

1919
const config = {
20-
context: resolve("./app"),
20+
context: resolve(__dirname, "app"),
2121
watchOptions: {
2222
ignored: [
23-
resolve("./app/App_Resources"),
23+
resolve(__dirname, "./app/App_Resources"),
2424
// Don't watch hidden files
2525
"**/.*",
2626
]

verify/update.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const { forceUpdateProjectFiles } = require("../projectFilesManager");
66
const { forceUpdateProjectDeps } = require("../dependencyManager");
77

88
const PLUGIN_NAME = "nativescript-dev-webpack";
9-
const PROJECT_DIR = getProjectDir({ nestingLvl: 2 });
9+
const PROJECT_DIR = getProjectDir();
1010

1111
function update({
1212
deps: shouldUpdateDeps,

0 commit comments

Comments
 (0)