Skip to content

Conversation

@jquense
Copy link

@jquense jquense commented Feb 10, 2020

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

ℹ️ What type of changes does your code introduce?

  • CI
  • Fix
  • Perf
  • Docs
  • Test
  • Style
  • Build
  • Chore
  • Feature
  • Refactor

SemVer

ℹ️ What changes to the current semver range does your PR introduce?

  • Fix (:label: Patch)
  • Feature (:label: Minor)
  • Breaking Change (:label: Major)

Checklist

ℹ️ You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. This is a reminder of what we are going to look for before merging your code.

  • Lint and unit tests pass with my changes
  • I have added tests that prove my fix is effective/works (if needed)
  • I have added necessary documentation (if appropriate)
  • Any dependent changes are merged and published in downstream modules

test('JSS - {String}', () => {
const config = {
loader: {
test: /style\.js$/,
Copy link
Author

@jquense jquense Feb 10, 2020

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

Copy link
Member

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

Copy link
Author

Choose a reason for hiding this comment

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

i did!

Copy link
Member

@alexander-akait alexander-akait left a 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?

@jquense
Copy link
Author

jquense commented Feb 10, 2020

I don't necessarily have strong use-case. Thinking a bit proactively.

you might want to do: css-loader!postcss-loader!css-module-loader for autoprefixing.

Basically the use case is loaders that use postcss but can't be simplified to just a postcss plugin.

@alexander-akait
Copy link
Member

Can you fix tests?

@jquense
Copy link
Author

jquense commented Feb 10, 2020

I can update the snapshots, but I believe these were broken before I made any changes, esp since i haven't touched that code...

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

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

Thanks!

@jquense
Copy link
Author

jquense commented May 1, 2020

anything else i can do here?


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 */"'
Copy link
Member

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

Copy link
Author

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)

Copy link
Member

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?

Copy link
Author

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

Copy link
Author

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

Copy link
Member

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

Copy link
Member

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

Copy link
Author

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.

Copy link
Author

Choose a reason for hiding this comment

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

see: #427

Copy link
Member

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

@jquense jquense mentioned this pull request May 13, 2020
17 tasks
Copy link
Member

@michael-ciniawsky michael-ciniawsky left a 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')
Copy link
Member

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

Copy link
Author

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

Copy link
Member

@alexander-akait alexander-akait May 14, 2020

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

Copy link
Member

@michael-ciniawsky michael-ciniawsky May 14, 2020

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}`)
Copy link
Member

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...

Copy link
Author

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

Copy link
Member

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...

Copy link
Author

Choose a reason for hiding this comment

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

oo great idea

@michael-ciniawsky
Copy link
Member

postcss/postcss#1359

@jquense
Copy link
Author

jquense commented May 15, 2020

split out test changes and addressed feedback

@jquense jquense mentioned this pull request Aug 3, 2020
14 tasks
@alexander-akait
Copy link
Member

@jquense friendly ping, still valid?

@jquense
Copy link
Author

jquense commented Aug 17, 2020

still valid, I haven't had a chance to rebase tho

@alexander-akait
Copy link
Member

@jquense can you provide link on loader there it can be used?

@jquense
Copy link
Author

jquense commented Aug 17, 2020

I don't have a specific case aside from like css-module-loader, the other case is another postcss-loader in front of it. I don't have a super strong justification other than "why not? css-loader does it"

@alexander-akait
Copy link
Member

@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)

@jquense
Copy link
Author

jquense commented Aug 19, 2020

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

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants