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

@garfish/remote-module

v1.19.2

Published

remote-module module.

Downloads

799

Readme

@garfish/remote-module

NPM version

@garfish/remote-module can be used alone or combined with Garfish.

Usage

// module
const React = require('React');

exports.One = function (props) {
  return React.createElement('div', null, [props.text]);
};

exports.Two = function () {
  return React.createElement('span');
};
import React from 'React';
import {
  hooks,
  preload,
  esModule,
  loadModule,
  loadModuleSync,
  setModuleConfig,
  cacheModules,
} from '@garfish/remote-module';

console.log(hooks); // See the documentation for `@garfish/hooks`

setModuleConfig({
  externals: { React }, // Environment variables required by remoteModules
  alias: { Component: 'https://xx.js' },
});

const RemoteCm = React.lazy(() =>
  loadModule('https://xx.js').then((modules) => {
    console.log(modules); // One, Two
    return esModule(modules.One);
  }),
);

// Or
const RemoteCm = React.lazy(() => {
  return loadModule('@Component.One').then(esModule);
});

// Use `React.Suspense` to use components
<React.Suspense fallback={<div>loading</div>}>
  <div>
    <RemoteCm text="cool!" />
  </div>
</React.Suspense>;

Other usage

// Or
loadModule('https://xx.js', {
  cache: true, // This will cache the module instance, default value is `true`
  externals: { React },
  error: (err, info, alias) => {
    console.error(err);
    return 'render error content';
  },
}).then((modules) => {
  console.log(modules); // One, Two
});

// Or load the remote modules synchronously
preload(['https://1.js', 'https://2.js']).then(() => {
  const modules = loadModuleSync('https://1.js');
  console.log(modules); // One, Two
});

// View already cached modules
console.log(cacheModules);

Combined with Garfish

If you are using "garfish" micro frontend.

// Child app
import { preload } from '@garfish/remote-module';

export const provider = () => {
  render({ dom }) {
    // When the resources of the remote module are preloaded,
    // You can use synchronous syntax to load remote modules in the current application.
    // You can combine "webpack5 module federation" or other "component markets"
    preload(menu.remoteModules).then(() => {
      ReactDom.render(<App/>, dom)
    })
  },

  destroy() {
    ...
  }
}

You can also configure the information of remote modules in the template, so that these modules can be used synchronously when the child application is initialized.

<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- Preload Module resources, but will not execute code -->
    <meta
      name="garfish-remote-module"
      alias="Component"
      src="http://localhost:3000/remoteModule1.js"
    />
    <!-- With the async attribute will not block page rendering -->
    <meta
      name="garfish-remote-module"
      src="http://localhost:3000/remoteModule2.js"
      async
    />
  </head>
  <body></body>
</html>
import { loadModuleSync } from '@garfish/remote-module';

function App() {
  // const One = loadModuleSync('@Component.One');
  const { One } = loadModuleSync('http://localhost:3000/remoteModule1');

  return (
    <div>
      <One />
    </div>
  );
}

Alias

You can simplify the long url with the alias config.

import { loadModule, setModuleConfig } from '@garfish/remote-module';

setModuleConfig({
  alias: { utils: 'http://localhost:3000/remoteModule' },
});

loadModule('@utils').then((utils) => {
  console.log(utils);
});

loadModule('@utils.isObject').then((isObject) => {
  console.log(isObject);
});

Remote module

The remote module only supports the commonjs format, but you can also package the module in the umd format.

module.exports = {
  a() {},
  b() {},
};

If the module wants to return asynchronous content.

When the module exports a promise, you can only use the RemoteModule.loadModule method, otherwise an error will be reported.

// The loadModule method is provided by default
const loadModule = require('loadModule');

module.exports = new Promise((resolve) => {
  loadModule('@otherModules').then((modules) => {
    resolve({
      a() {},
      b() {},
      ...modules,
    });
  });
});