-
Notifications
You must be signed in to change notification settings - Fork 817
number-formatters: build dual CJS and ESM versions #43106
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
Conversation
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! |
Code Coverage SummaryThis PR did not change code coverage! That could be good or bad, depending on the situation. Everything covered before, and still is? Great! Nothing was covered before? Not so great. 🤷 |
d47044b
to
7c36e1d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd really rather we don't add more use of rollup. The configs we have tend to be unmaintained, and to depend on unmaintained plugins, and throw warnings which are ignored. This one, for example, issues this warning:
(!) Plugin typescript: @rollup/plugin-typescript TS5110: Option 'module' must be set to 'NodeNext' when option 'moduleResolution' is set to 'NodeNext'.
Is anyone committing to maintaining the rollup configuration here, including replacing outdated plugins and such?
For a library we probably shouldn't need a bundler at all, just tsc to convert the TypeScript to JavaScript.
I also find that the resulting files in dist/cjs/
are not correct. The package.json has "type": "module"
, indicating that files such as dist/cjs/index.js
are supposed to be in ESM rather than CommonJS format. While some tooling may ignore this mismatch, other tooling will fail. To work correctly, these files need to be named like dist/cjs/index.cjs
instead.
"target": "ES2024" | ||
}, | ||
// List all sources and source-containing subdirs. | ||
"include": [ "./src" ], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find that blanket-listing directories (rather than listing the specific entry point files such as src/index.ts
) in a tsconfig that's used for compilation tends to give odd results.
In this case we seem to be ok because everything is reachable from the entry point, but in other packages I've seen this pointlessly compile .test.js
and .stories.js
files.
"import": "./dist/mjs/index.js", | ||
"require": "./dist/cjs/index.js", | ||
"types": "./dist/index.d.ts" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typescript calls for "types" to come before "import" and "require".
"import": "./dist/mjs/index.js", | |
"require": "./dist/cjs/index.js", | |
"types": "./dist/index.d.ts" | |
"types": "./dist/index.d.ts", | |
"import": "./dist/mjs/index.js", | |
"require": "./dist/cjs/index.js" |
Thanks for your feedback @anomiex 👍 I'm getting rid of Rollup and building the dual package with TypeScript instead. There is one The only thing that doesn't work yet are The little issues reported (order of the |
15d0d6c
to
978c9d4
Compare
"typeRoots": [ "./node_modules/@types/", "src/*" ], | ||
"target": "ES2024", | ||
"importHelpers": true, // Import helpers from `tslib` instead of inlining them | ||
"customConditions": null // CommonJS build fails when customConditions are defined and this package doesn't need them anyway |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wary of this. the customConditions
are what makes
"jetpack:src": "./src/index.ts", |
At the moment, disabling it here is probably ok, since this package doesn't depend on anything else within the monorepo. And maybe this package never will. OTOH, it may be a bad pattern since this may get copied into other packages and break things there.
"clean": "rm -rf dist/", | ||
"build": "pnpm run clean && pnpm run build:esm && pnpm run build:cjs", | ||
"build:esm": "tsc --declarationDir dist/types --outDir dist/mjs", | ||
"build:cjs": "tsc --module CommonJS --moduleResolution node10 --declaration false --outDir dist/cjs", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty wary of this too. https://www.typescriptlang.org/docs/handbook/modules/reference.html#commonjs, for example, says "You probably shouldn’t use this."
It seems the more modern way to do something like this, in the absence of offical support in tsc, is to somehow munge the "type" in package.json just before the build to have tsc with "nodenext" build the cjs version, then restore it after. For example, it looks like @knighted/duel does that directly, while tshy assumes sources are in src/
and creating a temporary stub src/package.json
will manage to DTRT. (of those two, BTW, duel seems more of the "do one specific thing" philosophy while tshy seems more "try to do all the things".)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I addressed both concerns by creating a special src/package.json
during the CommonJS build, with type: 'commonjs'
. Just like tshy
does. It's easy to do, just a cp
and rm
command.
We don't need to remove customConditions
any more, because module
and moduleResolution
are always nodenext
. It's the type
field that triggers the CommonJS build.
Ensuring that files inside @anomiex What do you think about using And the
|
I think, if we really do want a dual package, that using |
688bb92
to
fa8629b
Compare
@anomiex I migrated the build to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good. Left a few suggestions for cleanup inline.
"@types/jest": "29.5.14", | ||
"jest": "29.7.0", | ||
"jest-environment-jsdom": "29.7.0", | ||
"oxc-parser": "0.64.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's this for?
... Ah, missing upstream peer dependency. Sigh. Thanks for filing https://href.li/?https://github.com/knightedcodemonkey/duel/issues/63 about it.
Personally I might be inclined to work around this by adding a hack to .pnpmfile.cjs
(in the monorepo root).
// Package is missing a dependency for one of its subpackages that has this as a peer dep.
// https://github.com/knightedcodemonkey/duel/issues/63
if ( pkg.name === '@knighted/duel' && ! pkg.dependencies?.[ 'oxc-parser' ] ) {
pkg.dependencies[ 'oxc-parser' ] = '*';
}
That lets us better document that the dep is only there because of an upstream bug.
(then remove it from package.json here)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like upstream has released a version with a fix already 🎉 So we can just remove this dep now. 🙂
"build": "pnpm run clean && duel --dirs", | ||
"clean": "rm -rf dist", | ||
"compile-ts": "tsc --pretty", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd probably do like this
"build": "pnpm run clean && duel --dirs", | |
"clean": "rm -rf dist", | |
"compile-ts": "tsc --pretty", | |
"build": "pnpm run clean && pnpm run compile-ts", | |
"clean": "rm -rf dist", | |
"compile-ts": "duel --dirs", |
But this would also be fine.
"build": "pnpm run clean && duel --dirs", | |
"clean": "rm -rf dist", | |
"compile-ts": "tsc --pretty", | |
"build": "pnpm run clean && duel --dirs", | |
"clean": "rm -rf dist", |
"@wordpress/date": "5.19.0", | ||
"debug": "4.4.0" | ||
"debug": "4.4.0", | ||
"tslib": "2.5.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While we're here, it might be worth making these deps be like
"@wordpress/date": "^5.19.0",
"debug": "^4.4.0",
"tslib": "^2.5.0"
That way reusers don't have to have these exact versions of the dependencies, if they have e.g. tslib 2.8.1 elsewhere in their project then they won't need this older version too just for this package.
outdated, remaining requests aren't really blockers
Thank you both for working on this! 🙇 I think once (or while) we ship this, we can also change version to These PRs are waiting for this bit to complete, which should complete migration in wp-calypso (then back to JP):
|
468501a
to
148699a
Compare
I upgraded to the fixed version of |
Thanks again. Copied I'm seeing some TS warnings in JP, in my editor. They appear in several files, but these ones from
![]()
![]() Code review obviously up to @anomiex |
This looks like a locally broken |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks reasonable to me. No idea about @chriskmnds's errors.
All good on my end. Refreshing dependencies and restarting the editor cleared these. |
p.s. If we wait for #42796 first (it's green) then we should be able to see this linked in the repo and if any issues on JP side. I think? However, I'm still trying fix things in my environment to point to a usage, so maybe the other way then. :) |
Sure, I don't mind waiting for this PR that switches usages from |
Got it. Let's deploy this pls so we can make progress wth the wp-calypso PRs. I can do the 1.0.0 release if you like, unless you want to PCYsg-Np7-p2 I'm having an awful hard time getting my environment in place anyhow to test #42796, but like you said, it doesn't even matter for these changes. |
I merged this PR now, and let's release 1.0.0 only after it's actually verified in Calypso 👍 |
This PR adds a Rollup build to the
number-formatters
package and makes it produce both CJS and ESM versions of the module. Also adds the "legacy"main
,module
andtypes
fields topackage.json
in addition to the modernexports
field.This makes the package consumable both by modern and legacy tooling setups.
The Rollup workflow is modelled after what we already have in the
@automattic/charts
package. I basically copied the config and then simplified it a lot. We don't need any CSS support innumber-formatters
for example. And also I think that Terser minification doesn't need to be done. It's up to the final consumer to do all the minification at once.When I use the resulting package in Automattic/wp-calypso#102475, all the errors reported there disappear: Jest is able to require the CJS version, and also TypeScript is able to consume the package without problems.
Testing instructions:
pnpm run build
in thedist
directory is valid and makes sense.@automattic/number-formatters
– use in section stats > insights wp-calypso#102475 (I just copied overpackage.json
anddist
) and verify that it fixes theyarn test-client
andyarn typecheck
errors.Does this pull request change what data or activity we track or use?
No.