Skip to content

Commit a7ee8ab

Browse files
Nicolas Burgerburgerni10
Nicolas Burger
and
burgerni10
authoredAug 19, 2022
fix loading .js with pkg (#1493)
* 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 <nicolas.burger@optimistik.fr>
1 parent cf49ecb commit a7ee8ab

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
 

‎lib/transport-stream.js

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ async function loadTransportStreamBuilder (target) {
3131
// See this PR for details: https://github.com/pinojs/thread-stream/pull/34
3232
if ((error.code === 'ENOTDIR' || error.code === 'ERR_MODULE_NOT_FOUND')) {
3333
fn = realRequire(target)
34+
} else if (error.code === undefined) {
35+
// When bundled with pkg, an undefined error is thrown when called with realImport
36+
fn = realRequire(decodeURIComponent(target))
3437
} else {
3538
throw error
3639
}

‎test/transport/bundlers-support.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,36 @@ test('pino.transport with worker destination overridden by bundler', async ({ sa
6666
globalThis.__bundlerPathsOverrides = undefined
6767
})
6868

69+
test('pino.transport with worker destination overridden by bundler and mjs transport', async ({ same, teardown }) => {
70+
globalThis.__bundlerPathsOverrides = {
71+
'pino-worker': join(__dirname, '..', '..', 'lib/worker.js')
72+
}
73+
74+
const destination = file()
75+
const transport = pino.transport({
76+
targets: [
77+
{
78+
target: join(__dirname, '..', 'fixtures', 'ts', 'to-file-transport.es2017.cjs'),
79+
options: { destination }
80+
}
81+
]
82+
})
83+
teardown(transport.end.bind(transport))
84+
const instance = pino(transport)
85+
instance.info('hello')
86+
await watchFileCreated(destination)
87+
const result = JSON.parse(await readFile(destination))
88+
delete result.time
89+
same(result, {
90+
pid,
91+
hostname,
92+
level: 30,
93+
msg: 'hello'
94+
})
95+
96+
globalThis.__bundlerPathsOverrides = undefined
97+
})
98+
6999
test('pino.transport with worker-pipeline destination overridden by bundler', async ({ same, teardown }) => {
70100
globalThis.__bundlerPathsOverrides = {
71101
'pino-pipeline-worker': join(__dirname, '..', '..', 'lib/worker-pipeline.js')

0 commit comments

Comments
 (0)
Please sign in to comment.