-
Notifications
You must be signed in to change notification settings - Fork 35
fix: modify type
field of transpiled ESM packages
#783
Conversation
// "/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> |
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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({ |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of microsoft/TypeScript#13086.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
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. |
…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
- Summary
When transpiling ESM packages to CJS, we should also modify their
package.json
so that thetype
field is set tocommonjs
, notmodule
.- Test plan
Existing tests reused.
- A picture of a cute animal (not mandatory but encouraged)