Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack 2: How to exclude all node_modules except for

I need to have babel run on /node_modules/identicons/ However I still want to exclude all other packages.

Reason is the identicons package is using template strings and breaks when I run

"webpack -p"

String in question (node_modules/identicons/index.js):

str += `<rect x="${x}" y="${y}" width="${xside}" height="${xside}" style="fill:${color}" />`

Webpack.config.babel

module: {
  rules: [
    {
      test: /\.jsx?$/,
      exclude: /node_modules/,
      //include: /node_modules/identicons/,
      use: ["babel-loader"]
    },

How would that pattern be written?

like image 478
Leon Gaban Avatar asked Jul 21 '17 20:07

Leon Gaban


People also ask

Does Webpack need node_modules?

During your web pack build process ,need the node modules folder , because when you import a file from the node_modules , web pack will try to fetch the file from the particular node_module folder recursively.

What is exclude in Webpack?

Actually, those 'include' and 'exclude' properties are telling the loaders whether to include/exclude the files described (such as the contents of node_modules ), not webpack itself. So the 'excluded' modules you import from node_modules will be bundled - but they won't be transformed by babel.

Do I need to include node_modules in production?

Answer for Do I need Node_modules in production React? No, You don't need to push your node_modules folder to production whether it is a static export or dynamic build. When you export a static build the source file is converted into HTML & js files. So there is no need for node modules on production env.


2 Answers

I think you can use regex, something like

exclude: [
  /node_modules\/(?!identicons).*/
]
like image 54
roxxypoxxy Avatar answered Sep 21 '22 12:09

roxxypoxxy


Another way:

exclude: [
  {
    test: [
      path.resolve(__dirname, './node_modules'),
    ],
    exclude: [
      path.resolve(__dirname, './node_modules/MODULE_TO_INCLUDE'),
      path.resolve(__dirname, './node_modules/ANOTHER_MODULE_TO_INCLUDE'),
    ]
  }
]

It worked for me.

like image 25
J.S.R - Silicornio Avatar answered Sep 23 '22 12:09

J.S.R - Silicornio