Skip to content

Commit 113db9a

Browse files
authored
fix(css): link sourcemap with source correctly (#140)
* fix(css): link `sourcemap` with source correctly * chore: fix comment typo
1 parent b57aed4 commit 113db9a

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,23 @@ class CssWriter {
9090

9191
this.sourcemap = (file, mapping) => {
9292
const ref = this.emit(file, this.code);
93-
const filename = context.getFileName(ref);
93+
const filename = context.getFileName(ref); // may be "assets/[name][ext]"
9494

95-
const mapfile = `${filename}.map`;
95+
const mapfile = `${filename}.map`; // may be "assets/[name][ext]"
9696
const toRelative = src => path.relative(path.dirname(file), src);
9797

9898
if (bundle[filename]) {
99-
bundle[filename].source += `\n/*# sourceMappingURL=${mapfile} */`;
99+
// use `basename` because files are siblings
100+
// aka, avoid `sourceMappingURL=assets/bundle.css.map` from `assets/bundle.css`
101+
bundle[filename].source += `\n/*# sourceMappingURL=${path.basename(mapfile)} */`;
100102
} else {
101103
// This should not ever happen, but just in case...
102104
return this.warn(`Missing "${filename}" ("${file}") in bundle; skipping sourcemap!`);
103105
}
104106

105107
const source = JSON.stringify({
106108
...mapping,
107-
file: filename,
109+
file: path.basename(filename), //=> sibling file
108110
sources: mapping.sources.map(toRelative),
109111
}, null, isDev ? 2 : 0);
110112

test/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ test('does not generate a CSS sourcemap by default', async () => {
101101
assetFileNames: '[name][extname]',
102102
});
103103

104-
assert.is(css.code.includes('sourceMappingURL'), false);
104+
assert.not.match(css.code, 'sourceMappingURL');
105105
assert.is(css.map, false);
106106
});
107107

@@ -495,8 +495,8 @@ test('ensures sourcemap and original files point to each other\'s hashed filenam
495495
const data1 = fs.readFileSync(path.join(assets, file), 'utf-8');
496496
const data2 = fs.readFileSync(path.join(assets, sourcemap), 'utf-8');
497497

498-
assert.match(data1, sourcemap, 'file has `sourcemap` reference');
499-
assert.match(data2, file, 'sourcemap has `file` reference');
498+
assert.match(data1, `sourceMappingURL=${sourcemap}`, 'file has `sourcemap` reference');
499+
assert.match(data2, `"file":"${file}"`, 'sourcemap has `file` reference');
500500
});
501501

502502
test('handles filenames that happen to contain .svelte', async () => {

0 commit comments

Comments
 (0)