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: adds support for builds with cache #10

Merged
merged 7 commits into from
Dec 4, 2021
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
27 changes: 26 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const footer = `
; if(typeof module !== 'undefined' && typeof __webpack_exports__ !== "undefined") { module.exports = __webpack_exports__; }
`

const CACHE_ID = 'pino-worker-plugin'

class PinoWebpackPlugin {
constructor(options) {
options = { transports: [], ...options }
Expand Down Expand Up @@ -63,7 +65,7 @@ class PinoWebpackPlugin {
generatedPaths[id] = this.getGeneratedFile(stats.compilation, id)
}

callback()
this.handleCache(compiler, generatedPaths, callback)
})
}
)
Expand All @@ -78,6 +80,29 @@ class PinoWebpackPlugin {
})
}

handleCache(compiler, generatedPaths, callback) {
// reference for cache https://github.com/webpack/webpack/blob/dc70535ef859e517e8659f87ca37f33261ad9092/lib/Cache.js
const cache = compiler.getCache(CACHE_ID)

// With this trick we are filling the missing entries to the generatedPaths
// caused by the fact that the Webpack Cache is enabled
// we are not invalidating cache because we are only filling what's missing
cache.get(CACHE_ID, CACHE_ID, (err, data = {}) => {
/* c8 ignore next 3 */
if (err) {
return callback(err)
}

for (const [id, fileName] of Object.entries(data)) {
if (!generatedPaths[id]) {
generatedPaths[id] = fileName
}
}

cache.store(CACHE_ID, CACHE_ID, generatedPaths, callback)
})
}

trackInclusions(workers, mod) {
if (mod.rawRequest === 'thread-stream') {
this.addExternalFile(workers, mod, 'thread-stream-worker', './lib/worker.js')
Expand Down
83 changes: 83 additions & 0 deletions test/cached-build.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
'use strict'

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

function runBuild(distFolder, onDone) {
webpack(
{
context: resolve(__dirname, 'fixtures'),
mode: 'production',
target: 'node',
entry: {
first: './first.js',
'abc/cde/second': './second.js'
},
output: {
path: distFolder,
filename: '[name]-[contenthash].js'
},
cache: {
name: 'test',
type: 'filesystem',
cacheDirectory: join(distFolder, 'cache'),
buildDependencies: {
config: [__filename]
}
},
plugins: [new PinoWebpackPlugin({ transports: ['pino-pretty'] })],
optimization: {
minimize: false
}
},
onDone
)
}

test('it should correctly generate all required pino files when the cache is enabled on Webpack', (t) => {
t.plan(7)

const distFolder = resolve(__dirname, '../tmp/cached-build')

t.teardown(() => {
spawnSync(`rm -rf ${distFolder}`, {
shell: true
})
})

runBuild(distFolder, (err) => {
t.error(err)

runBuild(distFolder, (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 nestedFiles = readdirSync(resolve(distFolder, 'abc/cde')).filter((e) => e.endsWith('.js'))

const firstFile = rootFiles.find((e) => e.startsWith('first-'))
const secondFile = nestedFiles.find((e) => e.startsWith('second-'))
const pinoFile = rootFiles.find((e) => e.startsWith('pino-file-'))
const pinoPretty = rootFiles.find((e) => e.startsWith('pino-pretty-'))

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

// Check that the root file starts with the banner and has the right path to pino-file
const secondContent = readFileSync(resolve(distFolder, `abc/cde/${secondFile}`), 'utf-8')
t.ok(secondContent.startsWith(banner))
t.ok(secondContent.includes(`'pino-pretty': pinoWebpackAbsolutePath('../../${pinoPretty}')`))
})
})
})
4 changes: 3 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ test('it should correctly generated all required pino files', (t) => {
const distFolder = resolve(__dirname, '../tmp/dist')

t.teardown(() => {
spawnSync(`rm -rf ${distFolder}`)
spawnSync(`rm -rf ${distFolder}`, {
shell: true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's because the command didn't locate correctly the rm executable so the tmp dir was not deleted.

})
})

webpack(
Expand Down