Skip to content

Refactor: CssWriter #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface Css {

declare class CssWriter {
code: string;
filename: string;
map: {
version: number;
file?: boolean;
Expand All @@ -17,6 +18,7 @@ declare class CssWriter {
mappings: string;
};
warn: RollupWarning;
emit(fileName: string, source: string): void;
write(dest: string, map: boolean): void;
toString(): string;
}
Expand Down
21 changes: 11 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ function exists(file) {
}

class CssWriter {
constructor (code, filename, map, warn, bundle) {
constructor(code, filename, map, warn, toAsset) {
this.code = code;
this.filename = filename;
this.emit = toAsset;
this.warn = warn;
this.map = {
version: 3,
file: null,
Expand All @@ -81,25 +83,23 @@ class CssWriter {
names: [],
mappings: map.mappings
};
this.warn = warn;
this.bundle = bundle;
}

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

if (map !== false) {
this.bundle.emitFile({type: 'asset', fileName: dest, source: `${this.code}\n/*# sourceMappingURL=${basename}.map */`});
this.bundle.emitFile({type: 'asset', fileName: `${dest}.map`, source: JSON.stringify({
this.emit(dest, `${this.code}\n/*# sourceMappingURL=${basename}.map */`);
this.emit(`${dest}.map`, JSON.stringify({
version: 3,
file: basename,
sources: this.map.sources.map(source => path.relative(path.dirname(dest), source)),
sourcesContent: this.map.sourcesContent,
names: [],
mappings: this.map.mappings
}, null, ' ')});
}, null, 2));
} else {
this.bundle.emitFile({type: 'asset', fileName: dest, source: this.code});
this.emit(dest, this.code);
}
}

Expand Down Expand Up @@ -324,12 +324,13 @@ module.exports = function svelte(options = {}) {
sources,
sourcesContent,
mappings: encode(mappings)
}, this.warn, this);
}, this.warn, (fileName, source) => {
this.emitFile({ type: 'asset', fileName, source });
});

css(writer);


}

if (pkg_export_errors.size < 1) return;

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');
Expand Down