Skip to content

Commit

Permalink
fix: ignore package.json on windows (#735)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
piotr-oles committed Apr 23, 2022
1 parent a2826a8 commit 4787553
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 7 deletions.
18 changes: 17 additions & 1 deletion src/files-change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,26 @@ interface FilesChange {
deletedFiles?: string[];
}

// we ignore package.json file because of https://github.com/TypeStrong/fork-ts-checker-webpack-plugin/issues/674
const IGNORED_FILES = ['package.json'];

const isIgnoredFile = (file: string) =>
IGNORED_FILES.some(
(ignoredFile) => file.endsWith(`/${ignoredFile}`) || file.endsWith(`\\${ignoredFile}`)
);

const compilerFilesChangeMap = new WeakMap<webpack.Compiler, FilesChange>();

function getFilesChange(compiler: webpack.Compiler): FilesChange {
return compilerFilesChangeMap.get(compiler) || { changedFiles: [], deletedFiles: [] };
const { changedFiles = [], deletedFiles = [] } = compilerFilesChangeMap.get(compiler) || {
changedFiles: [],
deletedFiles: [],
};

return {
changedFiles: changedFiles.filter((changedFile) => !isIgnoredFile(changedFile)),
deletedFiles: deletedFiles.filter((deletedFile) => !isIgnoredFile(deletedFile)),
};
}

function consumeFilesChange(compiler: webpack.Compiler): FilesChange {
Expand Down
9 changes: 3 additions & 6 deletions src/watch/inclusive-node-watch-file-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import type { ForkTsCheckerWebpackPluginState } from '../plugin-state';
import type { WatchFileSystem } from './watch-file-system';

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

function createIsIgnored(
ignored: string | RegExp | (string | RegExp)[] | undefined,
Expand All @@ -35,10 +33,9 @@ function createIsIgnored(
excluded.some((excludedPath) => path.startsWith(excludedPath))
);
ignoredFunctions.push((path: string) =>
BUILTIN_IGNORED_DIRS.some((ignoredDir) => path.includes(`/${ignoredDir}/`))
);
ignoredFunctions.push((path: string) =>
BUILTIN_IGNORED_FILES.some((ignoredFile) => path.endsWith(`/${ignoredFile}`))
BUILTIN_IGNORED_DIRS.some(
(ignoredDir) => path.includes(`/${ignoredDir}/`) || path.includes(`\\${ignoredDir}\\`)
)
);

return function isIgnored(path: string) {
Expand Down
1 change: 1 addition & 0 deletions test/e2e/fixtures/typescript-package/package/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function sayHello(who: string): string;
7 changes: 7 additions & 0 deletions test/e2e/fixtures/typescript-package/package/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";
exports.__esModule = true;
exports.sayHello = void 0;
function sayHello(who) {
return 'Hello ' + who + '!';
}
exports.sayHello = sayHello;
10 changes: 10 additions & 0 deletions test/e2e/fixtures/typescript-package/package/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "typescript-nested-project",
"version": "1.0.0",
"license": "MIT",
"main": "./index.js",
"types": "./index.d.ts",
"files": [
"./index.js"
]
}
48 changes: 48 additions & 0 deletions test/e2e/webpack-inclusive-watcher.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import path from 'path';

import { createProcessDriver } from 'karton';

import { createWebpackDevServerDriver } from './driver/webpack-dev-server-driver';

describe('Webpack Inclusive Watcher', () => {
it.each([{ async: false }, { async: true }])(
'ignores package.json change for %p',
async ({ async }) => {
await sandbox.load(path.join(__dirname, 'fixtures/typescript-basic'));
await sandbox.load(path.join(__dirname, 'fixtures/typescript-package'));
await sandbox.install('yarn', { typescript: '4.6.3' });
await sandbox.patch('webpack.config.js', 'async: false,', `async: ${JSON.stringify(async)},`);

// add import to typescript-nested-project project
await sandbox.patch(
'src/index.ts',
"import { getUserName } from './model/User';",
[
"import { getUserName } from './model/User';",
'import { sayHello } from "../package";',
'',
"sayHello('World');",
].join('\n')
);

// start webpack dev server
const process = sandbox.spawn('yarn webpack serve --mode=development');
const baseDriver = createProcessDriver(process);
const webpackDriver = createWebpackDevServerDriver(process, async);

await webpackDriver.waitForNoErrors();

// update nested package.json file
await sandbox.patch('package/package.json', '"1.0.0"', '"1.0.1"');

// wait for 5 seconds and fail if there is Debug Failure. in the console output
await expect(() =>
baseDriver.waitForStderrIncludes('Error: Debug Failure.', 5000)
).rejects.toEqual(
new Error('Exceeded time on waiting for "Error: Debug Failure." to appear in the stderr.')
);

await webpackDriver.waitForNoErrors();
}
);
});

0 comments on commit 4787553

Please sign in to comment.