Skip to content

Commit

Permalink
build(esbuild): add esbuild-plugin-pino to build script
Browse files Browse the repository at this point in the history
  • Loading branch information
wd-David committed Jun 18, 2022
1 parent b7b2d08 commit 0cd0d8c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 101 deletions.
105 changes: 4 additions & 101 deletions esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ const env = process.argv[2]
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable no-undef */
const { readdirSync, statSync } = require('fs')
const { dirname, sep, join } = require('path')
const { join } = require('path')
const { build } = require('esbuild')
const { readFile } = require('fs/promises')
const esbuildPluginPino = require('esbuild-plugin-pino')

let fileArray = []
const getFilesRecursively = (dir) => {
Expand All @@ -23,110 +23,13 @@ getFilesRecursively('src')

const entryPoints = fileArray.filter((file) => file.endsWith('.ts'))

const pinoPlugin = (options) => ({
name: 'pino',
setup(currentBuild) {
const pino = dirname(require.resolve('pino'))
const threadStream = dirname(require.resolve('thread-stream'))

let entrypoints = currentBuild.initialOptions.entryPoints
if (Array.isArray(entrypoints)) {
let outbase = currentBuild.initialOptions.outbase
if (!outbase) {
const hierarchy = entrypoints[0].split(sep)
let i = 0
outbase = ''
let nextOutbase = ''
do {
outbase = nextOutbase
i++
nextOutbase = hierarchy.slice(0, i).join(sep)
} while (entrypoints.every((e) => e.startsWith(`${nextOutbase}${sep}`)))
}
const newEntrypoints = {}
for (const entrypoint of entrypoints) {
const destination = (
outbase ? entrypoint.replace(`${outbase}${sep}`, '') : entrypoint
).replace(/.(js|ts)$/, '')
newEntrypoints[destination] = entrypoint
}
entrypoints = newEntrypoints
}

const customEntrypoints = {
'thread-stream-worker': join(threadStream, 'lib/worker.js'),
'pino-worker': join(pino, 'lib/worker.js'),
'pino-pipeline-worker': join(pino, 'lib/worker-pipeline.js'),
'pino-file': join(pino, 'file.js')
}
const transportsEntrypoints = Object.fromEntries(
(options.transports || []).map((t) => [
t,
join(dirname(require.resolve(t)), 'index.js')
])
)
currentBuild.initialOptions.entryPoints = {
...entrypoints,
...customEntrypoints,
...transportsEntrypoints
}

let pinoBundlerRan = false

currentBuild.onEnd(() => {
pinoBundlerRan = false
})

currentBuild.onLoad({ filter: /pino\.js$/ }, async (args) => {
if (pinoBundlerRan) return
pinoBundlerRan = true

console.log(args.path)

const contents = await readFile(args.path, 'utf8')

const functionDeclaration = `
function pinoBundlerAbsolutePath(p) {
try {
return require('path').join(__dirname, p)
} catch(e) {
const f = new Function('p', 'return new URL(p, import.meta.url).pathname');
return f(p)
}
}
`

const pinoOverrides = Object.keys(customEntrypoints)
.map(
(id) =>
`'${
id === 'pino-file' ? 'pino/file' : id
}': pinoBundlerAbsolutePath('./${id}.js')`
)
.join(',')

const globalThisDeclaration = `
globalThis.__bundlerPathsOverrides =
globalThis.__bundlerPathsOverrides
? {...globalThis.__bundlerPathsOverrides, ${pinoOverrides}}
: {${pinoOverrides}};
`

const code = functionDeclaration + globalThisDeclaration

return {
contents: code + contents
}
})
}
})

build({
entryPoints,
logLevel: 'info',
outdir: env === 'dev' ? 'dist' : 'build',
bundle: env === 'dev' ? false : true,
platform: 'node',
format: 'cjs',
plugins: env === 'dev' ? [] : [pinoPlugin({ transport: ['pino-pretty'] })]
plugins:
env === 'dev' ? [] : [esbuildPluginPino({ transports: ['pino-pretty'] })]
})
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@typescript-eslint/parser": "^5.27.1",
"commitizen": "^4.2.4",
"esbuild": "^0.14.43",
"esbuild-plugin-pino": "^1.1.6",
"eslint": "^8.17.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.0.0",
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0cd0d8c

Please sign in to comment.