Skip to content
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

feat: esm and cjs exports #427

Merged
merged 36 commits into from
Sep 27, 2023
Merged

feat: esm and cjs exports #427

merged 36 commits into from
Sep 27, 2023

Conversation

kanadgupta
Copy link
Member

@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

@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.

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

@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
Member 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
Member

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
Member 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 on lines -17 to -21
"polyfills": [
"fetch",
"Headers",
"Request"
]
Copy link
Member 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
package.json Outdated Show resolved Hide resolved
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
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!

.github/workflows/ci.yml Show resolved Hide resolved
README.md Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
"undici": "^5.24.0"
"@readme/data-urls": "^3.0.0",
"@types/har-format": "^1.2.13",
"undici": "^5.25.1"
Copy link
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
Member 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
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
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
@@ -5,6 +5,7 @@ import fs from 'node:fs/promises';
import DatauriParser from 'datauri/parser';
import express from 'express';
import multer from 'multer';
// @ts-expect-error this file is ESM
Copy link
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
Member 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
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