Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add input option to plugin-svelte #1347

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
]
`);
});
});