-
Notifications
You must be signed in to change notification settings - Fork 896
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Support bundlers. * test: Added bundlers support tests. * docs: Added bundling docs.
- Loading branch information
1 parent
38ff645
commit 4e08b37
Showing
7 changed files
with
155 additions
and
6 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
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,30 @@ | ||
# Bundling | ||
|
||
Due to its internal architecture based on Worker Threads, it is not possible to bundle Pino *without* generating additional files. | ||
|
||
In particular, a bundler must ensure that the following files are also bundle separately: | ||
|
||
* `lib/worker.js` from the `thread-stream` dependency | ||
* `file.js` | ||
* `lib/worker.js` | ||
* `lib/worker-pipeline.js` | ||
* Any transport used by the user (like `pino-pretty`) | ||
|
||
Once the files above have been generated, the bundler must also add information about the files above by injecting a code which sets `__bundlerPathsOverrides` in the `globalThis` object. | ||
|
||
The variable is a object whose keys are identifier for the files and the the values are the paths of files relative to the currently bundle files. | ||
|
||
Example: | ||
|
||
```javascript | ||
// Inject this using your bundle plugin | ||
globalThis.__bundlerPathsOverrides = { | ||
'thread-stream-worker': pinoWebpackAbsolutePath('./thread-stream-worker.js') | ||
'pino/file': pinoWebpackAbsolutePath('./pino-file.js'), | ||
'pino-worker': pinoWebpackAbsolutePath('./pino-worker.js'), | ||
'pino-pipeline-worker': pinoWebpackAbsolutePath('./pino-pipeline-worker.js'), | ||
'pino-pretty': pinoWebpackAbsolutePath('./pino-pretty.js'), | ||
}; | ||
``` | ||
|
||
Note that `pino/file`, `pino-worker`, `pino-pipeline-worker` and `thread-stream-worker` are required identifiers. Other identifiers are possible based on the user configuration. |
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,106 @@ | ||
'use strict' | ||
|
||
const os = require('os') | ||
const { join } = require('path') | ||
const { readFile } = require('fs').promises | ||
const { watchFileCreated } = require('../helper') | ||
const { test } = require('tap') | ||
const pino = require('../../pino') | ||
|
||
const { pid } = process | ||
const hostname = os.hostname() | ||
|
||
test('pino.transport with destination overriden by bundler', async ({ same, teardown }) => { | ||
globalThis.__bundlerPathsOverrides = { | ||
foobar: join(__dirname, '..', 'fixtures', 'to-file-transport.js') | ||
} | ||
|
||
const destination = join( | ||
os.tmpdir(), | ||
'_' + Math.random().toString(36).substr(2, 9) | ||
) | ||
const transport = pino.transport({ | ||
target: 'foobar', | ||
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 destination overriden by bundler', async ({ same, teardown }) => { | ||
globalThis.__bundlerPathsOverrides = { | ||
'pino-worker': join(__dirname, '..', '..', 'lib/worker.js') | ||
} | ||
|
||
const destination = join( | ||
os.tmpdir(), | ||
'_' + Math.random().toString(36).substr(2, 9) | ||
) | ||
const transport = pino.transport({ | ||
targets: [ | ||
{ | ||
target: join(__dirname, '..', 'fixtures', 'to-file-transport.js'), | ||
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 overriden by bundler', async ({ same, teardown }) => { | ||
globalThis.__bundlerPathsOverrides = { | ||
'pino-pipeline-worker': join(__dirname, '..', '..', 'lib/worker-pipeline.js') | ||
} | ||
|
||
const destination = join( | ||
os.tmpdir(), | ||
'_' + Math.random().toString(36).substr(2, 9) | ||
) | ||
const transport = pino.transport({ | ||
pipeline: [ | ||
{ | ||
target: join(__dirname, '..', 'fixtures', 'to-file-transport.js'), | ||
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 | ||
}) |