Repository: webpack-contrib/source-map-loader
Pull request #111 (2020-05-26 09:39:18)

test: coverage
<!--
  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:

- [ ] **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

Tests

### Breaking Changes

No

### Additional Info

No

codecov[bot]: # [Codecov](https://codecov.io/gh/webpack-contrib/source-map-loader/pull/111?src=pr&el=h1) Report
> Merging [#111](https://codecov.io/gh/webpack-contrib/source-map-loader/pull/111?src=pr&el=desc) into [master](https://codecov.io/gh/webpack-contrib/source-map-loader/commit/0d77b18d6411f0ba3b987e9c52c310e80631b48d&el=desc) will **increase** coverage by `3.96%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/webpack-contrib/source-map-loader/pull/111/graphs/tree.svg?width=650&height=150&src=pr&token=yyUOoO5dbI)](https://codecov.io/gh/webpack-contrib/source-map-loader/pull/111?src=pr&el=tree)

```diff
@@             Coverage Diff             @@
##           master      #111      +/-   ##
===========================================
+ Coverage   96.03%   100.00%   +3.96%     
===========================================
  Files           3         3              
  Lines         126       126              
  Branches       33        33              
===========================================
+ Hits          121       126       +5     
+ Misses          4         0       -4     
+ Partials        1         0       -1     
```


| [Impacted Files](https://codecov.io/gh/webpack-contrib/source-map-loader/pull/111?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/utils.js](https://codecov.io/gh/webpack-contrib/source-map-loader/pull/111/diff?src=pr&el=tree#diff-c3JjL3V0aWxzLmpz) | `100.00% <100.00%> (+6.84%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/webpack-contrib/source-map-loader/pull/111?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/webpack-contrib/source-map-loader/pull/111?src=pr&el=footer). Last update [0d77b18...3e3460c](https://codecov.io/gh/webpack-contrib/source-map-loader/pull/111?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/.gitignore b/.gitignore
index cb9b7d9..05252a1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,12 @@
 /node_modules
 test/output/*
+test/fixtures/absolute-path.js
+test/fixtures/absolute-sourceRoot-source-map.map
+test/fixtures/file-protocol-path.js
+test/fixtures/file-protocol-path.js.map
+test/fixtures/map-with-sourceroot.js.map
+test/fixtures/map-without-sourceroot.js.map
+test/fixtures/normal-map.js.map
 logs
 *.log
 npm-debug.log*
@@ -14,4 +21,4 @@ Thumbs.db
 *.iml
 .vscode
 *.sublime-project
-*.sublime-workspace
\ No newline at end of file
+*.sublime-workspace
diff --git a/src/utils.js b/src/utils.js
index fe105d7..aa1336c 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -151,7 +151,8 @@ async function fetchFromURL(
 
     if (protocol === 'file:') {
       const pathFromURL = urlUtils.fileURLToPath(url);
-      const sourceURL = getAbsolutePath(context, pathFromURL, sourceRoot);
+
+      const sourceURL = path.normalize(pathFromURL);
 
       let sourceContent;
 


Test patch:
diff --git a/test/__snapshots__/loader.test.js.snap b/test/__snapshots__/loader.test.js.snap
index 345920e..7ece89b 100644
--- a/test/__snapshots__/loader.test.js.snap
+++ b/test/__snapshots__/loader.test.js.snap
@@ -361,6 +361,62 @@ Object {
 
 exports[`source-map-loader should support absolute sourceRoot paths in sourcemaps: warnings 1`] = `Array []`;
 
+exports[`source-map-loader should support file protocol path: css 1`] = `
+"// Some content 
+ "
+`;
+
+exports[`source-map-loader should support file protocol path: errors 1`] = `Array []`;
+
+exports[`source-map-loader should support file protocol path: map 1`] = `
+Object {
+  "mappings": "CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOA",
+  "sources": Array [
+    "/test/fixtures/normal-file.js - (normalized for test)",
+    "/test/fixtures/normal-file2.js - (normalized for test)",
+  ],
+  "sourcesContent": Array [
+    "without SourceMap",
+    "// without SourceMap
+anInvalidDirective = \\"\\\\n/*# sourceMappingURL=data:application/json;base64,\\"+btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap))))+\\" */\\";
+// comment
+",
+  ],
+  "version": 3,
+}
+`;
+
+exports[`source-map-loader should support file protocol path: warnings 1`] = `Array []`;
+
+exports[`source-map-loader should support indexed sourcemaps 2: css 1`] = `
+"console.log('with SourceMap')
+"
+`;
+
+exports[`source-map-loader should support indexed sourcemaps 2: errors 1`] = `Array []`;
+
+exports[`source-map-loader should support indexed sourcemaps 2: map 1`] = `
+Object {
+  "mappings": "CAAC,IAAI,IAAM,SAAU,GAClB,OAAO,IAAI;CCDb,IAAI,IAAM,SAAU,GAClB,OAAO",
+  "names": Array [],
+  "sources": Array [
+    "/test/fixtures/indexed-sourcemap/nested1.js - (normalized for test)",
+    "/different/root/nested2.js",
+  ],
+  "sourcesContent": Array [
+    " ONE.foo = function (bar) {
+   return baz(bar);
+ };",
+    " TWO.inc = function (n) {
+   return n + 1;
+ };",
+  ],
+  "version": 3,
+}
+`;
+
+exports[`source-map-loader should support indexed sourcemaps 2: warnings 1`] = `Array []`;
+
 exports[`source-map-loader should support indexed sourcemaps: css 1`] = `
 "console.log('with SourceMap')
 // Map taken from here
