Repository: webpack-contrib/source-map-loader
Pull request #148 (2021-05-17 13:16:22)

Fix null source content ignoring on processing source map
<!--
  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
#147 

If you will fix it on your own, feel free to use any from this PR and close it


alexander-akait: Thanks
alexander-akait: I will fix other error, not related to this fix

Patch:
diff --git a/src/index.js b/src/index.js
index d3fe4a6..fa03809 100644
--- a/src/index.js
+++ b/src/index.js
@@ -94,7 +94,9 @@ export default async function loader(input, inputMap) {
       let sourceContent;
 
       const originalSourceContent =
-        map.sourcesContent && typeof map.sourcesContent[i] !== "undefined"
+        map.sourcesContent &&
+        typeof map.sourcesContent[i] !== "undefined" &&
+        map.sourcesContent[i] !== null
           ? map.sourcesContent[i]
           : // eslint-disable-next-line no-undefined
             undefined;


Test patch:
diff --git a/test/__snapshots__/loader.test.js.snap b/test/__snapshots__/loader.test.js.snap
index ef9ec67..3ba1991 100644
--- a/test/__snapshots__/loader.test.js.snap
+++ b/test/__snapshots__/loader.test.js.snap
@@ -334,6 +334,32 @@ Object {
 
 exports[`source-map-loader should process inlined sources: warnings 1`] = `Array []`;
 
+exports[`source-map-loader should process null in sources content: css 1`] = `
+"with SourceMap
+// comment
+"
+`;
+
+exports[`source-map-loader should process null in sources content: errors 1`] = `Array []`;
+
+exports[`source-map-loader should process null in sources content: map 1`] = `
+Object {
+  "file": "null-in-sources-content.js",
+  "mappings": "AAAA",
+  "sources": Array [
+    "/test/fixtures/null-in-sources-content.txt - (normalized for test)",
+  ],
+  "sourcesContent": Array [
+    "with SourceMap
+// comment
+",
+  ],
+  "version": 3,
+}
+`;
+
+exports[`source-map-loader should process null in sources content: warnings 1`] = `Array []`;
+
 exports[`source-map-loader should process percent-encoding path: css 1`] = `
 "with SourceMap
 // comment
diff --git a/test/fixtures/null-in-sources-content.js b/test/fixtures/null-in-sources-content.js
new file mode 100644
index 0000000..1f007b5
--- /dev/null
+++ b/test/fixtures/null-in-sources-content.js
@@ -0,0 +1,3 @@
+with SourceMap
+// #sourceMappingURL=null-in-sources-content.js.map
+// comment
diff --git a/test/fixtures/null-in-sources-content.js.map b/test/fixtures/null-in-sources-content.js.map
new file mode 100644
index 0000000..d51134b
--- /dev/null
+++ b/test/fixtures/null-in-sources-content.js.map
@@ -0,0 +1,1 @@
+{"version":3,"file":"null-in-sources-content.js","sources":["null-in-sources-content.txt"],"sourcesContent":[null],"mappings":"AAAA"}
diff --git a/test/fixtures/null-in-sources-content.txt b/test/fixtures/null-in-sources-content.txt
new file mode 100644
index 0000000..345c125
--- /dev/null
+++ b/test/fixtures/null-in-sources-content.txt
@@ -0,0 +1,2 @@
+with SourceMap
+// comment
diff --git a/test/loader.test.js b/test/loader.test.js
index f484636..44d4927 100644
--- a/test/loader.test.js
+++ b/test/loader.test.js
@@ -107,6 +107,27 @@ describe("source-map-loader", () => {
     expect(getErrors(stats)).toMatchSnapshot("errors");
   });
 
+  it("should process null in sources content", async () => {
+    const testId = "null-in-sources-content.js";
+    const compiler = getCompiler(testId);
+    const stats = await compile(compiler);
+    const codeFromBundle = getCodeFromBundle(stats, compiler);
+    const deps = stats.compilation.fileDependencies;
+
+    const dependencies = [
+      path.resolve(__dirname, "fixtures", "null-in-sources-content.js.map"),
+    ];
+
+    dependencies.forEach((fixture) => {
+      expect(deps.has(fixture)).toBe(true);
+    });
+    expect(codeFromBundle.map).toBeDefined();
+    expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot("map");
+    expect(codeFromBundle.css).toMatchSnapshot("css");
+    expect(getWarnings(stats)).toMatchSnapshot("warnings");
+    expect(getErrors(stats)).toMatchSnapshot("errors");
+  });
+
   it("should reject http SourceMaps", async () => {
     const testId = "http-source-map.js";
     const compiler = getCompiler(testId);