Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add "fileResolve" option for deep import resolving #142

Closed
wants to merge 3 commits into from

Conversation

KingSora
Copy link
Contributor

@KingSora KingSora commented Oct 11, 2022

Good day!

I wanted to use the resolve option in order to resolve files from the compose: import. Unfortunately this option only gives me the possibility to resolve the "first layer" of composes.

Consider the following setup:

// a.css
.a {
  composes: b from "./b.css";
}
// b.css
.b {
  composes: c from "./c.css";
}
// c.css
.c {
  content: 'c';
}

In this setup the a.css file is my entry point. The resolve function will only be fired for the composes: b from "./b.css" statement. The "deeper" import inside b.css is completely opaque.

For this usecase I've implemented the fileResolve option in a non-breaking manner. This function will be called for all composes statements no matter how deep they are.

  • It receives two arguments file and importer, both of which are strings. The file is essentially the part between the brackets after the from keyword (same as the first argument of the resolve option). The importer is the file from which this compose statements comes from.
  • It has to return string | null or a Promise<string | null>. The returned string has to be an absolute path. If null is returned the default resultion takes place.

I haven't done that, but with this change I would propose to deprecate the current resolve option, because the new fileResolve
option is a bit more powerful, still straight forward to use and can still do the same thing as the resolve function.

The README would also need an update, but I would do that after I the feedback from this PR.

Fixes: #131

@madyankin
Copy link
Owner

@KingSora thanks for the PR, I really appreciate your contribution. I have no time to implement features myself now, so this is a huge help for me.

I’ll review the PR in a few days.

@KingSora KingSora mentioned this pull request Oct 12, 2022
@KingSora
Copy link
Contributor Author

@madyankin looking forward for the review! I've also created another PR which should fix sourcemaps #143

@KingSora
Copy link
Contributor Author

@madyankin just a little reminder :)

@madyankin
Copy link
Owner

@KingSora thanks for pinging me. I have a todo in my Things, but now I'm relocating to another country, so a bit overwhelmed. Will have some time for the review next week

Copy link
Owner

@madyankin madyankin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KingSora hey, I found some time to review the PR. I hope you're not demotivated by such a long time waiting for the review. Could you please make a few changes in the PR?

path.resolve(this.root, relativeDir),
newPath
);
(useFileResolve
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rewrite this using async/await. I'm thinking of rewriting all the promises into async/await in the future

src/css-loader-core/loader.js Show resolved Hide resolved
@@ -50,6 +50,11 @@ declare interface Options {
Loader?: typeof Loader;

resolve?: (file: string) => string | Promise<string>;

fileResolve?: (
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 for updating the typings

@@ -85,6 +85,15 @@ module.exports = (opts = {}) => {
if (resultPluginIndex === -1) {
throw new Error("Plugin missing from options.");
}
// resolve and fileResolve can't be used together
Copy link
Owner

@madyankin madyankin Oct 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for thinking about backwards compatibility. Anyway, I'd prefer to introduce a breaking change and bump the major version. So, let's remove the original resolve version, replace it with yours, and update the readme. Feel free to use the resolve option name for this.

@madyankin madyankin closed this Nov 4, 2022
@KingSora
Copy link
Contributor Author

KingSora commented Nov 4, 2022

@madyankin please leave it open. I'll come back to it as soon as possible (next week possibly) I just need to finish an different project first

@madyankin
Copy link
Owner

@KingSora don't worry — I have merged it and fixed the things I asked. Thank you for the implementation!

I'm going to release the new version after merging #143 — there are some failing tests. Could you take a look please?

@KingSora
Copy link
Contributor Author

KingSora commented Nov 4, 2022

@madyankin on it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose importer in resolve method
2 participants