jfortunato / esbuild-plugin-manifest

Esbuild plugin for building an asset manifest file.
MIT License
31 stars 5 forks source link

Support multiple formats #13

Closed ComLock closed 10 months ago

ComLock commented 1 year ago

I'm using format: ['cjs','esm']

Which executes the manifestPlugin twice and the last result ends up in the file.

I did a hacky workaround by keeping an external obj and returning that: https://github.com/enonic/starter-tsup/blob/c800b77027d9d114c85f894fc286b821a66dcdc5/tsup/static.ts#L54 https://github.com/enonic/starter-tsup/blob/c800b77027d9d114c85f894fc286b821a66dcdc5/tsup/static.ts#L76

Which means the last write to the file contains a "merged" result.

But quite often the file gets corrupt and contains an extra curly end bracket (probably some race condition, no write lock?):

{
  "react/App.js": "react/App-WROXAUKF.js",
  "react/App.css": "react/App-67S276UR.css"
}  "react/App.mjs": "react/App-U7LJBZT3.mjs"
}

If would be nice if this plugins would make a single "merged" manifest file, or at least have a write lock.

ComLock commented 1 year ago

Perhaps use 'writeFileSync' here: https://github.com/jfortunato/esbuild-plugin-manifest/blob/develop/src/index.ts#LL124C14-L124C35

ref https://nodejs.org/api/fs.html#fswritefilesyncfile-data-options

'as' is interesting, but there is no 'ws'? https://nodejs.org/api/fs.html#file-system-flags

ComLock commented 1 year ago

Side note: I even add some of my own entries https://github.com/enonic/starter-tsup/blob/6e12022809b5c589d5535021ceb1bec59eaf7a08/tsup/static.ts#L26-L29

ComLock commented 1 year ago

I guess it has to stay asyncronous https://esbuild.github.io/plugins/#on-resolve

So a different solution would be to allow setting filename based on a function which has format in it's context...

ComLock commented 1 year ago

Made this since it got silent: https://www.npmjs.com/package/@enonic/tsup-plugin-manifest

jfortunato commented 10 months ago

The format https://esbuild.github.io/api/#format option for esbuild only allows you to specify 1 format at a time, so specifying format: ['cjs','esm'] wouldn't be valid when using esbuild on its own. (Looks like you got that from tsup)

To support multiple formats like this using esbuild on its own, you can utilize a combination of esbuilds outExtension option along with esbuild-plugin-manifests append option, like the example here: https://github.com/jfortunato/esbuild-plugin-manifest#multiple-formats