Home > database >  How to retain the name of the current source file while doing a WebPack bundling?
How to retain the name of the current source file while doing a WebPack bundling?

Time:01-28

I want to be able to create an object that tracks back to the original source file. In the past I manually created them with different IDs:

   const TRACER = new Tracker('mySource.ts')

But the project grows, code being copied/pasted without changing the IDs and this starts to be unmanagable. I wish I could just have

   const TRACKER = new Tracker(__filename)

or something similar in every source file - no room for errors etc. Such boilerplate can be copied as much as one wants withoug breaking.

Node already exposes the property __filename - it is set to the current file being executed. It works great in dev. However, once the code is bundled by Webpack (or any other bundler) and run, that will be set to the name of the bundle. I would like to still be able to access the original source filename, even after bundling, not necesarily by using __filename.

CodePudding user response:

All right, found it. https://webpack.js.org/configuration/node/#node__filename

In the webpack.config.js, I've added:

   node: {
      __filename: true
   },
  • Related