npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nicecactus/srm

v0.1.7

Published

Core library for Standalone React Module

Downloads

5

Readme

SRM

Core library for Standalone React Module

NPM

How it works

SRM are react applications with extra perks. They can take props and mounted on a specific element. They are designed to be lazy or eagerly loaded into any other framework.

Building an SRM will produce several .js and .css files, all listed in the asset-manifest.json bundled along them.
The build folder content can then be served by a static server, keeping the folder structure and making sure all files are publicly accessible. A simple way to do so in production would be to use an AWS S3 bucket.

The build/asset-manifest.json file describes the entry points and assets to be fetched in order to use your SRM.
A direct url to this file will be required to load and run the SRM, as can be seen in @nicecactus/ng-srm-wrapper or nicecactus/react-srm-wrapper.

Example for https://your-domain.com:
+-- /
|   +-- your-project/
|       +-- asset-manifest.json
|       +-- favicon.ico
|       +-- index.html
|       +-- static/

Asset manifest URL: https://your-domain.com/your-project/asset-manifest.json

Getting started

1. Installation

yarn

yarn add @nicecactus/srm

npm

npm install --save @nicecactus/srm

2. Project setup

You will need react, react-dom and react-intl as dependencies.

3. CSS encapsulation (optionnal)

The following section allows to prevent CSS leaking between the SRM and the host by prefixing all classes and styles. Please consider skipping this step if you don't need this feature as it will increase the bundle size.

Note: we only provide steps for CRA at the moment, however it should be straight forward to adapt these instructions to any other bundling system.

1) Install extra dependencies

The following dev dependencies are required to customize the CRA build process without ejecting:

yarn

yarn add -D customize-cra prefix-css-loader react-app-rewired string-replace-loader

npm

npm install --save-dev customize-cra prefix-css-loader react-app-rewired string-replace-loader
2) 'Flip' the existing calls to react-scripts in npm scripts for start, build and test
package.json
  "scripts": {
-   "start": "react-scripts start",
+   "start": "react-app-rewired start",
-   "build": "react-scripts build",
+   "build": "react-app-rewired build",
-   "test": "react-scripts test",
+   "test": "react-app-rewired test",
    "eject": "react-scripts eject"
}

Note: Do NOT flip the call for the eject script. That gets run only once for a project, after which you are given full control over the webpack configuration making react-app-rewired no longer required. There are no configuration options to rewire for the eject script.

3) Copy the config-overrides.js from the example folder

Copy the config-overrides.js to the root directory.

+-- your-project/
|   +-- config-overrides.js
|   +-- node_modules/
|   +-- package.json
|   +-- public/
|   +-- README.md
|   +-- src/

4. SRM creation

index.tsx
/* Import the library */
import { SRM } from "@nicecactus/srm";

/* Create the SRM */
const orgName = 'myOrg';
const appName = 'myApp';

export interface Props {
  // Add Props to your SRM here
}

const render = SRM(
  `${orgName}.${appName}`,
  (props: Props) => {
    return (
      <>
        <span>Hello world 🏆</span>
      </>
    );
  }
);

/* Declare typings */
declare global {
  export interface Window {
    [orgName]: { [appName]: { render: typeof render } };
  }
}

/* Export render function */
export default render;

5. Add i18n support (optionnal)

The last parameter loadMessages of the SRM() function can be used to return different dictionary based on the language set through the SRM props.
It expects a function of the following signature: (lang: string) => { [term: string]: string } in order to fit your custom translation setup.
For example, when using a different json file for each language:

const render = SRM(
  `${orgName}.${appName}`,
  ({ getUsername }: Props) => {
    ...
  },
  (lang: string) => require(`./translations/${lang}.json`) // require the json file from the translation folder
);

Example

An example SRM with a custom store and i18n can be found in the example folder.
Please see example/README.md for more details on how to run it.