From 5b37474781bc0ed1c0604a02f1b64fe0910ba8a5 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Fri, 4 Jan 2019 19:05:17 +0000 Subject: [PATCH] fix: generate normalized cache context relative paths (#54) --- package-lock.json | 5 ++-- package.json | 2 +- src/index.js | 5 ++-- .../cacheContext-option.test.js.snap | 4 +-- test/cacheContext-option.test.js | 25 +++++++++++++++---- 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index 54d368e..8e3b196 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5668,7 +5668,7 @@ }, "git-config-path": { "version": "1.0.1", - "resolved": "https://registry.npmjs.org/git-config-path/-/git-config-path-1.0.1.tgz", + "resolved": "http://registry.npmjs.org/git-config-path/-/git-config-path-1.0.1.tgz", "integrity": "sha1-bTP37WPbDQ4RgTFQO6s6ykfVRmQ=", "dev": true, "requires": { @@ -9029,8 +9029,7 @@ "normalize-path": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", - "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==", - "dev": true + "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==" }, "npm-path": { "version": "2.0.4", diff --git a/package.json b/package.json index 4fb6399..d603dba 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "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": { @@ -66,7 +67,6 @@ "jest": "^23.6.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 f8b1f50..8542244 100644 --- a/src/index.js +++ b/src/index.js @@ -3,6 +3,7 @@ */ const fs = require('fs'); const path = require('path'); +const normalizePath = require('normalize-path'); const async = require('neo-async'); const crypto = require('crypto'); const mkdirp = require('mkdirp'); @@ -33,13 +34,13 @@ function pathWithCacheContext(cacheContext, originalPath) { if (originalPath.includes(cacheContext)) { return originalPath .split('!') - .map((subPath) => path.relative(cacheContext, subPath)) + .map((subPath) => normalizePath(path.relative(cacheContext, subPath))) .join('!'); } return originalPath .split('!') - .map((subPath) => path.resolve(cacheContext, subPath)) + .map((subPath) => normalizePath(path.resolve(cacheContext, subPath))) .join('!'); } diff --git a/test/__snapshots__/cacheContext-option.test.js.snap b/test/__snapshots__/cacheContext-option.test.js.snap index 96b349c..c3aead6 100644 --- a/test/__snapshots__/cacheContext-option.test.js.snap +++ b/test/__snapshots__/cacheContext-option.test.js.snap @@ -1,8 +1,8 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`cacheContext option should generate absolute paths to the project root 2: errors 1`] = `Array []`; +exports[`cacheContext option should generate absolute paths to the project root: errors 1`] = `Array []`; -exports[`cacheContext option should generate absolute paths to the project root 2: warnings 1`] = `Array []`; +exports[`cacheContext option should generate absolute paths to the project root: warnings 1`] = `Array []`; exports[`cacheContext option should generate relative paths to the project root: errors 1`] = `Array []`; diff --git a/test/cacheContext-option.test.js b/test/cacheContext-option.test.js index b6d82ee..f41d818 100644 --- a/test/cacheContext-option.test.js +++ b/test/cacheContext-option.test.js @@ -1,5 +1,7 @@ const path = require('path'); + const normalizePath = require('normalize-path'); + const { webpack } = require('./helpers'); const mockCacheLoaderWriteFn = jest.fn(); @@ -26,9 +28,7 @@ const mockRelativeWebpackConfig = { }; const buildSnapshotReadyDeps = (deps) => - deps.map((dep) => - Object.assign({}, dep, { mtime: null, path: normalizePath(dep.path) }) - ); + deps.map((dep) => Object.assign({}, dep, { mtime: null, path: dep.path })); const buildCacheLoaderCallsData = (calls) => Array.from( @@ -38,7 +38,7 @@ const buildCacheLoaderCallsData = (calls) => return builtCalls.set(rawData.remainingRequest, { ...rawData, - remainingRequest: normalizePath(rawData.remainingRequest), + remainingRequest: rawData.remainingRequest, dependencies: buildSnapshotReadyDeps(rawData.dependencies), contextDependencies: buildSnapshotReadyDeps( rawData.contextDependencies @@ -79,7 +79,22 @@ describe('cacheContext option', () => { expect(stats.compilation.errors).toMatchSnapshot('errors'); }); - it('should generate absolute paths to the project root 2', async () => { + it('should generate normalized relative paths to the project root', async () => { + const testId = './basic/index.js'; + await webpack(testId, mockRelativeWebpackConfig); + + const cacheLoaderCallsData = buildCacheLoaderCallsData( + mockCacheLoaderWriteFn.mock.calls + ).sort(sortData); + + expect( + cacheLoaderCallsData.every( + (call) => call.remainingRequest === normalizePath(call.remainingRequest) + ) + ); + }); + + it('should generate absolute paths to the project root', async () => { const testId = './basic/index.js'; const stats = await webpack(testId, mockBaseWebpackConfig);