@@ -3,8 +3,8 @@ import path from 'node:path'
33import crypto from 'crypto'
44import minify from 'babel-minify'
55import { invariant } from 'outvariant'
6- import type { Plugin } from 'esbuild '
7- import copyServiceWorker from '../../copyServiceWorker.js '
6+ import type { TsdownPlugin } from 'tsdown '
7+ import copyServiceWorker from '../../copyServiceWorker.ts '
88
99const SERVICE_WORKER_ENTRY_PATH = path . resolve (
1010 process . cwd ( ) ,
@@ -27,15 +27,16 @@ export function getWorkerChecksum(): string {
2727 return getChecksum ( workerContents )
2828}
2929
30- export function copyWorkerPlugin ( checksum : string ) : Plugin {
30+ export function copyWorkerPlugin ( checksum : string ) : TsdownPlugin {
3131 return {
3232 name : 'copyWorkerPlugin' ,
33- async setup ( build ) {
33+ async buildStart ( ) {
3434 invariant (
3535 SERVICE_WORKER_ENTRY_PATH ,
3636 'Failed to locate the worker script source file' ,
3737 )
38-
38+ } ,
39+ async writeBundle ( ) {
3940 if ( fs . existsSync ( SERVICE_WORKER_OUTPUT_PATH ) ) {
4041 console . warn (
4142 'Skipped copying the worker script to "%s": already exists' ,
@@ -44,31 +45,14 @@ export function copyWorkerPlugin(checksum: string): Plugin {
4445 return
4546 }
4647
47- // Generate the checksum from the worker script's contents.
48- // const workerContents = await fs.readFile(workerSourcePath, 'utf8')
49- // const checksum = getChecksum(workerContents)
50-
51- build . onLoad ( { filter : / m o c k S e r v i c e W o r k e r \. j s $ / } , async ( ) => {
52- return {
53- // Prevent the worker script from being transpiled.
54- // But, generally, the worker script is not in the entrypoints.
55- contents : '' ,
56- }
57- } )
48+ // eslint-disable-next-line no-console
49+ console . log ( 'worker script checksum:' , checksum )
5850
59- build . onEnd ( ( ) => {
60- // eslint-disable-next-line no-console
61- console . log ( 'worker script checksum:' , checksum )
62-
63- // Copy the worker script on the next tick.
64- process . nextTick ( async ( ) => {
65- await copyServiceWorker (
66- SERVICE_WORKER_ENTRY_PATH ,
67- SERVICE_WORKER_OUTPUT_PATH ,
68- checksum ,
69- )
70- } )
71- } )
51+ await copyServiceWorker (
52+ SERVICE_WORKER_ENTRY_PATH ,
53+ SERVICE_WORKER_OUTPUT_PATH ,
54+ checksum ,
55+ )
7256 } ,
7357 }
7458}
0 commit comments