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

Fix usage with raw loader #69

Merged
10 changes: 10 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -64,6 +64,7 @@
"eslint-config-webpack": "^1.0.0",
"eslint-plugin-import": "^2.0.0",
"eslint-plugin-prettier": "^3.0.0",
"file-loader": "^3.0.1",
"husky": "^1.2.1",
"jest": "^24.5.0",
"lint-staged": "^8.1.0",
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Expand Up @@ -253,4 +253,5 @@ function cacheKey(options, request) {
return path.join(cacheDirectory, `${hash}.json`);
}

export const raw = true;
export { loader as default, pitch };
217 changes: 205 additions & 12 deletions test/__snapshots__/cacheContext-option.test.js.snap
Expand Up @@ -22,9 +22,55 @@ Array [
],
"remainingRequest": "test/fixtures/basic/file_1.js",
"result": Array [
"/* eslint-disable */
console.log('file_1');
",
Object {
"data": Array [
47,
42,
32,
101,
115,
108,
105,
110,
116,
45,
100,
105,
115,
97,
98,
108,
101,
32,
42,
47,
10,
99,
111,
110,
115,
111,
108,
101,
46,
108,
111,
103,
40,
39,
102,
105,
108,
101,
95,
49,
39,
41,
59,
10,
],
"type": "Buffer",
mistic marked this conversation as resolved.
Show resolved Hide resolved
},
],
},
Object {
Expand All @@ -41,9 +87,55 @@ console.log('file_1');
],
"remainingRequest": "test/fixtures/basic/file_2.js",
"result": Array [
"/* eslint-disable */
console.log('file_2');
",
Object {
"data": Array [
47,
42,
32,
101,
115,
108,
105,
110,
116,
45,
100,
105,
115,
97,
98,
108,
101,
32,
42,
47,
10,
99,
111,
110,
115,
111,
108,
101,
46,
108,
111,
103,
40,
39,
102,
105,
108,
101,
95,
50,
39,
41,
59,
10,
],
"type": "Buffer",
},
],
},
Object {
Expand All @@ -60,15 +152,116 @@ console.log('file_2');
],
"remainingRequest": "test/fixtures/basic/index.js",
"result": Array [
"/* eslint-disable */
require('./file_1.js');
require('./file_2.js');

console.log('basic_entry');
",
Object {
"data": Array [
47,
42,
32,
101,
115,
108,
105,
110,
116,
45,
100,
105,
115,
97,
98,
108,
101,
32,
42,
47,
10,
114,
101,
113,
117,
105,
114,
101,
40,
39,
46,
47,
102,
105,
108,
101,
95,
49,
46,
106,
115,
39,
41,
59,
10,
114,
101,
113,
117,
105,
114,
101,
40,
39,
46,
47,
102,
105,
108,
101,
95,
50,
46,
106,
115,
39,
41,
59,
10,
10,
99,
111,
110,
115,
111,
108,
101,
46,
108,
111,
103,
40,
39,
98,
97,
115,
105,
99,
95,
101,
110,
116,
114,
121,
39,
41,
59,
10,
],
"type": "Buffer",
},
],
},
]
`;

exports[`cacheContext option should generate relative paths to the project root: warnings 1`] = `Array []`;

exports[`cacheContext option should load as a raw loader to support images: errors 1`] = `Array []`;

exports[`cacheContext option should load as a raw loader to support images: warnings 1`] = `Array []`;
51 changes: 33 additions & 18 deletions test/cacheContext-option.test.js
Expand Up @@ -27,6 +27,18 @@ const mockRelativeWebpackConfig = {
},
};

const sortData = (a, b) => {
if (a.remainingRequest < b.remainingRequest) {
return -1;
}

if (a.remainingRequest > b.remainingRequest) {
return 1;
}

return 0;
};

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

Expand All @@ -46,19 +58,7 @@ const buildCacheLoaderCallsData = (calls) =>
});
}, new Map())
.values()
);

const sortData = (a, b) => {
if (a.remainingRequest < b.remainingRequest) {
return -1;
}

if (a.remainingRequest > b.remainingRequest) {
return 1;
}

return 0;
};
).sort(sortData);

describe('cacheContext option', () => {
it('should generate relative paths to the project root', async () => {
Expand All @@ -67,13 +67,13 @@ describe('cacheContext option', () => {

const cacheLoaderCallsData = buildCacheLoaderCallsData(
mockCacheLoaderWriteFn.mock.calls
).sort(sortData);
);

expect(
cacheLoaderCallsData.every(
(call) => !call.remainingRequest.includes(path.resolve('.'))
)
);
).toBeTruthy();
expect(cacheLoaderCallsData).toMatchSnapshot('generated cache-loader data');
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
expect(stats.compilation.errors).toMatchSnapshot('errors');
Expand All @@ -85,13 +85,13 @@ describe('cacheContext option', () => {

const cacheLoaderCallsData = buildCacheLoaderCallsData(
mockCacheLoaderWriteFn.mock.calls
).sort(sortData);
);

expect(
cacheLoaderCallsData.every(
(call) => call.remainingRequest === normalizePath(call.remainingRequest)
)
);
).toBeTruthy();
});

it('should generate absolute paths to the project root', async () => {
Expand All @@ -100,12 +100,27 @@ describe('cacheContext option', () => {

const cacheLoaderCallsData = buildCacheLoaderCallsData(
mockCacheLoaderWriteFn.mock.calls
).sort(sortData);
);

expect(
cacheLoaderCallsData.every((call) =>
call.remainingRequest.includes(path.resolve('.'))
)
).toBeFalsy();
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
expect(stats.compilation.errors).toMatchSnapshot('errors');
});

it('should load as a raw loader to support images', async () => {
const testId = './img/index.js';
const stats = await webpack(testId, mockBaseWebpackConfig);

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

expect(
cacheLoaderCallsData.every((call) => Buffer.isBuffer(call.result[0]))
);
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
expect(stats.compilation.errors).toMatchSnapshot('errors');
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/img/index.js
@@ -0,0 +1,4 @@
/* eslint-disable */
import png from '../img/webpack_logo.png';

console.log(png);
Binary file added test/fixtures/img/webpack_logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.