Skip to content

Commit

Permalink
Escape window paths in quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
madtisa committed Oct 16, 2023
1 parent 539dced commit 0456686
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const webpack = require('webpack')
const CommonJsRequireDependency = require('webpack/lib/dependencies/CommonJsRequireDependency')
const { createFsFromVolume, Volume } = require('memfs')
const { sep, dirname, relative } = require('path')
const { join, dirname, relative } = require('path')
// const { cache } = require('webpack') // Cache handling missing

const fileBanner = `/* Start of pino-webpack-plugin additions */
Expand All @@ -25,6 +25,10 @@ const PLUGIN_NAME = 'PinoWebpackPlugin'

const fs = createFsFromVolume(new Volume())

function quote(path) {
return `'${path.replace(/([\\'])/g, '\\$1')}'`
}

class PinoWebpackPlugin {
constructor(options) {
options = { transports: [], ...options }
Expand Down Expand Up @@ -164,7 +168,7 @@ class PinoWebpackPlugin {
fileBanner,
'\n',
`globalThis.__bundlerPathsOverrides = {${dependenciesFiles.map(
([workerId, file]) => `'${workerId}': pinoWebpackAbsolutePath('${relativePath}${sep}${file}')`
([workerId, file]) => `'${workerId}': pinoWebpackAbsolutePath(${quote(join(relativePath, file))})`
)}};`,
'\n',
'/* End of pino-webpack-plugin additions */',
Expand Down
16 changes: 10 additions & 6 deletions test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,18 @@ function createTests(tapInstance, webpackConfig, distFolder) {
const fileContent = readFileSync(resolve(distFolder, filePath), 'utf-8')
// Test if the file starts with the expected banner
tapInstance.ok(fileContent.startsWith(banner), `${basename(filePath)} should starts with banner`)
const quoteRegex = (path) => `'${path.replace(/\\/g, '\\\\\\\\')}'`
for (const dependencyKey in dependencies) {
// For each dependency, test if the expeted file is in the __bundlerPathsOverrides object
// For each dependency, test if the expected file is in the __bundlerPathsOverrides object
const dependencyRegexp = new RegExp(
`globalThis\\.__bundlerPathsOverrides = \\{.*?'${dependencyKey}': pinoWebpackAbsolutePath\\('${
(relative(dirname(filePath), dirname(dependencies[dependencyKey])) || '.') +
'/' +
dependencies[dependencyKey]
}'\\).*?\\}`
`globalThis\\.__bundlerPathsOverrides = \\{.*?'${dependencyKey}': pinoWebpackAbsolutePath\\(${
quoteRegex(
join(
relative(dirname(filePath), dirname(dependencies[dependencyKey])),
dependencies[dependencyKey]
)
)
}\\).*?\\}`
)
tapInstance.ok(fileContent.match(dependencyRegexp), `${dependencyKey} should have correct path in ${filePath}`)
}
Expand Down

0 comments on commit 0456686

Please sign in to comment.