-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a post bundle script for preview-api
To workaround vercel/next.js#57962 cc @ndelangen. Not sure we want to merge this but it gets us through the demo for now.
- Loading branch information
Showing
2 changed files
with
32 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/// <reference types="node" /> | ||
|
||
// rewrite code from dist that triggers this bug: https://github.com/vercel/next.js/issues/57962 | ||
import { join, relative } from 'path'; | ||
import { cwd } from 'process'; | ||
import { promises } from 'fs'; | ||
|
||
const DIST = relative(cwd(), './dist'); | ||
|
||
async function go() { | ||
const filenames = await promises.readdir(DIST); | ||
|
||
await Promise.all( | ||
filenames.map(async (filename) => { | ||
if (filename.endsWith('.mjs')) { | ||
const fullFilename = join(DIST, filename); | ||
|
||
const content = await promises.readFile(fullFilename, 'utf-8'); | ||
|
||
const newContent = content | ||
.replace(/\(exports, module\)/g, '(exports, mod)') | ||
.replace(/module.exports = /g, 'mod.exports = '); | ||
|
||
await promises.writeFile(fullFilename, newContent); | ||
} | ||
}) | ||
); | ||
} | ||
|
||
go(); |
8406fc1
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.
I cannot find an alternative, I looked if esbuild allowed you to setup this variable name, but it does not look like it.
I wonder if esbuild would change their code if we asked them to?
For reference, this is the generated implementation code of
__commonJs
:And here, it's usage (a random non-important module):
...notice how the implementation does NOT use the word
module
?8406fc1
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.
I mean I think NextJS will need to fix the issue, I think assuming the first occurrence of the string
module.exports =
in a (.mjs
!) file is the exports (if that is what they are doing) is unreliable for a few reasons.I'm not quite sure what is best to do. It does seem like it would be safer for esbuild to change their code, that's true.