Skip to content

Commit 63da94a

Browse files
committed
refactor: replace bundle context with emit func
1 parent 1398af2 commit 63da94a

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Plugin, RollupWarning, PluginContext } from 'rollup';
1+
import { Plugin, RollupWarning } from 'rollup';
22
import { PreprocessorGroup } from 'svelte/types/compiler/preprocess';
33

44
interface Css {
@@ -18,7 +18,7 @@ declare class CssWriter {
1818
mappings: string;
1919
};
2020
warn: RollupWarning;
21-
bundle: PluginContext;
21+
emit(fileName: string, source: string): void;
2222
write(dest: string, map: boolean): void;
2323
toString(): string;
2424
}

index.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ function exists(file) {
7070
}
7171

7272
class CssWriter {
73-
constructor (code, filename, map, warn, bundle) {
73+
constructor(code, filename, map, warn, toAsset) {
7474
this.code = code;
7575
this.filename = filename;
76+
this.emit = toAsset;
77+
this.warn = warn;
7678
this.map = {
7779
version: 3,
7880
file: null,
@@ -81,25 +83,23 @@ class CssWriter {
8183
names: [],
8284
mappings: map.mappings
8385
};
84-
this.warn = warn;
85-
this.bundle = bundle;
8686
}
8787

8888
write(dest = this.filename, map) {
8989
const basename = path.basename(dest);
9090

9191
if (map !== false) {
92-
this.bundle.emitFile({type: 'asset', fileName: dest, source: `${this.code}\n/*# sourceMappingURL=${basename}.map */`});
93-
this.bundle.emitFile({type: 'asset', fileName: `${dest}.map`, source: JSON.stringify({
92+
this.emit(dest, `${this.code}\n/*# sourceMappingURL=${basename}.map */`);
93+
this.emit(`${dest}.map`, JSON.stringify({
9494
version: 3,
9595
file: basename,
9696
sources: this.map.sources.map(source => path.relative(path.dirname(dest), source)),
9797
sourcesContent: this.map.sourcesContent,
9898
names: [],
9999
mappings: this.map.mappings
100-
}, null, ' ')});
100+
}, null, 2));
101101
} else {
102-
this.bundle.emitFile({type: 'asset', fileName: dest, source: this.code});
102+
this.emit(dest, this.code);
103103
}
104104
}
105105

@@ -324,12 +324,13 @@ module.exports = function svelte(options = {}) {
324324
sources,
325325
sourcesContent,
326326
mappings: encode(mappings)
327-
}, this.warn, this);
327+
}, this.warn, (fileName, source) => {
328+
this.emitFile({ type: 'asset', fileName, source });
329+
});
328330

329331
css(writer);
330-
331-
332332
}
333+
333334
if (pkg_export_errors.size < 1) return;
334335

335336
console.warn('\nrollup-plugin-svelte: The following packages did not export their `package.json` file so we could not check the `svelte` field. If you had difficulties importing svelte components from a package, then please contact the author and ask them to export the package.json file.\n');

0 commit comments

Comments
 (0)