Can't modify build output from plugin's onEnd callback
- Dominant language
- Go
- Stars
- 40.1k
- Forks
- 1.3k
- PR merge metrics
- No merged PRs in 30d
Description
I'm trying to add a Service Worker and I'd like its `scope` to be at the root of my app (I know about the HTTP header but I'm trying to avoid that). Right now my esbuild configuration looks something like this:
```js
{
entryNames: '[dir]/[name]-[hash]',
entryPoints: // function returning array of all entry files
outdir: 'dist/scripts',
}
```
I'd like to be able to register my Service Worker like this (at the root, file with no hash):
```js
navigator.serviceWorker.register('/serviceWorker.js')
```
But the problems I'm facing are the hash, and the output of my worker being in `/dist/scripts`. I want to leave the result of the build unchanged and only change the output for this one file. I saw that a [plugin's `onEnd` callback](https://esbuild.github.io/plugins/#on-end) could modify the build output:
> It can modify the build result before returning and can delay the end of the build by returning a promise.
I thought this meant that I would be able to have a plugin that would move the built file up to the root of the output directory and remove the hash. Here's a sample of the callback I've been testing with:
```js
build.onEnd((result) => {
console.log('RESULT KEYS', Object.keys(result));
console.log('OUTPUT FILES', result.outputFiles);
});
```
And here's the output:
```
RESULT KEYS [ 'errors', 'warnings', 'outputFiles', 'metafile', 'mangleCache' ]
OUTPUT FILES undefined
```
I didn't expect that `result.outputFiles` would be undefined, and any modification I make to `result.metafile` to delete the current serviceWorker key and add a new one doesn't seem to change anything. I also tried changing `entryPoints` so that it would conditionally add something like `{ out: 'serviceWorker', in: 'path/to/source/serviceWorker.ts'},` instead of just the filename but that didn't seem to work either. Am I trying to do something that's just not possible, or am I doing it wrong?
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the plugin's onEnd callback and the build result fields shown here, especially outputFiles, metafile, and entryPoints. Reproduce the configuration using entryNames, entryPoints, and outdir, then determine whether a plugin can relocate and rename only the service worker while leaving the rest unchanged. Done means a supported approach or a clearly documented limitation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- build-system, web-dev
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100