-
-
Notifications
You must be signed in to change notification settings - Fork 209
feat: re-use postcss ast if passed from previous loader #425
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
Conversation
test/options/exec.test.js
Outdated
| test('JSS - {String}', () => { | ||
| const config = { | ||
| loader: { | ||
| test: /style\.js$/, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this test was wrong, wasn't actually testing anything
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to fix it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i did!
alexander-akait
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for information - what is use case? Why do not use postcss plugins directly?
|
I don't necessarily have strong use-case. Thinking a bit proactively. you might want to do: Basically the use case is loaders that use postcss but can't be simplified to just a postcss plugin. |
|
Can you fix tests? |
|
I can update the snapshots, but I believe these were broken before I made any changes, esp since i haven't touched that code... |
alexander-akait
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
|
anything else i can do here? |
test/options/sourceMap.test.js
Outdated
|
|
||
| expect(source).toEqual( | ||
| 'module.exports = "a { color: rgba(255, 0, 0, 1.0) }\\n\\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvZml4dHVyZXMvY3NzL3N0eWxlLmNzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxJQUFJLDJCQUFZLEVBQUUiLCJmaWxlIjoidGVzdC9maXh0dXJlcy9jc3Mvc3R5bGUuY3NzIiwic291cmNlc0NvbnRlbnQiOlsiYSB7IGNvbG9yOiBibGFjayB9XG4iXX0= */"' | ||
| 'module.exports = "a { color: rgba(255, 0, 0, 1.0) }\\n\\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvZml4dHVyZXMvY3NzL3N0eWxlLmNzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxJQUFJLDRCQUFhIiwiZmlsZSI6InRlc3QvZml4dHVyZXMvY3NzL3N0eWxlLmNzcyIsInNvdXJjZXNDb250ZW50IjpbImEgeyBjb2xvcjogYmxhY2sgfVxuIl19 */"' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/cc @jquense It is strange, why source map was changed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't know, this was broken before my PR I think: #425 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jquense maybe postcss have bugs with source maps when we share AST?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no these tests are failing on master right now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the source map difference is unrelated to this PR, my guess is it's a difference from node 10 to 12, this repo is really inactive so maybe no one has noticed until now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is bug on postcss side when we share AST
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I faced with same problem in css-loader and put it in my TODO for the next major release
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really don't think it's a bug with this PR, if I make NO changes to the code on a fresh checkout of postcss-loader/master these two tests fail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see: #427
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, let's merge, improving source maps for CSS pipeline in my TODO, so let's check and tests all in other PRs
michael-ciniawsky
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please open separate PR's for the unrelated test changes? It's not a blocker for this PR to land and better to investigate them separately.
Looks good overall and worthwhile to add just a few nits and it should be good to go
| @@ -1,9 +1,12 @@ | |||
| const path = require('path') | |||
|
|
|||
| const { satisfies } = require('semver') | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really want to add another dependency if possible, do we really need the 'full' semver package or would a smaller utility function also solve it e.g
const SEMVER_RE = /\^?\~?(\d+)\.(\d+).(\d+)/
function semver (version) {
const [ _, major, minor, patch ] = version.match(SEMVER_RE)
return {
major,
minor,
patch
}
}
const version = semver(postcssPkg.version)
// ...Please see the comment below for further discussion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parsing semver with a simple regex is a generally a bad idea, and can fail in unexpected ways. I get not wanting another dependency but css-loader already does this and it's probably more worthwhile to match the behavior esp since the dependency is most likely already installed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your regexp is invalid, parsing versions using regexp is a bad idea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? I mean I'm simply not convinced this requires to have all blows and whistles of the semver spec to work and therefore the whole semver package as a dependecy might be avoidable, but let's not hung ourselves up on this for to long, like mentioned above I will open an issue in the postcss repo
| meta && | ||
| meta.ast && | ||
| meta.ast.type === 'postcss' && | ||
| satisfies(meta.ast.version, `^${postcssPkg.version}`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What/Which format does meta.ast.version currently return? I think the major should be enough as it is unlinkely to have AST changes without introducing a BREAKING CHANCE. Needs to be documented somehow in any case.
Assumming to return the major version ist schould be sufficient to e.g
if (
meta &&
meta.ast &&
meta.ast.type === 'postcss' &&
meta.ast.version === version.major
) ...I also think the meta property is always defined, but not 100% sure anymore...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's the full version string from the postcss Processor. hopefully just the major version is enough to determine breaking changes, but ppl and computers are messy and breaking changes in AST's happen whenever in practice. Yes this a "best guess", but i think it's wise to encode more rather than less information here since we know it. It means that down the road if something does break unexpectedly and we (or consumers of this metadata downstream) need to adjust on a minor or patch version it would be possible, and not require a breaking API change here to add
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will open an issue in the postcss repo maybe it's possible to add the version to e.g the ast.root so any third party tool does have to invent it's own way to handle this (not blocking this PR on it) but maybe we are luky...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oo great idea
|
split out test changes and addressed feedback |
|
@jquense friendly ping, still valid? |
|
still valid, I haven't had a chance to rebase tho |
|
@jquense can you provide link on loader there it can be used? |
|
I don't have a specific case aside from like |
|
@jquense just asked it, because want to know what somebody use it, we will shipped it in the next major release (ETA is current week I think) |
|
ya, it's more of an internal optimization that seemed good to do since postcss is often uses as a tooling base for other more specific loader chains |
Notable Changes
Allows reusing a postcss AST if the previous loader passed it. Same logic as css-loader, accepts semver compatible AST versions. It skips doing parser specific logic, since a previous AST makes parsing irrelevant
Type
SemVer
Checklist