Catalog npm
Back to packageRELEASE DETAILS

0.0.0

npm

2024-2-2-standalone-component-build_example_testing

Recently, I worked on a project to rewrite a legacy software system. After we began active development, we realized that we would be able to solve some acute needs of the legacy system with some of the features that we were building. We weren't ready to r

Published2 Feb 2024
Direct dependencies85
Transitive dependenciesNot calculated

README

Current package documentation

Building and Exporting a React Component For Another Web Application

Recently, I worked on a project to rewrite a legacy software system. After we began active development, we realized that we would be able to solve some acute needs of the legacy system with some of the features that we were building. We weren't ready to release the whole application, but we wanted to release some of the new features we've built.

So, we worked through that problem: exporting a handful of components from our React application for use in a Vue web application. When I first started looking into it, I had no idea where to begin (other than assuming I'd need to wrangle with Webpack), but after a few conversations with colleagues, discovered Web Components.

Web Components to the Rescue

From the docs on MDN, Web Components are defined as:

Web Components aims to solve such problems — it consists of three main technologies, which can be used together to create versatile custom elements with encapsulated functionality that can be reused wherever you like without fear of code collisions.

The React component can be registered as a Web Component, which can be imported and used from another application (or just a plain HTML page in fact). With this approach, components are encapsulated within a shadow DOM and can't interfere with the rest of the application.

This sounded like a perfect fit for our needs.

Creating our Web Component

React includes tools for creating and registering a Web Component, and we can use them in conjunction with our build tools. In the following example, I'm going to use Webpack. The full code is available on GitHub.

import * as React from "react";
import * as ReactDom from "react-dom";
import { FetchData } from "./fetch-data";

class StandaloneComponent extends HTMLElement {
  mountPoint!: HTMLSpanElement;
  name!: string;

  connectedCallback() {
    const mountPoint = document.createElement("span");
    this.attachShadow({ mode: "open" }).appendChild(mountPoint);

    const name = this.getAttribute("name");
    if (name) {
      ReactDom.render(<FetchData name={name} />, mountPoint);
    } else {
      console.error("You must declare a name!");
    }
  }
}
export default StandaloneComponent;

window.customElements.get("standalone-component") ||
  window.customElements.define("standalone-component", StandaloneComponent);

There's a few things going on in here - some of which are just a byproduct of the React & Web Component API. We must declare the class & extend the HTMLElement, we need to create a mountPoint & specify that we're attaching to the shadow DOM, and we need to define our Web Component (we don't need to redefine it if we already have). For our custom component, we require a name prop, and we can access it via the HTML "attributes" for prop-like parity.

I'll include some of the relevant Webpack configuration below, but feel free to refer to the GitHub repo for more details. This may not be relevant to your own build configuration, whether it's Webpack, ESBuild, or something else, but I've tried to highlight a few of the differences between our client Webpack configuration.

module.exports = {
  // much of this is shared with webpack/client.config.js, but for simplicity, we are removing any chunking / minimizing / code splitting / other optimization and we have no need for HtmlWebpackPlugin or the devServer.
  entry: {
    app: ["./src/standalone.tsx"],
  },
  output: {
    path: path.resolve(__dirname, "../dist/standalone"),
    publicPath: `${JSON.stringify(config.get("server.publicHost"))}/standalone`, // looks like http://localhost:3000/standalone when running locally
    filename: "client.js",
  },
};

To use, it's important that the deployment & distribution process is easy. Therefore, we want to be able to serve the JavaScript bundle on a web server, rather than building into another web application.

We can do that pretty easily with Express, like:

const port = process.env.PORT || 3001;

let app = express();

// other API endpoints

// serve the dist directory, where our standalone component is built
app.use(express.static("./dist/"));

app.listen(port, () => {
  console.info("up and running on port", port);
});

And we can use our script like:

<html>
  <head>
    <script src="http://localhost:3001/standalone/client.js"></script>
  </head>

  <body>
    <standalone-component name="Web Component" />
  </body>
</html>

By including a script tag, it will fetch the JavaScript and register the Web Component, which allows us to run our component later in the page. Of course, we can follow a similar pattern in other languages and JavaScript frameworks, but I find that it's easiest to boil this down to the most simple case and stick with HTML when testing this out.

Part 1. Why Web Components Part 2. Registering a Web Component in React, initial webpack configuration Part 3. Importing a Web Component in Vue, importance of easy updates (src tag) Part 4. Running a Web Worker from another domain Part ?? Styled Components

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 85

Native requirements
85 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
@babel/core
Environment
{
  "local_name": "@babel/core",
  "alias": false,
  "peer_metadata": null
}
7.12.3runtime
@pmmmwh/react-refresh-webpack-plugin
Environment
{
  "local_name": "@pmmmwh/react-refresh-webpack-plugin",
  "alias": false,
  "peer_metadata": null
}
0.4.3runtime
@svgr/webpack
Environment
{
  "local_name": "@svgr/webpack",
  "alias": false,
  "peer_metadata": null
}
5.5.0runtime
@testing-library/jest-dom
Environment
{
  "local_name": "@testing-library/jest-dom",
  "alias": false,
  "peer_metadata": null
}
^5.11.4runtime
@testing-library/react
Environment
{
  "local_name": "@testing-library/react",
  "alias": false,
  "peer_metadata": null
}
^11.1.0runtime
@testing-library/user-event
Environment
{
  "local_name": "@testing-library/user-event",
  "alias": false,
  "peer_metadata": null
}
^12.1.10runtime
@types/cors
Environment
{
  "local_name": "@types/cors",
  "alias": false,
  "peer_metadata": null
}
^2.8.12runtime
@types/express
Environment
{
  "local_name": "@types/express",
  "alias": false,
  "peer_metadata": null
}
^4.17.13runtime
@types/jest
Environment
{
  "local_name": "@types/jest",
  "alias": false,
  "peer_metadata": null
}
^26.0.15runtime
@types/node
Environment
{
  "local_name": "@types/node",
  "alias": false,
  "peer_metadata": null
}
^12.0.0runtime
@types/react
Environment
{
  "local_name": "@types/react",
  "alias": false,
  "peer_metadata": null
}
^17.0.0runtime
@types/react-dom
Environment
{
  "local_name": "@types/react-dom",
  "alias": false,
  "peer_metadata": null
}
^17.0.0runtime
@typescript-eslint/eslint-plugin
Environment
{
  "local_name": "@typescript-eslint/eslint-plugin",
  "alias": false,
  "peer_metadata": null
}
^4.5.0runtime
@typescript-eslint/parser
Environment
{
  "local_name": "@typescript-eslint/parser",
  "alias": false,
  "peer_metadata": null
}
^4.5.0runtime
babel-eslint
Environment
{
  "local_name": "babel-eslint",
  "alias": false,
  "peer_metadata": null
}
^10.1.0runtime
babel-jest
Environment
{
  "local_name": "babel-jest",
  "alias": false,
  "peer_metadata": null
}
^26.6.0runtime
babel-loader
Environment
{
  "local_name": "babel-loader",
  "alias": false,
  "peer_metadata": null
}
8.1.0runtime
babel-plugin-named-asset-import
Environment
{
  "local_name": "babel-plugin-named-asset-import",
  "alias": false,
  "peer_metadata": null
}
^0.3.7runtime
babel-preset-react-app
Environment
{
  "local_name": "babel-preset-react-app",
  "alias": false,
  "peer_metadata": null
}
^10.0.0runtime
bfj
Environment
{
  "local_name": "bfj",
  "alias": false,
  "peer_metadata": null
}
^7.0.2runtime
camelcase
Environment
{
  "local_name": "camelcase",
  "alias": false,
  "peer_metadata": null
}
^6.1.0runtime
case-sensitive-paths-webpack-plugin
Environment
{
  "local_name": "case-sensitive-paths-webpack-plugin",
  "alias": false,
  "peer_metadata": null
}
2.3.0runtime
circular-dependency-plugin
Environment
{
  "local_name": "circular-dependency-plugin",
  "alias": false,
  "peer_metadata": null
}
^5.2.2runtime
compression-webpack-plugin
Environment
{
  "local_name": "compression-webpack-plugin",
  "alias": false,
  "peer_metadata": null
}
^9.0.0runtime
concurrently
Environment
{
  "local_name": "concurrently",
  "alias": false,
  "peer_metadata": null
}
^6.3.0runtime
config
Environment
{
  "local_name": "config",
  "alias": false,
  "peer_metadata": null
}
^3.3.6runtime
core-js
Environment
{
  "local_name": "core-js",
  "alias": false,
  "peer_metadata": null
}
^3.19.1runtime
cors
Environment
{
  "local_name": "cors",
  "alias": false,
  "peer_metadata": null
}
^2.8.5runtime
css-loader
Environment
{
  "local_name": "css-loader",
  "alias": false,
  "peer_metadata": null
}
4.3.0runtime
dotenv
Environment
{
  "local_name": "dotenv",
  "alias": false,
  "peer_metadata": null
}
^10.0.0runtime
dotenv-expand
Environment
{
  "local_name": "dotenv-expand",
  "alias": false,
  "peer_metadata": null
}
5.1.0runtime
eslint
Environment
{
  "local_name": "eslint",
  "alias": false,
  "peer_metadata": null
}
^7.11.0runtime
eslint-config-react-app
Environment
{
  "local_name": "eslint-config-react-app",
  "alias": false,
  "peer_metadata": null
}
^6.0.0runtime
eslint-plugin-flowtype
Environment
{
  "local_name": "eslint-plugin-flowtype",
  "alias": false,
  "peer_metadata": null
}
^5.2.0runtime
eslint-plugin-import
Environment
{
  "local_name": "eslint-plugin-import",
  "alias": false,
  "peer_metadata": null
}
^2.22.1runtime
eslint-plugin-jest
Environment
{
  "local_name": "eslint-plugin-jest",
  "alias": false,
  "peer_metadata": null
}
^24.1.0runtime
eslint-plugin-jsx-a11y
Environment
{
  "local_name": "eslint-plugin-jsx-a11y",
  "alias": false,
  "peer_metadata": null
}
^6.3.1runtime
eslint-plugin-react
Environment
{
  "local_name": "eslint-plugin-react",
  "alias": false,
  "peer_metadata": null
}
^7.21.5runtime
eslint-plugin-react-hooks
Environment
{
  "local_name": "eslint-plugin-react-hooks",
  "alias": false,
  "peer_metadata": null
}
^4.2.0runtime
eslint-plugin-testing-library
Environment
{
  "local_name": "eslint-plugin-testing-library",
  "alias": false,
  "peer_metadata": null
}
^3.9.2runtime
eslint-webpack-plugin
Environment
{
  "local_name": "eslint-webpack-plugin",
  "alias": false,
  "peer_metadata": null
}
^2.5.2runtime
express
Environment
{
  "local_name": "express",
  "alias": false,
  "peer_metadata": null
}
^4.17.1runtime
file-loader
Environment
{
  "local_name": "file-loader",
  "alias": false,
  "peer_metadata": null
}
6.1.1runtime
fs-extra
Environment
{
  "local_name": "fs-extra",
  "alias": false,
  "peer_metadata": null
}
^9.0.1runtime
html-webpack-plugin
Environment
{
  "local_name": "html-webpack-plugin",
  "alias": false,
  "peer_metadata": null
}
5.5.0runtime
identity-obj-proxy
Environment
{
  "local_name": "identity-obj-proxy",
  "alias": false,
  "peer_metadata": null
}
3.0.0runtime
jest
Environment
{
  "local_name": "jest",
  "alias": false,
  "peer_metadata": null
}
26.6.0runtime
jest-circus
Environment
{
  "local_name": "jest-circus",
  "alias": false,
  "peer_metadata": null
}
26.6.0runtime
jest-resolve
Environment
{
  "local_name": "jest-resolve",
  "alias": false,
  "peer_metadata": null
}
26.6.0runtime
jest-watch-typeahead
Environment
{
  "local_name": "jest-watch-typeahead",
  "alias": false,
  "peer_metadata": null
}
0.6.1runtime
mini-css-extract-plugin
Environment
{
  "local_name": "mini-css-extract-plugin",
  "alias": false,
  "peer_metadata": null
}
0.11.3runtime
nodemon
Environment
{
  "local_name": "nodemon",
  "alias": false,
  "peer_metadata": null
}
^2.0.15runtime
optimize-css-assets-webpack-plugin
Environment
{
  "local_name": "optimize-css-assets-webpack-plugin",
  "alias": false,
  "peer_metadata": null
}
5.0.4runtime
pnp-webpack-plugin
Environment
{
  "local_name": "pnp-webpack-plugin",
  "alias": false,
  "peer_metadata": null
}
1.6.4runtime
postcss-flexbugs-fixes
Environment
{
  "local_name": "postcss-flexbugs-fixes",
  "alias": false,
  "peer_metadata": null
}
4.2.1runtime
postcss-loader
Environment
{
  "local_name": "postcss-loader",
  "alias": false,
  "peer_metadata": null
}
3.0.0runtime
postcss-normalize
Environment
{
  "local_name": "postcss-normalize",
  "alias": false,
  "peer_metadata": null
}
8.0.1runtime
postcss-preset-env
Environment
{
  "local_name": "postcss-preset-env",
  "alias": false,
  "peer_metadata": null
}
6.7.0runtime
postcss-safe-parser
Environment
{
  "local_name": "postcss-safe-parser",
  "alias": false,
  "peer_metadata": null
}
5.0.2runtime
progress-bar-webpack-plugin
Environment
{
  "local_name": "progress-bar-webpack-plugin",
  "alias": false,
  "peer_metadata": null
}
^2.1.0runtime
prompts
Environment
{
  "local_name": "prompts",
  "alias": false,
  "peer_metadata": null
}
2.4.0runtime
react
Environment
{
  "local_name": "react",
  "alias": false,
  "peer_metadata": null
}
^17.0.2runtime
react-app-polyfill
Environment
{
  "local_name": "react-app-polyfill",
  "alias": false,
  "peer_metadata": null
}
^2.0.0runtime
react-dev-utils
Environment
{
  "local_name": "react-dev-utils",
  "alias": false,
  "peer_metadata": null
}
^11.0.3runtime
react-dom
Environment
{
  "local_name": "react-dom",
  "alias": false,
  "peer_metadata": null
}
^17.0.2runtime
react-refresh
Environment
{
  "local_name": "react-refresh",
  "alias": false,
  "peer_metadata": null
}
^0.8.3runtime
resolve
Environment
{
  "local_name": "resolve",
  "alias": false,
  "peer_metadata": null
}
1.18.1runtime
resolve-url-loader
Environment
{
  "local_name": "resolve-url-loader",
  "alias": false,
  "peer_metadata": null
}
^3.1.2runtime
sass-loader
Environment
{
  "local_name": "sass-loader",
  "alias": false,
  "peer_metadata": null
}
^10.0.5runtime
semver
Environment
{
  "local_name": "semver",
  "alias": false,
  "peer_metadata": null
}
7.3.2runtime
style-loader
Environment
{
  "local_name": "style-loader",
  "alias": false,
  "peer_metadata": null
}
1.3.0runtime
terser-webpack-plugin
Environment
{
  "local_name": "terser-webpack-plugin",
  "alias": false,
  "peer_metadata": null
}
4.2.3runtime
ts-loader
Environment
{
  "local_name": "ts-loader",
  "alias": false,
  "peer_metadata": null
}
^9.2.6runtime
ts-node
Environment
{
  "local_name": "ts-node",
  "alias": false,
  "peer_metadata": null
}
^10.4.0runtime
ts-pnp
Environment
{
  "local_name": "ts-pnp",
  "alias": false,
  "peer_metadata": null
}
1.2.0runtime
typescript
Environment
{
  "local_name": "typescript",
  "alias": false,
  "peer_metadata": null
}
^4.1.2runtime
url-loader
Environment
{
  "local_name": "url-loader",
  "alias": false,
  "peer_metadata": null
}
4.1.1runtime
web-vitals
Environment
{
  "local_name": "web-vitals",
  "alias": false,
  "peer_metadata": null
}
^1.0.1runtime
webpack
Environment
{
  "local_name": "webpack",
  "alias": false,
  "peer_metadata": null
}
^5.54.0runtime
webpack-cli
Environment
{
  "local_name": "webpack-cli",
  "alias": false,
  "peer_metadata": null
}
^4.7runtime
webpack-dev-server
Environment
{
  "local_name": "webpack-dev-server",
  "alias": false,
  "peer_metadata": null
}
3.11.1runtime
webpack-manifest-plugin
Environment
{
  "local_name": "webpack-manifest-plugin",
  "alias": false,
  "peer_metadata": null
}
2.2.0runtime
webpack-node-externals
Environment
{
  "local_name": "webpack-node-externals",
  "alias": false,
  "peer_metadata": null
}
^3.0.0runtime
whatwg-fetch
Environment
{
  "local_name": "whatwg-fetch",
  "alias": false,
  "peer_metadata": null
}
^3.6.2runtime
workbox-webpack-plugin
Environment
{
  "local_name": "workbox-webpack-plugin",
  "alias": false,
  "peer_metadata": null
}
5.1.4runtime

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "integrity": "sha512-HOy5+MA3hnurfMjGpkiJDwqvSZhabX78BvzPCJAW52TuzN/6Rd4xAWHxAxTBWH0jWmgyg8benfQhih1coecFHQ==",
  "shasum": "8d6ae6284df1c265dcf9f7a5a22f73345d194208",
  "tarball": "https://registry.npmjs.org/2024-2-2-standalone-component-build_example_testing/-/2024-2-2-standalone-component-build_example_testing-0.0.0.tgz",
  "fileCount": 22,
  "unpackedSize": 32123,
  "signatures": [
    {
      "keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA",
      "sig": "MEYCIQDM7RDqx4WNePa5URUniCfX65bLdHJpr6bZKiQVAo6yHQIhANBcpMXO1028L1yKvw4TJVmUuoD1cyDSTY0hGltEf0pb"
    }
  ]
}

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 18:55 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:0b8ed731aa09ed0e5199cee6e96ebad010f1eebe1840ddd70f689a5cce9a281d

Other collected versions

View all collected versions →