Remote Module Loader

Loads a module from a remote url.
Install
npm install @paciolan/remote-module-loader
Simple Example
If your module has no external dependencies, this is the easiest method to fetch the remote module.
import { createLoadRemoteModule } from "@paciolan/remote-module-loader";
export default createLoadRemoteModule();
import loadRemoteModule from "./lib/loadRemoteModule";
const myModule = loadRemoteModule("http://fake.url/modules/my-module.js");
myModule.then(m => {
const value = m.default();
console.log({ value });
});
Require Example
You can pass dependencies to the module.
import { createLoadRemoteModule } from "@paciolan/remote-module-loader";
const dependencies = {
react: require("react")
};
const requires = name => dependencies[name];
export default createLoadRemoteModule({ requires });
import loadRemoteModule from "./lib/loadRemoteModule";
const myModule = loadRemoteModule("http://fake.url/modules/my-module.js");
myModule.then(m => {
const value = m.default();
console.log({ value });
});
Using your own fetcher
By default loadRemoteModule
will use the XMLHttpRequest
object avaiable in the browser. This can be overridden if you want to use an alternate method.
import { createLoadRemoteModule } from "@paciolan/remote-module-loader";
import axios from "axios";
const fetcher = url => axios.get(url).then(request => request.data);
export default createLoadRemoteModule({ fetcher });
import loadRemoteModule from "./lib/loadRemoteModule";
const myModule = loadRemoteModule("http://fake.url/modules/my-module.js");
myModule.then(m => {
const value = m.default();
console.log({ value });
});
React Hooks
The same techniques to include a requires
and a fetcher
with createLoadRemoteModule
also work with createUseRemoteComponent
. No need to go over them again.
import { createUseRemoteComponent } from "@paciolan/remote-module-loader";
export default createUseRemoteComponent();
Contributors
Joel Thoms (jthoms@paciolan.com)
Icon made by Freepik from www.flaticon.com