From 3e3460c6a0c5ab298576d541bfec7f2e16c6951f Mon Sep 17 00:00:00 2001 From: cap-Bernardito Date: Tue, 26 May 2020 12:38:26 +0300 Subject: [PATCH] test: coverage --- .gitignore | 9 ++- src/utils.js | 3 +- test/__snapshots__/loader.test.js.snap | 56 +++++++++++++++ test/fixtures/indexed-sourcemap/file2.js | 2 + test/fixtures/indexed-sourcemap/file2.js.map | 34 +++++++++ test/loader.test.js | 72 +++++++++++++++++++- 6 files changed, 173 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/indexed-sourcemap/file2.js create mode 100644 test/fixtures/indexed-sourcemap/file2.js.map 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; 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 = {