diff --git a/test/fixtures/indexed-sourcemap/file2.js b/test/fixtures/indexed-sourcemap/file2.js
new file mode 100644
index 0000000..14962e4
--- /dev/null
+++ b/test/fixtures/indexed-sourcemap/file2.js
@@ -0,0 +1,2 @@
+console.log('with SourceMap')
+//#sourceMappingURL=file2.js.map
diff --git a/test/fixtures/indexed-sourcemap/file2.js.map b/test/fixtures/indexed-sourcemap/file2.js.map
new file mode 100644
index 0000000..031a2b5
--- /dev/null
+++ b/test/fixtures/indexed-sourcemap/file2.js.map
@@ -0,0 +1,34 @@
+{
+  "version": 3,
+  "sections": [
+    {
+      "offset": {
+        "line": 0,
+        "column": 0
+      },
+      "map": {
+        "version": 3,
+        "sources": ["nested1.js"],
+        "sourcesContent": [
+          " ONE.foo = function (bar) {\n   return baz(bar);\n };"
+        ],
+        "names": ["bar", "baz"],
+        "mappings": "CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID"
+      }
+    },
+    {
+      "offset": {
+        "line": 1,
+        "column": 0
+      },
+      "map": {
+        "version": 3,
+        "sources": ["nested2.js"],
+        "sourcesContent": [" TWO.inc = function (n) {\n   return n + 1;\n };"],
+        "names": ["n"],
+        "mappings": "CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOA",
+        "sourceRoot": "/different/root"
+      }
+    }
+  ]
+}
diff --git a/test/loader.test.js b/test/loader.test.js
index 2daf5f8..338ec03 100644
--- a/test/loader.test.js
+++ b/test/loader.test.js
@@ -117,6 +117,43 @@ describe('source-map-loader', () => {
     expect(getErrors(stats)).toMatchSnapshot('errors');
   });
 
+  it('should support file protocol path', async () => {
+    const sourceRoot = path.resolve(__dirname, 'fixtures');
+    const javaScriptFilename = 'file-protocol-path.js';
+    const entryFileAbsolutePath = path.join(sourceRoot, javaScriptFilename);
+    const sourceMapPath = path.join(sourceRoot, 'file-protocol-path.js.map');
+
+    // Create the sourcemap file
+    const rawSourceMap = {
+      version: 3,
+      sources: [
+        'normal-file.js',
+        `file://${path
+          .resolve(__dirname, 'fixtures', 'normal-file2.js')
+          .replace(/\\/g, '/')}`,
+      ],
+      mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOA',
+    };
+    fs.writeFileSync(sourceMapPath, JSON.stringify(rawSourceMap));
+
+    // Create the entryPointFile file
+    const entryFileContent = `// Some content \r\n // # sourceMappingURL=file://${sourceMapPath.replace(
+      /\\/g,
+      '/'
+    )}`;
+    fs.writeFileSync(entryFileAbsolutePath, entryFileContent);
+
+    const compiler = getCompiler(javaScriptFilename);
+    const stats = await compile(compiler);
+    const codeFromBundle = getCodeFromBundle(stats, compiler);
+
+    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 use last SourceMap directive', async () => {
     const testId = 'multi-source-map.js';
     const compiler = getCompiler(testId);
@@ -319,6 +356,36 @@ describe('source-map-loader', () => {
     expect(getErrors(stats)).toMatchSnapshot('errors');
   });
 
+  it('should support indexed sourcemaps 2', async () => {
+    const currentDirPath = path.join(
+      __dirname,
+      'fixtures',
+      'indexed-sourcemap'
+    );
+
+    const testId = path.join(currentDirPath, 'file2.js');
+    const compiler = getCompiler(testId);
+    const stats = await compile(compiler);
+    const codeFromBundle = getCodeFromBundle(stats, compiler);
+    const deps = stats.compilation.fileDependencies;
+
+    const dependencies = [
+      path.join(currentDirPath, 'file2.js'),
+      path.join(currentDirPath, 'file2.js.map'),
+      path.join(currentDirPath, 'nested1.js'),
+      path.normalize(`/different/root/nested2.js`),
+    ];
+
+    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 transform to webpack', async () => {
     const currentDirPath = path.join(
       __dirname,
@@ -359,7 +426,10 @@ describe('source-map-loader', () => {
     const sourceRoot = path.resolve(__dirname, 'fixtures');
     const javaScriptFilename = 'absolute-path.js';
     const entryFileAbsolutePath = path.join(sourceRoot, javaScriptFilename);
-    const sourceMapPath = path.join(sourceRoot, 'map-with-sourceroot.js.map');
+    const sourceMapPath = path.join(
+      sourceRoot,
+      'map-without-sourceroot.js.map'
+    );
 
     // Create the sourcemap file
     const rawSourceMap = {