-
-
Notifications
You must be signed in to change notification settings - Fork 143
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
Multiple issues with ES module #50
Comments
This is not CommonJS: import { clsx } from 'clsx'; the const { clsx } = require('clsx');
// or
const clsx = require('clsx'); those are your only options with CommonJS |
This code will be transpiled to CommonJS 👌
exclusively ? Transpilers allow to use the This is how 95% of developers code and have done so for almost a decade. ESM is activated with I think there is a misunderstanding here. |
- mixed exports (named + default) broke old bundt... because it's discouraged - closes #50
I saw what you meant re: the broken CommonJS file ... fixed it & the UMD file However:
Yes. There's a misunderstanding here & with your initial commit. The file definitions are fine & your thinking regarding requiring This is the footer of the // ...
module.exports = clsx;
exports.clsx = clsx; Thru CommonJS semantics, this means that the only declaration that takes effect is The This fixes it for the next release: module.exports = clsx;
module.exports.clsx = clsx; Separately, your bundler configuration is pretty whacky. It doesn't matter if you're building for CommonJS – if you're still going to author/write your source files in ESM ( This means that you should have automatically picked up the You should only ever load the import { clsx } from 'clsx';
// into
const { clsx } = require('clsx'); when building for & targeting CJS output.
Hope that helps~! Will publish patch shortly. |
Version 1.2.1 fixes the issue I had, thanks a lot! I did some experimentations with 1.2.1.
(You can reproduce this by generating the default projects, import clsx and add console.logs to the different files inside It's like create-react-app recognizes |
Got it, the pattern you are currently following is https://github.com/rollup/rollup/wiki/pkg.module and this is still relevant. |
module
is obsoletepackage.json specifies:
The proper way is:
Related issue: lukeed/bundt#7
.m.js
extension should be.mjs
https://nodejs.org/docs/latest-v16.x/api/packages.html#determining-module-system
(
.mjs
is only necessary if"type": "module"
is not specified)Syntax
import { clsx } from 'clsx'
does not work in an app transpiled to CJSclsx
isundefined
TypeError: (0 , clsx__WEBPACK_IMPORTED_MODULE_0__.clsx) is not a function
Last line fromdist/clsx.js
isexports.clsx = clsx
andexports
comes from nowhere, I don't think this can work. Caused by https://github.com/lukeed/bundt I guess.Solution I've found for
import { clsx } from 'clsx'
to work in an app transpiled to CJSWith webpack, create an alias
clsx: 'clsx/dist/clsx.m.js'
, example with Next.js webpack config fromnext.config.js
:This will force the import of
dist/clsx.m.js
instead ofdist/clsx.js
. (You will need to transpilenode_modules/clsx
with next-transpile-modules in the case of Next.js).Yep, ES Module is a real challenge and a big mess :-/
The text was updated successfully, but these errors were encountered: