Open9
deno to node の dnt 試す
dnt の READEM に従う
import { build } from "https://deno.land/x/dnt/mod.ts";
await build({
entryPoints: ["./mod.ts"],
outDir: "./npm",
shims: {
deno: true,
},
package: {
name: "@mizchi/deno-pkg-test",
version: Deno.args[0],
description: "deno-pkg-test",
license: "MIT",
repository: {
type: "git",
url: "git+https://github.com/mizchi/deno-pkg-test.git",
},
bugs: {
url: "https://github.com/mizchi/deno-pkg-test/issues",
},
},
});
// post build steps
Deno.copyFileSync("LICENSE", "npm/LICENSE");
Deno.copyFileSync("README.md", "npm/README.md");
適当なファイルを置く
deps.ts
export { delay } from "https://deno.land/x/delay@v0.2.0/mod.ts";
main.ts
export const run = () => console.log("run");
mod.ts
import { run } from "./main.ts";
export const x = 1;
export { run };
export { delay } from "./deps.ts";
意図としてはローカル解決、リモート解決、ESM export をしたらどうなるかを確認したい
dnt は README.md
と LICENSE
のファイルを要求してくるので置く
README.md
# deno-pkg-test
LICENSE
Copyright 2022 mizchi
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
実行する
$ deno run -A ./compile_to_node.ts
$ tree npm/
npm/
├── LICENSE
├── README.md
├── esm
│ ├── deps
│ │ └── deno_land_x
│ │ └── delay_v0.2.0
│ │ ├── mod.js
│ │ └── src
│ │ └── delay.js
│ ├── deps.js
│ ├── main.js
│ ├── mod.js
│ └── package.json
├── node_modules
│ ├── ansi-styles
│ │ ├── index.d.ts
│ │ ├── index.js
│ │ ├── license
│ │ ├── package.json
│ │ └── readme.md
│ ├── chalk
│ │ ├── index.d.ts
│ │ ├── license
│ │ ├── package.json
│ │ ├── readme.md
│ │ └── source
│ │ ├── index.js
│ │ ├── templates.js
│ │ └── util.js
│ ├── color-convert
│ │ ├── CHANGELOG.md
│ │ ├── LICENSE
│ │ ├── README.md
│ │ ├── conversions.js
│ │ ├── index.js
│ │ ├── package.json
│ │ └── route.js
│ ├── color-name
│ │ ├── LICENSE
│ │ ├── README.md
│ │ ├── index.js
│ │ └── package.json
│ ├── has-flag
│ │ ├── index.d.ts
│ │ ├── index.js
│ │ ├── license
│ │ ├── package.json
│ │ └── readme.md
│ └── supports-color
│ ├── browser.js
│ ├── index.js
│ ├── license
│ ├── package.json
│ └── readme.md
├── package-lock.json
├── package.json
├── src
│ ├── deps
│ │ └── deno_land_x
│ │ └── delay_v0.2.0
│ │ ├── mod.ts
│ │ └── src
│ │ └── delay.ts
│ ├── deps.ts
│ ├── main.ts
│ └── mod.ts
├── test_runner.js
├── types
│ ├── deps
│ │ └── deno_land_x
│ │ └── delay_v0.2.0
│ │ ├── mod.d.ts
│ │ └── src
│ │ └── delay.d.ts
│ ├── deps.d.ts
│ ├── main.d.ts
│ └── mod.d.ts
└── umd
├── deps
│ └── deno_land_x
│ └── delay_v0.2.0
│ ├── mod.js
│ └── src
│ └── delay.js
├── deps.js
├── main.js
├── mod.js
└── package.json
28 directories, 60 files
例えば https で依存を解決した deps.ts はこう変換されている。
npm/src/deps.ts
export { delay } from "./deps/deno_land_x/delay_v0.2.0/mod.js";
mapping で 解決ルールを手動で定義することもできそう。
await build({
// ...etc...
mappings: {
"https://deno.land/x/code_block_writer@11.0.0/mod.ts": {
name: "code-block-writer",
version: "^11.0.0",
},
},
});
node から呼ぶ。
// yarn workspace として参照
import { run } from "@mizchi/deno-pkg-test";
// local 参照
import { run } from "./npm/esm/index.js";
run();
TODO: Deno.readFile 等がどう動くか確認
やってることが複雑なので、外部依存がなく単一ファイルにコンパイルできるなら rollup 等で固めてしまったほうがいい気がする。
やってることが結構複雑。細かいオプションは全部切ってESM出力に絞る、みたいなことをやったほうがいい。
あくまで Deno First で作って node で使う口をあけるときに、使えることがあるかも、という感じ。
Deno.readTextFile() がどう変換されるか
main.ts
export const run = () => {
Deno.writeTextFile("./out", "Hello World");
console.log("run");
};
こうなる
npm/src/main.ts
import * as dntShim from "./_dnt.shims.js";
export const run = () => {
dntShim.Deno.writeTextFile("./out", "Hello World");
console.log("run");
};
dntShim というものに変換される。
npm/src/_dnt.shims.ts
import { Deno } from "@deno/shim-deno";
export { Deno } from "@deno/shim-deno";
const dntGlobals = {
Deno,
};
export const dntGlobalThis = createMergeProxy(globalThis, dntGlobals);
// deno-lint-ignore ban-types
function createMergeProxy<T extends object, U extends object>(
baseObj: T,
extObj: U,
): Omit<T, keyof U> & U {
return new Proxy(baseObj, {
get(_target, prop, _receiver) {
if (prop in extObj) {
return (extObj as any)[prop];
} else {
return (baseObj as any)[prop];
}
},
set(_target, prop, value) {
if (prop in extObj) {
delete (extObj as any)[prop];
}
(baseObj as any)[prop] = value;
return true;
},
deleteProperty(_target, prop) {
let success = false;
if (prop in extObj) {
delete (extObj as any)[prop];
success = true;
}
if (prop in baseObj) {
delete (baseObj as any)[prop];
success = true;
}
return success;
},
ownKeys(_target) {
const baseKeys = Reflect.ownKeys(baseObj);
const extKeys = Reflect.ownKeys(extObj);
const extKeysSet = new Set(extKeys);
return [...baseKeys.filter((k) => !extKeysSet.has(k)), ...extKeys];
},
defineProperty(_target, prop, desc) {
if (prop in extObj) {
delete (extObj as any)[prop];
}
Reflect.defineProperty(baseObj, prop, desc);
return true;
},
getOwnPropertyDescriptor(_target, prop) {
if (prop in extObj) {
return Reflect.getOwnPropertyDescriptor(extObj, prop);
} else {
return Reflect.getOwnPropertyDescriptor(baseObj, prop);
}
},
has(_target, prop) {
return prop in extObj || prop in baseObj;
},
}) as any;
}
@deno/shim-deno
はここ
一応 denoland 傘下なので公式パッケージなのかな?