Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: Add ESM build#23

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
drwpow wants to merge1 commit intofastify:main
base:main
Choose a base branch
Loading
fromdrwpow:main
Open

feat: Add ESM build#23

drwpow wants to merge1 commit intofastify:mainfromdrwpow:main

Conversation

drwpow
Copy link

@drwpowdrwpow commentedJan 12, 2025
edited
Loading

Fixes#22 by adding an ESM build.This is backwards-compatible! It is only an additive change.

⚠️ This adds anpm run build command that is now necessary to generate the ESM files.

  • Adds types that satisfy TypeScript’s requirements for dual-build packages
  • Updatesexports to match bothNode.js’ standards as well asTypeScript’s
    • ⚠️ Verify these are backwards-compatible with all consumers! It should be, but verify
  • Improves.npmignore by hiding test files and GitHub cruft from the npm package (which saves considerable network traffic, taking into account how often this package is downloaded!)

Alternatives

  • The codebase could be converted to ESM, instead, with CJS generated as a side-effect. But that alternative wasn’t taken to minimize disruption.
  • A bundler such as Rollup could be introduced to remove the custom one-off script. But that seemed heavier-weight, and would impact CI times far more than the current method

Checklist

{
source: './index.js',
types: './types/index.d.ts',
replacements: [
Copy link
Author

@drwpowdrwpowJan 12, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Note: RegEx replacements can be brittle in larger setups, but this seemed ideal for this package’s current lightweight setup. It’s easier to modify simple string replacements than burdening this package with some robust AST traversal + mutation + reassembly script… that only replaces a couple lines in the end.

Of course, alternatives can be discussed!

fs.writeFileSync(new URL(source.replace(/\.js$/, '.mjs'), CWD), output)

// write types
fs.copyFileSync(new URL(types, CWD), new URL(types.replace(/\.d\.ts$/, '.d.mts'), CWD))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Note: thisseems unnecessary—copying existing.d.ts declarations 1:1. But TypeScript does require all-new declaration types for different file extensions, i.e. it won’t applyindex.d.ts to bothindex.jsandindex.mjs (not even withexports—I’ve tried). This is unfortunately TypeScript behavior, and it’s not easy to bypass.

Alternately, there could be some clever type forwarding, e.g.export * from './index', but in addition to being harder to autogenerate, something could slip through the cracks compared to copying 1:1

@@ -0,0 +1,284 @@
'use strict'

import { dequal as deepEqual } from 'dequal'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

dequal ships ESM, so this package would benefit from upgrading, too

@drwpowdrwpow changed the titleAdd ESM buildfeat: Add ESM buildJan 12, 2025
@Fdawgs
Copy link
Member

Fdawgs commentedJan 12, 2025
edited
Loading

Thanks for the PR@drwpow!
If this is being automatically built via a script then my suggestions would be:

  • Add index.mjs and types/index.d.mts to.gitignore, no point committing something that gets built
  • Add a"prepublish": "npm run build" script or something to that effect
  • Scripts are sorted alphabetically ascending, sobuild should go at the top etc.

You're going to get pushback for adding an.npmignore due tofastify/skeleton#42.

Obviously wait for @fastify/libraries to chime in before actioning any of this, as my frontend knowledge is... dire.

@FdawgsFdawgs requested review fromivan-tymoshenko anda teamJanuary 12, 2025 08:52
@mcollina
Copy link
Member

I don't think running any of our libraries in a bundle-less browser environment should be a goal (all bundlers compile cjs... React is cjs).

Copy link
Member

@jsumnersjsumners left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Why is it necessary to duplicate everything?

@drwpow
Copy link
Author

Y’all can feel free to close this if using ESM (the new language standard) isn’t a goal. That’s fine!

@Fdawgs all good suggestions! I went back-and-forth on committing generated files, just because this package didn’t have a build step (but maybe it should—I noticed some of the declarations have drifted from the runtime)

I don't think running any of our libraries in a bundle-less browser environment should be a goal (all bundlers compile cjs... React is cjs).

May be true now but ESM is the new standard. It’s also useful in edge workers (which may also be bundled behind-the-scenes, but ESM saves work)

Why is it necessary to duplicate everything?

ESM is a different module system entirely. I’d recommend reading about it onNode’s documentation but also if you inspect any universal packages you’ll find it (evendequal)

@mcollina
Copy link
Member

ESM is a different module system entirely. I’d recommend reading about it onNode’s documentation but also if you inspect any universal packages you’ll find it (evendequal)

Ok, I will take a look.

ovflowd and gurgunday reacted with laugh emoji

@gurgunday
Copy link
Member

Hi, thanks for your PR and interest!

I don't think we should do this unless there is a need as Node will probably never remove CJS and it's supported by pretty much everyone

I understand this won't be a satisfactory answer for you since ESM isthe standard, I get that, but this is just how things are

Luckily, withnodejs/node#51977, maybe we can simply move to ESM one day with most packages

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@jsumnersjsumnersjsumners left review comments

@ivan-tymoshenkoivan-tymoshenkoAwaiting requested review from ivan-tymoshenko

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

ESM Build
5 participants
@drwpow@Fdawgs@mcollina@gurgunday@jsumners

[8]ページ先頭

©2009-2025 Movatter.jp