Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Deno Node 両刀

Koutarou Chikuba
March 17, 2022
2.3k

Deno Node 両刀

Node 学園 #38

Koutarou Chikuba

March 17, 2022
Tweet

Transcript

  1. Deno ͍͍ΑͶ… • ࣄલ install ෆཁ: εχϖοτ͕ಈ͘อূ͕ͱΓ΍͍͢ • => node+ts

    ͸࣮ߦ؀ڥ͕ࣗ໌Ͱ͸ͳ͍ • ΤίγεςϜ͕ TS ܾΊଧͪ • ૊ΈࠐΈ lint | fmt | test ͷମݧͷྑ͞ • => node ͸ eslint+typescript+prettier ؀ڥͷߏங͕ͩΔ͍ • Deno ຊମͷίʔυ͕ಡΈ΍͍͢ • Rust Ͱ tokio + rusty-v8(v8 Binding)
  2. node: ͜ΕͲ͏΍ͬͯಈ͘ͷʁ // native esm ͔ɺ comnonjs ܦ༝͔ෆ໌ import foo

    from "foo"; // module.exports? export default? import { xxx } from "@/x"; // ͩΕ͕͜ͷύεΛղܾ͢Δʁ; // ؀ڥม਺ͳͷ͔ɺόϯυϥʹ replace ͞ΕΔͷ͔ console.log(process.env.NODE_ENV);
  3. Deno (ͷར༻ऀ)ͷ՝୊ • طଘࢿ࢈ͷར༻ • طଘͷ΋ͷ͸ Node ༻ͩ͠… • ҆શੑ͕આ໌Ͱ͖Δ͔

    • ಈతͳωοτϫʔΫ૬खʹґଘղܾ͢Δෆ҆ • ։ൃମݧ • vscode ͕ͪΌΜͱରԠͯ͘͠Εͳ͍ͱ࢖͑ͳ͍ΑͶ
  4. ܗ͔ΒೖΔ Deno • node Ͱ΋ lint ͱ fmt ͚ͩआ༻͠Α͏ͥʂ •

    ࢀߟ: node ϓϩδΣΫτͰ΋ deno lint | deno fmt ͢Δ https:// zenn.dev/mizchi/articles/just-lint-by-deno • deno.jsonc Λஔ͘ $ deno fmt $ deno lint eslint+typescript+prettier ૬౰
  5. ґଘղܾ: Ϟδϡʔϧ Deno ͸ωοτϫʔΫܦ༝ͰґଘΛղܾ͢Δ // node: npm install zod --save

    import { z } from "zod"; // deno import { z } from "https://deno.land/x/[email protected]/mod.ts";
  6. ґଘղܾ: ϩʔΧϧ ֦ுࢠ/σΟϨΫτϦͷলུෆՄ // node + tsc import { foo

    } from "./foo"; import { bar } from "./bar"; // deno import { foo } from "./foo.ts"; import { bar } from "./bar/index.ts"; ※ node Ͱ΋ Native ESM ͷͱ͖ɺdeno ͱಉ֦͘͡ுࢠলུෆՄ
  7. Deno ͕ Node ޓ׵Ͱ͸ͳ͍ཧ༝ • ry ʮDeno ͷ࣮ߦ͸ݖݶ෇༩ͱηοτͳͷͰɺ৴པͰ͖ͳ͍ωοτϫʔ Ϋ͔ΒґଘΛղܾͯ͠΋໰୊ͳ͍ʯ (ޙड़)

    • ֦ுࢠͷলུෆՄ͸ RTT ΛݮΒͨ͢Ίʹඞཁ • node: ./foo => foo/package.json#main, foo/index{.*}, foo{.*} • ͦ΋ͦ΋ ESM ͷ࢓༷ʹύεͷ࣮ମղܾϧʔϧ͸ఆٛ͞Εͯͳ͍ • ϒϥ΢β্ͷ ESM Ͱ΋جຊతʹ֦ுࢠলུෆՄ(※ઃఆʹΑΔ)
  8. First Step: ؾ͚࣋ͪͩ Deno • Rust crate ෩: mod.rs Ͱ֎ʹ

    export • deps.ts Ͱ֎෦ϞδϡʔϧΛ import // mylib/deps.ts export { z } from "https://deno.land/x/[email protected]/mod.ts"; // mylib/mod.ts import { z } from "./deps.ts"; export const version: number = 1; export function validate(input: any) { return z.string().safeParse(input) }; (งғؾΛ஌͍ͬͯΕ͹ޙͰೃછΈ΍͍͢ɺؾ͕͢Δ…) • ۃ࿦ɺ֎෦ import ΋ Deno.* ΋࢖Θͳ͍୯ମϑΝΠϧ͸྆༻Մ
  9. Deno ͷηΩϡϦςΟ • --allow-env=<allow-env> • --allow-hrtime • --allow-net=<allow-net> • --allow-read=<allow-read>

    • --allow-write=<allow-write> • --allow-ffi • --allow-run=<allow-run>: • -A, --allow-all
  10. Deno ͷηΩϡϦςΟ: ਅ໘໨ʹॻ͘ deno deploy ͷ CLI Πϯετʔϧͷྫ $ deno

    install \ --allow-read \ --allow-write \ --allow-env \ --allow-net \ --allow-run \ --no-check -r -f https://deno.land/x/deploy/deployctl.ts
  11. Deno ͷηΩϡϦςΟ: ๞͖ͯ͜͏ͳΔ # શ෦ڐՄ $ deno run -A build.ts

    # velociraptor (task runner) ͷ install ྫ $ deno install -qAn vr https://deno.land/x/[email protected]/cli.ts
  12. ๷Ӵࡦ: ಈతURLΛݕ஌ • αϓϥΠνΣʔϯΞλοΫͰಈత URL ͸߈ܸର৅ • https://deno.land/std/http/server.ts • =>

    https://deno.land/[email protected]/http/server.ts • ࠓޙ΋҆શͳ URL ͔୭͕ݴ͑Δͷ͔ʁ • nest.land: ϒϩοΫνΣʔϯ্ͷύʔϚωϯτͳ URL ʹσϓϩΠ • deno vendor (v1.19): node_modules ͱಉ͡Α͏ʹϩʔΧϧʹݻΊͯ ͔Β࣮ߦ
  13. Deno ͔Β npm ΛݺͿ: CDN • ୯ମϑΝΠϧ(όϯυϧࡁΈ)ͳΒޓ׵ • CDN ͕

    X-Typescript-Types Λฦ͍ͯ͠Ε͹ɺ.d.ts Λ͔ͦ͜Βղ ܾ • package.json ͷ types Λ࢖͏ • skypack.dev ΍ esm.sh ͕ରԠ // ͜Ε͕ܕ෇Ͱ࢖͑Δ import React from "https://cdn.esm.sh/react@17";
  14. deno ͔Β node ΛݺͿ: deno --compat https://deno.land/[email protected]/node import { createRequire

    } from "https://deno.land/[email protected]/node/module.ts"; const require = createRequire(import.meta.url); const path = require("path"); $ deno run --compat --unstable --allow-read deno-compat.ts • https://deno.land/std/node ʹޓ׵ϨΠϠʔͷ࣮૷͕͋Δ • ͋Μ·Γಈ͍ͯͳ͍
  15. node ͔Β deno ݺͿ: dnt • https://github.com/denoland/dnt • ࢀߟ: deno

    to node ͷ dnt ࢼ͢ https://zenn.dev/mizchi/scraps/ a37ea7c793094f // ͜͏͍͏ίʔυΛల։ export { delay } from "https://deno.land/x/[email protected]/mod.ts"; • https://...ͷ ϞδϡʔϧΛϩʔΧϧʹղܾ • Deno Λ node ༻ʹม׵ => import { Deno } from "@deno/shim- deno";
  16. dnt ʹΑΔల։ྫ npm/ !"" LICENSE !"" README.md !"" esm #

    !"" deps # # $"" deno_land_x # # $"" delay_v0.2.0 # # !"" mod.js # # $"" src # # $"" delay.js
  17. Node Λ Deno ͬΆ͘ಈ͔͢ $ node --experimental-network-imports code.js Node ͷ

    Deno ෩ network-import (experimental) https://nodejs.org/api/esm.html#https-and-http-imports // file.mjs import worker_threads from 'worker_threads'; import { configure, resize } from 'https://example.com/imagelib.mjs'; configure({ worker_threads });
  18. VSCode • vscode_deno ΛΠϯετʔϧͯ͠࠶ىಈ https://github.com/ denoland/vscode_deno • Shift+Command+P ͰҎԼͷίϚϯυΛ࣮ߦ •

    Deno: Initialize Workspace Configuration • Deno: Cache Dependencies // .vscode/settings.json { "deno.enabled": true }
  19. vscode: Node Deno ڞଘ ## ௨ৗͷ Node .vscode/settings.json # "deno.enabled":

    false ## ͚ͩ͜͜ Deno deno/ .vscode/settings.json # "deno.enabled": true code . code deno --add ※೔ʹΑͬͯಈ͍ͨΓಈ͔ͳ͔ͬͨΓ͠·͢
  20. ࣗ෼͕ࢥ͏ Deno ͷݱ࣌఺ͷϢʔεέʔε • ॻ͖ࣺͯͷ CLI (εχϖοτ࣮ߦՄೳੑ͕ߴ͍) • ϏϧυπʔϧνΣΠϯ •

    Rust ࿈ܞ෦෼ • αʔόʔӡ༻͸೉͍͚͠Ͳ deno deploy ͳΒϑϧϚωʔδυͰ͋Γ