Anidetrix / rollup-plugin-styles

🎨 Universal Rollup plugin for styles: PostCSS, Sass, Less, Stylus and more.
https://anidetrix.github.io/rollup-plugin-styles
MIT License
242 stars 43 forks source link

Fix resolution of node_modules for stylus loader #151

Open javier-garcia-meteologica opened 3 years ago

javier-garcia-meteologica commented 3 years ago

Stylus loader uses the following options to support loading files from node_modules

https://github.com/Anidetrix/rollup-plugin-styles/blob/418a5db352a8b402a723adfe74dc0cc830ba4121/src/loaders/stylus.ts#L22-L24

The problem with this configuration is that it only resolves node_modules correctly if the file sits on the project root. If the file is in <project_root>/src/a/b/c.styl, the extracted dirname will be <project_root>/src/a/b and stylus will be given these paths: <project_root>/src/a/b and <project_root>/src/a/b/node_modules.

failed to locate @import file <imported file>

    at Evaluator.visitImport

The node_modules path was wrong and rollup-plugin-styles should have used <project_root>/node_modules instead. As a quick workaround the following configuration should suffice:

stylesPlugin({
  stylus: {
    paths: [`${process.cwd()}/node_modules`]
  }
}),

I propose to expand tests to cover this case and use this to fix the problem.

 const basePath = normalizePath(path.dirname(this.id)); 

 const paths = [`${process.cwd()}/node_modules`, basePath]; 

If you think this is a good idea, I can submit a PR.

EDIT: Add workaround