Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve module formats (cjs, esm, iife) (#1247)
* chore: adopt "tsup" * chore: copy worker with tsup * chore: bundle iife correctly, fix call frames * chore: inject "setTimeout" for node build * chore: log worker checksum * chore(postinstall): use early return over process.exit
- Loading branch information
1 parent
77d1612
commit 00e9c58
Showing
25 changed files
with
515 additions
and
621 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
lib | ||
node | ||
native | ||
test/typings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import path from 'path' | ||
import fs from 'fs-extra' | ||
import crypto from 'crypto' | ||
import minify from 'babel-minify' | ||
import { invariant } from 'outvariant' | ||
import type { Plugin } from 'esbuild' | ||
import copyServiceWorker from '../../copyServiceWorker' | ||
|
||
function getChecksum(contents: string): string { | ||
const { code } = minify( | ||
contents, | ||
{}, | ||
{ | ||
// @ts-ignore "babel-minify" has no type definitions. | ||
comments: false, | ||
}, | ||
) | ||
|
||
return crypto.createHash('md5').update(code, 'utf8').digest('hex') | ||
} | ||
|
||
let hasRunAlready = false | ||
|
||
export function workerScriptPlugin(): Plugin { | ||
return { | ||
name: 'workerScriptPlugin', | ||
async setup(build) { | ||
const workerSourcePath = path.resolve( | ||
process.cwd(), | ||
'./src/mockServiceWorker.js', | ||
) | ||
const workerOutputPath = path.resolve( | ||
process.cwd(), | ||
'./lib/mockServiceWorker.js', | ||
) | ||
|
||
invariant( | ||
workerSourcePath, | ||
'Failed to locate the worker script source file', | ||
) | ||
invariant( | ||
workerOutputPath, | ||
'Failed to locate the worker script output file', | ||
) | ||
|
||
// Generate the checksum from the worker script's contents. | ||
const workerContents = await fs.readFile(workerSourcePath, 'utf8') | ||
const checksum = getChecksum(workerContents) | ||
|
||
// Inject the global "SERVICE_WORKER_CHECKSUM" variable | ||
// for runtime worker integrity check. | ||
build.initialOptions.define = { | ||
SERVICE_WORKER_CHECKSUM: JSON.stringify(checksum), | ||
} | ||
|
||
// Prevent from copying the worker script multiple times. | ||
// esbuild will execute this plugin for *each* format. | ||
if (hasRunAlready) { | ||
return | ||
} | ||
|
||
hasRunAlready = true | ||
|
||
build.onLoad({ filter: /mockServiceWorker\.js$/ }, async () => { | ||
return { | ||
// Prevent the worker script from being transpiled. | ||
// But, generally, the worker script is not in the entrypoints. | ||
contents: '', | ||
} | ||
}) | ||
|
||
build.onEnd(() => { | ||
console.log('worker script checksum:', checksum) | ||
|
||
// Copy the worker script on the next tick. | ||
setTimeout(async () => { | ||
await copyServiceWorker(workerSourcePath, workerOutputPath, checksum) | ||
}, 100) | ||
}) | ||
}, | ||
} | ||
} |
24 changes: 0 additions & 24 deletions
24
config/plugins/rollup-integrity-check-plugin/getChecksum.js
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { setTimeout as nodeSetTimeout } from 'timers' | ||
|
||
// Polyfill the global "setTimeout" so MSW could be used | ||
// with "jest.useFakeTimers()". MSW response handling | ||
// is wrapped in "setTimeout", and without this polyfill | ||
// you'd have to manually advance the timers for the response | ||
// to finally resolve. | ||
export const setTimeout = nodeSetTimeout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"main": "./lib/index.js", | ||
"types": "../lib/types/native" | ||
"main": "../lib/native/index.js", | ||
"module": "../lib/native/index.mjs", | ||
"typings": "../lib/native/index.d.ts" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"main": "./lib/index.js", | ||
"types": "../lib/types/node" | ||
"main": "../lib/node/index.js", | ||
"module": "../lib/node/index.mjs", | ||
"typings": "../lib/node/index.d.ts" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.