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

Support dots in directory names when running with esm: true #168

Merged
merged 1 commit into from
Nov 23, 2024

Conversation

thegedge
Copy link
Contributor

As the title suggests, before this PR if you had something like:

my-package/
--> package.json
--> src.abc/main.ts

The process of determining package type by ascending the parent directory chain would break, because eventually !!extname("src.abc") would be true and we'd run fileURLToPath on a non-URL because the first call with main.ts would have isFilePath true, and hence would have stripped the file:///.

We could add it back, but as called out in the comment, checking extname is not a perfect solution. Instead, we allow passing in whether or not the given path is a file path or not, because as we ascend we know with certainty that we have directories. It's just the first call where we're uncertain, and will do a slightly more expensive readdir check. This should also be able to handle scenarios where you import a directory with an index.ts, for example.

There was also a secondary problem we had here: if we found a package.json file while ascending, but it had no type value, we'd keep ascending. This is actually wrong, as per the node docs that state:

The nearest parent package.json is defined as the first package.json found when searching in the current folder, that folder's parent, and so on up until a node_modules folder or the volume root is reached.

If the nearest parent package.json lacks a "type" field, or contains "type": "commonjs", .js files are treated as CommonJS.

@thegedge thegedge requested a review from airhorns November 23, 2024 17:42
@thegedge thegedge force-pushed the allow-dots-in-dirnames branch 2 times, most recently from 92def6c to c89525b Compare November 23, 2024 18:07
@thegedge thegedge force-pushed the allow-dots-in-dirnames branch from c89525b to 7bec77a Compare November 23, 2024 18:13
@thegedge
Copy link
Contributor Author

✅ Did a prerelease and verified everything working in our main Gadget repo

@thegedge thegedge merged commit 9995d68 into main Nov 23, 2024
2 checks passed
@thegedge thegedge deleted the allow-dots-in-dirnames branch November 23, 2024 18:25
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.

2 participants