-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Recipe: Compiling a sveltekit library project directly to web components js output #10320
Comments
I don't see that this is a duplicate of the other two issues and I do not see a solution in sight as well. |
|
@dummdidumm since there are quite a few up-votes, is there a chance this gets re-opened? |
@jnehlmeier I'm also working on a library of web components compiled with Sveltekit. In my case too, I created a second Vite config file, but without the need to go through the Sveltekit build output. For reference, the versions of the main packages I'm using are: "@rollup/plugin-replace": "^5.0.7",
"@sveltejs/adapter-auto": "^3.0.0",
"@sveltejs/kit": "^2.0.0",
"@sveltejs/package": "^2.0.0",
"@sveltejs/vite-plugin-svelte": "^3.0.0",
"svelte": "^4.2.7",
"typescript": "^5.0.0",
"vite": "^5.0.11", svelte.config.js and vite.config.ts are almost identical. svelte.config.js import adapter from '@sveltejs/adapter-auto';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
const config = {
preprocess: vitePreprocess(),
kit: {
adapter: adapter()
},
compilerOptions: {
customElement: false
}
};
export default config; vite.config.ts import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vitest/config';
export default defineConfig({
plugins: [
sveltekit()
],
test: {
include: ['src/**/*.{test,spec}.{js,ts}']
}
}); And then I have vite.webcomponents.config.js: import { svelte } from '@sveltejs/vite-plugin-svelte';
import { defineConfig } from 'vite';
import { resolve } from 'path';
import replace from '@rollup/plugin-replace';
const LIBRARY_PREFIX = 'stg';
export default defineConfig({
build: {
lib: {
entry: resolve(__dirname, 'src/lib/index.ts'),
name: 'AiSearchWebComponents',
fileName: (format) => `ai.search.web.components.${format}.js`,
formats: ['es', 'umd']
},
outDir: 'dist-web-components'
},
plugins: [
replace({
preventAssignment: true,
'PREFIX': LIBRARY_PREFIX ?? 'stg',
}),
svelte({
compilerOptions: {
customElement: true
}
})
]
}); I am re-exporting the components from export { default as ChatSearch } from './components/ChatSearch.svelte';
export { default as Counter } from './components/Counter.svelte';
For instance, where I define Then in my
invoking the build with:
|
Describe the problem
Sveltekit allows creating a library project which is great since you can use the
routes
folder to play with your library directly in the same project.Svelte allows compiling components to web components.
It should be possible to have a sveltekit library project that directly compiles to web components JS output and you only have to adjust how the JS output should be generated via rollup/vite config (one file per web component + vendor.js, everything in a single file, iife vs. module output).
Describe the proposed solution
Ideally
npm create svelte@latest my-app
should ask for such a project type (maybe named "web components library" in addition to "library") and set-up the build correctly.Alternatives considered
Currently the only solution I found using some google-fu was to create a second vite config that takes the output of the sveltekit build and compiles it again to web components js.
This works but feels super strange since I believe this should be possible in a single sveltekit library build using some sveltekit option.
svelte.config.js
vite.config.js
This produces a svelte js library in dist folder containing index.js that exports components, svelte components (*.svelte) and *.d.ts files.
Then I use
vite.webcomponents.config.js
for a second build step:The second file uses the output in the dist folder and uses svelte again to produce web components js output in iife format.
Compilation then happens with
vite build && vite -c vite.webcomponents.config.js build
Importance
would make my life easier
Additional Information
No response
The text was updated successfully, but these errors were encountered: