lightyen/typescript-paths

rollup-plugin-tsconfig-paths: Pass along resolve options in resolveId hook

lukastaegert opened this issue · 2 comments

Hi!

While debugging rollup/plugins#1038 (comment), I stumbled upon an issue with your rollup plugin implementation. Basically you are doing this:

async resolveId(request: string, importer?: string) {
  // ...
  if (!moduleName) {
    return this.resolve(request, importer, { skipSelf: true })
  }
  // ...
}

Unfortunately, there is a third parameter to resolveId that @rollup/plugin-node-resolve (and @rollup/plugin-commonjs) depends upon, which you do not pass along. It is encodes whether we are resolving an entry point and additional custom plugin options which for the node-resolve plugin encode if we are resolving an import or a require. Depending on the package, this can lead to different (and wrong) resolutions.

See https://rollupjs.org/guide/en/#resolveid including the first example there, as well as https://rollupjs.org/guide/en/#custom-resolver-options for how custom options work, and why it is important for plugins to forward them.

The solution can be as simple as

async resolveId(request: string, importer: string | undefined, options: {isEntry: boolean, custom?: {[plugin: string]: any}) {
  // ...
  if (!moduleName) {
    return this.resolve(request, importer, { skipSelf: true, ...options })
  }
  // ...
}

I would strongly advise to update your implementation to support this.

Thank you for the feedback!

Wow, this was fast, great work!