Skip to content

Commit

Permalink
Add input option to plugin-svelte (#1347)
Browse files Browse the repository at this point in the history
  • Loading branch information
firefish5000 authored Oct 18, 2020
1 parent dd025f0 commit 8ecc4a1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions plugins/plugin-svelte/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ By default, this plugin will look for a `svelte.config.js` file in your project
| Name | Type | Description |
| :---------------- | :--------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------------ |
| `configFilePath` | `string` | Relative path to a Svelte config file. Defaults to load `svelte.config.js` from the current project root directory. |
| `input` | `string[]` | Array of file extensions to process. Defaults to `['.svelte']`. |
| `preprocess` | [svelte.preprocess options](https://svelte.dev/docs#svelte_preprocess) | Configure the Svelte pre-processor. If this option is given, the config file `preprocess` option will be ignored. |
| `compilerOptions` | [svelte.compile options](https://svelte.dev/docs#svelte_compile) | Configure the Svelte compiler.If this option is given, the config file `preprocess` option will be ignored. |
| `hmrOptions` | [svelte-hmr options](https://github.com/rixo/svelte-hmr) | Configure HMR & "fast refresh" behavior for Svelte. |
11 changes: 10 additions & 1 deletion plugins/plugin-svelte/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,19 @@ module.exports = function plugin(snowpackConfig, pluginOptions = {}) {
`[plugin-svelte] Could not recognize "compileOptions". Did you mean "compilerOptions"?`,
);
}
if (pluginOptions.input && !Array.isArray(pluginOptions.input)) {
throw new Error(
`[plugin-svelte] Option "input" must be an array (e.g. ['.svelte', '.svx'])`,
);
}
if (pluginOptions.input && pluginOptions.input.length === 0) {
throw new Error(`[plugin-svelte] Option "input" must specify at least one filetype`);
}

let configFilePath = path.resolve(cwd, pluginOptions.configFilePath || 'svelte.config.js');
let compilerOptions = pluginOptions.compilerOptions;
let preprocessOptions = pluginOptions.preprocess;
let resolveInputOption = pluginOptions.input || ['.svelte'];
const hmrOptions = pluginOptions.hmrOptions;

if (fs.existsSync(configFilePath)) {
Expand All @@ -58,7 +67,7 @@ module.exports = function plugin(snowpackConfig, pluginOptions = {}) {
return {
name: '@snowpack/plugin-svelte',
resolve: {
input: ['.svelte'],
input: resolveInputOption,
output: ['.js', '.css'],
},
knownEntrypoints: [
Expand Down
39 changes: 39 additions & 0 deletions plugins/plugin-svelte/test/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,23 @@ describe('@snowpack/plugin-svelte (mocked)', () => {
).toThrow(badOptionCheck);
});

it('logs error if resolve input is invalid', async () => {
expect(() => {
plugin(mockConfig, {
input: '.svelte'
});
}).toThrow(
`[plugin-svelte] Option "input" must be an array (e.g. ['.svelte', '.svx'])`
);
expect(() => {
plugin(mockConfig, {
input: []
});
}).toThrow(
`[plugin-svelte] Option "input" must specify at least one filetype`
);
});

it('passes compilerOptions to compiler', async () => {
const compilerOptions = {
__test: 'compilerOptions',
Expand Down Expand Up @@ -93,4 +110,26 @@ describe('@snowpack/plugin-svelte (mocked)', () => {
});
expect(mockPreprocessor.mock.calls[0][1]).toEqual({__test: 'custom-config.js::preprocess'});
});

it('resolves custom file extensions', async () => {
expect(
plugin(mockConfig, {
input: ['.svelte','.svx'],
}).resolve.input,
).toMatchInlineSnapshot(`
Array [
".svelte",
".svx",
]
`);
expect(
plugin(mockConfig, {
input: ['.svx'],
}).resolve.input,
).toMatchInlineSnapshot(`
Array [
".svx",
]
`);
});
});

1 comment on commit 8ecc4a1

@vercel
Copy link

@vercel vercel bot commented on 8ecc4a1 Oct 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.