Skip to content

Commit

Permalink
fix: do not output undefined when sourceRoot is unavailable (#1036)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Jan 3, 2020
1 parent b60e62a commit ded2a79
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/runtime/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 7 additions & 1 deletion test/runtime/__snapshots__/api.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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`] = `
Expand All @@ -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; }"`;
19 changes: 18 additions & 1 deletion test/runtime/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
});
});

0 comments on commit ded2a79

Please sign in to comment.