Skip to content

feat: esm and cjs exports#427

Merged
kanadgupta merged 36 commits intomainfrom
esm-and-cjs
Sep 27, 2023
Merged

feat: esm and cjs exports#427
kanadgupta merged 36 commits intomainfrom
esm-and-cjs

Conversation

@kanadgupta
Copy link
Copy Markdown
Contributor

@kanadgupta kanadgupta commented Sep 21, 2023

🧰 Changes

This PR refactors this codebase to support both ESM and CJS exports. I was originally blocked in #426 (comment) — turns out the issue was the treeshaking flag. Removing that (and separating out the type exports into types.ts) fixes the issue!

This PR also does a little bit of housekeeping:

  • Runs Prettier on everything
  • Bumps a few dependencies to the latest

🧬 QA & Testing

Before/after for Are The Types Wrong

CleanShot 2023-09-26 at 16 30 05@2x CleanShot 2023-09-26 at 16 30 31@2x

If you check out this repo and run the following commands, they should work as expected:

# install + build
npm ci && npm run build

# run example scripts
node example.mjs
node example.cjs
deno run -A example.mjs

@kanadgupta kanadgupta added the enhancement New feature or request label Sep 21, 2023
@kanadgupta kanadgupta changed the base branch from main to ts-strict-mode September 21, 2023 21:10
Copy link
Copy Markdown

@rafegoldberg rafegoldberg left a comment

Choose a reason for hiding this comment

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

Gonna hold off approving till you mark this as "Ready for review", but generally this looks good to me. Left some comments/questions below.

Comment thread src/index.ts Outdated
Comment thread test/file-upload-quirks.test.ts Outdated
Comment thread .github/workflows/ci.yml
Comment thread .github/workflows/ci.yml
Comment thread .github/workflows/codeql-analysis.yml
Comment thread README.md
Comment thread example.cjs
@@ -1,4 +1,4 @@
const fetchHAR = require('.').default;
const fetchHAR = require('.');
Copy link
Copy Markdown

@rafegoldberg rafegoldberg Sep 26, 2023

Choose a reason for hiding this comment

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

Will this change to the export/import pattern break things for CJS users? Or will it also work from .default?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

nah the default is no longer required — this is likely going to be a breaking change! the default thing was actually a bit of a code smell since we probably weren't exporting types properly

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Worth calling out in the changelog (or in the commit message)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yep will do in the GitHub release!

Comment thread .eslintrc
Comment on lines -17 to -21
"polyfills": [
"fetch",
"Headers",
"Request"
]
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

these weren't necessary!

@kanadgupta kanadgupta marked this pull request as ready for review September 26, 2023 21:33
Comment thread package.json Outdated
kanadgupta and others added 6 commits September 26, 2023 16:54
* chore(deps): bump @vitest/browser to latest

* test: add MSW tests
(server) implies the existence of (client), which isn't a thing 😔
Base automatically changed from ts-strict-mode to main September 27, 2023 14:01
Copy link
Copy Markdown
Member

@domharrington domharrington left a comment

Choose a reason for hiding this comment

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

LGTM!

Comment thread .github/workflows/ci.yml
Comment thread README.md
Comment thread README.md Outdated
Comment thread package.json
"undici": "^5.24.0"
"@readme/data-urls": "^3.0.0",
"@types/har-format": "^1.2.13",
"undici": "^5.25.1"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we need to bring this in if we're just using whatever native fetch() is present in the environment?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

hmm we'll have to ask jon but i believe we still need it for node environments (see this comment and below):

fetch-har/src/index.ts

Lines 16 to 25 in 9e0360d

if (!globalThis.File) {
try {
// Node's native `fetch` implementation unfortunately does not make this API global so we need
// to pull it in if we don't have it.
// eslint-disable-next-line @typescript-eslint/no-var-requires
globalThis.File = require('undici').File;
} catch (e) {
throw new Error('The File API is required for this library. https://developer.mozilla.org/en-US/docs/Web/API/File');
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@kanadgupta @domharrington We could move this to optionalDependencies but yeah this is only really needed for server environments.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Annoyingly, File only became a global in Node v20: https://nodejs.org/docs/latest/api/globals.html#class-file otherwise we could just use that.

@kanadgupta kanadgupta merged commit bd9482a into main Sep 27, 2023
@kanadgupta kanadgupta deleted the esm-and-cjs branch September 27, 2023 14:33
import DatauriParser from 'datauri/parser';
import express from 'express';
import multer from 'multer';
// @ts-expect-error this file is ESM
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@kanadgupta Is this still needed?

Screen Shot 2023-10-04 at 1 41 04 PM

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

oh whoops yeah as of #431 we don't need this! fixed in 0eea0ff

Copy link
Copy Markdown
Member

@erunion erunion left a comment

Choose a reason for hiding this comment

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

sick

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

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants