-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
Vue 3 Web Component target #6033
Comments
@y-sanchez we for our part didn't necessary require web components for now, so we've been ok with custom elements, too. So for now we've used custom elements, following is a (shortened) sample code that we use: import { createApp, watch } from "vue";
import App from "./App.vue";
class CustomElement extends HTMLElement {
constructor() {
super();
}
connectedCallback() {
const options = typeof App === "function" ? App.options : App;
const propsList: string[] = Array.isArray(options.props) ? options.props : Object.keys(options.props || {});
const props: { [index: string]: string} = {};
// Validate, if all props are present
for (const prop of propsList) {
const propValue = process.env.NODE_ENV === "development" ? process.env[`VUE_APP_${prop.toUpperCase()}`] : this.attributes.getNamedItem(prop)?.value;
if (!propValue) {
console.error(`Missing attribute ${prop}`);
return;
}
props[prop] = propValue;
}
const app = createApp(App, props);
const wrapper = document.createElement("div");
app.mount(wrapper);
this.appendChild(wrapper.children[0]);
}
}
window.customElements.define("simedia-cloud-checkin", CustomElement); |
@ivansieder, Thank you for your sample. |
Does someone have proof-of-concept repo or codesandbox for a workaround on this? @y-sanchez @ivansieder |
@dekadentno we've worked around by just using custom elements (instead of actually web components) with the above little wrapper: #6033 (comment) |
I am using
Except from the triviality of having to export the components in the Is there anything that could go wrong with this approach? It's my first lib and I am not sure if this is the best aproach. |
Thank you for your quick replies @melenaos @ivansieder |
Please let me know when this will be supported |
I totally need this feature, I am trying to export some components to let users embed them into their website by just importing them via script tag. If anyone has a workaround on how to achieve this, would love to know. Thanks in advance.
|
@ricvillagrana if customElements is enough for you, we're currently using this workaround: #6033 (comment) But please be aware that this implementation ist not WebComponent-compliant. It's just one part of the WebComponent spec (customElements) |
@ivansieder |
@mrfy yes, we're exporting the package as library. If you need/want to include Vue itself in the package (it gets externalized by default), you need to change that in your config, too. |
Does anyone know if there is a roadmap for future Vue development which might give some insight into when exporting a Vue app as a web component will be implemented? Unfortunately upgrading to Vue 3 is not an option until this is available as it is with Vue 2. ps. Thanks to all the Vue contributors for continually making Vue amazing. |
Anyone in dev to know when this |
Vue 3.2 added support for compiling WebComponents, but the implementation is different from Vue 2 |
Thanks @vikaspotluri123 That's all I ask :) |
Is it available in Vue-CLI 5 Beta ? |
For anybody that is having a bit of a hard time getting ivansieders neat and great fix to work, here is a little bit of a helping hand. Besides my main.ts file, I have created the file export-wp-plugin.ts (as I am exporting the component as a customElement to become part of a wordpress plugin). My code now looks like this
The key differences is that I create my app with createApp and then load all the necessary stuff, like app.use(store) that I had defined in my main.ts file before (as this is not loaded in by this script). I can build this by running:
I hope this can be of help to somebody. |
Okay I'm confused - so I jumped onto vue3 / typescript / composition API / Web Component band wagon after reading multiple blogs about developing web components with the Vue and also read https://cli.vuejs.org/guide/build-targets.html#web-component I have my tiny web component running in dev mode on my local and now I'm finding out that it's not possible to build it with the 'wc' target? Or am I doing something wrong ? Why this is not mentioned anywhere in the documentation? Why we are saying Vue 3.2 release has support for defineCustomElement when (if I understand correctly) is not possible to build it? Sorry but I'm completely confused now... |
I'm not sure we are looking at this correctly. Do we even NEED a web component build target? The purpose of defineCustomElement appears to be to create a registrable web component. I converted my main.ts to look like this: (notice ALL the Vue bootstrapping is gone)
And my public/index.html is now:
This works GREAT in both npm run serve AND npm run build. |
Proper building as WC is definitely needed asap for Vue 3 adoption. |
I thought I can get arround that web component error as well by using custom element but as soon as you want to build something bigger you're going to come accross: |
@dparish Isn't the idea, to get the webcomponent only (including everything that is needed for the webcomponent to function)? Or in other words: How do I use the result of the build as a webcomponent? |
Also, what is the status? Is it work in Progress? Blocked by something? Is there some estimate when it will be implemented? |
It will generate the .js file you need to include to run your web component. I modified my index.html to use my web component tags instead of bootstrap the vue app and it just worked. There should be enough in my example above for you to be able to bootstrap something on your own. If there's not let me know and I'll see what I can do. |
@dparish
Edit: Upon Rereading the Docs: So the main question is the part with |
Ok I managed to pull it of somehow: so my final command is: Now the demo was the part I was interested in: <!DOCTYPE HTML>
<meta charset="utf-8" />
<title>myElement demo</title>
<script src="./myElement.umd.js"></script>
<link rel="stylesheet" href="./myElement.css" />
<my-element></my-element>
<script>
console.log(myElement);
</script> However, I had to manually add the line //main.ts
import { defineCustomElement } from "vue";
import App from "./App.ce.vue";
customElements.define("my-element", defineCustomElement(App)); To get around vuejs/core#4662 and while I was at dealing with https://stackoverflow.com/questions/70878556/how-do-i-use-primevue-in-customelements as well, I did this: // App.ce.vue
<template>
<someOtherComponent msg="name" />
</template>
<script lang="ts">
import { defineComponent } from "vue";
import someOtherComponent from "@/components/someOtherComponent.ce.vue";
export default defineComponent({
name: "my-element", //I think this should be the same string as the one used in customElements.define() but not sure
components: { someOtherComponent },
});
</script>
<style>
@import url("https://unpkg.com/primevue/resources/primevue.min.css");
@import url("https://unpkg.com/[email protected]/primeflex.css");
@import url("https://unpkg.com/primevue/resources/themes/tailwind-light/theme.css");
</style>
<style lang="scss" src="@/assets/scss/globals.scss"></style> /*//globals.scss*/
/* Global Styles here: */
.someClass { color: red; }
/* Component Style should be imported here till https://github.com/vuejs/core/issues/4662 is resolved */
@import "@/components/someOtherComponent.scss"; Apparently, there are Issues with scoped (s)css so I added them on the "root" wc component. Then I created a global .scss file where I instruct everyone in the team to import the scss code from their child components as shown. //vue.config.js
const { defineConfig } = require("@vue/cli-service");
module.exports = defineConfig({
transpileDependencies: true,
chainWebpack: (config) => {
config.module
.rule("vue")
.use("vue-loader")
.tap((options) => {
options.customElement = true;
return options;
});
},
}); I'm am not sure if the change in vue.config.js is necessary, but I just report what I changed in my project to get where I am. |
@telion2, that's exactly the conclusion I came to. Do you think it will be fixed? |
@leonheess I have no Idea. I am just a Developer of which a client asked to create a Web Component. After Planning, Comparing SPA-Frameworks, Reading Documentation and evaluating multiple aspects of a project, we decided to use Vue 3, just to find out, it only works with serious research and workarounds. The Problem with Vue 2 is that you are forced to use Libraries that don't look like they are actively maintained:
Meaning, that for a new project you risk the lack of support or fixes in that area. |
@sodatea do you know the current progress of this feature? it will be present in vue-cli 5 ? |
@tunessofia Probably not, judging from this: Vue CLI is the official webpack-based toolchain for Vue. It is now in maintenance mode and we recommend starting new projects with Vite unless you rely on specific webpack-only features. Vite will provide superior developer experience in most cases. — https://vuejs.org/guide/scaling-up/tooling.html#project-scaffolding |
That's exactly what I want to need. |
is this dead then? |
having the same problem |
Even though it may be far away, is there a planned implementation date by official? |
I'm doing web components via Vite + Vue3 composition API now - if someone would be interested I could create and upload base project... |
@nztomas Yes please, thanks. |
@nztomas how did you solve the scoped styles issue? |
@nztomas Can you please share the sample repository doing so? |
I'm following up on this issue. Do we have an estimated time of completion for the fix? |
Interested here 🤗 |
When I'm trying to publish multiple web components using the vue cli command vue-cli-service build --target wc --inline-vue --name my-element src/*.vue. it is building perfectly fine but external dependencies like CSS and javascript file is not importing in the build. Can anyone help me with how to build web components with all external dependencies? import all CSS from node_modules as a dependency. |
What problem does this feature solve?
As a vue-cli and Vue 3 user I would like to be able to build and deploy a package as a web compomponent, so that it can be built and deployed in the same way as it's currently possible with vue-cli and Vue 2
What does the proposed API look like?
i.e.
vue-cli-service build --target wc --name my-web-component-name --inline-vue
The text was updated successfully, but these errors were encountered: