Skip to content
This repository has been archived by the owner on Oct 27, 2020. It is now read-only.

Commit

Permalink
fix: watch on windows (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic authored and evilebottnawi committed May 10, 2019
1 parent 1369c05 commit 4bc6732
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"loader-utils": "^1.1.0",
"mkdirp": "^0.5.1",
"neo-async": "^2.6.0",
"normalize-path": "^3.0.0",
"schema-utils": "^1.0.0"
},
"devDependencies": {
Expand All @@ -70,6 +69,7 @@
"jest": "^24.5.0",
"lint-staged": "^8.1.0",
"memory-fs": "^0.4.1",
"normalize-path": "^3.0.0",
"pre-commit": "^1.0.0",
"prettier": "^1.15.2",
"standard-version": "^4.0.0",
Expand Down
5 changes: 2 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
const fs = require('fs');
const os = require('os');
const path = require('path');
const normalizePath = require('normalize-path');
const async = require('neo-async');
const crypto = require('crypto');
const mkdirp = require('mkdirp');
Expand Down Expand Up @@ -38,13 +37,13 @@ function pathWithCacheContext(cacheContext, originalPath) {
if (originalPath.includes(cacheContext)) {
return originalPath
.split('!')
.map((subPath) => normalizePath(path.relative(cacheContext, subPath)))
.map((subPath) => path.relative(cacheContext, subPath))
.join('!');
}

return originalPath
.split('!')
.map((subPath) => normalizePath(path.resolve(cacheContext, subPath)))
.map((subPath) => path.resolve(cacheContext, subPath))
.join('!');
}

Expand Down
52 changes: 38 additions & 14 deletions test/cacheContext-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,32 @@ const sortData = (a, b) => {
return 0;
};

const buildSnapshotReadyDeps = (deps) =>
deps.map((dep) => Object.assign({}, dep, { mtime: null, path: dep.path }));

const buildCacheLoaderCallsData = (calls) =>
const buildSnapshotReadyDeps = (deps, normalizePaths = true) =>
deps.map((dep) =>
Object.assign({}, dep, {
mtime: null,
path: normalizePaths ? normalizePath(dep.path) : dep.path,
})
);

const buildCacheLoaderCallsData = (calls, normalizePaths = true) =>
Array.from(
calls
.reduce((builtCalls, call) => {
const [, rawData] = call;

return builtCalls.set(rawData.remainingRequest, {
...rawData,
remainingRequest: rawData.remainingRequest,
dependencies: buildSnapshotReadyDeps(rawData.dependencies),
remainingRequest: normalizePaths
? normalizePath(rawData.remainingRequest)
: rawData.remainingRequest,
dependencies: buildSnapshotReadyDeps(
rawData.dependencies,
normalizePaths
),
contextDependencies: buildSnapshotReadyDeps(
rawData.contextDependencies
rawData.contextDependencies,
normalizePaths
),
});
}, new Map())
Expand Down Expand Up @@ -82,19 +93,32 @@ describe('cacheContext option', () => {
expect(stats.compilation.errors).toMatchSnapshot('errors');
});

it('should generate normalized relative paths to the project root', async () => {
it('should generate non normalized relative paths to the project root on windows', async () => {
const testId = './basic/index.js';
await webpack(testId, mockRelativeWebpackConfig);

const cacheLoaderCallsData = buildCacheLoaderCallsData(
mockCacheLoaderWriteFn.mock.calls
mockCacheLoaderWriteFn.mock.calls,
false
);

expect(
cacheLoaderCallsData.every(
(call) => call.remainingRequest === normalizePath(call.remainingRequest)
)
).toBeTruthy();
// NOTE: this test prevents to generate normalized paths for the generated cache assets
// under windows which will break the watcher due to a bug on watchpack/chokidar
if (process.platform === 'win32') {
expect(
cacheLoaderCallsData.every(
(call) =>
call.remainingRequest !== normalizePath(call.remainingRequest)
)
).toBeTruthy();
} else {
expect(
cacheLoaderCallsData.every(
(call) =>
call.remainingRequest === normalizePath(call.remainingRequest)
)
).toBeTruthy();
}
});

it('should generate absolute paths to the project root', async () => {
Expand Down

0 comments on commit 4bc6732

Please sign in to comment.