You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As of now, esbuild inserts a banner before every built file even if it's bundled within a single chunk.
In my case, I insert the use client directive, and it's being inserted multiple times within a single chunk, which leads to the next error when my package is used in nextjs:
ReactServerComponentsError:
The "use client" directive must be placed before other expressions. Move it to the top of the file to resolve this issue.
My proposal is to extend the BannerOrFooter to accept a new option single: boolean which would default to false to ensure backwards compatibility, yet that would insert a banner or a footer only once in a compiled file.
// banner: {// js: '"use client";',// },// Hack to avoid multiple `use client` directives in single file instead of using banner.// Delete when implemented https://github.com/evanw/esbuild/issues/3517asynconSuccess(){// recursively go through each js file in dist and add "use client" to the topconstdistDir=path.join(__dirname,'dist')constfiles=awaitfs.readdir(distDir)asyncfunctionprocessFilesRecursively(dir: string){// Read dirents of the current folderconstdirents=awaitfs.readdir(dir,{withFileTypes: true})constpromises=dirents.map(async(dirent)=>{constfilePath=dirent.path+'/'+dirent.nameif(dirent.isDirectory())returnprocessFilesRecursively(filePath)if(dirent.name.endsWith('.js')){constfileContents=awaitfs.readFile(filePath,'utf8')constnewFileContents=`'use client'\n${fileContents.replaceAll('"use client"','').replaceAll("'use client'",'')}`awaitfs.writeFile(filePath,newFileContents)}})returnPromise.all(promises.flat())}awaitprocessFilesRecursively(distDir)},
As of now,
esbuild
inserts a banner before every built file even if it's bundled within a single chunk.In my case, I insert the
use client
directive, and it's being inserted multiple times within a single chunk, which leads to the next error when my package is used in nextjs:My proposal is to extend the
BannerOrFooter
to accept a new optionsingle: boolean
which would default tofalse
to ensure backwards compatibility, yet that would insert a banner or a footer only once in a compiled file.The text was updated successfully, but these errors were encountered: