Skip to content

Commit 9c5126c

Browse files
feat: support es modules for assets loader (#984)
1 parent 2ac79c3 commit 9c5126c

File tree

5 files changed

+93
-0
lines changed

5 files changed

+93
-0
lines changed

src/runtime/getUrl.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
module.exports = (url, needQuotes) => {
2+
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
3+
url = url.__esModule ? url.default : url;
4+
25
if (typeof url !== 'string') {
36
return url;
47
}

test/__snapshots__/loader.test.js.snap

+12
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ exports[`loader should compile with \`css\` entry point (with \`modules\` and sc
103103
104104
exports[`loader should compile with \`css\` entry point (with \`modules\` and scope \`global\`): escape 1`] = `
105105
"module.exports = (url, needQuotes) => {
106+
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
107+
url = url.__esModule ? url.default : url;
108+
106109
if (typeof url !== 'string') {
107110
return url;
108111
}
@@ -393,6 +396,9 @@ exports[`loader should compile with \`css\` entry point (with \`modules\` and sc
393396
394397
exports[`loader should compile with \`css\` entry point (with \`modules\` and scope \`local\`): escape 1`] = `
395398
"module.exports = (url, needQuotes) => {
399+
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
400+
url = url.__esModule ? url.default : url;
401+
396402
if (typeof url !== 'string') {
397403
return url;
398404
}
@@ -707,6 +713,9 @@ exports[`loader should compile with \`css\` entry point: errors 1`] = `Array []`
707713
708714
exports[`loader should compile with \`css\` entry point: escape 1`] = `
709715
"module.exports = (url, needQuotes) => {
716+
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
717+
url = url.__esModule ? url.default : url;
718+
710719
if (typeof url !== 'string') {
711720
return url;
712721
}
@@ -997,6 +1006,9 @@ exports[`loader should compile with \`js\` entry point: errors 1`] = `Array []`;
9971006
9981007
exports[`loader should compile with \`js\` entry point: escape 1`] = `
9991008
"module.exports = (url, needQuotes) => {
1009+
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
1010+
url = url.__esModule ? url.default : url;
1011+
10001012
if (typeof url !== 'string') {
10011013
return url;
10021014
}

test/runtime/__snapshots__/getUrl.test.js.snap

+22
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,25 @@ exports[`escape should escape url 10`] = `"\\"image.png\\""`;
2323
exports[`escape should escape url 11`] = `"\\"image other.png\\""`;
2424

2525
exports[`escape should escape url 12`] = `"\\"image other.png\\""`;
26+
27+
exports[`escape should escape url 13`] = `"image.png"`;
28+
29+
exports[`escape should escape url 14`] = `"image.png"`;
30+
31+
exports[`escape should escape url 15`] = `"image.png"`;
32+
33+
exports[`escape should escape url 16`] = `"\\"image other.png\\""`;
34+
35+
exports[`escape should escape url 17`] = `"\\"image other.png\\""`;
36+
37+
exports[`escape should escape url 18`] = `"\\"image other.png\\""`;
38+
39+
exports[`escape should escape url 19`] = `"\\"image\\\\\\"other.png\\""`;
40+
41+
exports[`escape should escape url 20`] = `"\\"image\\\\nother.png\\""`;
42+
43+
exports[`escape should escape url 21`] = `"\\"image.png\\""`;
44+
45+
exports[`escape should escape url 22`] = `"\\"image.png\\""`;
46+
47+
exports[`escape should escape url 23`] = `"\\"image.png\\""`;

test/runtime/api.test.js

