npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-wc-compose

v1.2.3

Published

Compose hosted vue components and web components

Downloads

18

Readme

vue-wc-compose

A tool for building and distributing vue components as vue and web components for easy sharing between teams.

Dependencies

  1. Document Register Element Polyfill

For IE 9+ compatibility only

	<script src="https://cdnjs.cloudflare.com/ajax/libs/document-register-element/1.4.1/document-register-element.js"></script>
  1. Webpack >= 4.x - only for building web components

Getting Started

All components start as regular Vue components.

<!-- SampleComponent.vue -->
<template>
	<div>Hello {{username}}</div>
</template>

<script>
export default {
	name: 'sample-component',
	props: {
		username: String,
	}
}
</script>

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.js
const path = require('path')
const baseWebpackConfig = require('./webpack.config.js')
const BuildTools = require('vue-wc-compose/src/BuildTools.js')

BuildTools.build(webpackConfig, [
	{name: 'SampleComponent', path: path.resolve(__dirname, '/path/to/SampleComponent.vue')}
])
# 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
import Vue from 'vue'
import SampleComponent from './SampleComponent.vue'
import ComponentWrappers from 'vue-wc-compose/src/ComponentWrappers.js'

export default ComponentWrappers.webComponentWrapper(Vue, DemoCards)

Now, we can augment the build-components.js script to use the wrapper as the build entry point (SampleComponent.wrapper.js)

// build-components.js
const baseWebpackConfig = require('./webpack.config.js')
const BuildTools = require('vue-wc-compose/src/BuildTools.js')

BuildTools.build(webpackConfig, [
	{name: 'SampleComponent', path: 'relative/path/to/SampleComponent.wrapper.js'}
])
# Build your web components
node build-components.js

3. Use a component from another vue application

Install the vue-wc-compose Plugin

import Vue from 'vue'
import VueCompose from 'vue-wc-compose'

Vue.use(VueCompose)

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>

export default {
	name: 'my-client-component',
	compose: {
		'sample-component': {type: 'vc', src: '/server/path/to/built/SampleComponent.js'},
	},
}

Compose Property Options:

  1. type: ('vc' | 'wc') This indicates whether or not we are composing a vue component or a web component.
  2. 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> -->
<template>
	<div class="my-client-component">
		...
		<sample-component username="my-username"/>
		...
	</div>
</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.