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 the workers path calculations #161

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
57 changes: 29 additions & 28 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,40 +171,41 @@ export default function esbuildPluginPino({

currentBuild.initialOptions.entryPoints = newEntrypoints

let pinoBundlerRan = false
currentBuild.onResolve(
{ filter: /^virtual:esbuild-plugin-pino-abspath$/ },
(args) => {
return {
path: args.path,
namespace: 'esbuild-plugin-pino'
}
}
)

currentBuild.onEnd(() => {
pinoBundlerRan = false
currentBuild.onLoad({filter: /^virtual:esbuild-plugin-pino-abspath$/, namespace: 'esbuild-plugin-pino'}, () => {
if (currentBuild.initialOptions.format === 'esm') {
return {
contents: `\
export function pinoBundlerAbsolutePath(p) {
return new URL(p, import.meta.url).pathname;
}
`
}
}

return {
contents: `\
exports.pinoBundlerAbsolutePath = function(p) {
return require('path').join(__dirname, p);
}
`
}
})

currentBuild.onLoad({ filter: /pino\.js$/ }, async (args) => {
if (pinoBundlerRan) return
pinoBundlerRan = true

const contents = await readFile(args.path, "utf8")

let absoluteOutputPath = ""
const { outdir = "dist" } = currentBuild.initialOptions
if (path.isAbsolute(outdir)) {
absoluteOutputPath = outdir.replace(/\\/g, "\\\\")
} else {
const workingDir = currentBuild.initialOptions.absWorkingDir
? `"${currentBuild.initialOptions.absWorkingDir.replace(/\\/g, "\\\\")}"`
: "process.cwd()"
absoluteOutputPath = `\${${workingDir}}\${require('path').sep}${
currentBuild.initialOptions.outdir || "dist"
}`
}

const functionDeclaration = `
function pinoBundlerAbsolutePath(p) {
try {
return require('path').join(\`${absoluteOutputPath}\`.replace(/\\\\/g, '/'), p)
} catch(e) {
const f = new Function('p', 'return new URL(p, import.meta.url).pathname');
return f(p)
}
}
const functionDeclaration = `\
const { pinoBundlerAbsolutePath } = require('virtual:esbuild-plugin-pino-abspath');
`

let extension = ".js"
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const arrayOfObjectsScriptPath = resolve(
)
const distFolder = "test/dist"

const functionDeclaration = "function pinoBundlerAbsolutePath(p)"
const functionDeclaration = "pinoBundlerAbsolutePath = function(p)"

describe("Test esbuildPluginPino", () => {
afterEach(() => {
Expand Down
Loading