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

Generate normalized cache context relative paths #54

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions 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
Expand Up @@ -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": {
Expand All @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Expand Up @@ -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');
Expand Down Expand Up @@ -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('!');
}

Expand Down
4 changes: 2 additions & 2 deletions 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 []`;

Expand Down
25 changes: 20 additions & 5 deletions 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();
Expand All @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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);

Expand Down