Skip to content
iambumblehead edited this page Oct 8, 2023 · 6 revisions

resolvewithplus

resolvewithplus( 'koa', '/root/resolvewith/test/' );
// 'file:///root/resolvewith/node_modules/koa/dist/koa.mjs'

resolvewithplus resolves module paths à la the import.meta.resolve node.js function or the import-meta-resolve npm package. resolvewithplus is only ~5.12kB compared to import-meta-resolve's ~182kB size, at this time of writing. resolvewithplus has limited goals and exists mostly to be small and to resolve module paths for esmock,

  • returns a path string or null, doesn't try to follow node.js' error-handling behaviour,
  • locates modules at the local-filesystem only,
  • locates paths with non-standard extensions such as ".tsx",
  • caches module paths it returns, and returns the same paths for subsequent matching calls,
  • is called the same way as import.meta.resolve, with two parameters "specifier" and a "parent",
  • is not async

resolvewithplus supports optional isbrowser, istypescript fields and when one of these is specified the resolver prioritizes browser-exported files and typecript files,

resolvewithplus('react-dom/server', '/resolvewith/test/', {
  isbrowser: true
}) // 'file:///resolvewith/node_modules/react-dom/server.browser.js'

resolvewithplus also supports an advanced priority option. When a package.json is parsed, the priority list determines the priority of named-exports in this order: browser (if isbrowser), then import then :spectype (import or require, depending upon package.json type) then default. Use the priority list to specify a custom ordering. If you are using nodejs, this is probably not needed,

resolvewithplus('react-dom/server', '/resolvewith/test/', {
  priority: ['deno', 'default']
}) // 'file:///resolvewith/node_modules/react-dom/server.deno.js'

resolvewithplus('react-dom/server', '/resolvewith/test/', {
  priority: ['browser', 'default']
}) // 'file:///resolvewith/node_modules/react-dom/server.browser.js'

A special priority value is ":spectype". This indicates the file type relative to the package type. For example, a package with type "module" resolves "import" values and a package with type "commonjs" resolves "require" values. The ":spectype" placeholder value is used because package type is known at runtime only.

resolvewithplus('react-dom/server', '/resolvewith/test/', {
  priority: [':spectype', 'default']
}) // 'file:///resolvewith/node_modules/react-dom/server.import.js'
Clone this wiki locally