Skip to content

Instantly share code, notes, and snippets.

@bmeck
Last active August 7, 2018 19:21
Show Gist options
  • Save bmeck/7ee7eb2147e2dafe3167c856d9b4151a to your computer and use it in GitHub Desktop.
Save bmeck/7ee7eb2147e2dafe3167c856d9b4151a to your computer and use it in GitHub Desktop.

File API

** bikeshed **

fs.getFile(url) - https://developer.mozilla.org/en-US/docs/Web/API/File

Can be used by loaders/servers if they wish to request information about the file's MIME as file.type.

It will get the .type by checking the context for the current MIME DB, falling back to the globally MIME DB.

Preconfigured MIME DBs

There are a few builtin DBs that are preconfigured for purposes. These are the relevant file extensions to each.

Extension "cjs" (default) "esm" "web"
.js application/node text/javascript; goal=module text/javascript
.mjs text/javascript; goal=module text/javascript; goal=module text/javascript; goal=module
.node application/vnd.node.addon application/vnd.node.addon application/vnd.node.addon
.json application/vnd.node.json application/json application/json

The null DB

Represented by either the value null or the string "null".

It is a void, it always resolves to "" instead of a MIME for any URL.

"mimes" field for "package.json

{
  "mimes": "esm"
}

Put one in each directory as desired.

This MIME DB is only used inside of package boundaries.

It accepts either an array or string.

As an array it will go down the list until a DB returns a value. The null DB will prevent accessing the global DB.

{
  "mimes": [null, "esm"]
}

Would effectively prevent any MIMEs from resolving inside of the package boundaries because the null DB would be used and always return a value of "" and prevent the resolution from reaching "esm".

URLs may be used and loaded instead of just preset values.

"--mimes" flag for CLI (repeatable)

node --mimes="/path/to/react/jsx-mime.db" app.jsx

Use the null DB if you need to compeltely exclude the builtin DB.

"--entry-mime" flag for CLI

cat http-server | node --entry-mime="text/javascript"

CLARIFICATION

DBs should be able to be provided by users and was always intended in this gist, a custom DB such as the following:

Extension "custom"
.js text/javascript
.cjs application/node
.mjs text/javascript; goal=module

Should be able to be loaded via w/e mechanisms load DBs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment