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

Commit e870005

Browse files
committed
refactor: ignore compilation contexts from CLI watcher
1 parent 77f2b86 commit e870005

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed

lib/before-watchPatterns.js

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
1+
const { basename } = require("path");
12
const { AppDirectoryLocation } = require("./constants");
3+
const { buildEnvData, getCompilationContext } = require("./utils");
4+
25

36
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-
}
7+
const { liveSyncData } = hookArgs;
8+
if (!liveSyncData || !liveSyncData.bundle) {
9+
return;
10+
}
11+
12+
const { platforms } = hookArgs;
13+
const { env } = liveSyncData;
14+
return (args, originalMethod) => {
15+
return originalMethod(...args).then(originalPatterns => {
16+
if (!platforms.length) {
17+
throw new Error("Target platform should be specified!");
18+
}
19+
20+
const compilationContexts = platforms.map(platform =>
21+
getContext(platform, env));
22+
23+
const ignorePatterns = compilationContexts.map(
24+
context => `!${context}`
25+
);
26+
27+
return [...originalPatterns, ...ignorePatterns];
28+
});
29+
};
30+
}
1131

12-
return originalPatterns;
13-
});
14-
};
15-
}
32+
function getContext(platform, env) {
33+
const fullEnvData = buildEnvData(platform, env);
34+
return getCompilationContext(fullEnvData);
1635
}

lib/compiler.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +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 { buildEnvData } = require("./utils");
78
const { AppDirectoryLocation } = require("./constants");
89

910
let hasBeenInvoked = false;
@@ -41,7 +42,9 @@ exports.runWebpackCompiler = function runWebpackCompiler(config, $mobileHelper,
4142
}
4243

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

4649
const snapshotEnvIndex = envFlagNames.indexOf("snapshot");
4750
if (snapshotEnvIndex !== -1 && !utils.shouldSnapshot($mobileHelper, config)) {

lib/utils.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,30 @@ const os = require("os");
22

33
const { getProjectDir, getWebpackConfig } = require("../projectHelpers");
44

5-
function shouldSnapshot($mobileHelper, config) {
6-
const platformSupportsSnapshot = $mobileHelper.isAndroidPlatform(config.platform);
7-
const osSupportsSnapshot = os.type() !== "Windows_NT";
8-
9-
return config.bundle && config.release && platformSupportsSnapshot && osSupportsSnapshot;
5+
function buildEnvData(platform, env) {
6+
return {
7+
...env,
8+
[platform.toLowerCase()]: true,
9+
};
1010
}
1111

12-
function getCompilationContext() {
12+
function getCompilationContext(env) {
1313
const projectDir = getProjectDir();
14-
const config = getWebpackConfig(projectDir);
14+
const config = getWebpackConfig(projectDir, env);
1515
const context = config.context || projectDir;
1616

1717
return context;
1818
}
1919

20+
function shouldSnapshot($mobileHelper, config) {
21+
const platformSupportsSnapshot = $mobileHelper.isAndroidPlatform(config.platform);
22+
const osSupportsSnapshot = os.type() !== "Windows_NT";
23+
24+
return config.bundle && config.release && platformSupportsSnapshot && osSupportsSnapshot;
25+
}
26+
2027
module.exports = {
21-
shouldSnapshot,
28+
buildEnvData,
2229
getCompilationContext,
30+
shouldSnapshot,
2331
};

0 commit comments

Comments
 (0)