Run your Deno code in Node.js!
Project is only proof of concept, many cases may not works. Pull requests and discussion are welcome!
- Bundle with Deno
bundle
absent replacement modules fromdeps
section of config. - Babel code to support by specified node version.
- Generate package.json with dependencies.
- Create global variable
Deno
with wome std features (now only fs is supported). - Add calls of npm modules (see Dependencies replacement explanation section).
denode is available as cli tool
deno run -A --unstable https://deno.land/x/denode/mod.ts function.ts ./bundle
Option | Default | Description |
---|---|---|
--config | bundle_config.json | denode configuration file (see description below) |
--importmap | undefined | Load import map file |
JSON file with next structure:
{
"node": "12",
"static": "src/static",
"deps": {
"some-npm-module": {
"name": "some-npm-module",
"npm": "1.1.1",
"global": "someNpmModule",
"path": ".someNpmModule"
},
"https://deno.land/std/fs/mod.ts": {
"std": "fs"
}
}
}
interface IConfig {
node: string; // Target Node.js version;
static?: string; // Static folder path. Denode will copy all files into #BUNDLE_PATH#;
deps?: { // Dependencies, that should be replaced in target bundle (module name from import_map.json or URL);
[module: string]: {
name?: string; // module name ( in result package.json );
npm?: string; // npm version ( in result package.json );
global?: string; // global variable name;
path?: string; // global variable path from root module;
std?: string; // Deno std module stub. Now only 'fs' is supported;
}
}
}
In some cases, there is a need in replace some module in Deno with Node (npm) similiar one or replace Deno native api with Node native.
Example: Code in Deno:
import 'someLib';
declare global {
var Lib: any;
}
Lib.doSomeJob();
...
To replace it with npm module you should add it to bundle_config.json
in deps
section:
"deps": {
"someLib": {
"name": "someLib",
"npm": "^1.1.1"
}
}
This will add "someLib": "^1.1.1"
in package.json
and will add in result bundle code (in start of it):
require('someLib'); // just require module
"deps": {
"someLib": {
"name": "someLib",
"npm": "^1.1.1",
"global": "globalSomeLib"
}
}
Will become:
const someLibModule = require('someLib');
global.globalSomeLib = someLibModule; // add global variable
"deps": {
"someLib": {
"name": "someLib",
"npm": "^1.1.1",
"global": "globalSomeLib",
"path": ".lib"
}
}
Will become:
const someLibModule = require('someLib');
global.globalSomeLib = someLibModule.lib; // path added
Now only fs
module is supported
Example: This code will work in both Deno and Node:
import * as fs from "https://deno.land/[email protected]/fs/mod.ts";
console.log(fs.existsSync('/some/path'));
Config:
"deps": {
"https://deno.land/[email protected]/fs/mod.ts": {
"std": "fs"
}
}