Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

fix: modify type field of transpiled ESM packages #783

Merged
merged 6 commits into from
Nov 2, 2021

Conversation

eduardoboucas
Copy link
Member

- Summary

When transpiling ESM packages to CJS, we should also modify their package.json so that the type field is set to commonjs, not module.

- Test plan

Existing tests reused.

- A picture of a cute animal (not mandatory but encouraged)

7f175148-2ad8-4170-a34f-6356948a6870-medium16x9_feat_6cd9db36ca604a249b8949fdb2de2df8

@eduardoboucas eduardoboucas added the type: bug code to address defects in shipped code label Nov 2, 2021
// "/my-transpiled-function.js" => "/my-function.js"
//
// When "/my-transpiled-function.js" is found in the list of files, it will
// be added to the archive with the "/my-function.js" path.
aliases?: Map<string, string>
Copy link
Member Author

Choose a reason for hiding this comment

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

aliases existed before, I've just added a comment to clarify what it's for.

// When "/my-function.js" is found in the list of files, it will be added to
// the archive with "console.log(`Hello!`)" as its source, replacing whatever
// the file at "/my-function.js" contains.
rewrites?: Map<string, string>
Copy link
Member Author

Choose a reason for hiding this comment

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

rewrites is a new concept.

const { fileList: dependencyPaths } = await nodeFileTrace([mainFile], {
const {
fileList: dependencyPaths,
esmFileList,
Copy link
Member Author

Choose a reason for hiding this comment

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

We were previously relying on NFT's cache object to inspect which of the paths corresponded to ESM files, but I realised there's a esmFileList property that contains just that, so there's no need to muck about with the internal cache.

// The version of ECMAScript to use as the build target. This will determine
// whether certain features are transpiled down or left untransformed.
const nodeTarget = getBundlerTarget(config.nodeVersion)

await build({
const transpiled = await build({
Copy link
Member Author

Choose a reason for hiding this comment

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

We were piping the output of esbuild to a temporary file, but actually we can just get the result loaded into memory by setting write: false and avoid creating a file altogether.


addZipFile(archive, srcFile, normalizedFilename, stat)
if (rewrites.has(srcFile)) {
addZipContent(archive, rewrites.get(srcFile) as string, normalizedDestPath)
Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Contributor

@netlify-team-account-1 netlify-team-account-1 left a comment

Choose a reason for hiding this comment

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

🚀

@hrishikesh-k
Copy link

Hey team, I believe this was supposed to be related with this thread: https://answers.netlify.com/t/nextjs-page-with-server-side-logic-throws-error-cannot-find-module-os/46431/? Could someone confirm? Users are still reporting it's not working.

Skn0tt pushed a commit to netlify/build that referenced this pull request May 21, 2024
…nd-ship-it#783)

* refactor: rename internal variables

* feat: add `rewrites` object

* refactor: load esbuild output from memory

* feat: modify package.json of transpiled ESM packages

* chore: update tests

* chore: remove unused import
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants