How can I pass the current entry name into a JavaScript file using Webpack?

Suppose we have multiple entries as shown below:

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

Is there a way to pass the current name (main or view) into entry.js?

An optimal solution would look like this:

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

This is similar to other configuration options, but unfortunately DefinePlugin does not know how to handle this...

Answer №1

If you're utilizing the code within Node.js, you have the option to utilize __filename and __dirname.

For non-Node.js environments, Webpack can simulate them.
Refer to the Webpack Node configuration

node: {
  __filename: true,
  __dirname: true
}

In such scenarios, you can make use of the __filename and __dirname global variables as typically seen in Node.js setups.

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".

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Instructions for invoking a JavaScript function encapsulated within a jQuery wrapper

I've been delving into a JavaScript file recently and noticed that some of the methods are enclosed within a jQuery function. Could someone kindly guide me on how to call the "testMethod" method? Also, what is the advantage or purpose of wrapping a me ...

Troubleshooting problems encountered when duplicating an array in JavaScript

I am attempting to utilize properties and flatmap to modify an array without altering the original data. I have implemented this method in two different instances within a single dispatch call in Vue.js when transferring data from parent to children comp ...

Obtain the Present Controller Identity within AngularJS

Currently, I am working on developing a directive that can create a grid dynamically. The code I have written functions properly but there is one limitation where I need to explicitly mention the controller name 'DemoCtrl'. I am wondering if ther ...

Firefox does not support event.dataTransfer.files for drag and drop operations

Currently, I am implementing drag and drop functionality into one of my projects and encountering an issue specifically with Firefox. The code I am using for testing purposes is as follows: document.getElementById("folder_files").addEventListener("drop", ...

Every div must have at least one checkbox checked

Coding in HTML <div class="response"> <input type="checkbox" /> <input type="checkbox" /> <input type="checkbox" /> <input type="checkbox" /> </div> <div class="response"> <input type="check ...

Using AJAX (jQuery) to call a web method declared in a .cs file that is not tied to any specific aspx or ascx file

After moving a web method from the code-behind file of an ASPX page to another CS file located in the data section (which does not have any associated ASPX page), I encountered an issue with accessing the web method using Ajax. Previously, I accessed the w ...

What makes sphere geometry take longer to render compared to cube geometry, even though they have the same number of faces?

Upon creating a cube using BoxGeometry and a sphere using SphereGeometry (with both objects spinning around themselves), I noticed that the sphere takes longer to render compared to the cube when increasing the number of objects. For instance: 500 cubes a ...

Failing to provide a response to the API for /api/register could potentially lead to requests becoming stuck

Currently working on a registration feature in Next.js where I am implementing password hashing using bcrypt before storing it in the database. Although the functionality seems to be working fine, calling the registration API results in a warning message: ...

What are the steps to integrate TypeScript into JavaScript code?

How can I import a TypeScript class in a Node CommonJS JavaScript file? When using mongoose in my TypeScript code, I typically do the following: // user.model.ts export const UserModel = model<User>('User', schema); In my JavaScript code: ...

Prevent redundant Webpack chunk creation

I am currently working on integrating a new feature into my Webpack project, and I have encountered a specific issue. Within the project, there are two entry points identified as about and feedback. The about entry point imports feedback, causing both abo ...

An issue in cordova android related to cross-domain restrictions

My small cordova app utilizes beacon plugins to send a get request to a specific page once beacons are discovered. However, I have been facing issues with sending the get request to my server using the code snippet below with jsonp. Despite trying out di ...

What are the steps to resolving a Dockerfile error regarding permission denied for /app?

Here is the Dockerfile for my node.js project: FROM node:14.16.0-alpine3.13 RUN addgroup app && adduser -S -G app app RUN mkdir /app && chown app:app /app USER app WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 80 CM ...

Converting an HTML table to a CSV file with pure JavaScript

I need to implement a CSV download feature on my website that converts the HTML table into downloadable content. While searching for a suitable plugin, I came across resources like http://www.dev-skills.com/export-html-table-to-csv-file/ which uses PHP s ...

Triggering Leaflet's Mouseout function during a MouseOver event

I am working on a leaflet map project where I am dynamically adding markers. One of my goals is to have the marker's popup appear not only when clicked, but also when the user hovers over it. This is the code snippet I have been using: function mak ...

I encountered an error message after rebooting my computer and attempting to run "npm start" for my React project. Can anyone assist me in resolving this issue?

Initializing the development server... events.js:292 throw er; // Unhandled 'error' event ^ Error: ENOSPC: System limit for number of file watchers reached, watch '/home/user_name/chat-app/public' at FSWatcher.<c ...

Is there a way to simulate Pinia and vue-i18n simultaneously?

Exploring Vue 3's Composition API with a twist: The store.ts file import { ref, Ref } from 'vue'; import { defineStore } from 'pinia'; export const useStore = defineStore('store', () => { const isLoading: Ref<bo ...

Is there any truth to the idea that storing in the session cache can prevent frequent executions of the backend code?

As I work on creating a website using the Zend Framework, I encounter a situation where I am utilizing AJAX to save store ratings in my database. One crucial piece of information required for this process is the key that identifies the specific store for w ...

Tips for placing an object within the boundaries of a canvas using three.js

At the moment, I am utilizing the threejs library to design 3D objects. However, I am facing an issue where the object is overflowing outside the canvas if it is too long. You can view my code snippet on JSFiddle. Script import * as THREE from 'https ...

Unable to retrieve parameters upon passing navigate

Having two navigators set up, one named Auth which is a StackNavigator containing the SignInScreen, and another called App which is a BottomTabNavigator with the HomeScreen. I am trying to navigate from the HomeScreen to the SignInScreen while passing some ...

Determine the maximum value in the array using the Math.max method with

How exactly does Math.max.apply(null, arr); work? Let's take for example var arr = [45,25,4,65] ; Will it compare 'null' and '45' and return the maximum number between the two, like 45? After comparing, will it then compare t ...