Skip to content

Commit

Permalink
fix: fix crash when source map is unavailable with external URL in `@…
Browse files Browse the repository at this point in the history
…import`
  • Loading branch information
cap-Bernardito committed Jul 13, 2021
1 parent adcc438 commit bb76fe4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/runtime/cssWithMappingToString.js
@@ -1,6 +1,10 @@
module.exports = function cssWithMappingToString(item) {
const [, content, , cssMapping] = item;

if (!cssMapping) {
return content;
}

if (typeof btoa === "function") {
// eslint-disable-next-line no-undef
const base64 = btoa(
Expand Down
2 changes: 2 additions & 0 deletions test/runtime/__snapshots__/api.test.js.snap
Expand Up @@ -18,6 +18,8 @@ exports[`api should toString with a source map without "sourceRoot" 1`] = `
/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC5zY3NzIiwic291cmNlcyI6WyIuL3BhdGgvdG8vdGVzdC5zY3NzIl0sIm1hcHBpbmdzIjoiQUFBQTsifQ== */"
`;

exports[`api should toString with a source map without map 1`] = `"@import url('https://fonts.googleapis.com/css?family=Open+Sans&display=swap');"`;

exports[`api should toString with media query 1`] = `"body { b: 2; }body { c: 3; }body { b: 2; }@media print {body { b: 2; }}@media print {body { d: 4; }}@media screen {body { a: 1; }}"`;

exports[`api should toString with source mapping 1`] = `
Expand Down
12 changes: 12 additions & 0 deletions test/runtime/api.test.js
Expand Up @@ -145,6 +145,18 @@ describe("api", () => {
expect(m.toString()).toMatchSnapshot();
});

// https://github.com/webpack-contrib/css-loader/issues/1322
it("should toString with a source map without map", () => {
const m = api(cssWithMappingToString);

m.push([
1,
"@import url('https://fonts.googleapis.com/css?family=Open+Sans&display=swap');",
]);

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

it("should import modules with dedupe", () => {
const m = api((i) => i[1]);

Expand Down
4 changes: 2 additions & 2 deletions test/sourceMap-option.test.js
Expand Up @@ -501,8 +501,8 @@ describe('"sourceMap" option', () => {

expect(chunkName).toBe(
webpack.version[0] === "5"
? "main.6480a869998e0b381c90.bundle.js"
: "main.19efc497c5c37fc5e355.bundle.js"
? "main.b67984bbf9dd298785fb.bundle.js"
: "main.a64c858ed0f4c1ed322c.bundle.js"
);
expect(
getModuleSource("fixtures/source-map/basic.css", stats)
Expand Down

0 comments on commit bb76fe4

Please sign in to comment.