vue-wc-compose
A tool for building and distributing vue components as vue and web components for easy sharing between teams.
Dependencies
- Document Register Element Polyfill
For IE 9+ compatibility only
- Webpack >= 4.x - only for building web components
Getting Started
All components start as regular Vue components.
<!-- SampleComponent.vue --> Hello {{username}}
1. Build a Shareable Vue component
Shareable Vue components are built to a single JS file and are composed as async components.
You are responsible for providing an appropriate webpack base configuration which can handle all the modules referenced by your component and bundle them into a single output JS file. Future releases may include a sample webpack configuration for you to use as a template.
The build process will duplicate this configuration for each component passed to the build function.
// build-components.jsconst path = const baseWebpackConfig = const BuildTools = BuildTools
# Build your vue components by executing the script node build-components.js
2. Build a Web Component
Unlike shareable Vue components, web components can run on any site, not only those with Vue running. In order to build one, we need to wrap our Sample Component in a web component.
// SampleComponent.wrapper.js
Now, we can augment the build-components.js script to use the wrapper as the build entry point (SampleComponent.wrapper.js)
// build-components.jsconst baseWebpackConfig = const BuildTools = BuildTools
# Build your web components node build-components.js
3. Use a component from another vue application
Install the vue-wc-compose Plugin
Vue
The plugin will expand the capabilities of your vue components. Let's compose the SampleComponent we just built using the new 'compose' syntax:
// MyClientComponent.vue > <script> name: 'my-client-component' compose: 'sample-component': type: 'vc' src: '/server/path/to/built/SampleComponent.js'
Compose Property Options:
- type: ('vc' | 'wc') This indicates whether or not we are composing a vue component or a web component.
- src: (String) The path to your component. If the component is being served by another domain, typical browser CORS rules apply.
Once the component has been referenced, we can use it in our component templates:
<!-- MyClientComponent.vue > <template> --> ... ...
Troubleshooting
Testing web components in an isolated scope can be difficult. The demo application allows you to easily test web components from this repository and others.
# build the demo components npm run demo:build # run the demo application npm run demo
The demo script uses http-server under the hood and all http-server options are available to you through the npm call. For example, to proxy all unresolved requests to another server:
npm run demo -- -P http://my-other-server.biz/web-components/
Edit the index.html file to request and render the components of your choosing.