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

Module Harmony について

Module Harmony について

2024/05/31 にCTO協会LTで発表した Module Harmony についてです。

Yosuke Furukawa

May 31, 2024
Tweet

More Decks by Yosuke Furukawa

Other Decks in Programming

Transcript

  1. ͜Μ͚ͩ͋Δ • Import attributes • Module Expressions • Module Declarations

    • Module local imports • Module Source imports • Module Instance imports • Deferred import evaluation • Module loader hooks • Module source static analysis • Virtual Module sources
  2. Θ͚Θ͔ΒΜ͘ͳͬͯΔ Module Source Module Instance Module loader hooks Source import

    instance import Module Expressions Module Declarations imports from local bindings Module Source Static Analysis Virtual module sources Deferred import Import attributes ґଘؔ܎໼ҹͷઌ͕ґଘઌ
  3. Θ͚Θ͔ΒΜ͘ͳͬͯΔ Module Source Module Instance Module loader hooks Source import

    instance import Module Expressions Module Declarations imports from local bindings Module Source Static Analysis Virtual module sources Deferred import Import attributes ґଘؔ܎໼ҹͷઌ͕ґଘઌ ґଘͳ͠ɺಠཱͯ͠ਐΊΒΕΔ ඃґଘڧ͠ɻ ૣ͘ίϯηϓτΛܾΊͳ͍ͱ ଞͷ΍ͭਐΊΒΕͳ͍ɻ
  4. Θ͚Θ͔ΒΜ͘ͳͬͯΔ Module Source Module Instance Module loader hooks Source import

    instance import Module Expressions Module Declarations imports from local bindings Module Source Static Analysis Virtual module sources Deferred import Import attributes ґଘؔ܎໼ҹͷઌ͕ґଘઌ ࢓༷ͱܾͯ͠·Δ·Ͱԕͦ͏ɻ ૬ޓґଘͯ͠Δɾɾɾ
  5. import attributes • ੲ import assertions ͬͯݴΘΕͯͨ΍ͭ • import ͢ΔࡍʹͲΜͳܗࣜͷ΋ͷΛimport͢Δͷ͔એݴͰ͖Δ

    • JSONΛimport͢Δ࣌ʹ࢖͏ͷ͕ݱࡏͷओͳϢʔεέʔε • ͦͷ͏ͪଞͷ΋ͷ΋ࢦఆͰ͖ΔΑ͏ʹ͢Δ͔΋ɻ import foo from "./bar.json" with { type: "json" }; import ("./baz.json", { with: { type: "json" } })
  6. Deferred import evaluation • import ࣌ʹ஗ԆධՁͰ͖Δ syntax Λ௥Ճ͢Δ࢓༷ • await

    import() Ͱ΋஗Ԇ͸Ͱ͖Δ͕ɺasync function͕ڧ੍͞Εͯ͠ ·͏ɻ import defer * as ns from "./mod.mjs"; function later() { ns.anExport; // ࣮ߦ࣌ʹ mod.mjs ΛධՁ͢Δ }
  7. Module Source ͱ͍͏֓೦ • ϑΝΠϧΛιʔε͔Βimportͨ͠ΒίϯύΠϧ΋͢Δɻ • WebAssembly.instatiateStreamingΛ࢖ΘͣʹϩʔυͰ͖Δ import source x

    from "<speci f ier>"; // wasm ͰϞδϡʔϧΛϩʔυ͢Δͱ͔͕۩ମྫ import source foo from "./foo.wasm";
  8. Module Source Phase Import • JS Ͱ΋ source Λ࢖ͬͯ import

    ͢Δɻ import source foo from "./foo.mjs"; // ͜ͷ࣌఺Ͱ͸ foo ͸ϑΝΠϧͱಉ͡Α͏ͳ֓೦ // worker ͱͯ͠ಡΈࠐ·ͤΔͱ͔Ͱ͖ΔΑ͏ʹ͍ͨ͠ɻ const fooWorker = new Worker(fooModule, { type: "module" });
  9. Module Declaration • module Λ import / export ͠ͳͯ͘΋ϑΝΠϧ಺෦ͰఆٛͰ͖ΔΑ ͏ʹͨ͠΋ͷɻ

    • bundlerͱ͔Ͱͦͷ··moduleΛ࣋ͬͯ͜ΕΔΑ͏ʹɻ module Foo { let i = 0; export function foo() { console.log("foo"); return i++; } }
  10. Module Expression • import จ΋ॻ͚Δɻ let mod = module {

    import { count } from "./db.js"; export let x = 2 * count(); };
  11. Module Instance Imports • ͦ͏΍ͬͯఆٛͨ͠ Module ͷ Instance ͚ͩ Import

    ͢ΔͨΊͷ import จ import module foo from "./foo.js";
  12. Module Instance Imports (local bindings) • ࣗ෼ͷϑΝΠϧ಺Ͱఆٛͨ͠΋ͷ͸ local Ͱ import

    Ͱ͖ΔΑ͏ʹ͢ Δɻ module foo {}; module bar { import foo from foo; // ͜ͷ࣌ʹࣝผࢠ͸จࣈྻ͡Όͳͯ͘΋͍ ͍ɺม਺ͷΑ͏ʹbinding͞Εͨ΋ͷ͕࢖͑Δ
  13. Module loader hooks • source level ͰಡΜͩ module Λϩʔυ͢Δ࣌ʹݕ஌ͯ͠ͻͱखؒ Ճ͑ͯ͘ΕΔ΍ͭɻ

    • Moduleͱͯ͠ context ΛՃ͑ΔࡍʹͳΜ͔৽͍ؔ͠਺௥Ճͨ͠Γͱ ϝλϓϩάϥϛϯάͰ࢖͏ import source foo from "foo.js"; new Module(source, { importHook(speci f ier) { // ... }, });
  14. Module Static Analysis • Module ͷ source Λղੳͨ͠ஈ֊Ͱ import /

    export ͷґଘάϥϑ Λ࡞ΔͨΊͷ৘ใΛఏڙ͢ΔϓϩύςΟ { import : "a", from: "a.js"} { import : "b", as: "c" from: "b.js"} ...
  15. Virtual Module Source • Ծ૝ͷ Module Source Λ࡞ΕΔ࢓༷ • new

    Module Ͱ৽͍͠ModuleΛ࡞ΔࡍʹݩͱͳΔίʔυ͕ͳͯ͘΋object ͔ΒԾ૝ϞδϡʔϧΛ࡞ΕΔ class FooModuleSource { bindings = { // ͜͜ʹ static analysis ͞ΕΔbindingsͷ৘ใ͕དྷΔ }; constructor(text) { this.#object = JSON.parse(text); }; execute(imports) { imports.default = clone(this.#object); }; } const source = new FooModuleSource(); const module = new Module(source);