@@ -4,11 +4,13 @@ const { createFilter } = require('rollup-pluginutils');
4
4
const { compile, preprocess } = require ( 'svelte/compiler' ) ;
5
5
const { encode, decode } = require ( 'sourcemap-codec' ) ;
6
6
7
+ const PREFIX = '[rollup-plugin-svelte]' ;
7
8
const pkg_export_errors = new Set ( ) ;
8
9
9
10
const plugin_options = new Set ( [
10
11
'include' , 'exclude' , 'extensions' ,
11
- 'emitCss' , 'preprocess' , 'onwarn' ,
12
+ 'preprocess' , 'onwarn' ,
13
+ 'emitCss' , 'css' ,
12
14
] ) ;
13
15
14
16
function to_entry_css ( bundle ) {
@@ -29,7 +31,7 @@ class CssWriter {
29
31
sources : map . sources ,
30
32
sourcesContent : map . sourcesContent ,
31
33
names : [ ] ,
32
- mappings : map . mappings
34
+ mappings : encode ( map . mappings )
33
35
} ;
34
36
35
37
this . warn = context . warn ;
@@ -78,22 +80,25 @@ class CssWriter {
78
80
}
79
81
}
80
82
81
- /** @returns {import('rollup').Plugin } */
83
+ /**
84
+ * @param [options] {Partial<import('.').Options>}
85
+ * @returns {import('rollup').Plugin }
86
+ */
82
87
module . exports = function ( options = { } ) {
83
- const extensions = options . extensions || [ '.svelte' ] ;
84
- const filter = createFilter ( options . include , options . exclude ) ;
88
+ const { compilerOptions= { } , ...rest } = options ;
89
+ const extensions = rest . extensions || [ '.svelte' ] ;
90
+ const filter = createFilter ( rest . include , rest . exclude ) ;
85
91
86
- /** @type { import('svelte/types/compiler/interfaces').ModuleFormat } */
87
- const format = 'esm' , config = { format } ;
92
+ compilerOptions . format = 'esm' ;
93
+ const isDev = ! ! compilerOptions . dev ;
88
94
89
- for ( let key in options ) {
90
- // forward `svelte/compiler` options
95
+ for ( let key in rest ) {
91
96
if ( plugin_options . has ( key ) ) continue ;
92
- config [ key ] = config [ key ] || options [ key ] ;
97
+ console . warn ( ` ${ PREFIX } Unknown " ${ key } " option. Please use \`compilerOptions\` for any Svelte compiler configuration.` ) ;
93
98
}
94
99
95
100
const css_cache = new Map ( ) ; // [filename]:[chunk]
96
- const { css, emitCss, onwarn } = options ;
101
+ const { css, emitCss, onwarn } = rest ;
97
102
98
103
const ctype = typeof css ;
99
104
const toWrite = ctype === 'function' && css ;
@@ -103,7 +108,7 @@ module.exports = function (options = {}) {
103
108
104
109
// block svelte's inline CSS if writer
105
110
const external_css = ! ! ( toWrite || emitCss ) ;
106
- if ( external_css ) config . css = false ;
111
+ if ( external_css ) compilerOptions . css = false ;
107
112
108
113
return {
109
114
name : 'svelte' ,
@@ -169,7 +174,7 @@ module.exports = function (options = {}) {
169
174
code = processed . code ;
170
175
}
171
176
172
- const compiled = compile ( code , { ...config , filename } ) ;
177
+ const compiled = compile ( code , { ...compilerOptions , filename } ) ;
173
178
174
179
( compiled . warnings || [ ] ) . forEach ( warning => {
175
180
if ( ! css && ! emitCss && warning . code === 'css-unused-selector' ) return ;
@@ -203,7 +208,7 @@ module.exports = function (options = {}) {
203
208
*/
204
209
generateBundle ( config , bundle ) {
205
210
if ( pkg_export_errors . size > 0 ) {
206
- 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' ) ;
211
+ console . warn ( `\n ${ PREFIX } 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` ) ;
207
212
console . warn ( Array . from ( pkg_export_errors , s => `- ${ s } ` ) . join ( '\n' ) + '\n' ) ;
208
213
}
209
214
@@ -239,13 +244,8 @@ module.exports = function (options = {}) {
239
244
}
240
245
} ) ;
241
246
242
- toWrite (
243
- new CssWriter ( this , bundle , ! ! options . dev , result , config . sourcemap && {
244
- sources,
245
- sourcesContent,
246
- mappings : encode ( mappings )
247
- } )
248
- ) ;
247
+ const sourceMap = config . sourcemap && { sources, sourcesContent, mappings } ;
248
+ toWrite ( new CssWriter ( this , bundle , isDev , result , sourceMap ) ) ;
249
249
}
250
250
} ;
251
251
} ;
0 commit comments