Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Make sure transports are required. Fixes #43. #45

Merged
merged 3 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
contents: read
strategy:
matrix:
node-version: [12.x, 14.x, 16.x, 17.x]
node-version: [14.x, 16.x, 18.x]
steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -31,7 +31,7 @@ jobs:
runs-on: ubuntu-latest
permissions:
pull-requests: write
contents: write
contents: write
steps:
- uses: fastify/github-action-merge-dependabot@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^5.1.1",
"execa": "^5.0.0",
"pino": "^7.6.1",
"pino": "^8.0.0",
"pino-pretty": "^8.0.0",
"prettier": "^2.4.1",
"tap": "^16.0.0"
Expand Down
17 changes: 15 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const webpack = require('webpack')
const CommonJsRequireDependency = require('webpack/lib/dependencies/CommonJsRequireDependency')

const { sep } = require('path')

const banner = `/* Start of pino-webpack-plugin additions */
Expand All @@ -14,7 +16,7 @@ function pinoWebpackAbsolutePath(p) {
`

const footer = `
/* The following statement is added by pino-webpack-bundler to make sure extracted file are valid commonjs modules */
/* The following statement is added by pino-webpack-plugin to make sure extracted file are valid commonjs modules */
; if(typeof module !== 'undefined' && typeof __webpack_exports__ !== "undefined") { module.exports = __webpack_exports__; }
`

Expand All @@ -34,6 +36,17 @@ class PinoWebpackPlugin {
// When requiring pino, thread-stream or users transports, prepare some required files for external bundling.
compilation.hooks.buildModule.tap('PinoWebpackPlugin', this.trackInclusions.bind(this, workers))

// When requiring pino, also make sure all transports in the options are required
compilation.hooks.succeedModule.tap('PinoWebpackPlugin', (mod) => {
if (mod.rawRequest !== 'pino') {
return
}

for (const transport of this.transports) {
mod.dependencies.push(new CommonJsRequireDependency(transport, null))
}
})

// When webpack has finished analyzing and bundling all files, compile marked external files
compilation.hooks.processAssets.tapAsync(
{
Expand Down Expand Up @@ -196,7 +209,7 @@ class PinoWebpackPlugin {
// Prepend the banner and the __bundlerPathsOverrides to the generated file.
assets[path] = new webpack.sources.ConcatSource(
banner,
`\nglobalThis.__bundlerPathsOverrides = {${declarations}};\n/* End of pino-webpack-bundler additions */\n\n`,
`\nglobalThis.__bundlerPathsOverrides = {${declarations}};\n/* End of pino-webpack-plugin additions */\n\n`,
assets[path]
)
}
Expand Down
73 changes: 73 additions & 0 deletions test/explicit-import.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict'

const { readFileSync, readdirSync } = require('fs')
const { resolve } = require('path')
const { test } = require('tap')
const webpack = require('webpack')
const { banner, footer, PinoWebpackPlugin } = require('../src/index')
const execa = require('execa')

test('it should correctly generated all required pino files', (t) => {
t.plan(16)

const distFolder = t.testdir()

webpack(
{
context: resolve(__dirname, 'fixtures'),
mode: 'production',
target: 'node',
entry: {
third: './third.mjs'
},
output: {
path: distFolder,
filename: '[name]-[contenthash].js'
},
plugins: [new PinoWebpackPlugin({ transports: ['pino-pretty'] })],
optimization: {
minimize: false
}
},
(err, stats) => {
t.error(err)
t.notOk(stats.hasErrors())

// Find all files in the folder
const rootFiles = readdirSync(distFolder).filter((e) => e.endsWith('.js'))

const thirdFile = rootFiles.find((e) => e.startsWith('third-'))
const threadStream = rootFiles.find((e) => e.startsWith('thread-stream-'))
const pinoWorker = rootFiles.find((e) => e.startsWith('pino-worker-'))
const pinoPipelineWorker = rootFiles.find((e) => e.startsWith('pino-pipeline-worker-'))
const pinoFile = rootFiles.find((e) => e.startsWith('pino-file-'))
const pinoPretty = rootFiles.find((e) => e.startsWith('pino-pretty-'))

// Check that all required files have been generated
t.ok(thirdFile)
t.ok(threadStream)
t.ok(pinoWorker)
t.ok(pinoPipelineWorker)
t.ok(pinoFile)
t.ok(pinoPretty)

// Check that generated pino related files have the right footer
for (const file of [threadStream, pinoWorker, pinoPipelineWorker, pinoFile, pinoPretty]) {
t.ok(readFileSync(resolve(distFolder, file), 'utf-8').endsWith(footer))
}

// Check that the root file starts with the banner and has the right path to pino-file
const thirdContent = readFileSync(resolve(distFolder, thirdFile), 'utf-8')
t.ok(thirdContent.startsWith(banner))
t.ok(
thirdContent.includes(
`globalThis.__bundlerPathsOverrides = {'pino/file': pinoWebpackAbsolutePath('./${pinoFile}')`
)
)

execa(process.argv[0], [resolve(distFolder, thirdFile)]).then(({ stdout }) => {
t.match(stdout, /This is third!/)
})
}
)
})
10 changes: 10 additions & 0 deletions test/fixtures/third.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pino from 'pino'
import 'pino-pretty'

const logger = pino(
pino.transport({
target: 'pino-pretty'
})
)

logger.info('This is third!')