Skip to content

Replace resolve with require.resolve #12

Description

@drwpow-figma

Hello! Thanks for taking on maintenance of this plugin, you’ve done a great job!

Problem

One issue with the original rollup-plugin-styles is the old, outdated module resolution handled by the resolve package. Specifically, it doesn’t respect package.json exports that are essential and standard nowadays.

For example, we have a pnpm monorepo, and in one package we have .css files in the ./dist/ folder that we shorten the import path for:

{
  "exports": {
    "./*.css": "./dist/*.css"
  }
}

So you’d import them at @import "local-pkg/foo.css" instead of @import "local-pkg/dist/foo.css". This plugin gives the following error:

(plugin styles) Error: Failed to find 'local-pkg/foo.css'
  in [
    ~/components/src/button
  ]
src/button/button.css

This is a longstanding problem with resolve, which has fallen behind modern ESM support.

Suggested solution

Simply replacing with Node’s built-in require.resolve() should perform much better, would support all of Node’s new ESM features and resolution. Node’s built-in resolve has come a long way and is now more mature than the old resolve package.

For example, my error comes from utils/resolve.ts, where the following change works:

- const resolverAsync = async (id: string, options: AsyncOpts = {}): Promise<string | undefined> =>
  new Promise(resolve => resolver(id, options, (_, res) => resolve(res)));
+ const resolverSync = (id: string, options: AsyncOpts = {}): string | undefined => {
+   try {
+     return require.resolve(res, { paths: [options.basedir] })
+   } catch {
+     // noop
+   }
+ };

Note: this is just an example; there’d be other related changes, but you get the idea

Would be happy to help with this change if this is desirable 🙂

Activity

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

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions