In the Editor.js repo
- Define super simple React component
import React from 'react'
export function SimplestReactComponent() {
return <div>Simplest React Component wrapped as a web component</div>
}
- Reexport component from .ts file
// Inside the web-component.ts
export { ReactWrapper } from './simple-web-component'
- Define vite.config.ts and ensure React gets bundled with the web component
build: {
lib: {
entry: resolve(__dirname, 'src/package/web-component.ts'),
name: 'editor',
fileName: 'editor',
formats: ['es'],
},
// !Important: React and react-dom reference needs to be removed, otherwise it doesn't get bundled and Vue.js can't consume the package. (errors like `createRoot` is not defined)
rollupOptions: {
// external: ['react', 'react-dom'],
// output: {
// globals: {
// react: 'React',
// 'react-dom': 'ReactDOM',
// },
// },
},
- Build & publish
yarn build
yarn yalc:publish
- In consuming repo (Vue), import component and render
<template>
<p>Hi from Vue!</p>
<react-wrapper></react-wrapper>
</template>
<script>
import { defineComponent } from 'vue'
// import the component that defines the Web Component
import { ReactWrapper } from '@serlo/editor'
export default defineComponent({
name: 'WebComponentTest'
})
</script>
- Tell Vue compiler about the web component to prevent warnings
The warning will look something like the following
WebComponentTest.vue:2 [Vue warn]: Failed to resolve component: react-wrapper
If this is a native custom element, make sure to exclude it from component resolution via compilerOptions.isCustomElement.
at <WebComponentTest>
at <App>
To prevent this, go inside the vite.config.ts and add the web component as a customElement to the compilerOptions.
vue({
template: {
compilerOptions: {
isCustomElement: (tag: string) => tag === 'react-wrapper'
}
}
})
In the frontend/packages/serlo-web-component package, trigger a complete rebuild:
yarn build && yarn yalc:publish
In this package, uninstall and reinstall dependencies with yalc:
yalc remove --all && npm i && yalc add @serlo/editor-web-component && npm i && npm run dev
VSCode + Volar (and disable Vetur) + TypeScript Vue Plugin (Volar).
TypeScript cannot handle type information for .vue
imports by default, so we replace the tsc
CLI with vue-tsc
for type checking. In editors, we need TypeScript Vue Plugin (Volar) to make the TypeScript language service aware of .vue
types.
If the standalone TypeScript plugin doesn't feel fast enough to you, Volar has also implemented a Take Over Mode that is more performant. You can enable it by the following steps:
- Disable the built-in TypeScript Extension
- Run
Extensions: Show Built-in Extensions
from VSCode's command palette - Find
TypeScript and JavaScript Language Features
, right click and selectDisable (Workspace)
- Run
- Reload the VSCode window by running
Developer: Reload Window
from the command palette.
See Vite Configuration Reference.
npm install
npm run dev
npm run build
Run Unit Tests with Vitest
npm run test:unit
Lint with ESLint
npm run lint