Utilize Babel transpiling specifically for necessary Node modules

My current setup in nextjs is configured to handle ES6 code for IE11.

 module.exports = {
  poweredByHeader: false,
  distDir: "ssr_build",
  webpack(config) {
    config.node = { fs: "empty", net: "empty", tls: "empty" }
    config.plugins = config.plugins || []
    config.module.rules.push({
      test: /\.js$/,
      include: /node_modules/,
      use: {
        loader: "babel-loader",
        options: {
          presets: [
            [
              "next/babel",
              {
                targets: { ie: 11 },
              },
            ],
          ],
        },
      },
    })
    return config
  },
}

I am looking for a way to transpile only the ES6 modules within node_modules, without wasting time transpiling everything. Can anyone provide guidance?

Update1.0:

After using https://github.com/obahareth/are-you-es5 to identify ES5 dependencies needing conversion, I attempted creating an exclusion regex without success. I will revisit this to check for any missed nested dependencies.

I also tested https://github.com/martpie/next-transpile-modules, but found that it required manual insertion of all node_modules, which appeared to be a cumbersome process. As a result, I abandoned this approach.

Answer №1

There are 2 pathways you can consider:

Choice 1 (recommended) - Delve into your dependencies. Many dependencies automatically transpile to ES5 during their build phase, which usually resolves any compatibility issues. You can inspect their dist folder or refer to documentation and reported issues for more insights.

If you come across dependencies that remain in ES6 form, you can opt to selectively transpile those components using the next.js plugin available at: https://github.com/martpie/next-transpile-modules

Choice 2 - If you're uncertain about which node_modules contain ES6 code and prefer not to spend time investigating every project dependency:

Set up @babel/preset-env with a configuration file as outlined below, allowing it to scan through all node-modules while only transpiling necessary parts of code based on your target environment.

{
  "targets": {
    "ie": "11"
  }
}

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

ng-options is not compatible with an object as a source

It seems like there should be a simple solution to this issue, but I'm struggling to get ng-options to work with my model object. Essentially, I want to populate my select dropdown with a list of countries. Each option value should be a country code a ...

The res.download() function is not functioning properly when called from within a function, yet it works perfectly when directly called through the API in a browser

I have a button that, when clicked, triggers the downloadFile function which contacts the backend to download a file. async downloadFile(name) { await this.$axios.$get(process.env.API_LINK + '/api/files/' + name) }, app.get('/api/files/ ...

Router Express, parsing the body, and submitting a POST request

I have been experimenting with express.Router to organize my routes and testing post requests using Postman. I noticed that when I make a post request to /test without using router body-parser, everything works fine and I can view the body content. However ...

Learn how to dynamically change metadata in Next.js when the context changes

Within my project, I have contextual data tied to the languages used on the website. Whenever a user switches the language on the website, the content adjusts accordingly. There is a feature called 'i18n' for handling languages, though I have not ...

Exploring Next.js Image Component for seamless integration of remote images

I am completely new to nextjs and I encountered an issue with the Image component. After researching, it seems that there are similar questions out there but none quite match my specific scenario. My problem lies in trying to load an image from a remote s ...

Which is more recommended to use in AJAX (XMLHttpRequest) - eventListener or readyStateChange method?

As I revisited a video from WWDC12 discussing advanced effects with HTML5, I couldn't help but notice that for the demo they utilized req.addEventListener("load",callback,true) instead of the usual onreadystatechange. This made me wonder: what differ ...

Guide on effectively sorting the second level ng-repeat data in a table

I have a collection of data objects that I want to present in a tabular format with filtering capabilities. The current filter, based on the 'name' model, successfully filters the nested object 'family'. However, it does not function as ...

Utilizing Angular 10 to Transform a JSON Data into a Custom String for HTML Rendering

I have received a JSON response from my backend built using Spring Boot. The "category" field in the response can either be 1 or 2, where 1 represents Notifications and 2 represents FAQs. { "pageNumber": 0, "size": 5, "totalPages&q ...

Tips for implementing vue.js composition api with vue-3-socket.io

Is there a way to access the socket instance within the setup function of a Vue.js component? I am utilizing vue-3-socket.io in my main.js import VueSocketIO from 'vue-3-socket.io' import SocketIO from 'socket.io-client' Vue.use(new ...

The event for closing the React Select MUI dropdown does not trigger when selecting multiple options

When multiple values are selected from the dropdown and clicked outside, the dropdown closes but the onClose event fails to trigger. I need to filter data based on the selections made in the dropdown. For example, looking at the image below, I want to dis ...

Sending a PHP string formatted as JSON to be processed by jQuery

When attempting to echo a string with a JSON structure, I am encountering issues with the jQuery ajax post not parsing it correctly. My question is whether this string will be recognized as JSON by the jQuery json parse function when echoed in a similar ma ...

Resetting a selected dropdown value in React Hook Form

Currently, I am in the process of updating the information on the location update form and I want to ensure that the existing values are displayed in the form fields. Below is the code snippet for the update component: import axios from 'axios' ...

The behavior of CSS position: sticky varies depending on whether the user is scrolling up or scrolling down

I am experiencing an issue in my Vue CLI app where a component with the position: sticky CSS property is being partially hidden under the top of the browser when scrolling down, but works correctly when scrolling up. This behavior is also observed on my Ga ...

What is the most effective way to compare two arrays of objects in JavaScript?

I'm working on a function that needs to return an array of elements based on certain filters. Here is the code for the function: filter_getCustomFilterItems(filterNameToSearch: string, appliedFilters: Array<any>) { let tempFilterArray = []; ...

Disable javascript when using an iPad

I'm working on a website with parallax scrolling that I don't want to function on iPads. Is there a way to prevent the parallax effect from happening when the site is accessed on an iPad? Any guidance on how to disable parallax scrolling based o ...

What is the best way to load the route calculation dynamically?

I am struggling with calculating the Google Maps route dynamically. The console shows an error stating that 'calcularRuta' is not defined: Uncaught ReferenceError: calcularRuta is not defined at HTMLInputElement.onclick (index.html:1) Despi ...

Add opening and closing HTML tags to enclose an already existing HTML structure

Is there a way to dynamically wrap the p tag inside a div with the class .description-wrapper using JavaScript or jQuery? This is the current html structure: <div class="coursePrerequisites"> <p> Lorem ipsum.. </p> </ ...

Methods for retrieving a file within a nodejs project from another file

Currently, my project has the following structure and I am facing an issue while trying to access db.js from CategoryController.js. The code snippet I have used is as follows: var db = require('./../routes/db'); Upon attempting to start the No ...

Changing column sorting type dynamically with a button click using jQuery and DataTables

In the column, I have data arranged as "firstValue secondValue". It is essential for this data to remain in a single column and not split into two separate columns. When sorting is applied, it currently sorts based on both firstValue and secondValue. How ...

Is there a way to switch the cursor to a pointer when hovering over a bar on a ChartJS bar graph?

Here's the chart I created: createChart = function (name, contributions, idArray) { globalIdArray = idArray; var ctx = document.getElementById("myChart"); var myChart = new Chart(ctx, { type: "bar", data: { lab ...