Vue Integration
Supports: Vue 3 • TypeScript 4.0+ • Stencil v2.9.0+
Stencil can generate Vue component wrappers for your web components. This allows your Stencil components to be used within a Vue 3 application. The benefits of using Stencil's component wrappers over the standard web components include:
- Type checking with your components.
- Integration with the router link and Vue router.
- Optionally, form control web components can be used with
v-model.
Setup
Project Structure
We recommend using a monorepo structure for your component library with component wrappers. Your project workspace should contain your Stencil component library and the library for the generate Vue component wrappers.
An example project set-up may look similar to:
top-most-directory/
└── packages/
├── vue-library/
│ └── lib/
│ ├── plugin.ts
│ └── index.ts
└── stencil-library/
├── stencil.config.js
└── src/components
This guide uses Lerna for the monorepo, but you can use other solutions such as Nx, Turborepo, etc.
To use Lerna with this walk through, globally install Lerna:
- npm
- Yarn
- pnpm
npm install --global lerna
yarn global add lerna
pnpm add --global lerna
Creating a Monorepo
If you already have a monorepo, skip this section.
- npm
- Yarn
- pnpm
# From your top-most-directory/, initialize a workspace
lerna init
# install dependencies
npm install
# install typescript and node types
npm install typescript @types/node --save-dev
# From your top-most-directory/, initialize a workspace
lerna init
# install dependencies
yarn install
# install typescript and node types
yarn add typescript @types/node --dev
# From your top-most-directory/, initialize a workspace
lerna init
# install dependencies
pnpm install
# install typescript and node types
pnpm add typescript @types/node --save-dev
Creating a Stencil Component Library
If you already have a Stencil component library, skip this section.
- npm
- Yarn
- pnpm
cd packages/
npm init stencil components stencil-library
cd stencil-library
# Install dependencies
npm install
cd packages/
yarn create stencil components stencil-library
cd stencil-library
# Install dependencies
yarn install
cd packages/
pnpm create stencil components stencil-library
cd stencil-library
# Install dependencies
pnpm install
Creating a Vue Component Library
If you already have a Vue component library, skip this section.
The first time you want to create the component wrappers, you will need to have a Vue library package to write to.
Using Lerna and Vue's CLI, generate a workspace and a library for your Vue component wrappers:
- npm
- Yarn
- pnpm
# From your top-most-directory/
lerna create vue-library
# Follow the prompts and confirm
cd packages/vue-library
# Install Vue dependency
npm install vue@3 --save-dev
# From your top-most-directory/
lerna create vue-library
# Follow the prompts and confirm
cd packages/vue-library
# Install Vue dependency
yarn add vue@3 --dev
# From your top-most-directory/
lerna create vue-library
# Follow the prompts and confirm
cd packages/vue-library
# Install Vue dependency
pnpm add vue@3 --save-dev
Lerna does not ship with a TypeScript configuration. At the root of the workspace, create a tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"noImplicitAny": false,
"removeComments": true,
"noLib": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"sourceMap": true,
"lib": ["es6"]
},
"exclude": ["node_modules", "**/*.spec.ts", "**/__tests__/**"]
}
In your vue-library project, create a project specific tsconfig.json that will extend the root config:
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"lib": ["dom", "es2020"],
"module": "es2015",
"moduleResolution": "node",
"target": "es2017",
"skipLibCheck": true
},
"include": ["lib"],
"exclude": ["node_modules"]
}
Update the generated package.json in your vue-library, adding the following options to the existing config:
{
- "main": "lib/vue-library.js",
+ "main": "dist/index.js",
+ "types": "dist/index.d.ts",
"files": [
- 'lib'
+ 'dist'
],
"scripts": {
- "test": "echo \"Error: run tests from root\" && exit 1"
+ "test": "echo \"Error: run tests from root\" && exit 1",
+ "build": "npm run tsc",
+ "tsc": "tsc -p . --outDir ./dist"
- }
+ },
+ "publishConfig": {
+ "access": "public"
+ },
+ "dependencies": {
+ "stencil-library": "*"
+ }
}
The stencil-library dependency is how Lerna knows to resolve the internal Stencil library dependency. See Lerna's documentation on
package dependency management for more information.
Adding the Vue Output Target
Install the @stencil/vue-output-target dependency to your Stencil component library package.
- npm
- Yarn
- pnpm
# Install dependency (from `packages/stencil-library`)
npm install @stencil/vue-output-target --save-dev
# Install dependency (from `packages/stencil-library`)
yarn add @stencil/vue-output-target --dev
# Install dependency (from `packages/stencil-library`)
pnpm add @stencil/vue-output-target --save-dev
In your project's stencil.config.ts, add the vueOutputTarget configuration to the outputTargets array:
import { vueOutputTarget } from '@stencil/vue-output-target';
export const config: Config = {
namespace: 'stencil-library',
outputTargets: [
// By default, the generated proxy components will
// leverage the output from the `dist` target, so we
// need to explicitly define that output alongside the
// Vue target
{
type: 'dist',
esmLoaderPath: '../loader',
},
vueOutputTarget({
componentCorePackage: 'stencil-library',
proxiesFile: '../vue-library/lib/components.ts',
}),
],
};