From 4bc673274b566072b817ce0a9d7fea9df29c917f Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Fri, 10 May 2019 14:18:23 +0100 Subject: [PATCH] fix: watch on windows (#74) --- package-lock.json | 3 +- package.json | 2 +- src/index.js | 5 ++- test/cacheContext-option.test.js | 52 +++++++++++++++++++++++--------- 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/package-lock.json b/package-lock.json index 292da4c..966129a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9147,7 +9147,8 @@ "normalize-path": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", - "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==" + "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==", + "dev": true }, "npm-path": { "version": "2.0.4", diff --git a/package.json b/package.json index 14d8d57..7a3a272 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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", diff --git a/src/index.js b/src/index.js index f6bf6e5..69b41a1 100644 --- a/src/index.js +++ b/src/index.js @@ -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'); @@ -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('!'); } diff --git a/test/cacheContext-option.test.js b/test/cacheContext-option.test.js index 4609187..48f598b 100644 --- a/test/cacheContext-option.test.js +++ b/test/cacheContext-option.test.js @@ -40,10 +40,15 @@ 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) => { @@ -51,10 +56,16 @@ const buildCacheLoaderCallsData = (calls) => 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()) @@ -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 () => {