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

Pass real filepath to transform plugins #3483

Merged
merged 1 commit into from Jun 24, 2021
Merged

Conversation

drwpow
Copy link
Collaborator

@drwpow drwpow commented Jun 24, 2021

Changes

In the dev server, our transform plugins receive a pseudo-URL, which neither exists on disk nor really at the final URL (but probably closer to the latter). The root cause here is when working with PostCSS on transformed files, you’ll get the following error:

Error: ENOENT: no such file or directory, stat '{projectRoot}/src/components/Button2.css'

More details here: withastro/astro#481

Because we weren’t passing a real location on disk to PostCSS, it would fail running on any CSS that was generated from a non-.css file (e.g. .astro, .pcss, .vue, etc.)

This PR adds a new srcPath option to plugins which is backwards-compatible, and fixes the original error.

Testing

Tested manually; fixes the error and doesn’t cause any other known issues.

Docs

No docs changes needed

@drwpow drwpow requested a review from a team as a code owner June 24, 2021 18:34
@vercel
Copy link

vercel bot commented Jun 24, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/pikapkg/snowpack/DJ1gRy7xY8XrnWgepjnBiL7SDL2N
✅ Preview: https://snowpack-git-fix-snowpack-plugin-postcss-pikapkg.vercel.app

@@ -181,7 +181,7 @@ async function runPipelineTransformStep(
isDev,
isPackage,
fileExt: destExt,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It’s important that fileExt not change! That would basically break all transform plugins

Copy link
Contributor

@natemoo-re natemoo-re left a comment

Choose a reason for hiding this comment

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

✅ Tests look good, this PR looks good to go!

snowpack/src/build/build-pipeline.ts Show resolved Hide resolved
@drwpow
Copy link
Collaborator Author

drwpow commented Jun 24, 2021

Before I merge, I’ll take a look at our most-used plugins and double-check this won’t break anything. If not, then we can push this in a patch and improve PostCSS support without needing to release the PostCSS plugin too.

@drwpow
Copy link
Collaborator Author

drwpow commented Jun 24, 2021

Update: I found that this change would actually break an internal plugin in esinstall, so I’ve modified the PR to add a new srcPath option rather than change the existing id. This will require a new release of snowpack and @snowpack/plugin-postcss, but overall it’s safer and won’t disrupt people.

@@ -37,7 +37,7 @@ module.exports = function postcssPlugin(snowpackConfig, options) {

const encodedResult = await worker.transformAsync(contents, {
config,
filepath: id,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

id is used in other parts of this file, and I believe those should stay as-is. This is the only line that causes errors, because filepath needs to point to a real file on disk

@drwpow drwpow merged commit 33cd564 into main Jun 24, 2021
@drwpow drwpow deleted the fix-snowpack-plugin-postcss branch June 24, 2021 20:36
@natemoo-re
Copy link
Contributor

@drwpow totally in favor of that, good call!

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

Successfully merging this pull request may close these issues.

None yet

2 participants