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

Commit eb4b8bc

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

File tree

3 files changed

+49
-21
lines changed

3 files changed

+49
-21
lines changed

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.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: 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)