Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support es modules for assets loader #984

Merged
merged 1 commit into from Aug 6, 2019
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
3 changes: 3 additions & 0 deletions src/runtime/getUrl.js
@@ -1,4 +1,7 @@
module.exports = (url, needQuotes) => {
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
url = url.__esModule ? url.default : url;

if (typeof url !== 'string') {
return url;
}
Expand Down
12 changes: 12 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Expand Up @@ -103,6 +103,9 @@ exports[`loader should compile with \`css\` entry point (with \`modules\` and sc

exports[`loader should compile with \`css\` entry point (with \`modules\` and scope \`global\`): escape 1`] = `
"module.exports = (url, needQuotes) => {
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
url = url.__esModule ? url.default : url;

if (typeof url !== 'string') {
return url;
}
Expand Down Expand Up @@ -393,6 +396,9 @@ exports[`loader should compile with \`css\` entry point (with \`modules\` and sc

exports[`loader should compile with \`css\` entry point (with \`modules\` and scope \`local\`): escape 1`] = `
"module.exports = (url, needQuotes) => {
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
url = url.__esModule ? url.default : url;

if (typeof url !== 'string') {
return url;
}
Expand Down Expand Up @@ -707,6 +713,9 @@ exports[`loader should compile with \`css\` entry point: errors 1`] = `Array []`

exports[`loader should compile with \`css\` entry point: escape 1`] = `
"module.exports = (url, needQuotes) => {
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
url = url.__esModule ? url.default : url;

if (typeof url !== 'string') {
return url;
}
Expand Down Expand Up @@ -997,6 +1006,9 @@ exports[`loader should compile with \`js\` entry point: errors 1`] = `Array []`;

exports[`loader should compile with \`js\` entry point: escape 1`] = `
"module.exports = (url, needQuotes) => {
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
url = url.__esModule ? url.default : url;

if (typeof url !== 'string') {
return url;
}
Expand Down
22 changes: 22 additions & 0 deletions test/runtime/__snapshots__/getUrl.test.js.snap
Expand Up @@ -23,3 +23,25 @@ exports[`escape should escape url 10`] = `"\\"image.png\\""`;
exports[`escape should escape url 11`] = `"\\"image other.png\\""`;

exports[`escape should escape url 12`] = `"\\"image other.png\\""`;

exports[`escape should escape url 13`] = `"image.png"`;

exports[`escape should escape url 14`] = `"image.png"`;

exports[`escape should escape url 15`] = `"image.png"`;

exports[`escape should escape url 16`] = `"\\"image other.png\\""`;

exports[`escape should escape url 17`] = `"\\"image other.png\\""`;

exports[`escape should escape url 18`] = `"\\"image other.png\\""`;

exports[`escape should escape url 19`] = `"\\"image\\\\\\"other.png\\""`;

exports[`escape should escape url 20`] = `"\\"image\\\\nother.png\\""`;

exports[`escape should escape url 21`] = `"\\"image.png\\""`;

exports[`escape should escape url 22`] = `"\\"image.png\\""`;

exports[`escape should escape url 23`] = `"\\"image.png\\""`;
21 changes: 21 additions & 0 deletions test/runtime/api.test.js
Expand Up @@ -21,46 +21,62 @@ describe('api', () => {

it('should toString a single module', () => {
const m = api();

m.push([1, 'body { a: 1; }', '']);

expect(m.toString()).toMatchSnapshot();
});

it('should toString multiple modules', () => {
const m = api();

m.push([2, 'body { b: 2; }', '']);
m.push([1, 'body { a: 1; }', '']);

expect(m.toString()).toMatchSnapshot();
});

it('should toString with media query', () => {
const m = api();

m.push([1, 'body { a: 1; }', 'screen']);

expect(m.toString()).toMatchSnapshot();
});

it('should import modules', () => {
const m = api();
const m1 = [1, 'body { a: 1; }', 'screen'];
const m2 = [2, 'body { b: 2; }', ''];
const m3 = [3, 'body { c: 3; }', ''];
const m4 = [4, 'body { d: 4; }', ''];

m.i([m2, m3], '');
m.i([m2], '');
m.i([m2, m4], 'print');
m.push(m1);

expect(m.toString()).toMatchSnapshot();
});

it('should import named modules', () => {
const m = api();
const m1 = ['./module1', 'body { a: 1; }', 'screen'];
const m2 = ['./module2', 'body { b: 2; }', ''];
const m3 = ['./module3', 'body { c: 3; }', ''];
const m4 = ['./module4', 'body { d: 4; }', ''];

m.i([m2, m3], '');
m.i([m2], '');
m.i([m2, m4], 'print');
m.push(m1);

expect(m.toString()).toMatchSnapshot();
});

it('should toString with source mapping', () => {
const m = api(true);

m.push([
1,
'body { a: 1; }',
Expand All @@ -72,11 +88,15 @@ describe('api', () => {
sourceRoot: 'webpack://',
},
]);

expect(m.toString()).toMatchSnapshot();
});

it('should toString without source mapping if btoa not avalibale', () => {
global.btoa = null;

const m = api(true);

m.push([
1,
'body { a: 1; }',
Expand All @@ -88,6 +108,7 @@ describe('api', () => {
sourceRoot: 'webpack://',
},
]);

expect(m.toString()).toMatchSnapshot();
});
});
35 changes: 35 additions & 0 deletions test/runtime/getUrl.test.js
Expand Up @@ -15,5 +15,40 @@ describe('escape', () => {
expect(getUrl('image.png', true)).toMatchSnapshot();
expect(getUrl("'image other.png'", true)).toMatchSnapshot();
expect(getUrl('"image other.png"', true)).toMatchSnapshot();

expect(
getUrl({ default: 'image.png', __esModule: true })
).toMatchSnapshot();
expect(
getUrl({ default: "'image.png'", __esModule: true })
).toMatchSnapshot();
expect(
getUrl({ default: '"image.png"', __esModule: true })
).toMatchSnapshot();
expect(
getUrl({ default: 'image other.png', __esModule: true })
).toMatchSnapshot();
expect(
getUrl({ default: '"image other.png"', __esModule: true })
).toMatchSnapshot();
expect(
getUrl({ default: "'image other.png'", __esModule: true })
).toMatchSnapshot();
expect(
getUrl({ default: 'image"other.png', __esModule: true })
).toMatchSnapshot();
expect(
getUrl({ default: 'image\nother.png', __esModule: true })
).toMatchSnapshot();

expect(
getUrl({ default: 'image.png', __esModule: true }, true)
).toMatchSnapshot();
expect(
getUrl({ default: "'image.png'", __esModule: true }, true)
).toMatchSnapshot();
expect(
getUrl({ default: '"image.png"', __esModule: true }, true)
).toMatchSnapshot();
});
});