fix(examples): resolve directory path in matching-build-files tokens for ESM#1630
Conversation
|
✅ Deploy Preview for styledictionaryjs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
@dbanksdesign @jorenbroekema |
jorenbroekema
left a comment
There was a problem hiding this comment.
I'll add a commit to apply my suggestion, and fix some readme snippets syntax highlighting. Thanks for raising this one.
I'll try to get back to your other PRs but I've been trying to get the pretty big Logging PR over the finish line first before I dive back into the sorting topic :) which requires a bit more mental capacity haha
| import { readdirSync } from 'node:fs'; | ||
| import { dirname } from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
|
|
||
| const tokensDir = dirname(fileURLToPath(import.meta.url)); | ||
|
|
||
| const dirs = (p) => | ||
| readdirSync(p, { withFileTypes: true }) | ||
| .filter((d) => d.isDirectory()) | ||
| .map((d) => d.name); | ||
|
|
||
| export default dirs(tokensDir); |
There was a problem hiding this comment.
| import { readdirSync } from 'node:fs'; | |
| import { dirname } from 'node:path'; | |
| import { fileURLToPath } from 'node:url'; | |
| const tokensDir = dirname(fileURLToPath(import.meta.url)); | |
| const dirs = (p) => | |
| readdirSync(p, { withFileTypes: true }) | |
| .filter((d) => d.isDirectory()) | |
| .map((d) => d.name); | |
| export default dirs(tokensDir); | |
| import { readdirSync } from 'node:fs'; | |
| const dirs = (p) => | |
| readdirSync(p, { withFileTypes: true }) | |
| .filter((d) => d.isDirectory()) | |
| .map((d) => d.name); | |
| export default dirs(import.meta.dirname); |
This is simpler I think. And supported in Node LTS version atm I think (v20.11+, v21.2+)
Let me know what you think @Yeom-JinHo , if it works on your end I'll merge this :)
There was a problem hiding this comment.
Yep, this works great on my end 👍
Tested locally and everything looks good with my setup. Feel free to merge it — thanks!
|
@jorenbroekema When I first started using this library, I relied a lot on the v4 examples. Right now it looks like all the examples are still written only for v4. Do you have any plans to upgrade the examples to v5? If there is a plan, I’d be happy to contribute gradually over the longer term. |
Yes we have an issue planned for the v6 milestone that would put the example in its own monorepo workspace, possibly a separate workspace for each example if it has a separate package.json + dependencies. See #1263 I am open to updating the examples for v5 though, I agree they were a bit left behind. Shouldn't be too many changes, since the v5 update was pretty subtle and non-breaking for most use cases. |
Totally makes sense — thanks for sharing the direction. I’d like to get a deeper understanding of the library, so I’ll take things slowly and, if the examples need updates along the way, I’ll work on them. Would you be okay with me updating each example workspace’s package.json to target the latest stable version (v5)? |
|
Yep that sounds like a good start! |
closed #1509
Description
This PR fixes an
ENOENTerror in theexamples/advanced/matching-build-filesexample when runningnpm run build.The issue was caused by passing
import.meta.url(a file URL string, e.g.file:///.../tokens/index.js) directly into Node’sfs.readdirSync, which expects a filesystem path. As a result, Node attempted to scan a non-existent path that literally starts withfile:///..., producingENOENT.This change converts the module URL to a real filesystem path and ensures we pass the tokens directory (not the
index.jsfile) toreaddirSync.Changes
import.meta.url→ filesystem path viafileURLToPath, then derive the directory usingdirname.tokens/directory instead of mistakenly scanning thetokens/index.jsfile URL.readdirSync(..., { withFileTypes: true })+Dirent.isDirectory()to avoid per-entrystatSync.Motivation
Error: ENOENT: no such file or directory, scandir 'file:///.../tokens/index.js'import.meta.urlreturns an absolutefile:URL, not a plain path string, so it must be converted before using it with path-basedfsAPIs.