#Replacing special string with name of file / current line number

9 messages · Page 1 of 1 (latest)

hybrid dragon
#

Hello again everyone!
I'll start by apologizing for the somewhat off-topic question, I've sadly not gotten any responses about this elsewhere and I figured someone here might at least be able to point me in the right direction

I'm writing an Electron application. To be able to easily tell what's going on, the app logs various things in various places. Problem is, in the Backend (where most of the logic happens), it's not that easy to tell where a message came from. Thus, I'd like to be able to do the following: Assume I have a file src/foo.ts. When writing

...
// Line 45 below
console.log('__filename', '__lineno')
...

I'd like to have my bundler modify that line so it reads

console.log('src/foo.ts', '45')

I'm using Vite to transpile code to JS, but I've been unable to find any tools/plugins that can do this. I'm not originally from a TypeScript background, sorry if this is just the "wrong" approach entirely!

arctic delta
#

usually, you shouldn't log things when in production

#

if you need to debug your app, you can always set breakpoints and use map files

#

you might asw ell write the file name and line number manually if it isn't use in lots of places

#

made a few quick search but wouldn't find any plugin doing what you describe
can be a great opportunity to create one yourself 👍

hybrid dragon
#

usually, you shouldn't log things when in production
If we'd be talking about a website or any type of user-facing scenario here, I'd agree. But a desktop app is a little too volatile for no logging whatsoever (especially the real-world use case of this)
No offense, but if just not logging anything would've been an option, I would've just done that

can be a great opportunity to create one yourself
Right, looks like that's the plan then

small barn
#

@rollup/plugin-replace is pretty close already

#

you just need to pass a few more arguments to the replacer function