Skip to content
This repository was archived by the owner on Sep 2, 2023. It is now read-only.

Terminology: Loader Phases #205

Closed
jkrems opened this issue Oct 17, 2018 · 5 comments
Closed

Terminology: Loader Phases #205

jkrems opened this issue Oct 17, 2018 · 5 comments

Comments

@jkrems
Copy link
Contributor

jkrems commented Oct 17, 2018

I'm not sure if this is a better fit for terminology or for a more implementation-focussed discussion. But I would suggest a separation of concerns when talking about module loading. The outcome would hopefully be a clear description of what happens when and what phases are responsible for which concerns.

One possible variant of this can be found here: https://github.com/jkrems/loader#module-loading

For ease of discussion, inlining the content below:

Module Loading

Module loading is split into three phases:

  1. Module resolution
  2. Resource fetching
  3. Module init

Module Resolution (resolve)

Given a specifier: string and referrerURL: string,
provide a url: string or a set of potential urls: string[] of a resource:

const resolve: (specifier: string, referrerURL: string) => string | string[];

Resource Fetching (fetch)

Given a resource url: string,
fetch the resource content and associated meta data.

type Resource = {
  bytes?: Buffer,
  contentType: string,
  contentTypeParameters?: string,
};

const fetch: (url: string) => Resource;

Module Init (init)

Given a resource: Resource and a target: Module module handle,
initialize the target.
Most implementations will check the resource.contentType
to select the appropriate behavior.

const init: (target: Module, resource: Resource, Module) => void;
@jkrems
Copy link
Contributor Author

jkrems commented Oct 17, 2018

Custom MIME types could be supported by a fetch hook:

async function detectTypescriptFetch(parent, url) {
  const resource = await parent(url);
  if (resource.contentType === null && /\.ts$/.test(url)) { // "unknown contentType"
    resource.contentType = 'typescript';
  }
  return resource;
}

@SMotaal
Copy link

SMotaal commented Oct 17, 2018

On that example, I would also consider:

  1. no extension
  2. url including query or fragment

Did you think of ways to deal with those?

@jkrems
Copy link
Contributor Author

jkrems commented Oct 17, 2018

@SMotaal That's an orthogonal concern. The above was meant to illustrate handling a content type that isn't covered by a built-in mime DB. Handling query or fragments is fairly simple and doesn't change anything fundamentally. No extension will always require an additional out-of-band signal. :)

@SMotaal
Copy link

SMotaal commented Oct 24, 2018

#208 relates if we have a "ModuleSourceText" interface that can be used to end the fetching portion (ie resolve blesses it, fetch allows it)

@jkrems
Copy link
Contributor Author

jkrems commented Nov 26, 2018

Replaced by #232

@jkrems jkrems closed this as completed Nov 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants