Skip to content

Commit

Permalink
[plugin-svelte] reveal config option (#1292)
Browse files Browse the repository at this point in the history
* reveal `config` option

* add docs

* configFilePath + error check
  • Loading branch information
bearcanrun authored Oct 13, 2020
1 parent e98f84c commit 5b375b3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
8 changes: 8 additions & 0 deletions plugins/plugin-svelte/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ npm install --save-dev @snowpack/plugin-svelte

## Plugin Options

- `configFilePath: string` - relative URL to Svelte config, usually named `svelte.config.js`. Defaults to `svelte.config.js` in project root directory.
```js
// Example usage
...
["@snowpack/plugin-svelte", { configFilePath: './dir/svelte.config.js' }]
...
```

This plugin also supports all Svelte compiler options. See [here](https://svelte.dev/docs#svelte_compile) for a list of supported options.

### HMR Options
Expand Down
17 changes: 11 additions & 6 deletions plugins/plugin-svelte/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,28 @@ module.exports = function plugin(snowpackConfig, {hot: hotOptions, ...sveltePlug
svelteRollupPlugin({include: '**/node_modules/**', dev: isDev}),
);

let svelteOptions;
let {configFilePath = 'svelte.config.js', ...svelteOptions} = sveltePluginOptions || {};
let userSvelteOptions;
let preprocessOptions;
// Note(drew): __config is for internal testing use; maybe we should make this public at some point?
const userSvelteConfigLoc =
sveltePluginOptions.__config || path.join(process.cwd(), 'svelte.config.js');

const userSvelteConfigLoc = path.resolve(process.cwd(), configFilePath);

if (fs.existsSync(userSvelteConfigLoc)) {
const userSvelteConfig = require(userSvelteConfigLoc);
const {preprocess, ..._svelteOptions} = userSvelteConfig;
preprocessOptions = preprocess;
svelteOptions = _svelteOptions;
userSvelteOptions = _svelteOptions;
} else {
//user svelte.config.js is optional and should not error if not configured
if (configFilePath !== 'svelte.config.js') console.error(`[plugin-svelte] failed to find Svelte config file: could not locate "${userSvelteConfigLoc}"`);
}

// Generate svelte options from user provided config (if given)
svelteOptions = {
dev: isDev,
css: false,
...userSvelteOptions,
...svelteOptions,
...sveltePluginOptions,
};

return {
Expand Down
9 changes: 6 additions & 3 deletions plugins/plugin-svelte/test/mocked.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@ describe('@snowpack/plugin-svelte (mocked)', () => {
generate: 'ssr',
isDev: false,
};
const sveltePlugin = plugin(mockConfig, options);
const optionsConfig = {configFilePath: './plugins/plugin-svelte/test/svelte.config.js'}

const sveltePlugin = plugin(mockConfig, {...options, ...optionsConfig});
await sveltePlugin.load({filePath: mockComponent});
const passedOptions = mockCompiler.mock.calls[0][1];

// this tests that all options passed above made it to the compiler
// objectContaining() allows additional options to be passed, but we only care that our options have been preserved
expect(passedOptions).toEqual(expect.objectContaining(options));
// `configFilePath` option is expected not to be passed into Svelte
expect(passedOptions).toEqual(expect.not.objectContaining(optionsConfig));
});

it('handles preprocessing', async () => {
// test-only config option
const options = {__config: path.join(__dirname, 'svelte.config.js')};
const options = {configFilePath: './plugins/plugin-svelte/test/svelte.config.js'};

const sveltePlugin = plugin(mockConfig, options);

Expand Down
10 changes: 9 additions & 1 deletion plugins/plugin-svelte/test/unmocked.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ const mockComponent = path.join(__dirname, 'Button.svelte');

describe('@snowpack/plugin-svelte (unmocked)', () => {
it('generates code', async () => {
const options = {};
const options = {configFilePath: './plugins/plugin-svelte/test/svelte.config.js'};
const sveltePlugin = plugin(mockConfig, options);
const result = await sveltePlugin.load({filePath: mockComponent});

// assume if some CSS & JS were returned, it transformed successfully
expect(result['.css'].code).toBeTruthy();
expect(result['.js'].code).toBeTruthy();
});
it('logs error if config options set but finds no file', async () => {
const consoleSpy = await jest.spyOn(console, 'error').mockImplementation(() => {});
const options = {configFilePath: './plugins/plugin-svelte/svelte.config.js'};
const sveltePlugin = plugin(mockConfig, options);
const result = await sveltePlugin.load({filePath: mockComponent});

expect(consoleSpy).toHaveBeenCalled();
});
});

1 comment on commit 5b375b3

@vercel
Copy link

@vercel vercel bot commented on 5b375b3 Oct 13, 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.