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

Commit 6b0f56b

Browse files
authored
fix: tell the {N} CLI to ignore the source dir when watching for changes (#586)
Stop requiring the webpack config file in the hook, because that may have side effects. Instead tell the {N} CLI watcher to ignore the whole source dir. fixes #584
1 parent 404abbb commit 6b0f56b

File tree

5 files changed

+10
-66
lines changed

5 files changed

+10
-66
lines changed

Diff for: lib/before-watchPatterns.js

+4-21
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,19 @@
11
const { basename } = require("path");
2-
const {
3-
buildEnvData,
4-
getCompilationContext,
5-
} = require("./utils");
2+
const { getAppPathFromProjectData } = require("../projectHelpers");
63

74
module.exports = function (hookArgs) {
85
const { liveSyncData } = hookArgs;
96
if (!liveSyncData || !liveSyncData.bundle) {
107
return;
118
}
129

13-
const { platforms } = hookArgs;
14-
const { env } = liveSyncData;
1510
return (args, originalMethod) => {
1611
return originalMethod(...args).then(originalPatterns => {
17-
if (!platforms || !platforms.length) {
18-
throw new Error("Target platform should be specified!");
19-
}
2012

21-
const compilationContexts = platforms.map(platform =>
22-
getContext(hookArgs.projectData, platform, env));
13+
const appPath = getAppPathFromProjectData(hookArgs.projectData);
14+
const ignorePattern = `!${appPath}`;
2315

24-
const ignorePatterns = compilationContexts.map(
25-
context => `!${context}`
26-
);
27-
28-
return [...originalPatterns, ...ignorePatterns];
16+
return [...originalPatterns, ignorePattern];
2917
});
3018
};
3119
}
32-
33-
function getContext(projectData, platform, env) {
34-
const fullEnvData = buildEnvData(projectData, platform, env);
35-
return getCompilationContext(projectData.projectDir, fullEnvData);
36-
}

Diff for: lib/compiler.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
const utils = require("./utils");
22
const { spawn } = require("child_process");
3-
const { join, resolve: pathResolve } = require("path");
3+
const { resolve: pathResolve } = require("path");
44
const { existsSync } = require("fs");
55
const readline = require("readline");
66

77
const { messages } = require("../plugins/WatchStateLoggerPlugin");
8-
const { buildEnvData, getCompilationContext } = require("./utils");
8+
const { buildEnvData } = require("./utils");
99

1010
let hasBeenInvoked = false;
1111

@@ -84,12 +84,7 @@ exports.runWebpackCompiler = function runWebpackCompiler(config, $projectData, $
8484
}
8585

8686
if (hookArgs.filesToSync && hookArgs.startSyncFilesTimeout) {
87-
const compilationContext = getCompilationContext(projectDir, envData);
88-
hookArgs.filesToSync.push(
89-
...message.emittedFiles.map(
90-
emittedFile => join(projectDir, compilationContext, emittedFile)
91-
)
92-
);
87+
hookArgs.filesToSync.push(...message.emittedFiles);
9388
hookArgs.startSyncFilesTimeout();
9489
}
9590
}

Diff for: lib/utils.js

-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const {
55
getAppPathFromProjectData,
66
getAppResourcesPathFromProjectData,
77
getProjectDir,
8-
getWebpackConfig,
98
isAndroid,
109
} = require("../projectHelpers");
1110

@@ -25,15 +24,6 @@ function buildEnvData($projectData, platform, env) {
2524
return envData;
2625
}
2726

28-
function getCompilationContext(projectDir, env) {
29-
const config = getWebpackConfig(projectDir, env);
30-
const { context } = config;
31-
32-
return context ?
33-
path.relative(projectDir, context) :
34-
".";
35-
}
36-
3727
function shouldSnapshot(config) {
3828
const platformSupportsSnapshot = isAndroid(config.platform);
3929
const osSupportsSnapshot = os.type() !== "Windows_NT";
@@ -43,6 +33,5 @@ function shouldSnapshot(config) {
4333

4434
module.exports = {
4535
buildEnvData,
46-
getCompilationContext,
4736
shouldSnapshot
4837
};

Diff for: plugins/WatchStateLoggerPlugin.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { join } from "path";
12

23
export enum messages {
34
compilationComplete = "Webpack compilation complete.",
@@ -31,7 +32,8 @@ export class WatchStateLoggerPlugin {
3132

3233
const emittedFiles = Object
3334
.keys(compilation.assets)
34-
.filter(assetKey => compilation.assets[assetKey].emitted);
35+
.filter(assetKey => compilation.assets[assetKey].emitted)
36+
.map(file => join(compiler.context, file));
3537

3638
process.send && process.send(messages.compilationComplete, error => null);
3739
// Send emitted files so they can be LiveSynced if need be

Diff for: projectHelpers.js

-25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const { resolve } = require("path");
22
const { readFileSync, writeFileSync } = require("fs");
3-
const { EOL } = require("os");
43

54
const hook = require("nativescript-hook")(__dirname);
65

@@ -28,29 +27,6 @@ const isAngular = ({ projectDir, packageJson } = {}) => {
2827
.some(dependency => /^@angular\b/.test(dependency));
2928
};
3029

31-
const getWebpackConfig = (projectDir, env, configPath = "webpack.config.js") => {
32-
const configAbsolutePath = resolve(projectDir, configPath);
33-
let config;
34-
35-
try {
36-
config = require(configAbsolutePath);
37-
} catch (e) {
38-
throw new Error(
39-
`Couldn't load webpack config from ${configAbsolutePath}. ` +
40-
`Original error:${EOL}${e}`
41-
);
42-
}
43-
if (typeof config === "function") {
44-
config = config(env);
45-
}
46-
47-
if (!config) {
48-
throw new Error(`Webpack config from ${configAbsolutePath} is empty!`);
49-
}
50-
51-
return config;
52-
};
53-
5430
const getPackageJson = projectDir => {
5531
const packageJsonPath = getPackageJsonPath(projectDir);
5632
return JSON.parse(readFileSync(packageJsonPath, "utf8"));
@@ -95,7 +71,6 @@ module.exports = {
9571
getAppResourcesPathFromProjectData,
9672
getPackageJson,
9773
getProjectDir,
98-
getWebpackConfig,
9974
isAndroid,
10075
isIos,
10176
isAngular,

0 commit comments

Comments
 (0)