Skip to content

Commit

Permalink
fix loading .js with pkg (#1493)
Browse files Browse the repository at this point in the history
* fix loading .js with pkg

* add test for non .js file with overridden bundler

* catch pkg undefined error when using realImport and use realRequire instead

Co-authored-by: burgerni10 <[email protected]>
  • Loading branch information
Nicolas Burger and burgerni10 authored Aug 19, 2022
1 parent cf49ecb commit a7ee8ab
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/transport-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ async function loadTransportStreamBuilder (target) {
// See this PR for details: https://github.com/pinojs/thread-stream/pull/34
if ((error.code === 'ENOTDIR' || error.code === 'ERR_MODULE_NOT_FOUND')) {
fn = realRequire(target)
} else if (error.code === undefined) {
// When bundled with pkg, an undefined error is thrown when called with realImport
fn = realRequire(decodeURIComponent(target))
} else {
throw error
}
Expand Down
30 changes: 30 additions & 0 deletions test/transport/bundlers-support.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,36 @@ test('pino.transport with worker destination overridden by bundler', async ({ sa
globalThis.__bundlerPathsOverrides = undefined
})

test('pino.transport with worker destination overridden by bundler and mjs transport', async ({ same, teardown }) => {
globalThis.__bundlerPathsOverrides = {
'pino-worker': join(__dirname, '..', '..', 'lib/worker.js')
}

const destination = file()
const transport = pino.transport({
targets: [
{
target: join(__dirname, '..', 'fixtures', 'ts', 'to-file-transport.es2017.cjs'),
options: { destination }
}
]
})
teardown(transport.end.bind(transport))
const instance = pino(transport)
instance.info('hello')
await watchFileCreated(destination)
const result = JSON.parse(await readFile(destination))
delete result.time
same(result, {
pid,
hostname,
level: 30,
msg: 'hello'
})

globalThis.__bundlerPathsOverrides = undefined
})

test('pino.transport with worker-pipeline destination overridden by bundler', async ({ same, teardown }) => {
globalThis.__bundlerPathsOverrides = {
'pino-pipeline-worker': join(__dirname, '..', '..', 'lib/worker-pipeline.js')
Expand Down

0 comments on commit a7ee8ab

Please sign in to comment.