From 1398af277c89898806b7c149c9522d16dcad6bb0 Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Mon, 3 Aug 2020 10:36:47 -0700 Subject: [PATCH 1/2] fix: define new `CssWriter` properties; - Related: #72 --- index.d.ts | 4 +++- index.js | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 8440de9..1df2aeb 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,4 +1,4 @@ -import { Plugin, RollupWarning } from 'rollup'; +import { Plugin, RollupWarning, PluginContext } from 'rollup'; import { PreprocessorGroup } from 'svelte/types/compiler/preprocess'; interface Css { @@ -8,6 +8,7 @@ interface Css { declare class CssWriter { code: string; + filename: string; map: { version: number; file?: boolean; @@ -17,6 +18,7 @@ declare class CssWriter { mappings: string; }; warn: RollupWarning; + bundle: PluginContext; write(dest: string, map: boolean): void; toString(): string; } diff --git a/index.js b/index.js index 9bbb46b..f9597ff 100644 --- a/index.js +++ b/index.js @@ -328,7 +328,7 @@ module.exports = function svelte(options = {}) { css(writer); - + } if (pkg_export_errors.size < 1) return; From 63da94a5f4f5a455bd6afdd9995ea240b5198d25 Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Mon, 3 Aug 2020 10:43:29 -0700 Subject: [PATCH 2/2] refactor: replace `bundle` context with `emit` func --- index.d.ts | 4 ++-- index.js | 21 +++++++++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/index.d.ts b/index.d.ts index 1df2aeb..cf1f10a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,4 +1,4 @@ -import { Plugin, RollupWarning, PluginContext } from 'rollup'; +import { Plugin, RollupWarning } from 'rollup'; import { PreprocessorGroup } from 'svelte/types/compiler/preprocess'; interface Css { @@ -18,7 +18,7 @@ declare class CssWriter { mappings: string; }; warn: RollupWarning; - bundle: PluginContext; + emit(fileName: string, source: string): void; write(dest: string, map: boolean): void; toString(): string; } diff --git a/index.js b/index.js index f9597ff..705595b 100644 --- a/index.js +++ b/index.js @@ -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, @@ -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); } } @@ -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');