diff --git a/test/__snapshots__/sourceMapperRegexp.test.js.snap b/test/__snapshots__/sourceMapperRegexp.test.js.snap index 8627b50..1bade89 100644 --- a/test/__snapshots__/sourceMapperRegexp.test.js.snap +++ b/test/__snapshots__/sourceMapperRegexp.test.js.snap @@ -22,6 +22,8 @@ exports[`source-map-loader should work: result 6`] = `"absolute-sourceRoot-sourc exports[`source-map-loader should work: result 7`] = `"http://sampledomain.com/external-source-map2.map"`; -exports[`source-map-loader should work: result 8`] = `"data:application/source-map;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5saW5lLXNvdXJjZS1tYXAuanMiLCJzb3VyY2VzIjpbImlubGluZS1zb3VyY2UtbWFwLnR4dCJdLCJzb3VyY2VzQ29udGVudCI6WyJ3aXRoIFNvdXJjZU1hcCJdLCJtYXBwaW5ncyI6IkFBQUEifQ=="`; +exports[`source-map-loader should work: result 8`] = `"//sampledomain.com/external-source-map2.map"`; -exports[`source-map-loader should work: result 9`] = `"/sample-source-map.map"`; +exports[`source-map-loader should work: result 9`] = `"data:application/source-map;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5saW5lLXNvdXJjZS1tYXAuanMiLCJzb3VyY2VzIjpbImlubGluZS1zb3VyY2UtbWFwLnR4dCJdLCJzb3VyY2VzQ29udGVudCI6WyJ3aXRoIFNvdXJjZU1hcCJdLCJtYXBwaW5ncyI6IkFBQUEifQ=="`; + +exports[`source-map-loader should work: result 10`] = `"/sample-source-map.map"`; diff --git a/test/cjs.test.js b/test/cjs.test.js new file mode 100644 index 0000000..8aba6ba --- /dev/null +++ b/test/cjs.test.js @@ -0,0 +1,8 @@ +import src from '../src'; +import cjs from '../src/cjs'; + +describe('cjs', () => { + it('should exported', () => { + expect(cjs).toEqual(src); + }); +}); diff --git a/test/fixtures/app.css b/test/fixtures/app.css new file mode 100644 index 0000000..ee77d0f --- /dev/null +++ b/test/fixtures/app.css @@ -0,0 +1,22 @@ +* { + box-sizing: border-box; } + +.row { + display: flex; + margin-right: -15px; + margin-left: -15px; } + +.col-inner { + display: flex; + align-items: center; + justify-content: center; + color: #fff; + height: 50px; + background: coral; } + +.col-s3 { + flex: 0 1 25%; + padding: 0 15px; } + + +/*# sourceMappingURL=app.css.map*/ \ No newline at end of file diff --git a/test/fixtures/app.css.map b/test/fixtures/app.css.map new file mode 100644 index 0000000..70f9234 --- /dev/null +++ b/test/fixtures/app.css.map @@ -0,0 +1 @@ +{"version":3,"sources":["webpack:///./src/app/app.scss"],"names":[],"mappings":"AAAA;EACE,sBAAsB;;AAGxB;EACE,aAAa;EACb,mBAAmB;EACnB,kBAAkB;;AAIlB;EACE,aAAa;EACb,mBAAmB;EACnB,uBAAuB;EACvB,WAAW;EACX,YAAY;EACZ,iBAAiB;;AAInB;EACE,aAAa;EACb,eAAe","file":"app.css","sourcesContent":["* {\n box-sizing: border-box;\n}\n\n.row {\n display: flex;\n margin-right: -15px;\n margin-left: -15px;\n}\n\n.col {\n &-inner {\n display: flex;\n align-items: center;\n justify-content: center;\n color: #fff;\n height: 50px;\n background: coral;\n //background: red;\n }\n\n &-s3 {\n flex: 0 1 25%;\n padding: 0 15px;\n }\n}\n"],"sourceRoot":""} \ No newline at end of file diff --git a/test/fixtures/inline-sources.js b/test/fixtures/inline-sources.js new file mode 100644 index 0000000..b4d450c --- /dev/null +++ b/test/fixtures/inline-sources.js @@ -0,0 +1,3 @@ +// Inline Sources in SourceMap +//#sourceMappingURL=inline-sources.js.map +// comment diff --git a/test/fixtures/inline-sources.js.map b/test/fixtures/inline-sources.js.map new file mode 100644 index 0000000..53fcc04 --- /dev/null +++ b/test/fixtures/inline-sources.js.map @@ -0,0 +1 @@ +{"version":3,"file":"inline-sources.js","sources":["data:application/json;base64,c29tZSBraW5kIGNvbnRlbnQ="],"mappings":"AAAA"} diff --git a/test/fixtures/protocol-relative-url-path.js b/test/fixtures/protocol-relative-url-path.js new file mode 100644 index 0000000..2a33530 --- /dev/null +++ b/test/fixtures/protocol-relative-url-path.js @@ -0,0 +1,3 @@ +// Some content +// # sourceMappingURL=//sampledomain.com/external-source-map2.map +// comment diff --git a/test/fixtures/skip-sourcesContent.js b/test/fixtures/skip-sourcesContent.js new file mode 100644 index 0000000..55d73c0 --- /dev/null +++ b/test/fixtures/skip-sourcesContent.js @@ -0,0 +1,3 @@ +// Skip SourcesContent in SourceMap +// # sourceMappingURL=skip-sourcesContent.js.map +// comment diff --git a/test/fixtures/skip-sourcesContent.js.map b/test/fixtures/skip-sourcesContent.js.map new file mode 100644 index 0000000..5879245 --- /dev/null +++ b/test/fixtures/skip-sourcesContent.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["normal-file.js","unresolved-file.js","normal-file2.js"],"mappings":"AAAA"} diff --git a/test/fixtures/unSupport-file-source-map.js b/test/fixtures/unSupport-file-source-map.js new file mode 100644 index 0000000..74afe08 --- /dev/null +++ b/test/fixtures/unSupport-file-source-map.js @@ -0,0 +1,3 @@ +// with SourceMap +//#sourceMappingURL=ftp://exampleurl.com +// comment diff --git a/test/helpers/getCompiler.js b/test/helpers/getCompiler.js index c816e2b..73b4b4e 100644 --- a/test/helpers/getCompiler.js +++ b/test/helpers/getCompiler.js @@ -40,6 +40,11 @@ export default ( test: /\.js/i, use: loaders, }, + + { + test: /\.css/i, + use: loaders, + }, ], }, plugins: [], diff --git a/test/helpers/normalizeMap.js b/test/helpers/normalizeMap.js index 0c5951b..69d7065 100644 --- a/test/helpers/normalizeMap.js +++ b/test/helpers/normalizeMap.js @@ -2,21 +2,21 @@ export default (map) => { const result = map; if (result.sources) { - result.sources = normilizeArr(result.sources); + result.sources = normalizeArr(result.sources); } if (result.file) { - [result.file] = normilizeArr([result.file]); + [result.file] = normalizeArr([result.file]); } if (result.sourceRoot) { - [result.sourceRoot] = normilizeArr([result.sourceRoot]); + [result.sourceRoot] = normalizeArr([result.sourceRoot]); } return result; }; -function normilizeArr(arr) { +function normalizeArr(arr) { return arr.map((str) => { const normilized = removeCWD(str); diff --git a/test/loader.test.js b/test/loader.test.js index 85f55e2..9837a39 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -341,4 +341,168 @@ describe('source-map-loader', () => { expect(bundle.indexOf(fixture) !== -1).toBe(true); }); }); + + it.skip('should process protocol-relative-url-path', async () => { + const testId = 'protocol-relative-url-path.js'; + const compiler = getCompiler(testId); + const stats = await compile(compiler); + const codeFromBundle = getCodeFromBundle(stats, compiler); + + expect(codeFromBundle.map).toBeUndefined(); + expect(codeFromBundle.css).toMatchSnapshot('css'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + }); + + it.skip('should support mixed paths in sources without sourceRoot', async () => { + 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'); + + // Create the sourcemap file + const rawSourceMap = { + version: 3, + sources: [ + 'normal-file.js', + path.resolve(__dirname, 'fixtures', 'normal-file2.js'), + 'http://path-to-map.com', + 'ftp://path-to-map.com', + ], + mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOA', + }; + fs.writeFileSync(sourceMapPath, JSON.stringify(rawSourceMap)); + + // Create the entryPointFile file + const entryFileContent = `// Some content \r\n // # sourceMappingURL=${sourceMapPath}`; + 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.skip('should support mixed paths in sources with sourceRoot', async () => { + 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'); + + // Create the sourcemap file + const rawSourceMap = { + version: 3, + sourceRoot, + sources: [ + 'normal-file.js', + path.resolve(__dirname, 'fixtures', 'normal-file2.js'), + 'http://path-to-map.com', + 'ftp://path-to-map.com', + ], + mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOA', + }; + fs.writeFileSync(sourceMapPath, JSON.stringify(rawSourceMap)); + + // Create the entryPointFile file + const entryFileContent = `// Some content \r\n // # sourceMappingURL=${sourceMapPath}`; + 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.skip('should support absolute paths to sourcemaps', async () => { + const sourceRoot = path.resolve(__dirname, 'fixtures'); + const javaScriptFilename = 'absolute-path.js'; + const entryFileAbsolutePath = path.join(sourceRoot, javaScriptFilename); + const sourceMapPath = path.join(sourceRoot, 'normal-map.js.map'); + + // Create the sourcemap file + const rawSourceMap = { + version: 3, + sourceRoot, + sources: [ + 'normal-file.js', + path.resolve(__dirname, 'fixtures', 'normal-file2.js'), + ], + mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOA', + }; + fs.writeFileSync(sourceMapPath, JSON.stringify(rawSourceMap)); + + // Create the entryPointFile file + const entryFileContent = `// Some content \r\n // # sourceMappingURL=${sourceMapPath}`; + 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.skip('should reject not support url', async () => { + const testId = 'unSupport-file-source-map.js'; + const compiler = getCompiler(testId); + const stats = await compile(compiler); + const codeFromBundle = getCodeFromBundle(stats, compiler); + + expect(codeFromBundle.css).toMatchSnapshot('css'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + }); + + it.skip('should process inlined sources', async () => { + const testId = 'inline-sources.js'; + const compiler = getCompiler(testId); + 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.skip('should process css sourceMap', async () => { + const testId = 'app.css'; + const compiler = getCompiler(testId); + 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.skip('should process css sourceMap', async () => { + const testId = 'skip-sourcesContent.js'; + const compiler = getCompiler(testId); + 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'); + }); }); diff --git a/test/sourceMapperRegexp.test.js b/test/sourceMapperRegexp.test.js index c819f87..3db379e 100644 --- a/test/sourceMapperRegexp.test.js +++ b/test/sourceMapperRegexp.test.js @@ -50,6 +50,13 @@ describe('source-map-loader', () => { expect(url).toMatchSnapshot('result'); }); + it('should work', async () => { + const code = `// #sourceMappingURL = //sampledomain.com/external-source-map2.map`; + const { url } = getSourceMappingUrl(code); + + expect(url).toMatchSnapshot('result'); + }); + it('should work', async () => { const code = `// @sourceMappingURL=data:application/source-map;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5saW5lLXNvdXJjZS1tYXAuanMiLCJzb3VyY2VzIjpbImlubGluZS1zb3VyY2UtbWFwLnR4dCJdLCJzb3VyY2VzQ29udGVudCI6WyJ3aXRoIFNvdXJjZU1hcCJdLCJtYXBwaW5ncyI6IkFBQUEifQ==`; const { url } = getSourceMappingUrl(code);