Skip to content

Commit 50a7f93

Browse files
committed
break: use compilerOptions for compiler config;
- show warning on presence of non-plugin option - Closes #144
1 parent 47c831c commit 50a7f93

File tree

3 files changed

+43
-51
lines changed

3 files changed

+43
-51
lines changed

index.d.ts

+19-29
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ 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+
type Arrayable<T> = T | T[];
56
type SourceMap = Omit<Mapping, 'toString' | 'toUrl'>;
67

78
type WarningHandler = (warning: RollupWarning | string) => void;
@@ -19,61 +20,50 @@ declare class CssWriter {
1920

2021
type CssEmitter = (css: CssWriter) => any;
2122

22-
interface Options extends CompileOptions {
23-
/**
24-
* By default, all ".svelte" files are compiled
25-
* @default ['.svelte']
26-
*/
27-
extensions?: string[];
23+
interface Options {
24+
/** One or more minimatch patterns */
25+
include: Arrayable<string>;
2826

29-
/**
30-
* You can restrict which files are compiled
31-
* using `include` and `exclude`
32-
* @typedef {string} InclureAndExclude
33-
*/
34-
35-
/**
36-
* @type {IncludeAndExclude}
37-
*/
38-
include?: string;
27+
/** One or more minimatch patterns */
28+
exclude: Arrayable<string>;
3929

4030
/**
41-
* @type {IncludeAndExclude}
31+
* By default, all ".svelte" files are compiled
32+
* @default ['.svelte']
4233
*/
43-
exclude?: string;
34+
extensions: string[];
4435

4536
/**
4637
* Optionally, preprocess components with svelte.preprocess:
47-
* https://svelte.dev/docs#svelte_preprocess
38+
* @see https://svelte.dev/docs#svelte_preprocess
4839
*/
49-
preprocess?: PreprocessorGroup | PreprocessorGroup[];
40+
preprocess: Arrayable<PreprocessorGroup>;
5041
// {
5142
// style: ({ content }) => {
5243
// return transformStyles(content);
5344
// }
5445
// },
5546

5647
/**
57-
* Add extra code for development and debugging purposes.
48+
* Emit CSS as "files" for other plugins to process
5849
* @default false
5950
*/
60-
dev?: boolean;
51+
emitCss: boolean;
6152

6253
/**
63-
* Emit CSS as "files" for other plugins to process
64-
* @default false
54+
* Extract CSS into a separate file (recommended).
6555
*/
66-
emitCss?: boolean;
56+
css: false | CssEmitter;
6757

6858
/**
69-
* Extract CSS into a separate file (recommended).
59+
* Options passed to `svelte.compile` method.
7060
*/
71-
css?: false | CssEmitter;
61+
compilerOptions: CompileOptions;
7262

7363
/**
7464
* let Rollup handle all other warnings normally
7565
*/
76-
onwarn?(warning: RollupWarning, handler: WarningHandler): void;
66+
onwarn(warning: RollupWarning, handler: WarningHandler): void;
7767
}
7868

79-
export default function svelte(options?: Options): Plugin;
69+
export default function svelte(options?: Partial<Options>): Plugin;

index.js

+21-21
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ const { createFilter } = require('rollup-pluginutils');
44
const { compile, preprocess } = require('svelte/compiler');
55
const { encode, decode } = require('sourcemap-codec');
66

7+
const PREFIX = '[rollup-plugin-svelte]';
78
const pkg_export_errors = new Set();
89

910
const plugin_options = new Set([
1011
'include', 'exclude', 'extensions',
11-
'emitCss', 'preprocess', 'onwarn',
12+
'preprocess', 'onwarn',
13+
'emitCss', 'css',
1214
]);
1315

1416
function to_entry_css(bundle) {
@@ -29,7 +31,7 @@ class CssWriter {
2931
sources: map.sources,
3032
sourcesContent: map.sourcesContent,
3133
names: [],
32-
mappings: map.mappings
34+
mappings: encode(map.mappings)
3335
};
3436

3537
this.warn = context.warn;
@@ -78,22 +80,25 @@ class CssWriter {
7880
}
7981
}
8082

81-
/** @returns {import('rollup').Plugin} */
83+
/**
84+
* @param [options] {Partial<import('.').Options>}
85+
* @returns {import('rollup').Plugin}
86+
*/
8287
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);
8591

86-
/** @type {import('svelte/types/compiler/interfaces').ModuleFormat} */
87-
const format = 'esm', config = { format };
92+
compilerOptions.format = 'esm';
93+
const isDev = !!compilerOptions.dev;
8894

89-
for (let key in options) {
90-
// forward `svelte/compiler` options
95+
for (let key in rest) {
9196
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.`);
9398
}
9499

95100
const css_cache = new Map(); // [filename]:[chunk]
96-
const { css, emitCss, onwarn } = options;
101+
const { css, emitCss, onwarn } = rest;
97102

98103
const ctype = typeof css;
99104
const toWrite = ctype === 'function' && css;
@@ -103,7 +108,7 @@ module.exports = function (options = {}) {
103108

104109
// block svelte's inline CSS if writer
105110
const external_css = !!(toWrite || emitCss);
106-
if (external_css) config.css = false;
111+
if (external_css) compilerOptions.css = false;
107112

108113
return {
109114
name: 'svelte',
@@ -169,7 +174,7 @@ module.exports = function (options = {}) {
169174
code = processed.code;
170175
}
171176

172-
const compiled = compile(code, { ...config, filename });
177+
const compiled = compile(code, { ...compilerOptions, filename });
173178

174179
(compiled.warnings || []).forEach(warning => {
175180
if (!css && !emitCss && warning.code === 'css-unused-selector') return;
@@ -203,7 +208,7 @@ module.exports = function (options = {}) {
203208
*/
204209
generateBundle(config, bundle) {
205210
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`);
207212
console.warn(Array.from(pkg_export_errors, s => `- ${s}`).join('\n') + '\n');
208213
}
209214

@@ -239,13 +244,8 @@ module.exports = function (options = {}) {
239244
}
240245
});
241246

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));
249249
}
250250
};
251251
};

test/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,9 @@ test('produces readable sourcemap output when `dev` is truthy', async () => {
214214
input: 'test/sourcemap-test/src/main.js',
215215
plugins: [
216216
plugin({
217-
dev: true,
217+
compilerOptions: {
218+
dev: true
219+
},
218220
css: value => {
219221
css = value;
220222
css.write('bundle.css');

0 commit comments

Comments
 (0)