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

Commit 4924341

Browse files
misticevilebottnawi
authored andcommitted
fix: usage with raw loaders (#69)
1 parent 74037ce commit 4924341

File tree

8 files changed

+130
-74
lines changed

8 files changed

+130
-74
lines changed

package-lock.json

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"webpack": "^4.0.0"
3939
},
4040
"dependencies": {
41+
"buffer-json": "^2.0.0",
4142
"find-cache-dir": "^2.1.0",
4243
"loader-utils": "^1.1.0",
4344
"mkdirp": "^0.5.1",
@@ -64,6 +65,7 @@
6465
"eslint-config-webpack": "^1.0.0",
6566
"eslint-plugin-import": "^2.0.0",
6667
"eslint-plugin-prettier": "^3.0.0",
68+
"file-loader": "^3.0.1",
6769
"husky": "^1.2.1",
6870
"jest": "^24.5.0",
6971
"lint-staged": "^8.1.0",

src/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const async = require('neo-async');
99
const crypto = require('crypto');
1010
const mkdirp = require('mkdirp');
1111
const findCacheDir = require('find-cache-dir');
12+
const BJSON = require('buffer-json');
1213

1314
const { getOptions } = require('loader-utils');
1415
const validateOptions = require('schema-utils');
@@ -211,7 +212,7 @@ const directories = new Set();
211212

212213
function write(key, data, callback) {
213214
const dirname = path.dirname(key);
214-
const content = JSON.stringify(data);
215+
const content = BJSON.stringify(data);
215216

216217
if (directories.has(dirname)) {
217218
// for performance skip creating directory
@@ -238,7 +239,7 @@ function read(key, callback) {
238239
}
239240

240241
try {
241-
const data = JSON.parse(content);
242+
const data = BJSON.parse(content);
242243
callback(null, data);
243244
} catch (e) {
244245
callback(e);
@@ -253,4 +254,5 @@ function cacheKey(options, request) {
253254
return path.join(cacheDirectory, `${hash}.json`);
254255
}
255256

257+
export const raw = true;
256258
export { loader as default, pitch };

test/__snapshots__/cacheContext-option.test.js.snap

+58-54
Original file line numberDiff line numberDiff line change
@@ -7,68 +7,72 @@ exports[`cacheContext option should generate absolute paths to the project root:
77
exports[`cacheContext option should generate relative paths to the project root: errors 1`] = `Array []`;
88

99
exports[`cacheContext option should generate relative paths to the project root: generated cache-loader data 1`] = `
10-
Array [
11-
Object {
12-
"contextDependencies": Array [],
13-
"dependencies": Array [
14-
Object {
15-
"mtime": null,
16-
"path": "test/fixtures/basic/file_1.js",
10+
"[
11+
{
12+
\\"remainingRequest\\": \\"test/fixtures/basic/file_1.js\\",
13+
\\"dependencies\\": [
14+
{
15+
\\"path\\": \\"test/fixtures/basic/file_1.js\\",
16+
\\"mtime\\": null
1717
},
18-
Object {
19-
"mtime": null,
20-
"path": "src/index.js",
21-
},
22-
],
23-
"remainingRequest": "test/fixtures/basic/file_1.js",
24-
"result": Array [
25-
"/* eslint-disable */
26-
console.log('file_1');
27-
",
18+
{
19+
\\"path\\": \\"src/index.js\\",
20+
\\"mtime\\": null
21+
}
2822
],
23+
\\"contextDependencies\\": [],
24+
\\"result\\": [
25+
{
26+
\\"type\\": \\"Buffer\\",
27+
\\"data\\": \\"base64:LyogZXNsaW50LWRpc2FibGUgKi8KY29uc29sZS5sb2coJ2ZpbGVfMScpOwo=\\"
28+
}
29+
]
2930
},
30-
Object {
31-
"contextDependencies": Array [],
32-
"dependencies": Array [
33-
Object {
34-
"mtime": null,
35-
"path": "test/fixtures/basic/file_2.js",
36-
},
37-
Object {
38-
"mtime": null,
39-
"path": "src/index.js",
31+
{
32+
\\"remainingRequest\\": \\"test/fixtures/basic/file_2.js\\",
33+
\\"dependencies\\": [
34+
{
35+
\\"path\\": \\"test/fixtures/basic/file_2.js\\",
36+
\\"mtime\\": null
4037
},
38+
{
39+
\\"path\\": \\"src/index.js\\",
40+
\\"mtime\\": null
41+
}
4142
],
42-
"remainingRequest": "test/fixtures/basic/file_2.js",
43-
"result": Array [
44-
"/* eslint-disable */
45-
console.log('file_2');
46-
",
47-
],
43+
\\"contextDependencies\\": [],
44+
\\"result\\": [
45+
{
46+
\\"type\\": \\"Buffer\\",
47+
\\"data\\": \\"base64:LyogZXNsaW50LWRpc2FibGUgKi8KY29uc29sZS5sb2coJ2ZpbGVfMicpOwo=\\"
48+
}
49+
]
4850
},
49-
Object {
50-
"contextDependencies": Array [],
51-
"dependencies": Array [
52-
Object {
53-
"mtime": null,
54-
"path": "test/fixtures/basic/index.js",
55-
},
56-
Object {
57-
"mtime": null,
58-
"path": "src/index.js",
51+
{
52+
\\"remainingRequest\\": \\"test/fixtures/basic/index.js\\",
53+
\\"dependencies\\": [
54+
{
55+
\\"path\\": \\"test/fixtures/basic/index.js\\",
56+
\\"mtime\\": null
5957
},
58+
{
59+
\\"path\\": \\"src/index.js\\",
60+
\\"mtime\\": null
61+
}
6062
],
61-
"remainingRequest": "test/fixtures/basic/index.js",
62-
"result": Array [
63-
"/* eslint-disable */
64-
require('./file_1.js');
65-
require('./file_2.js');
66-
67-
console.log('basic_entry');
68-
",
69-
],
70-
},
71-
]
63+
\\"contextDependencies\\": [],
64+
\\"result\\": [
65+
{
66+
\\"type\\": \\"Buffer\\",
67+
\\"data\\": \\"base64:LyogZXNsaW50LWRpc2FibGUgKi8KcmVxdWlyZSgnLi9maWxlXzEuanMnKTsKcmVxdWlyZSgnLi9maWxlXzIuanMnKTsKCmNvbnNvbGUubG9nKCdiYXNpY19lbnRyeScpOwo=\\"
68+
}
69+
]
70+
}
71+
]"
7272
`;
7373

7474
exports[`cacheContext option should generate relative paths to the project root: warnings 1`] = `Array []`;
75+
76+
exports[`cacheContext option should load as a raw loader to support images: errors 1`] = `Array []`;
77+
78+
exports[`cacheContext option should load as a raw loader to support images: warnings 1`] = `Array []`;

test/cacheContext-option.test.js

+36-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const path = require('path');
22

33
const normalizePath = require('normalize-path');
4+
const BJSON = require('buffer-json');
45

56
const { webpack } = require('./helpers');
67

@@ -27,6 +28,18 @@ const mockRelativeWebpackConfig = {
2728
},
2829
};
2930

31+
const sortData = (a, b) => {
32+
if (a.remainingRequest < b.remainingRequest) {
33+
return -1;
34+
}
35+
36+
if (a.remainingRequest > b.remainingRequest) {
37+
return 1;
38+
}
39+
40+
return 0;
41+
};
42+
3043
const buildSnapshotReadyDeps = (deps) =>
3144
deps.map((dep) => Object.assign({}, dep, { mtime: null, path: dep.path }));
3245

@@ -46,19 +59,7 @@ const buildCacheLoaderCallsData = (calls) =>
4659
});
4760
}, new Map())
4861
.values()
49-
);
50-
51-
const sortData = (a, b) => {
52-
if (a.remainingRequest < b.remainingRequest) {
53-
return -1;
54-
}
55-
56-
if (a.remainingRequest > b.remainingRequest) {
57-
return 1;
58-
}
59-
60-
return 0;
61-
};
62+
).sort(sortData);
6263

6364
describe('cacheContext option', () => {
6465
it('should generate relative paths to the project root', async () => {
@@ -67,14 +68,16 @@ describe('cacheContext option', () => {
6768

6869
const cacheLoaderCallsData = buildCacheLoaderCallsData(
6970
mockCacheLoaderWriteFn.mock.calls
70-
).sort(sortData);
71+
);
7172

7273
expect(
7374
cacheLoaderCallsData.every(
7475
(call) => !call.remainingRequest.includes(path.resolve('.'))
7576
)
77+
).toBeTruthy();
78+
expect(BJSON.stringify(cacheLoaderCallsData, 2)).toMatchSnapshot(
79+
'generated cache-loader data'
7680
);
77-
expect(cacheLoaderCallsData).toMatchSnapshot('generated cache-loader data');
7881
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
7982
expect(stats.compilation.errors).toMatchSnapshot('errors');
8083
});
@@ -85,13 +88,13 @@ describe('cacheContext option', () => {
8588

8689
const cacheLoaderCallsData = buildCacheLoaderCallsData(
8790
mockCacheLoaderWriteFn.mock.calls
88-
).sort(sortData);
91+
);
8992

9093
expect(
9194
cacheLoaderCallsData.every(
9295
(call) => call.remainingRequest === normalizePath(call.remainingRequest)
9396
)
94-
);
97+
).toBeTruthy();
9598
});
9699

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

101104
const cacheLoaderCallsData = buildCacheLoaderCallsData(
102105
mockCacheLoaderWriteFn.mock.calls
103-
).sort(sortData);
106+
);
104107

105108
expect(
106109
cacheLoaderCallsData.every((call) =>
107110
call.remainingRequest.includes(path.resolve('.'))
108111
)
112+
).toBeFalsy();
113+
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
114+
expect(stats.compilation.errors).toMatchSnapshot('errors');
115+
});
116+
117+
it('should load as a raw loader to support images', async () => {
118+
const testId = './img/index.js';
119+
const stats = await webpack(testId, mockBaseWebpackConfig);
120+
121+
const cacheLoaderCallsData = buildCacheLoaderCallsData(
122+
mockCacheLoaderWriteFn.mock.calls
123+
);
124+
125+
expect(
126+
cacheLoaderCallsData.every((call) => Buffer.isBuffer(call.result[0]))
109127
);
110128
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
111129
expect(stats.compilation.errors).toMatchSnapshot('errors');

test/fixtures/img/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* eslint-disable */
2+
import png from '../img/webpack_logo.png';
3+
4+
console.log(png);

test/fixtures/img/webpack_logo.png

121 KB
Loading

test/helpers.js

+11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const path = require('path');
2+
23
const del = require('del');
34
const webpack = require('webpack');
45
const MemoryFS = require('memory-fs');
@@ -26,6 +27,16 @@ const moduleConfig = (config) => {
2627
: []
2728
),
2829
},
30+
{
31+
test: /\.png$/,
32+
use: [
33+
{
34+
loader: path.resolve(__dirname, '../src/index.js'),
35+
options: (config.loader && config.loader.options) || {},
36+
},
37+
'file-loader',
38+
],
39+
},
2940
],
3041
};
3142
};

0 commit comments

Comments
 (0)