remote-import v0.0.11
REMOTE-IMPORT (REMOTE-RUN)
Import remote modules from URLs. It's implemented with a customized node module loader so that it supports URL imports in transitive-dependent modules. It also provides a CLI tool remote-run to run remote js file directly.
Background
When Deno just launched, I was excited with the ability to import modules from any remote URL so that we can get rid of the cumbersome packages.json setup and bloated node-modules folders. If not because of the NPM ecosystem, I'd completely move to Deno. Unfortunately, Deno is not well integrated with the existing NPM ecosystem. I still have to stick with NPM. After a bit of research, I build this small library/CLI to bring the ability to import or run modules from a URL.
There're few existing solutions, but none of them satisfy my requirement. Most of them support the first level of remote import. They won't work if there're remote imports within the imported module. This library solved this issue by customizing Node module loader logic. It overrides Module._resolveFilename(), so that whenever a remote URL is detected, it will download the remote module and cache it locally.
Features
- Support import with URL starts with
http://andhttps:// - Support transitive imports in the dependent modules
- Support import CommonJs modules
- Support ES6 modules with the pre-loaded library: esm.
node -r esm - Customizable local cache folder
- Customizable cache refresh duration
- Support Http head of
if-matchandif-modified-sincefor efficient file downloads and caching - Remote-run is a CLI tool to execute javascript from a remote URL
Usage
Install & Setup
- npm install:
npm i remote-importor install globally:npm i --global remote-import In Javascript/Typescript:
// During app initialization import RemoteImport from 'remote-import'; RemoteImport.get().init({refreshDuration:10000}); // Optional, call init only if custom configuration is needed // Anywhere else import _ from "https://jspm.dev/lodash"; _.add(1,2);If the dependent modules contain ES6 modules, make sure to add the following parameter when launching the app:
node -r esmIt can also be used as pre-load module, e.g.
node -r remote-importNote it doesn't work if combined with esm on REPL (Not sure why): e.g.
node -r esm -r remote-importis not working if esm is needed in REPL, a pre-load esm onlynode -r esmand manually run require("remote-import") inside REPL.Command line tools
- Installation:
npm install -g remote-importoryarn global add remote-import remote-run: Directly invoke JS on remote URL:npm install -g remote-import remote-run https://raw.githubusercontent.com/treedoc/remote-import/main/sample/sample.js argsremote-node: Start a node REPL session with remote import support
- Installation:
- For more live examples, please refer to folder sample
- Custom configuration by calling
RemoteImport.get().init(config)refer to class RemoteImportConfig
Future Enhancement
- Add URL rules to allow list URL and indicate if the URL is immutable for security reasons.
- Support typescript
Contributions
Even this library may provide useful functions. It's still rudimentary—lots of opportunities for enhancements. If you find any issues or have any better ideas, you are welcome to open Github issues or submit PRs.
License
Copyright 2021 TreeDoc.org Author/Developer: Jianwu Chen
Use of this source code is governed by an MIT-style license that can be found in the LICENSE file or at https://opensource.org/licenses/MIT.