跳转至

Loaders#

History

Version

Changes

v18.6.0, v16.17.0

Add support for chaining loaders.

v16.12.0

Removed getFormat, getSource, transformSource, and globalPreload; added load hook and getGlobalPreload hook.

v8.8.0

Added in: v8.8.0

Stability: 1 - Experimental

This API is currently being redesigned and will still change.

To customize the default module resolution, loader hooks can optionally be provided via a --experimental-loader ./loader-name.mjs argument to Node.js.

When hooks are used they apply to each subsequent loader, the entry point, and all import calls. They won't apply to require calls; those still follow CommonJS rules.

Loaders follow the pattern of --require:

1
2
3
4
node \
  --experimental-loader unpkg \
  --experimental-loader http-to-https \
  --experimental-loader cache-buster

These are called in the following sequence: cache-buster calls http-to-https which calls unpkg.

Hooks#

Hooks are part of a chain, even if that chain consists of only one custom (user-provided) hook and the default hook, which is always present. Hook functions nest: each one must always return a plain object, and chaining happens as a result of each function calling next<hookName>(), which is a reference to the subsequent loader's hook.

A hook that returns a value lacking a required property triggers an exception. A hook that returns without calling next<hookName>() and without returning shortCircuit: true also triggers an exception. These errors are to help prevent unintentional breaks in the chain.

resolve(specifier, context, nextResolve)#

History

Version

Changes

v18.6.0, v16.17.0

Add support for chaining resolve hooks. Each hook must either call nextResolve() or include a shortCircuit property set to true in its return.

v17.1.0, v16.14.0

Add support for import assertions.

The loaders API is being redesigned. This hook may disappear or its signature may change. Do not rely on the API described below.