From 0456686be54aca283e0009c7b3a3735489041949 Mon Sep 17 00:00:00 2001 From: madtisa Date: Sun, 15 Oct 2023 23:47:38 +0300 Subject: [PATCH] Escape window paths in quotes --- src/index.js | 8 ++++++-- test/test-utils.js | 16 ++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/index.js b/src/index.js index 3a1284c..83f7502 100644 --- a/src/index.js +++ b/src/index.js @@ -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 */ @@ -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 } @@ -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 */', diff --git a/test/test-utils.js b/test/test-utils.js index dbaa61a..f1672a7 100644 --- a/test/test-utils.js +++ b/test/test-utils.js @@ -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}`) }