Skip to content

Commit

Permalink
🐞 fix: vercel bug,Dynamic require of "assert" [esbuild #1921](evanw/e…
Browse files Browse the repository at this point in the history
  • Loading branch information
WhaleFell committed Jun 26, 2024
1 parent a52e274 commit f8170ba
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 24 deletions.
46 changes: 28 additions & 18 deletions addConfig.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import * as fs from 'fs'
import * as fs from "fs"

// check if the file path is provided
if (process.argv.length < 3) {
console.log('Offer a file path as argument to add Vercel config, Please!')
process.exit(1)
console.log("Offer a file path as argument to add Vercel config, Please!")
process.exit(1)
}

const filePath = process.argv[2]

// the content to add in the file
const contentToAdd = `
// https://github.com/evanw/esbuild/issues/1921#issuecomment-1898197331
import { createRequire } from 'node:module';
import path from 'node:path';
import url from 'node:url';
globalThis.require = createRequire(import.meta.url);
globalThis.__filename = url.fileURLToPath(import.meta.url);
globalThis.__dirname = path.dirname(__filename);
export const config = {
supportsResponseStreaming: true,
api: {
Expand All @@ -20,19 +30,19 @@ export const dynamic = 'force-dynamic'
`

fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(`Cannot read file: ${err.message}`)
process.exit(1)
}
const newData = contentToAdd + data

fs.writeFile(filePath, newData, 'utf8', (err) => {
if (err) {
console.error(`Cannot write file: ${err.message}`)
process.exit(1)
}

console.log('Add Vercel config in compiled JS file successfully!')
})
fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
console.error(`Cannot read file: ${err.message}`)
process.exit(1)
}
const newData = contentToAdd + data

fs.writeFile(filePath, newData, "utf8", (err) => {
if (err) {
console.error(`Cannot write file: ${err.message}`)
process.exit(1)
}

console.log("Add Vercel config in compiled JS file successfully!")
})
})
Loading

0 comments on commit f8170ba

Please sign in to comment.