Skip to content

Commit 121d255

Browse files
authored
breaking: CssWriter respects Rollup config (#136)
* break: emit CSS via `name` key; - will respect Rollup's `output` naming conventions * fix: respect `output.sourcemap` rollup configl - do not emit sourcemap if Rollup told not to - synchronize hashed output filename * fix: respect `output.sourcemapExcludeSources` config * fix: output minified sourcemap if not `dev` mode * chore: update tests, removing forced assumptions * chore: update & add tests * chore: fix test dir paths
1 parent 1871d5f commit 121d255

File tree

3 files changed

+249
-47
lines changed

3 files changed

+249
-47
lines changed

index.d.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
1-
import { Plugin, RollupWarning } from 'rollup';
1+
import { Plugin, RollupWarning, SourceMap as Mapping } from 'rollup';
22
import { PreprocessorGroup } from 'svelte/types/compiler/preprocess';
33
import { CompileOptions } from 'svelte/types/compiler/interfaces';
44

5-
interface Css {
6-
code: any;
7-
map: any;
8-
}
5+
type SourceMap = Omit<Mapping, 'toString' | 'toUrl'>;
96

107
declare class CssWriter {
118
code: string;
129
filename: string;
13-
map: {
14-
version: number;
15-
file?: boolean;
16-
sources: string[];
17-
sourcesContent: string[];
18-
names: any[];
19-
mappings: string;
20-
};
10+
map: false | SourceMap;
2111
warn: RollupWarning;
22-
emit(fileName: string, source: string): void;
23-
write(dest: string, map?: boolean): void;
12+
write(file: string, map?: boolean): void;
13+
emit(name: string, source: string): string;
14+
sourcemap(file: string, sourcemap: SourceMap): void;
2415
toString(): string;
2516
}
2617

@@ -60,6 +51,12 @@ interface Options extends CompileOptions {
6051
// }
6152
// },
6253

54+
/**
55+
* Add extra code for development and debugging purposes.
56+
* @default false
57+
*/
58+
dev?: boolean;
59+
6360
/**
6461
* Emit CSS as "files" for other plugins to process
6562
* @default false

index.js

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,34 +70,52 @@ function exists(file) {
7070
}
7171

7272
class CssWriter {
73-
constructor(code, filename, map, warn, toAsset) {
73+
constructor(context, bundle, isDev, code, filename, map) {
7474
this.code = code;
7575
this.filename = filename;
76-
this.emit = toAsset;
77-
this.warn = warn;
78-
this.map = {
76+
77+
this.map = map && {
7978
version: 3,
8079
file: null,
8180
sources: map.sources,
8281
sourcesContent: map.sourcesContent,
8382
names: [],
8483
mappings: map.mappings
8584
};
85+
86+
this.warn = context.warn;
87+
this.emit = (name, source) => context.emitFile({
88+
type: 'asset', name, source
89+
});
90+
91+
this.sourcemap = (file, mapping) => {
92+
const ref = this.emit(file, this.code);
93+
const filename = context.getFileName(ref);
94+
95+
const mapfile = `${filename}.map`;
96+
const toRelative = src => path.relative(path.dirname(file), src);
97+
98+
if (bundle[filename]) {
99+
bundle[filename].source += `\n/*# sourceMappingURL=${mapfile} */`;
100+
} else {
101+
// This should not ever happen, but just in case...
102+
return this.warn(`Missing "${filename}" ("${file}") in bundle; skipping sourcemap!`);
103+
}
104+
105+
const source = JSON.stringify({
106+
...mapping,
107+
file: filename,
108+
sources: mapping.sources.map(toRelative),
109+
}, null, isDev ? 2 : 0);
110+
111+
// use `fileName` to prevent additional Rollup hashing
112+
context.emitFile({ type: 'asset', fileName: mapfile, source });
113+
}
86114
}
87115

88-
write(dest = this.filename, map = true) {
89-
const basename = path.basename(dest);
90-
91-
if (map) {
92-
this.emit(dest, `${this.code}\n/*# sourceMappingURL=${basename}.map */`);
93-
this.emit(`${dest}.map`, JSON.stringify({
94-
version: 3,
95-
file: basename,
96-
sources: this.map.sources.map(source => path.relative(path.dirname(dest), source)),
97-
sourcesContent: this.map.sourcesContent,
98-
names: [],
99-
mappings: this.map.mappings
100-
}, null, 2));
116+
write(dest = this.filename, map = !!this.map) {
117+
if (map && this.map) {
118+
this.sourcemap(dest, this.map);
101119
} else {
102120
this.emit(dest, this.code);
103121
}
@@ -299,32 +317,33 @@ module.exports = function svelte(options = {}) {
299317
/**
300318
* If css: true then outputs a single file with all CSS bundled together
301319
*/
302-
generateBundle(options, bundle) {
320+
generateBundle(config, bundle) {
303321
if (css) {
304322
// TODO would be nice if there was a more idiomatic way to do this in Rollup
305323
let result = '';
306324

307325
const mappings = [];
308-
const sources = [];
309326
const sourcesContent = [];
327+
const sources = [];
310328

311329
const chunks = Array.from(cssLookup.keys()).sort().map(key => cssLookup.get(key));
312330

313331
for (let chunk of chunks) {
314332
if (!chunk.code) continue;
315333
result += chunk.code + '\n';
316334

317-
if (chunk.map) {
318-
const i = sources.length;
319-
sources.push(chunk.map.sources[0]);
320-
sourcesContent.push(chunk.map.sourcesContent[0]);
335+
336+
if (config.sourcemap && chunk.map) {
337+
const len = sources.length;
338+
config.sourcemapExcludeSources || sources.push(chunk.map.sources[0]);
339+
config.sourcemapExcludeSources || sourcesContent.push(chunk.map.sourcesContent[0]);
321340

322341
const decoded = decode(chunk.map.mappings);
323342

324-
if (i > 0) {
343+
if (len > 0) {
325344
decoded.forEach(line => {
326345
line.forEach(segment => {
327-
segment[1] = i;
346+
segment[1] = len;
328347
});
329348
});
330349
}
@@ -335,12 +354,10 @@ module.exports = function svelte(options = {}) {
335354

336355
const filename = Object.keys(bundle)[0].split('.').shift() + '.css';
337356

338-
const writer = new CssWriter(result, filename, {
357+
const writer = new CssWriter(this, bundle, !!options.dev, result, filename, config.sourcemap && {
339358
sources,
340359
sourcesContent,
341360
mappings: encode(mappings)
342-
}, this.warn, (fileName, source) => {
343-
this.emitFile({ type: 'asset', fileName, source });
344361
});
345362

346363
css(writer);

0 commit comments

Comments
 (0)