From 9adb330fc87d77788a8adfce90173d75f5d4fde4 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Fri, 3 Jan 2020 18:16:52 +0300 Subject: [PATCH] fix: do not output `undefined` when sourceRoot is unavailable --- src/runtime/api.js | 2 +- test/runtime/__snapshots__/api.test.js.snap | 8 +++++++- test/runtime/api.test.js | 19 ++++++++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/runtime/api.js b/src/runtime/api.js index b2acad8c..bf8a1cbc 100644 --- a/src/runtime/api.js +++ b/src/runtime/api.js @@ -58,7 +58,7 @@ function cssWithMappingToString(item, useSourceMap) { if (useSourceMap && typeof btoa === 'function') { const sourceMapping = toComment(cssMapping); const sourceURLs = cssMapping.sources.map( - (source) => `/*# sourceURL=${cssMapping.sourceRoot}${source} */` + (source) => `/*# sourceURL=${cssMapping.sourceRoot || ''}${source} */` ); return [content] diff --git a/test/runtime/__snapshots__/api.test.js.snap b/test/runtime/__snapshots__/api.test.js.snap index 59695e48..23ba2486 100644 --- a/test/runtime/__snapshots__/api.test.js.snap +++ b/test/runtime/__snapshots__/api.test.js.snap @@ -8,6 +8,12 @@ exports[`api should toString a single module 1`] = `"body { a: 1; }"`; exports[`api should toString multiple modules 1`] = `"body { b: 2; }body { a: 1; }"`; +exports[`api should toString with a source map without "sourceRoot" 1`] = ` +"body { a: 1; } +/*# sourceURL=./path/to/test.scss */ +/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC5zY3NzIiwic291cmNlcyI6WyIuL3BhdGgvdG8vdGVzdC5zY3NzIl0sIm1hcHBpbmdzIjoiQUFBQTsifQ== */" +`; + 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`] = ` @@ -16,4 +22,4 @@ exports[`api should toString with source mapping 1`] = ` /*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC5zY3NzIiwic291cmNlcyI6WyIuL3BhdGgvdG8vdGVzdC5zY3NzIl0sIm1hcHBpbmdzIjoiQUFBQTsiLCJzb3VyY2VSb290Ijoid2VicGFjazovLyJ9 */" `; -exports[`api should toString without source mapping if btoa not avalibale 1`] = `"body { a: 1; }"`; +exports[`api should toString without source mapping if btoa not available 1`] = `"body { a: 1; }"`; diff --git a/test/runtime/api.test.js b/test/runtime/api.test.js index 508139a5..8ff7a5d5 100644 --- a/test/runtime/api.test.js +++ b/test/runtime/api.test.js @@ -101,7 +101,7 @@ describe('api', () => { expect(m.toString()).toMatchSnapshot(); }); - it('should toString without source mapping if btoa not avalibale', () => { + it('should toString without source mapping if btoa not available', () => { global.btoa = null; const m = api(true); @@ -120,4 +120,21 @@ describe('api', () => { expect(m.toString()).toMatchSnapshot(); }); + + it.only('should toString with a source map without "sourceRoot"', () => { + const m = api(true); + + m.push([ + 1, + 'body { a: 1; }', + '', + { + file: 'test.scss', + sources: ['./path/to/test.scss'], + mappings: 'AAAA;', + }, + ]); + + expect(m.toString()).toMatchSnapshot(); + }); });