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

resolving @import #2039

Open
tcurdt opened this issue Oct 26, 2015 · 13 comments
Open

resolving @import #2039

tcurdt opened this issue Oct 26, 2015 · 13 comments

Comments

@tcurdt
Copy link

tcurdt commented Oct 26, 2015

While the docs here are great https://github.com/stylus/stylus/blob/dev/docs/import.md they left me wondering how to control the loading/resolving of imports. I am using stylus on Node:

  var stylus = Stylus("....");
  stylus.set('filename', file.path);
  stylus.render(function(err, result){
    ...
  });

Is this possible at all?

@Panya
Copy link
Member

Panya commented Oct 27, 2015

What do you mean by "loading/resolving"?

@tcurdt
Copy link
Author

tcurdt commented Oct 27, 2015

I want to tell Stylus where to find the imports - or even provide them.

Similar to https://mozilla.github.io/nunjucks/api.html#loader or https://github.com/less/less-plugin-npm-import/blob/master/lib/npm-file-manager.js

@Panya
Copy link
Member

Panya commented Oct 27, 2015

I see. Unfortunately Stylus' importer is not extensible in this way currently.

@tcurdt
Copy link
Author

tcurdt commented Oct 27, 2015

@Panya Urgh - that's unexpected

Stylus has one of the nicest APIs from the other alternatives out there. So how do we fix this oversight? :)

If you guide me a little I might be able to help.

@tcurdt
Copy link
Author

tcurdt commented Oct 27, 2015

Seems like this is where things need to change a little
https://github.com/stylus/stylus/blob/dev/lib/visitor/deps-resolver.js#L112

@Panya
Copy link
Member

Panya commented Oct 28, 2015

It will be a little bit harder :) We need to extract default logic for importing files from the evaluator. These methods are importFile (messy) and visitImport.

@tcurdt
Copy link
Author

tcurdt commented Oct 28, 2015

@Panya well, that would have been too easy otherwise ;)

Maybe we start thinking about the API and then see how it could be implemented in those places.
Since I just looked at the various APIs of different libraries I have a pretty clear idea of what I would love to see:

resolve = function(absPathFrom, includeArgument, cb) {
  var absPathTo = path.resolve(absPathFrom, includeArgument);
  var content = fs.readFileSync(absPathTo, 'utf-8');
  cb(null, content);
}

The absPathFrom would be the absolute path of the file including and includeArgument the argument passed to the include statement.

Sounds sane?

@Panya
Copy link
Member

Panya commented Oct 29, 2015

Yep, but you also need to know that all internal Stylus' processes are currently synchronous, so that would limit the functionality to synchronous-only resolvers (otherwise we need to go deeper :)).

@tcurdt
Copy link
Author

tcurdt commented Oct 29, 2015

Synchronous is fine - for me, for now :)
I would still use callback style for the API though

@SamHwang1990
Copy link

Hi~~~
Is there any ways to allow optinal @import file like less?
The mainly purpose of this demand is to let stylus to import a file which may be not existed without throwing error.

@tcurdt
Copy link
Author

tcurdt commented Nov 6, 2015

@SamHwang1990 that sounds like a very different issue. Better open an individual issue for it.

@tcurdt
Copy link
Author

tcurdt commented Nov 6, 2015

@Panya tag as enhancement request?

I will see if I can find some time to dig into this (fingers crossed) but as less and sass is working fine the urge is not that strong. Would love to support stylus though.

@SamHwang1990
Copy link

Sorry about my comment~~~
I have open a new issue about it. #2041
Thank you! @tcurdt

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

No branches or pull requests

3 participants