Package Exports
- vue-web-component-wrapper
Readme
vue-web-component-wrapper
Introduction
vue-web-component-wrapper
is a powerful Vue 3 plugin designed for transforming full-fledged Vue applications into reusable web components (custom elements). These web components can be integrated into any website, enhancing flexibility and reusability.
Key Features:
- Vue Compatibility: Seamlessly integrates with Vue ecosystem plugins such as Vuex, Vue Router, and Vue I18n.
- CSS Framework Support: Works with popular CSS frameworks like Tailwind CSS, Bootstrap, and SASS.
- Scoped css: Allows you to use scoped css in your components.
- Shadow DOM Support: Facilitates the encapsulation of styles and scripts for your components, preventing clashes with the rest of your application.
Installation
Install the plugin using npm or yarn:
npm install vue-web-component-wrapper
# or
yarn add vue-web-component-wrapper
Usage
First, import the necessary modules in your entry file:
import App from './App.vue';
import tailwindStyles from './assets/tailwind.css?raw';
import { createWebHashHistory, createRouter } from "vue-router";
import { createI18n } from 'vue-i18n';
import { createStore } from 'vuex'
import { defaultRoutes} from './main.routes.js'
import {store} from './store/index.js'
import { defineCustomElement as VueDefineCustomElement, h, createApp, getCurrentInstance } from 'vue';
import { createWebComponent } from 'vue-web-component-wrapper';
Next, create the necessary instances and use your plugin:
const pluginsWrapper = {
install(GivenVue) {
const Vue = GivenVue;
const createdStore = createStore(store)
Vue.use(createdStore)
const router = createRouter({
history: createWebHashHistory(),
routes: defaultRoutes,
})
Vue.use(router);
const i18n = createI18n()
Vue.use(i18n);
}
}
Finally, use createWebComponent
:
createWebComponent({
rootComponent: App,
elementName: 'my-web-component',
plugins: pluginsWrapper,
cssFrameworkStyles: tailwindStyles,
VueDefineCustomElement,
h,
createApp,
getCurrentInstance
});
Webpack Configuration
The plugin is compatible with webpack. Here's a sample webpack configuration:
const path = require('path');
const { VueLoaderPlugin } = require('vue-loader');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
mode: 'production',
entry: './src/main.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'my-web-component.js',
},
module: {
rules: [
{
test: /\.(vue|ce\.vue)$/,
loader: 'vue-loader',
options: {
customElement: true,
},
},
{
test: /\.(css|scss)$/,
oneOf: [
{
resourceQuery: /raw/,
use: [
'to-string-loader',
'css-loader',
'postcss-loader',
{
loader: 'sass-loader',
options: {
sassOptions: {
indentedSyntax: false, // Use the SCSS syntax
},
},
},
],
},
{
use: [
'style-loader',
'css-loader',
'postcss-loader',
{
loader: 'sass-loader',
options: {
sassOptions: {
indentedSyntax: false, // Use the SCSS syntax
},
},
},
],
},
],
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'file-loader',
options: {
name: 'assets/[name].[hash:7].[ext]',
},
},
],
},
plugins: [
new VueLoaderPlugin(),
new HtmlWebpackPlugin({
template: './public/index.html',
}),
],
resolve: {
alias: {
vue$: 'vue/dist/vue.esm-bundler.js',
},
extensions: ['.js', '.vue', '.json'],
},
};
This configuration file helps webpack understand how to load and process .vue, .css, and .scss files. Support for vite might be considered in the future.
Contributing
Contributions are welcome! For details, please refer to our contribution guidelines.
License
MIT