-
Notifications
You must be signed in to change notification settings - Fork 921
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
Improve CJS Named Exports support #440
Comments
I thought I'd mention here if you're interested in automatic detection I was trying to get this PR into Node.js based on a lexing approach (fork of es module shims!) - nodejs/node#33416. It's still quite uncertain if that will ever get through, but the wasm library that does the extraction is available here https://github.com/guybedford/cjs-module-lexer, which could be a consideration and saving over requiring users to provide this metadata manually which is always somewhat of a pain. Certainly pros and cons to be weighed here but if you are interested in integrating the main difficulty is handling the |
just curious, isn't checking package.json for a few fields good enough (module vs main) vs a lexer? |
I missed that you were taking advantage of Rollup's code analysis process for named exports detection here. In which case, yes please ignore my comment! |
Thanks for the links, we actually do have a need for this! Right now, we basically create a fake wrapper that gets sent to Rollup's named export detection: https://github.com/pikapkg/snowpack/blob/master/src/rollup-plugin-wrap-install-targets.ts#L91 But, to generate this list of named exports we use a super naive Because of the risk of side effects, we keep this as an allowlist of popular packages for now. But, this lexer could help us open this this for all CJS packages, automatically. @guybedford How reliable is the lexer? Works on any packages, any known issues, etc etc? |
@FredKSchott it's certainly not 100% accurate, in fact about 28% of packages aren't supported under this transform. This is mostly cases like |
Gotcha, will keep that in mind. Thanks for sharing 👍 |
lodash is not working too. import {memoize, orderBy} from "lodash";
// error in browser
// Uncaught SyntaxError: The requested module '/web_modules/lodash.js' does not provide an export named 'memoize' |
Oh, that's a good one to handle. The workaround for now is:
|
I use workaround for now: Rewriting to default import |
Can anyone confirm that this works when you run |
Doesn't seem to be working for me. I thought it was working fine with 2.7.0-pre.1 but that seems to no longer work. I'm not sure what's changed. |
Hi, when I build my react+typescript snowpack project with this config: {
"extends": "@snowpack/app-scripts-react",
"plugins": [],
"alias": {
"@app": "./src/"
}
}
I obtain |
Tracked in #844 |
From @Papierschiff in #844
^ seems to be a workable short-term fix, while we triage |
hmm... kinda sad to see this - i was trying out moving my company's typescript create-react-app project to snowpack but the lack of support for named exports for CJS libraries means that an overwhelming number of libraries we depend on don't work with snowpack and for a project of our size checking to see if snowpack has an issue with each import by running snowpack dev takes a really long time, not to mention that everytime a teammate adds a library there will be a high chance that this will be a problem, requiring everyone in my team to be aware of these details - which goes against the ethos of "it will just work" ethos of the snowpack website. I'll probably be moving onto the next alternative, but wanted to see out of curiosity if having an option for flexibility to avoid being incompatible with a very large number of npm libraries is on the table. also, i was trying to deal with these import errors with the |
We're just following Node.js behavior here, but I do get your frustration. It's a technical limitation: CJS named exports are defined at runtime, so can't be statically analyzed. However, I've heard rumors that Node.js is working on a pre-pass scanner that will do it's best to detect named exports automatically from Common.js packages. We'll wait until that's official before we try to implement ourselves, but that would mean that in a future version MOST common.js packages would support named exports by default. Many popular packages are ESM now, so hopefully this won't be too hard to handle today. We can also look into adding a warning if we see you doing named imports from a common.js package. |
I basically experience the same issue with |
You know what, now that we have that error in the browser, we can add some extra debugging info to it! Something like:
|
Although, @jsefiani can you confirm that you're on the latest version of Snowpack? We recently added automatic CJS named export detection, and it looks like it should be picking that up: |
Thank you for the quick reply, that resolved my issue! Nope, I'm not on the latest version, will update it shortly (current version: 2.11.0). Thank again mate! |
Awesome! Yea if you update, that namedExports config should no longer be needed. Cheers! |
Sorry. this is closed issue. I posted in a new issue now. Hi, we are running into a similar issue still using the latest snowpack 2.13.3 with the namedExports support. It's a typescript project. could be an issue with unwrapping exports and mixing of import types. not sure if there's rollup config I can use to address this. I'm importing a custom styled component library so I declared the exports with something like this:
in the app itself, I import the custom styled component:
When I start up the dev build, I get a I suspect the error is likely from the unpacking.
The runtime file in browser:
any advice on how best to handle this? custom rollup that I can pass into snowpack.config? |
Warning system would be good, or perhaps even something like a |
@cherihung did you figure out how to handle your issue? I'm running into the same issue with styled-components. |
@ted-mccarthyfinch no, I wasn't able to solve with changing snowpack config. since I had also control of the package I was consuming, I ended up tweaking it to publish both cjs and mjs components. and adding a "module" field to that package. I know it's not a real solution to the problem but it worked for my purpose at this time. am still very much looking to implement a real solution though. |
Original Discussion: https://www.pika.dev/npm/snowpack/discuss/309
/cc @bugzpodder, @FredKSchott
We have users requesting named export support for Common.js packages, usually because these packages documentation is written based on Webpack, and not Node's official ESM support.
This is also important for TypeScript users, where TS actually tells the user to use the named export format, so that they can get type information back.
We should support some config to allowlist Common.js packages for named exports. We already support a allowlist, it's just static. This would be a new config value of an array of install target (ie: package names) that should be supported.
We should also warn when we detect this usage without a allowlist entry. We can detect this during the source code scan phase, and then log a warning to the user.
The text was updated successfully, but these errors were encountered: