-
-
Notifications
You must be signed in to change notification settings - Fork 39
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
Supply a Root stringifier #82
Conversation
@jwilsson code looks good. But I'd like to get some more documentation in the PR for future reference. Some "before" code blocks that demonstrate the problem, and maybe the situation in which we can't pass a stringifier, and some "after" code that shows the fix in action. (this will probably need a blog post from me to explain the change, for which that stuff will be immensely helpful). I haven't seen the situation that this fixes, so that'd be helpful to conceptualize the problem as well. |
@shellscape Sure thing! In It worked earlier when called directly on child nodes to Minimum example demostrating the problem (even when passing the custom stringifier): const postcss = require('postcss');
const postcssLess = require('postcss-less');
const stringify = require('postcss-less/dist/less-stringify');
const lessCode = '@import "foo.less"';
postcss().process(lessCode, {
syntax: postcssLess,
stringifier: stringify,
})
.then((result) => {
console.log(result.root.positionBy({ // This is where it'll currently fail
word: 'foo',
}));
}).catch((error) => {
console.error(error);
}); I hope this makes sense! |
const postcss = require('postcss');
const postcssLess = require('postcss-less');
const stringify = require('postcss-less/dist/less-stringify');
const lessCode = '@import "foo.less"';
postcss().process(lessCode, {
syntax: postcssLess,
stringifier: stringify,
})
.then((result) => {
console.log(result.root.toString()); // Just try to stringify it
}).catch((error) => {
console.error(error);
}); But that can be fixed by passing the stringifier to const postcss = require('postcss');
const postcssLess = require('postcss-less');
const stringify = require('postcss-less/dist/less-stringify');
const lessCode = '@import "foo.less"';
postcss().process(lessCode, {
syntax: postcssLess,
stringifier: stringify,
})
.then((result) => {
console.log(result.root.toString({
stringify: stringify, // Note this change here. It'll will work without this using this PR though
}));
}).catch((error) => {
console.error(error);
}); How pretty/ugly that is, I'll leave unsaid but it won't be needed given the code in this PR. I should've made this more clear in my previous post, sorry. |
I think using the Node.positionBy method would be a fine test of this. If you'd rather I added that after merging this PR, I can certainly take care of that. |
@shellscape No worries, I figured out a simple test for it. And I'm glad the explanation made sense! |
@shellscape Is there anything I can help you with to get a new version up on npm? |
1.1.0 is out |
Which issue # if any, does this resolve?
No issue, see #81 for some related discussion.
I'm not really sure how to test this, if I come up with something I'll complete this PR with it.
Please check one:
This PR:
I discovered that stringifying the
Root
node when it containsImport
nodes would cause problems if thepostcss-less
stringifier wasn't supplied. However, it's not always possible to pass the stringifier and thus, the stringifying would fail.Creating our own
Root
node with a stringifier (extending from the regularpostcss
Root
node) we can supply our stringifier straight from the start and it'll always be used without the caller having to do anything.