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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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