Skip to content

Commit 4787553

Browse files
authored
fix: ignore package.json on windows (#735)
In this commit I added an E2E test for the case described in #674, fixed the slash bug, and moved package.json ignore to a different place (because ignoring watch on the package.json could interfere with other plugins/loaders) Closes: #674
1 parent a2826a8 commit 4787553

File tree

6 files changed

+86
-7
lines changed

6 files changed

+86
-7
lines changed

src/files-change.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,26 @@ interface FilesChange {
55
deletedFiles?: string[];
66
}
77

8+
// we ignore package.json file because of https://github.com/TypeStrong/fork-ts-checker-webpack-plugin/issues/674
9+
const IGNORED_FILES = ['package.json'];
10+
11+
const isIgnoredFile = (file: string) =>
12+
IGNORED_FILES.some(
13+
(ignoredFile) => file.endsWith(`/${ignoredFile}`) || file.endsWith(`\\${ignoredFile}`)
14+
);
15+
816
const compilerFilesChangeMap = new WeakMap<webpack.Compiler, FilesChange>();
917

1018
function getFilesChange(compiler: webpack.Compiler): FilesChange {
11-
return compilerFilesChangeMap.get(compiler) || { changedFiles: [], deletedFiles: [] };
19+
const { changedFiles = [], deletedFiles = [] } = compilerFilesChangeMap.get(compiler) || {
20+
changedFiles: [],
21+
deletedFiles: [],
22+
};
23+
24+
return {
25+
changedFiles: changedFiles.filter((changedFile) => !isIgnoredFile(changedFile)),
26+
deletedFiles: deletedFiles.filter((deletedFile) => !isIgnoredFile(deletedFile)),
27+
};
1228
}
1329

1430
function consumeFilesChange(compiler: webpack.Compiler): FilesChange {

src/watch/inclusive-node-watch-file-system.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import type { ForkTsCheckerWebpackPluginState } from '../plugin-state';
1212
import type { WatchFileSystem } from './watch-file-system';
1313

1414
const BUILTIN_IGNORED_DIRS = ['node_modules', '.git', '.yarn', '.pnp'];
15-
// we ignore package.json file because of https://github.com/TypeStrong/fork-ts-checker-webpack-plugin/issues/674
16-
const BUILTIN_IGNORED_FILES = ['package.json'];
1715

1816
function createIsIgnored(
1917
ignored: string | RegExp | (string | RegExp)[] | undefined,
@@ -35,10 +33,9 @@ function createIsIgnored(
3533
excluded.some((excludedPath) => path.startsWith(excludedPath))
3634
);
3735
ignoredFunctions.push((path: string) =>
38-
BUILTIN_IGNORED_DIRS.some((ignoredDir) => path.includes(`/${ignoredDir}/`))
39-
);
40-
ignoredFunctions.push((path: string) =>
41-
BUILTIN_IGNORED_FILES.some((ignoredFile) => path.endsWith(`/${ignoredFile}`))
36+
BUILTIN_IGNORED_DIRS.some(
37+
(ignoredDir) => path.includes(`/${ignoredDir}/`) || path.includes(`\\${ignoredDir}\\`)
38+
)
4239
);
4340

4441
return function isIgnored(path: string) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export declare function sayHello(who: string): string;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
exports.__esModule = true;
3+
exports.sayHello = void 0;
4+
function sayHello(who) {
5+
return 'Hello ' + who + '!';
6+
}
7+
exports.sayHello = sayHello;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "typescript-nested-project",
3+
"version": "1.0.0",
4+
"license": "MIT",
5+
"main": "./index.js",
6+
"types": "./index.d.ts",
7+
"files": [
8+
"./index.js"
9+
]
10+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import path from 'path';
2+
3+
import { createProcessDriver } from 'karton';
4+
5+
import { createWebpackDevServerDriver } from './driver/webpack-dev-server-driver';
6+
7+
describe('Webpack Inclusive Watcher', () => {
8+
it.each([{ async: false }, { async: true }])(
9+
'ignores package.json change for %p',
10+
async ({ async }) => {
11+
await sandbox.load(path.join(__dirname, 'fixtures/typescript-basic'));
12+
await sandbox.load(path.join(__dirname, 'fixtures/typescript-package'));
13+
await sandbox.install('yarn', { typescript: '4.6.3' });
14+
await sandbox.patch('webpack.config.js', 'async: false,', `async: ${JSON.stringify(async)},`);
15+
16+
// add import to typescript-nested-project project
17+
await sandbox.patch(
18+
'src/index.ts',
19+
"import { getUserName } from './model/User';",
20+
[
21+
"import { getUserName } from './model/User';",
22+
'import { sayHello } from "../package";',
23+
'',
24+
"sayHello('World');",
25+
].join('\n')
26+
);
27+
28+
// start webpack dev server
29+
const process = sandbox.spawn('yarn webpack serve --mode=development');
30+
const baseDriver = createProcessDriver(process);
31+
const webpackDriver = createWebpackDevServerDriver(process, async);
32+
33+
await webpackDriver.waitForNoErrors();
34+
35+
// update nested package.json file
36+
await sandbox.patch('package/package.json', '"1.0.0"', '"1.0.1"');
37+
38+
// wait for 5 seconds and fail if there is Debug Failure. in the console output
39+
await expect(() =>
40+
baseDriver.waitForStderrIncludes('Error: Debug Failure.', 5000)
41+
).rejects.toEqual(
42+
new Error('Exceeded time on waiting for "Error: Debug Failure." to appear in the stderr.')
43+
);
44+
45+
await webpackDriver.waitForNoErrors();
46+
}
47+
);
48+
});

0 commit comments

Comments
 (0)