Coder Social home page Coder Social logo

Comments (3)

Jamesernator avatar Jamesernator commented on May 29, 2024

I definitely agree both would be useful, in particular for webassembly import source is what you would be wanting if you are intending to use WebAssembly.instantiate yourself.

import instance is more useful when you just want a module to link/evaluate later, i.e. for transferring to a Worker or just lazily evaluating the module. In these cases you don't really care what the source is (WASM or JS) just that provides the right module namespace when finally imported with import(...).

This is not achievable if this proposal only supports reflecting the module instance (and encourages us to use module.source to get the source) because the import will fail at the first time and there is no chance to get the source.

This is only true if import instance also fetches all recursive dependencies, which Module as proposed doesn't do anyway. It is the import(...) of a module that triggers recursively fetching/linking, so only at that point would you actually hit the error.

i.e. If we had invalid module for browsers:

// some-node-dependent-module.js
import fs from "fs";

// do thing with fs

Then we had a loader than created a browser fs like you're doing (supposing only the equivalent of import instance existed):

import instance browserFs from "browserFs";
// The browser could SPECULATIVELY try to link this module, but failures in linking
// won't be shown till import(...) on someNodeDependentModule is attempted
import instance someNodeDependentModule from "./some-node-dependent-module.js";

// Browser hasn't linked someNodeDependentModule so this is fine
const someNodeDependentModule = new Module(someNodeDependentModule.source, {
    importHook(spec) {
        if (spec === "fs") return browserFs;
    },
});

// But trying this will fail as the browser would now attempt to link it
await import(someNodeDependentModule); // can't resolve "fs"

from proposal-source-phase-imports.

Jack-Works avatar Jack-Works commented on May 29, 2024

This is only true if import instance also fetches all recursive dependencies, which Module as proposed doesn't do anyway. It is the import(...) of a module that triggers recursively fetching/linking, so only at that point would you actually hit the error.

Thanks! You're correct, so instance reflection actually works?

from proposal-source-phase-imports.

lucacasonato avatar lucacasonato commented on May 29, 2024

We are deferring module instance reflection until module expression or module loader hooks progresses.

from proposal-source-phase-imports.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.