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

[JS API] .set("imports", []) overrides internally required imports option #2585

Open
qnp opened this issue Apr 4, 2021 · 2 comments
Open

Comments

@qnp
Copy link

qnp commented Apr 4, 2021

Greetings,

Obsevations

As I coined in Vite project (vitejs/vite#2857), compiling stylus source with the following code will raise an error because .set completely overrides stylus option that are required for correct compilation, such as imports: ['node_modules/stylus/lib/functions/index.styl']:

const stylus = require('stylus')
const source = `$easing := cubic-bezier(0.4, 0.0, 0.2, 1)`
const options = { imports: ['path/to/my/variables.styl'] }

const ref = stylus(source)
// Here if you console.log(ref), you will see that this ref has
// an `import` option with the required internal file I mentioned

Object.keys(options).forEach((key) => ref.set(key, options[key]))
// Fails with "Cannot read property 'nodes' of undefined"
const result = ref.render() 

What is expected

ref.set(...) should merge options properly.

Workarounds are still possible (that is to say, pass options directly to const ref = stylus(source, options), but I had to mention this buggy behgaviour.

Best regards,

@zouhangwithsweet
Copy link

I have the same problem

@iChenLei
Copy link
Member

Yes, this case is cause by design. tj design some built-in functions via functions/index.styl. So PRs welcome.

  options.imports = [join(__dirname, 'functions/index.styl')].concat(options.imports || []);

stylus/lib/renderer.js

Lines 162 to 164 in baba86d

Renderer.prototype.set = function(key, val){
this.options[key] = val;
return this;

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