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
Directory *imports* throwing in ESM node #432
Comments
I'd definitely be down with an experimental flag to add this. I wouldn't want to force it since I don't think there's a reasonable way of adding an For the linked repo, adding this to the package.json in "exports": {
"./package.json": "./package.json",
".": {
"module": "./dist/vanilla-extract-sprinkles.esm.js",
"default": "./dist/vanilla-extract-sprinkles.cjs.js"
},
"./createRuntimeSprinkles": {
"module": "./createRuntimeSprinkles/dist/vanilla-extract-sprinkles-createRuntimeSprinkles.esm.js",
"default": "./createRuntimeSprinkles/dist/vanilla-extract-sprinkles-createRuntimeSprinkles.cjs.js"
},
"./createUtils": {
"module": "./createUtils/dist/vanilla-extract-sprinkles-createUtils.esm.js",
"default": "./createUtils/dist/vanilla-extract-sprinkles-createUtils.cjs.js"
}
}, Note the One thing we should check is, do bundlers still respect the Also with the whole TypeScript Node ESM stuff, do we need a // packages/pkg-a/src/a.ts (is not an entrypoint)
export type X = {
x?: X;
};
export function x(): X {
return {};
}
// packages/pkg-a/src/index.ts (is an entrypoint)
export { x } from "./x";
// packages/pkg-b/src/index.ts (is an entrypoint)
import { x } from "pkg-a";
export const a = x(); I would expect TypeScript to fail when generating the declarations for Config thoughtsI would be totally fine with an implementation that doesn't look like this to start and ofc I'm keen to hear thoughts/suggestions on this design/alternatives. Opt-in on a project level or package level basis. For now, this would be in addition to enabling an experimental flag for it. "preconstruct": {
"exports": true
} And Preconstruct would completely control the "preconstruct": {
"exports": {
"extra": {
"./blah": "./blah.js"
}
}
} And ideally it could deeply merge conditions. I'm thinking that if |
what kind of breaking changes this can introduce if this doesnt try to introduce ESM support in node?
I have been testing this recently with
We’d have to test with the
What is emitted for that today? Maybe type-level
Agreed, but i think that package.json should always be exported.
I would skip that for now - it sounds niche, potentially YAGNI
Sounds good to me - if users dont give us control over exports then we should simply ignore it, they might want to manage it themselves. |
The general "i was importing
On 4.5, you would get a import to On the latest nightly as of this comment ( src/index.ts:2:14 - error TS2742: The inferred type of 'a' cannot be named without a reference to '../node_modules/blah/src/a'. This is likely not portable. A type annotation is necessary.
2 export const a = x();
~ (I didn't test exactly the example shown above but the same general thing) To be clear, I don't think this is something that should be "fixed". The current status quo of packages easily having implicit dependencies on their dependencies internal module structure imo is a worse problem than breaking once(or not at all if you don't need to generate declarations that need to reference something that isn't exported). Getting an error upfront and being able to address it is much better imo than some package changing their internal structure and your package that depends on it breaking.
Yeah
I agree that it can be skipped it for now but I don't think it's that niche, having some other js/json file or etc. that you want to allow people to import from and isn't managed by Preconstruct seems like a reasonably common use case. (Emotion does that for example) |
Eh, that could really use a suggestion from TS on how to fix it 🙄 I expect many people to trip over this.
Yeah, I got your intention - was just wondering what are the exact implications. Nice catch on this one.
IIRC Emotion only "exports" macros this way - and those entrypoints could be easily included in |
I extended PR #435 to also support package exports. I used the vanilla-extract repo to test and I can confirm it is working. |
How to reproduce:
"preconstruct": { "entrypoints": [ "index.ts", "foo.ts"] }
package.json
inside itimport { foo } from "pkg/foo";
Repro:
https://github.com/markdalgleish/preconstruct-node-esm-import-issue/tree/f181ab2243c98255723ee106ae665df1fc590db6
Possible solution:
Emit
package.json#exports
with the appropriate fields for the declared entrypoints. If we do this just for CJS, without attempting to support ESM bundles in node then it should work and not break anything (like with everything related to this... we should still be extra careful about this though).This would limit what can be imported/required in node from a package. However, the whole point of Preconstruct is to only allow what is declared so if somebody has been reaching into "internal" files that's kinda on them. This could be added under an experimental flag if we are worried about the unwanted side-effects. Optionally we could consider always adding
package.json
to the exports as that seems to be a pretty common requirement and I don't see why somebody would really want to "hide" this file.cc @markdalgleish
The text was updated successfully, but these errors were encountered: