Skip to content

Commit bb76fe4

Browse files
fix: fix crash when source map is unavailable with external URL in @import
1 parent adcc438 commit bb76fe4

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

src/runtime/cssWithMappingToString.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
module.exports = function cssWithMappingToString(item) {
22
const [, content, , cssMapping] = item;
33

4+
if (!cssMapping) {
5+
return content;
6+
}
7+
48
if (typeof btoa === "function") {
59
// eslint-disable-next-line no-undef
610
const base64 = btoa(

test/runtime/__snapshots__/api.test.js.snap

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ exports[`api should toString with a source map without "sourceRoot" 1`] = `
1818
/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC5zY3NzIiwic291cmNlcyI6WyIuL3BhdGgvdG8vdGVzdC5zY3NzIl0sIm1hcHBpbmdzIjoiQUFBQTsifQ== */"
1919
`;
2020

21+
exports[`api should toString with a source map without map 1`] = `"@import url('https://fonts.googleapis.com/css?family=Open+Sans&display=swap');"`;
22+
2123
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; }}"`;
2224

2325
exports[`api should toString with source mapping 1`] = `

test/runtime/api.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,18 @@ describe("api", () => {
145145
expect(m.toString()).toMatchSnapshot();
146146
});
147147

148+
// https://github.com/webpack-contrib/css-loader/issues/1322
149+
it("should toString with a source map without map", () => {
150+
const m = api(cssWithMappingToString);
151+
152+
m.push([
153+
1,
154+
"@import url('https://fonts.googleapis.com/css?family=Open+Sans&display=swap');",
155+
]);
156+
157+
expect(m.toString()).toMatchSnapshot();
158+
});
159+
148160
it("should import modules with dedupe", () => {
149161
const m = api((i) => i[1]);
150162

test/sourceMap-option.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,8 @@ describe('"sourceMap" option', () => {
501501

502502
expect(chunkName).toBe(
503503
webpack.version[0] === "5"
504-
? "main.6480a869998e0b381c90.bundle.js"
505-
: "main.19efc497c5c37fc5e355.bundle.js"
504+
? "main.b67984bbf9dd298785fb.bundle.js"
505+
: "main.a64c858ed0f4c1ed322c.bundle.js"
506506
);
507507
expect(
508508
getModuleSource("fixtures/source-map/basic.css", stats)

0 commit comments

Comments
 (0)