-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
add documentation on "compiling" Svelte components to web components #3638
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
Comments
I did a couple of videos on using Svelte in this way... on v2, and using Angular for the host app. |
Short version of how to produce a custom element is here in the docs: |
Like the docs @kylecordes linked say, you can compile custom elements by setting an option for the compiler. The only problem with that link is that it says to set |
Thanks Kyle! I just read that part of the Svelte docs again (maybe the
fourth time) and I still don't see it.
What I expected to see is that I can run some command (something like "npm
run bundle-wc") and get a .js file that contains definitions of web
components for all the Svelte components I choose to export in that way.
Then I want to import that generated .js file into an Angular/React/Vue
application and use those web components. These docs seem to just show how
to use a Svelte component as a web component inside a Svelte app. What am I
missing?
…On Sun, Sep 29, 2019 at 7:14 PM Kyle Cordes ***@***.***> wrote:
Short version of how to produce a custom element is here in the docs:
https://svelte.dev/docs#Custom_element_API
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#3638?email_source=notifications&email_token=AAATLUGF4I4UT4E7OAF2723QMFAFFA5CNFSM4I3UUJXKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD74CHOY#issuecomment-536355771>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAATLUAV5FESG3HRLBGR7C3QMFAFFANCNFSM4I3UUJXA>
.
--
R. Mark Volkmann
Object Computing, Inc.
|
@mvolkmann All you need to do is set the As the docs say though, you should also either use the import MyElement from './MyElement.svelte';
customElements.define('my-element', MyElement); when you import it. |
Thanks Jake! After configuring everything correctly, do I run "npm run build" to generate the web component definition code? Is just written to public/bundle.js? It seems like that file would contain an entire Svelte application, not just web component definitions. |
If everything is part of the same application then you can just use If however you do actually want individual standalone files that are not part of your application and contain a single component each, then you need to compile them individually setting the component file itself as the entry point. I don't know about Rollup, but in Webpack you can just write a loop to generate a new config for each custom component you want to compile and then import/download those files when you need them. |
When you say "everything is part of the same application", are you referring to using custom elements inside a Svelte application? I'm looking for the steps to use a Svelte component as a web component in a non-Svelte application. That's the only reason I want to "compile" the Svelte components to a I think I'm just tripping up on the exact steps to get all of this to work. I'll throw out a challenge. Find any web page on the internet that lists the steps in terms of the actual code and commands to run. I have spent hours searching for such a site. That's why I created this issue. I assert that no such page exists. ;-) |
You can import If you're using Webpack, for example, then you set up a rule to trigger when it sees the In the The fact you're using the Svelte compiler for some files inside of a mostly React project shouldn't make any difference, since the final compiled output is normal JS anyway, even if you |
I’m under the impression that Doesn’t it seem like there should be a way to produce a .js file as a build step that reads a set of Svelte components and outputs web component definitions ... then be able to include that .js in any web app and not even be aware that the web components were defined using Svelte? Maybe that’s something we will be able to do in the future, but not now. This is what I imagine when I hear someone like Rich Harris say “compile to web components”. |
Yes And if you want individual standalone JS files then you need to build the Svelte components separately from your main project. All you need to do with Webpack is setup a config that uses your You can then use that JS file in any project you like without anyone ever knowing it came from Svelte. |
Thanks @jakelucas! I'll try to get that approach to work and will close this issue if I can. |
@jakelucas I gave it a shot here: https://github.com/mvolkmann/svelte-in-react/tree/master/svelte-components. I have one very simple Svelte component in the file
To me it looks like I have correctly configured use of |
@mvolkmann It works fine for me. Your I'm guessing you just don't have |
Thanks @jakelucas! So close now. I think there's just one more thing I want to figure out. It works if I tell Webpack to compile just one Svelte component. But I'd like to have it compile a set of components to custom elements, all within a single |
@mvolkmann Webpack can run multiple individual configs at once if you set First of all, I had to install the Second, I had to move the Next, change your const configs = []
const components = ['Counter', 'Greet']
for (const component of components) {
configs.push({
mode: 'development',
entry: `${__dirname}/src/${component}.svelte`,
output: {
path: `${__dirname}/build`,
filename: `web-component-${component.toLowerCase()}.js`
},
module: {
rules: [
{
test: /\.svelte$/,
exclude: /node_modules/,
use: {
loader: 'svelte-loader',
options: {
customElement: true
}
}
}
]
}
});
}
module.exports = configs Then you can run That loop is just one approach, but you can of course modify the loop to be as complex as you like. |
@mvolkmann It looks like I misread your last message. You want all of the components in a single file. In that case leave your export { default as Counter } from './Counter.svelte';
export { default as Greet } from './Greet.svelte'; That way you can access them through normal imports, or if it's in the browser you can access your elements via |
Thanks so much for sticking with me through this @jakelucas! It's all working now! Do you think an example like mine, including the webpack.config.js and the package.json |
@mvolkmann I don't know whether it's worth having a full blown tutorial or not, but maybe something to point people in the right direction could be a good thing. Also, I'm not sure of your reason for using custom elements, but it's worth noting that if you build for the DOM (which is the default) then you can access the components without them being custom elements anyway. For example if you change your Webpack output: {
path: __dirname + '/build',
filename: 'web-components.js',
libraryTarget: 'umd',
umdNamedDefine: true,
library: 'svelteComponents'
} Then you will be able to access the components like It's also worth noting that you're currently only building the DOM/Client-side version of your components. If you want to use them in SSR then you'll also need to build a second server-side version of the same components by setting the |
My reason for wanting to learn how to compile Svelte components to custom elements is only to allow them to be easily used in web apps built with other frameworks like React, Vue, and Angular. I feel like this capability of Svelte is one the things people mention when they are advocating for Svelte and I wanted to make sure I understand how to use that feature. My preference is to build web apps entirely in Svelte and I wouldn’t use custom elements in that case. I hadn’t considered building components for SSR. Thanks for sharing that detail. I don’t recall seeing anything about the two options you shared in the official docs (output.library and the generate ssr option). I think those would also make great additions to the docs! |
I have struggled with some of the same issues as @mvolkmann. |
This part is missing from the documentation at: https://svelte.dev/docs#Custom_element_API To get it working with Rollup instead of (above mentioned) Webpack,
plugins: [
svelte({
customElement: true,
IMHO Fine for simple components, expect some required re-factoring for complex components HTH |
I have been working on a small cli tool to quickly publish svelte components to npm. It compiles into three formats umd, es and svelte. I think it is pretty neat (but I am likely biased). It is called "pelte" - a weird concatenation of publish and svelte. npm install -g pelte You compile, bundle and publish with the command below: (No configuration is needed): pelte ./PathToSvelteComponent.svelte (The command requires that you are logged in to npm) Pelte will also automatically create a web component, if you add <svelte:options tag="my-svelte-element"/> to the svelte component. The bundle is normally cleaned up after publish, but you can examine it if you run: The bundle will be located in a folder with the name of the Svelte component. The folder will also contain a file "index-example-umd.html" that shows the component in action. The html-file shows how to use a UMD module, but if you added the <svelte:options tag="my-svelte-element"/> in your svelte component, you should be able to just add in html-file as well. You can read more here: |
Hi, I hope someone can help here... I have done all the steps required.
And I keep having the message:
What am I doing wrong? My
The rollup file:
|
https://www.youtube.com/watch?v=xIYOyiAE-sY An example from Svelte Master |
@thojanssens
|
I don't see a description of how to do this at the svelte.dev site and I haven't been able to find an example of this online. It would be good to see the steps required to build custom elements and then use them in a few other frameworks like React and Vue.
The text was updated successfully, but these errors were encountered: