Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack: pass current entry name into js?

For example we have multiple entries:

entry: {
    main:  "./app/entry.js",
    view:  "./app/entry.js",
},

how to pass current name (main or view) into entry.js?

Ideal solution will be something like this:

new webpack.DefinePlugin({
    '_ENTRY_': '[name]'
}),

like other config options can have, but of course DefinePlugin dont know how to process this...

like image 704
itspers Avatar asked Aug 10 '16 17:08

itspers


1 Answers

If you're running the code inside Node.js then you can use __filename and __dirname.

Webpack can mock them for non-Node.js environments.
Please see Webpack Node configuration

node: {
  __filename: true,
  __dirname: true
}

In this case you can use __filename and __dirname globals as usually done in Node.js enviroments.

if(__filename.indexOf('index.js') != -1) {
    // Code here
}

node.__filename

Default: "mock"

Options:

  • true: The filename of the input file relative to the context option.

  • false: The regular Node.js __filename behavior. The filename of the output file when run in a Node.js environment.

  • "mock": The fixed value "index.js".

like image 128
Piotr Styczyński Avatar answered Oct 13 '22 17:10

Piotr Styczyński