evanw / esbuild

An extremely fast bundler for the web
https://esbuild.github.io/
MIT License
38.02k stars 1.14k forks source link

onResolve filter by 'kind' #1548

Open unilynx opened 3 years ago

unilynx commented 3 years ago

A plugin's onResolve can filter by path and namespace, but it would be nice to add 'kind' to this too

My use case: I want to allow

.test_external_asset_css
{
  background: url(/.mydesign/test_external_asset_css.png);
}

to point to an external asset without further processing. onResolves receives this path as follows from the css loader:

{
  path: '/.mydesign/test_external_asset_css.png',
  importer: '/Users/unilynx/tests/dependencies/base-for-deps.css',
  namespace: 'file',
  resolveDir: '/Users/unilynx/tests/dependencies',
  kind: 'url-token',
  pluginData: undefined
}

But almost every path starts with '/', making the regex filter /^\// not that effective. Filtering on url-token would limit the amount of paths sent back to JavaScript for resolving.

AielloChan commented 1 year ago

Also wanna filter by importer, because I wanna figure out all the first required packages, I just do like bellow:

module.exports = {
  name: 'onlyShallowDependencies',
  setup(build) {
    build.onResolve({ filter: /./ }, args => {
      if (/node_modules/.test(args.importer)) {
        return { path: args.path, external: true }
      } else {
        return undefined
      }
    })
  },
}

Which will mark package's requirement as external!

Maybe like this: 🤪

module.exports = {
  name: 'onlyShallowDependencies',
  setup(build) {
    build.onResolve({ filter: { filed: 'importer', regex: /node_modules/ } }, args => {
        return { path: args.path, external: true }
    })
  },
}