Skip to content

Commit 79fe265

Browse files
authored
break: use compilerOptions for svelte.compile config (#145)
* fix(types): correct `warn` type on CssWriter * fix(types): share `options.onwarn` type * break: use `compilerOptions` for compiler config; - show warning on presence of non-plugin option - Closes #144 * chore(types): consolidate * chore: reference `rest.preprocess` for consistency * chore: pr comments
1 parent a765821 commit 79fe265

File tree

3 files changed

+49
-67
lines changed

3 files changed

+49
-67
lines changed

index.d.ts

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ 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

8+
type WarningHandler = (warning: RollupWarning | string) => void;
9+
710
declare class CssWriter {
811
code: string;
912
filename: string;
13+
warn: WarningHandler;
1014
map: false | SourceMap;
11-
warn: RollupWarning;
1215
write(file: string, map?: boolean): void;
1316
emit(name: string, source: string): string;
1417
sourcemap(file: string, sourcemap: SourceMap): void;
@@ -17,64 +20,41 @@ declare class CssWriter {
1720

1821
type CssEmitter = (css: CssWriter) => any;
1922

20-
interface Options extends CompileOptions {
21-
/**
22-
* By default, all ".svelte" files are compiled
23-
* @default ['.svelte']
24-
*/
25-
extensions?: string[];
26-
27-
/**
28-
* You can restrict which files are compiled
29-
* using `include` and `exclude`
30-
* @typedef {string} InclureAndExclude
31-
*/
23+
interface Options {
24+
/** One or more minimatch patterns */
25+
include: Arrayable<string>;
3226

33-
/**
34-
* @type {IncludeAndExclude}
35-
*/
36-
include?: string;
27+
/** One or more minimatch patterns */
28+
exclude: Arrayable<string>;
3729

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

4336
/**
4437
* Optionally, preprocess components with svelte.preprocess:
45-
* https://svelte.dev/docs#svelte_preprocess
38+
* @see https://svelte.dev/docs#svelte_preprocess
4639
*/
47-
preprocess?: PreprocessorGroup | PreprocessorGroup[];
40+
preprocess: Arrayable<PreprocessorGroup>;
4841
// {
4942
// style: ({ content }) => {
5043
// return transformStyles(content);
5144
// }
5245
// },
5346

54-
/**
55-
* Add extra code for development and debugging purposes.
56-
* @default false
57-
*/
58-
dev?: boolean;
47+
/** Emit CSS as "files" for other plugins to process */
48+
emitCss: boolean;
5949

60-
/**
61-
* Emit CSS as "files" for other plugins to process
62-
* @default false
63-
*/
64-
emitCss?: boolean;
50+
/** Extract CSS into a separate file (recommended) */
51+
css: false | CssEmitter;
6552

66-
/**
67-
* Extract CSS into a separate file (recommended).
68-
*/
69-
css?: false | CssEmitter;
53+
/** Options passed to `svelte.compile` method. */
54+
compilerOptions: CompileOptions;
7055

71-
/**
72-
* let Rollup handle all other warnings normally
73-
*/
74-
onwarn?: (
75-
warning: RollupWarning,
76-
handler: (w: RollupWarning | string) => void
77-
) => void;
56+
/** Custom warnings handler; defers to Rollup as default. */
57+
onwarn(warning: RollupWarning, handler: WarningHandler): void;
7858
}
7959

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

index.js

Lines changed: 23 additions & 23 deletions
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',
@@ -163,13 +168,13 @@ module.exports = function (options = {}) {
163168
const dependencies = [];
164169
const filename = path.relative(process.cwd(), id);
165170

166-
if (options.preprocess) {
167-
const processed = await preprocess(code, options.preprocess, { filename });
171+
if (rest.preprocess) {
172+
const processed = await preprocess(code, rest.preprocess, { filename });
168173
if (processed.dependencies) dependencies.push(...processed.dependencies);
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

Lines changed: 3 additions & 1 deletion
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)