0.0.5 • Published 3 years ago

@implicitcast/web-component-wrapper v0.0.5

Weekly downloads
37
License
ISC
Repository
github
Last release
3 years ago

Web Component Wrapper

Wrapper utility encapsulates react application in a web component.

Installation

npm install --save @implicitcast/web-component-wrapper @webcomponents/webcomponentsjs @craco/craco

Add Boilerplate files

  1. Create file 'craco.config.js' at root path with content
const {
  reactWrapperWebpackConfigure,
} = require("@implicitcast/web-component-wrapper");
module.exports = {
  webpack: {
    configure: reactWrapperWebpackConfigure,
  },
};
  1. Create a file with path 'src/index.webcomponent.tsx'
import "@webcomponents/webcomponentsjs/custom-elements-es5-adapter";
import "@webcomponents/webcomponentsjs/webcomponents-loader";
import { reactWebComponentWrapper } from "@implicitcast/web-component-wrapper";
import App from "./App";
reactWebComponentWrapper(App, "web-component-test"); // Second parameter names the webComponent
  1. Edit 'public/index.html' file
<html>
  .....
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <web-component-test></web-component-test>
    <!-- Add webComponent tag -->
  </body>
</html>
  1. Update 'package.json' file
{
  "scripts": {
    "start": "craco start",
    "build": "craco build",
    "start:webcomp": "craco start webComponentEntry:src/index.webcomponent.tsx useWebComponent:true",
    "build:webcomp": "craco start webComponentEntry:src/index.webcomponent.tsx useWebComponent:true"
  }
}
  1. run npm command to start app with web-component wrapper
npm run start:webcomp