Skip to content

Commit

Permalink
refactor: use 'export =' to build a CommonJS module that exports a fu…
Browse files Browse the repository at this point in the history
…nction
  • Loading branch information
wd-David committed Jun 18, 2022
1 parent 5fc632d commit f3f96fd
Showing 1 changed file with 87 additions and 85 deletions.
172 changes: 87 additions & 85 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,106 +33,108 @@ import type { Plugin } from 'esbuild'
* }).catch(() => process.exit(1))
* ```
*/
const esbuildPluginPino = ({
export = function esbuildPluginPino({
transports
}: {
transports: string[]
}): Plugin => ({
name: 'pino',
setup(currentBuild) {
const pino = dirname(require.resolve('pino'))
const threadStream = dirname(require.resolve('thread-stream'))
}): Plugin {
return {
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: Record<string, string> = {}
for (const entrypoint of entrypoints) {
const destination = (
outbase ? entrypoint.replace(`${outbase}${sep}`, '') : entrypoint
).replace(/.(js|ts)$/, '')
newEntrypoints[destination] = entrypoint
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: Record<string, string> = {}
for (const entrypoint of entrypoints) {
const destination = (
outbase ? entrypoint.replace(`${outbase}${sep}`, '') : entrypoint
).replace(/.(js|ts)$/, '')
newEntrypoints[destination] = entrypoint
}
entrypoints = newEntrypoints
}
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(
(transports || []).map((t) => [t, require.resolve(t)])
)
currentBuild.initialOptions.entryPoints = {
...entrypoints,
...customEntrypoints,
...transportsEntrypoints
}
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(
(transports || []).map((t) => [t, require.resolve(t)])
)
currentBuild.initialOptions.entryPoints = {
...entrypoints,
...customEntrypoints,
...transportsEntrypoints
}

let pinoBundlerRan = false
let pinoBundlerRan = false

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

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

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

const absoluteOutputPath = join(
resolve('./'),
currentBuild.initialOptions.outdir || 'dist'
)
const absoluteOutputPath = join(
resolve('./'),
currentBuild.initialOptions.outdir || 'dist'
)

const functionDeclaration = `
function pinoBundlerAbsolutePath(p) {
try {
return require('path').join('${absoluteOutputPath}', p)
} catch(e) {
const f = new Function('p', 'return new URL(p, import.meta.url).pathname');
return f(p)
const functionDeclaration = `
function pinoBundlerAbsolutePath(p) {
try {
return require('path').join('${absoluteOutputPath}', 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 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 globalThisDeclaration = `
globalThis.__bundlerPathsOverrides =
globalThis.__bundlerPathsOverrides
? {...globalThis.__bundlerPathsOverrides, ${pinoOverrides}}
: {${pinoOverrides}};
`

const code = functionDeclaration + globalThisDeclaration
const code = functionDeclaration + globalThisDeclaration

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

export default esbuildPluginPino
}

0 comments on commit f3f96fd

Please sign in to comment.