A
A
Anton M2020-05-26 12:57:28
JavaScript
Anton M, 2020-05-26 12:57:28

How to transpile library code from node_modules via babel?

I include the library via import. The production bundle gets its code containing es6 features.
Since the `exclude: /node_modules/` rule is usually written in webpack, the libraries connected from there are not run through the beybl. Although usually the creators of the libraries themselves run the code through the beyble and give it to production, there is such a version, sometimes it happens that their settings differ from those that are needed.

How can I add an exception so that code from node_modules is also processed through my babel settings?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton M, 2020-05-26
@Bluorenge

There are many answers to this question here
. I liked this solution the most:

function excludeNodeModulesExcept(modules) {
  var pathSep = path.sep
  if (pathSep == '\\')
    pathSep = '\\\\'
  var moduleRegExps = modules.map(function (modName) {
    return new RegExp('node_modules' + pathSep + modName)
  })

  return function (modulePath) {
    if (/node_modules/.test(modulePath)) {
      for (var i = 0; i < moduleRegExps.length; i++)
        if (moduleRegExps[i].test(modulePath)) return false
      return true
    }
    return false
  }
}

And then in the rules:
rules: [
      {
        test: /\.js$/,
        // Сюда передаём название нужного модуля.
        exclude: excludeNodeModulesExcept(['your_module']),
        use: {
          loader: `babel-loader`,
        },
      },
]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question