Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Current file path in webpack

Tags:

webpack

Is there any way to receive current file path, like in requirejs?

define(['module'], function (module) {     console.log(module.uri) }); 
like image 621
Sergey Lapin Avatar asked Aug 28 '14 16:08

Sergey Lapin


People also ask

What is webpack public path?

publicPath specifies the virtual directory in web server from where bundled file, app. js is going to get served up from. Keep in mind, the word server when using publicPath can be either webpack-dev-server or express server or other server that you can use with webpack.

Where is the webpack config file?

To answer your specific question, the webpack configuration is stored wherever your global node_modules are installed; on Windows this is typically %AppData%\Roaming\npm\node_modules\powerbi-visuals-tools\lib\webpack. config. js.

What is libraryTarget in webpack?

This is according to their documentation: "libraryTarget: "umd" - This exposes your library under all the module definitions, allowing it to work with CommonJS, AMD and as global variable." Also, I built the exact same code with Webpack 3 and it produced a proper bundle.

What is __ Dirname in webpack?

The __dirname in a node script returns the path of the folder where the current JavaScript file resides. __filename and __dirname are used to get the filename and directory name of the currently executing file. The ./ gives the current working directory. It works similar to process.


2 Answers

Yep there is one: __filename.

But by default webpack doesn't leak path information and you need to set a config flag to get real filename instead of a mock ("/index.js").

// /home/project/webpack.config.js module.exports = {   context: __dirname,   node: {     __filename: true   } } 

Than you can use __filename get the current filename relative to the context option:

// in /home/project/dir/file.js console.log(__filename); // => logs "dir/file.js" 

The filename is only embedded into modules where __filename is used. So you don't have to be affraid that paths are leaked from other modules.

like image 171
Tobias K. Avatar answered Oct 11 '22 19:10

Tobias K.


To get the filename an the dir name I added this to the web pack config

node : {    __filename: true,    __dirname: true, }, 

setting the context to __dirname messed up my web pack config since I have my webpackconfig not placed in root but the paths are setup that way

like image 35
P-A Avatar answered Oct 11 '22 20:10

P-A