Repository: webpack-contrib/css-loader (js)

Issue:
cssWithMappingToString() throw exception when loading css with @import url(https://...)
<!--
  Issues are so 🔥

  If you remove or skip this template, you'll make the 🐼 sad and the mighty god
  of Github will appear and pile-drive the close button from a great height
  while making animal noises.

  👉🏽 Need support, advice, or help? Don't open an issue!
  Head to StackOverflow or https://gitter.im/webpack/webpack.
-->

- Operating System: Windows 10
- Node Version: 16.4.2
- NPM Version: 7.18.1
- webpack Version: 5.43.0
- css-loader Version: 5.2.6

### Expected Behavior
I would expect CSS files with @import url(https://...) to load properly.

### Actual Behavior
CSS files with @import url(https://...) fail to load.  The method cssWithMappingToString() is throwing an exception because the @import entries don't have a cssMapping.  The previous version of this loader, for 4.X.X of webpack, included the following code block.  This doesn't exist in the current version.  I can submit a PR for this, however this seems deliberate, so I suspect there's something else going on.

### Code

```js
  if (!cssMapping) {
    return content;
  }
```
### How Do We Reproduce?
Add the following line to a css file. 

```css
@import url(https://fonts.googleapis.com/css?family=Open+Sans&display=swap);
```

Pull request description:
fix: issue 1322
<!--
  HOLY CRAP a Pull Request. We ❤️ those!

  If you remove or skip this template, you'll make the 🐼 sad and the mighty god
  of Github will appear and pile-drive the close button from a great height
  while making animal noises.

  Please place an x (no spaces!) in all [ ] that apply
-->

This PR contains a:

- [x] **bugfix**
- [ ] new **feature**
- [ ] **code refactor**
- [x] **test update** <!-- if bug or feature is checked, this should be too -->
- [ ] **typo fix**
- [ ] **metadata update**

### Motivation / Use-Case

Fixes #1322 

### Breaking Changes

No

### Additional Info

No


Patch:
diff --git a/src/runtime/cssWithMappingToString.js b/src/runtime/cssWithMappingToString.js
index 2a7fda6..e6c7603 100644
--- a/src/runtime/cssWithMappingToString.js
+++ b/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(


Test patch:
diff --git a/test/runtime/__snapshots__/api.test.js.snap b/test/runtime/__snapshots__/api.test.js.snap
index 3304a23..03a5529 100644
--- a/test/runtime/__snapshots__/api.test.js.snap
+++ b/test/runtime/__snapshots__/api.test.js.snap
@@ -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`] = `
diff --git a/test/runtime/api.test.js b/test/runtime/api.test.js
index 7c1bbaf..337ddee 100644
--- a/test/runtime/api.test.js
+++ b/test/runtime/api.test.js
@@ -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]);
 
diff --git a/test/sourceMap-option.test.js b/test/sourceMap-option.test.js
index 635e7fc..3d0c695 100644
--- a/test/sourceMap-option.test.js
+++ b/test/sourceMap-option.test.js
@@ -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)