+21
Original file line numberDiff line numberDiff line change
@@ -21,46 +21,62 @@ describe('api', () => {
2121

2222
it('should toString a single module', () => {
2323
const m = api();
24+
2425
m.push([1, 'body { a: 1; }', '']);
26+
2527
expect(m.toString()).toMatchSnapshot();
2628
});
29+
2730
it('should toString multiple modules', () => {
2831
const m = api();
32+
2933
m.push([2, 'body { b: 2; }', '']);
3034
m.push([1, 'body { a: 1; }', '']);
35+
3136
expect(m.toString()).toMatchSnapshot();
3237
});
38+
3339
it('should toString with media query', () => {
3440
const m = api();
41+
3542
m.push([1, 'body { a: 1; }', 'screen']);
43+
3644
expect(m.toString()).toMatchSnapshot();
3745
});
46+
3847
it('should import modules', () => {
3948
const m = api();
4049
const m1 = [1, 'body { a: 1; }', 'screen'];
4150
const m2 = [2, 'body { b: 2; }', ''];
4251
const m3 = [3, 'body { c: 3; }', ''];
4352
const m4 = [4, 'body { d: 4; }', ''];
53+
4454
m.i([m2, m3], '');
4555
m.i([m2], '');
4656
m.i([m2, m4], 'print');
4757
m.push(m1);
58+
4859
expect(m.toString()).toMatchSnapshot();
4960
});
61+
5062
it('should import named modules', () => {
5163
const m = api();
5264
const m1 = ['./module1', 'body { a: 1; }', 'screen'];
5365
const m2 = ['./module2', 'body { b: 2; }', ''];
5466
const m3 = ['./module3', 'body { c: 3; }', ''];
5567
const m4 = ['./module4', 'body { d: 4; }', ''];
68+
5669
m.i([m2, m3], '');
5770
m.i([m2], '');
5871
m.i([m2, m4], 'print');
5972
m.push(m1);
73+
6074
expect(m.toString()).toMatchSnapshot();
6175
});
76+
6277
it('should toString with source mapping', () => {
6378
const m = api(true);
79+
6480
m.push([
6581
1,
6682
'body { a: 1; }',
@@ -72,11 +88,15 @@ describe('api', () => {
7288
sourceRoot: 'webpack://',
7389
},
7490
]);
91+
7592
expect(m.toString()).toMatchSnapshot();
7693
});
94+
7795
it('should toString without source mapping if btoa not avalibale', () => {
7896
global.btoa = null;
97+
7998
const m = api(true);
99+
80100
m.push([
81101
1,
82102
'body { a: 1; }',
@@ -88,6 +108,7 @@ describe('api', () => {
88108
sourceRoot: 'webpack://',
89109
},
90110
]);
111+
91112
expect(m.toString()).toMatchSnapshot();
92113
});
93114
});

test/runtime/getUrl.test.js

+35
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,40 @@ describe('escape', () => {
1515
expect(getUrl('image.png', true)).toMatchSnapshot();
1616
expect(getUrl("'image other.png'", true)).toMatchSnapshot();
1717
expect(getUrl('"image other.png"', true)).toMatchSnapshot();
18+
19+
expect(
20+
getUrl({ default: 'image.png', __esModule: true })
21+
).toMatchSnapshot();
22+
expect(
23+
getUrl({ default: "'image.png'", __esModule: true })
24+
).toMatchSnapshot();
25+
expect(
26+
getUrl({ default: '"image.png"', __esModule: true })
27+
).toMatchSnapshot();
28+
expect(
29+
getUrl({ default: 'image other.png', __esModule: true })
30+
).toMatchSnapshot();
31+
expect(
32+
getUrl({ default: '"image other.png"', __esModule: true })
33+
).toMatchSnapshot();
34+
expect(
35+
getUrl({ default: "'image other.png'", __esModule: true })
36+
).toMatchSnapshot();
37+
expect(
38+
getUrl({ default: 'image"other.png', __esModule: true })
39+
).toMatchSnapshot();
40+
expect(
41+
getUrl({ default: 'image\nother.png', __esModule: true })
42+
).toMatchSnapshot();
43+
44+
expect(
45+
getUrl({ default: 'image.png', __esModule: true }, true)
46+
).toMatchSnapshot();
47+
expect(
48+
getUrl({ default: "'image.png'", __esModule: true }, true)
49+
).toMatchSnapshot();
50+
expect(
51+
getUrl({ default: '"image.png"', __esModule: true }, true)
52+
).toMatchSnapshot();
1853
});
1954
});

0 commit comments

Comments
 (0)