Skip to content

serlo/block-serlo-editor-with-vue-js

Repository files navigation

serlo-block-editor-vue

How to expose a React web component and import from Vue

In the Editor.js repo

  1. Define super simple React component
import React from 'react'

export function SimplestReactComponent() {
  return <div>Simplest React Component wrapped as a web component</div>
}
  1. Reexport component from .ts file
// Inside the web-component.ts
export { ReactWrapper } from './simple-web-component'
  1. 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',
    //   },
    // },
  },
  1. Build & publish
yarn build
yarn yalc:publish
  1. 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>
  1. 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'
    }
  }
})

How to solve cache issues in local development

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

Recommended IDE Setup

VSCode + Volar (and disable Vetur) + TypeScript Vue Plugin (Volar).

Type Support for .vue Imports in TS

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:

  1. Disable the built-in TypeScript Extension
    1. Run Extensions: Show Built-in Extensions from VSCode's command palette
    2. Find TypeScript and JavaScript Language Features, right click and select Disable (Workspace)
  2. Reload the VSCode window by running Developer: Reload Window from the command palette.

Customize configuration

See Vite Configuration Reference.

Project Setup

npm install

Compile and Hot-Reload for Development

npm run dev

Type-Check, Compile and Minify for Production

npm run build

Run Unit Tests with Vitest

npm run test:unit

Lint with ESLint

npm run lint

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published