sveltejs/rollup-plugin-svelte

Svelte format compile option is ignored

williammartin opened this issue · 4 comments

Hello,

We ran into a bit of confusion today as we wanted to use this plugin to compile a Svelte component for SSR.

The type definitions provided for the CompileOptions are pulled directly from Svelte:

import { CompileOptions } from 'svelte/types/compiler/interfaces';

compilerOptions: CompileOptions;

https://github.com/sveltejs/svelte/blob/ca94762fe973da14152269ee4191db37f3e90cc7/src/compiler/interfaces.ts#L142
https://github.com/sveltejs/svelte/blob/ca94762fe973da14152269ee4191db37f3e90cc7/src/compiler/interfaces.ts#L132

As such, we expected that if we set the format to cjs it would be passed through to the Svelte compiler. However, it seems like this plugin has always forced the format to esm (or es):

compilerOptions.format = 'esm';

Unfortunately, we spent a bit of time (especially because we're not super familiar with module options) extremely confused why we seemed to always be getting esm compiled output. I have a couple of questions:

Firstly, is it reasonable that we want to use this plugin to compile to cjs? Since we're novices in this area we may be making a mistake expecting this to work (a fork that allows cjs it seems to work for us), so I'm very much open to other ideas.

Secondly, would you accept a PR that allows the cjs option to be passed through? If not, would you accept a PR that perhaps uses an Omit<> or in some other way overrides and docs that esm are the only allowable configuration with an explanation for why, to help others avoid this in future?

Cheers!

Reading webpack stuff kind of hurts my brain but I believe this is where they pass through directly, allowing cjs: https://github.com/sveltejs/svelte-loader/blob/5e7853f233d7be5592b0b5fc22da9b4d1a5b0c12/index.js#L33

Typically, you would configure Rollup to output your whole bundle as CJS. Having the Svelte plugin output CJS just introduces extra overhead as Rollup has to convert back to ESM as it's bundling everything.

That generally makes sense to me. We were using Rollup to compile Svelte SSR and picking out the compiled files as part of the generated bundle, but maybe there's a better way to do this using Rollup's config directly.

I created a sample PR of how this (novice) confusion might be avoided in future, but honestly, it might be more confusion and maintenance than it's worth, since you now have to jump through an Omit type. Feel free to close it and this when you are happy.

Thanks for the quick response!

Closing this as I no longer feel it's relevant. It can always be found and reopened again.