Skip to content

Commit bd2fb05

Browse files
JLHwungexistentialismnicolo-ribaudo
authored
Add trouble shooting about how to transpile node_modules (#883)
* Add trouble shooting about how to transpile node_modules * Apply suggestions from code review Co-authored-by: Brian Ng <[email protected]> * Update README.md Co-authored-by: Nicolò Ribaudo <[email protected]> * Apply suggestions from code review Co-authored-by: Brian Ng <[email protected]> Co-authored-by: Brian Ng <[email protected]> Co-authored-by: Nicolò Ribaudo <[email protected]>
1 parent 5deb8a6 commit bd2fb05

File tree

1 file changed

+48
-13
lines changed

1 file changed

+48
-13
lines changed

README.md

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
> This README is for babel-loader v8 + Babel v7
2-
> Check the [7.x branch](https://github.com/babel/babel-loader/tree/7.x) for docs with Babel v6
2+
> If you are using legacy Babel v6, see the [7.x branch](https://github.com/babel/babel-loader/tree/7.x) docs
33
44
[![NPM Status](https://img.shields.io/npm/v/babel-loader.svg?style=flat)](https://www.npmjs.com/package/babel-loader)
55
[![codecov](https://codecov.io/gh/babel/babel-loader/branch/main/graph/badge.svg)](https://codecov.io/gh/babel/babel-loader)
@@ -21,7 +21,7 @@ This package allows transpiling JavaScript files using [Babel](https://github.co
2121

2222
<h2 align="center">Install</h2>
2323

24-
> webpack 4.x | babel-loader 8.x | babel 7.x
24+
> webpack `4.x || 5.x` | babel-loader 8.x | babel 7.x
2525
2626
```bash
2727
npm install -D babel-loader @babel/core @babel/preset-env webpack
@@ -38,11 +38,13 @@ module: {
3838
rules: [
3939
{
4040
test: /\.m?js$/,
41-
exclude: /(node_modules|bower_components)/,
41+
exclude: /node_modules/,
4242
use: {
4343
loader: 'babel-loader',
4444
options: {
45-
presets: ['@babel/preset-env']
45+
presets: [
46+
['@babel/preset-env', { targets: "defaults" }]
47+
]
4648
}
4749
}
4850
}
@@ -54,19 +56,21 @@ module: {
5456

5557
See the `babel` [options](https://babeljs.io/docs/en/options).
5658

57-
You can pass options to the loader by using the [`options`](https://webpack.js.org/configuration/module/#rule-options-rule-query) property:
59+
You can pass options to the loader by using the [`options`](https://webpack.js.org/configuration/module/#ruleoptions--rulequery) property:
5860

5961
```javascript
6062
module: {
6163
rules: [
6264
{
6365
test: /\.m?js$/,
64-
exclude: /(node_modules|bower_components)/,
66+
exclude: /node_modules/,
6567
use: {
6668
loader: 'babel-loader',
6769
options: {
68-
presets: ['@babel/preset-env'],
69-
plugins: ['@babel/plugin-proposal-object-rest-spread']
70+
presets: [
71+
['@babel/preset-env', { targets: "defaults" }]
72+
],
73+
plugins: ['@babel/plugin-proposal-class-properties']
7074
}
7175
}
7276
}
@@ -94,6 +98,35 @@ To exclude `node_modules`, see the `exclude` option in the `loaders` config as d
9498

9599
You can also speed up babel-loader by as much as 2x by using the `cacheDirectory` option. This will cache transformations to the filesystem.
96100

101+
### Some files in my node_modules are not transpiled for IE 11
102+
103+
Although we typically recommend not compiling `node_modules`, you may need to when using libraries that do not support IE 11.
104+
105+
For this, you can either use a combination of `test` and `not`, or [pass a function](https://webpack.js.org/configuration/module/#condition) to your `exclude` option. You can also use negative lookahead regex as suggested [here](https://github.com/webpack/webpack/issues/2031#issuecomment-294706065).
106+
107+
```javascript
108+
{
109+
test: /\.m?js$/,
110+
exclude: {
111+
test: /node_modules/, // Exclude libraries in node_modules ...
112+
not: [
113+
// Except for a few of them that needs to be transpiled because they use modern syntax
114+
/unfetch/,
115+
/d3-array|d3-scale/,
116+
/@hapi[\\/]joi-date/,
117+
]
118+
},
119+
use: {
120+
loader: 'babel-loader',
121+
options: {
122+
presets: [
123+
['@babel/preset-env', { targets: "ie 11" }]
124+
]
125+
}
126+
}
127+
}
128+
```
129+
97130
### Babel is injecting helpers into each file and bloating my code!
98131

99132
Babel uses very small helpers for common functions such as `_extend`. By default, this will be added to every file that requires it.
@@ -112,11 +145,13 @@ rules: [
112145
// require the runtime instead of inlining it.
113146
{
114147
test: /\.m?js$/,
115-
exclude: /(node_modules|bower_components)/,
148+
exclude: /node_modules/,
116149
use: {
117150
loader: 'babel-loader',
118151
options: {
119-
presets: ['@babel/preset-env'],
152+
presets: [
153+
['@babel/preset-env', { targets: "defaults" }]
154+
],
120155
plugins: ['@babel/plugin-transform-runtime']
121156
}
122157
}
@@ -202,9 +237,9 @@ You will need to exclude them form `babel-loader`.
202237
"loader": "babel-loader",
203238
"options": {
204239
"exclude": [
205-
// \\ for Windows, \/ for Mac OS and Linux
206-
/node_modules[\\\/]core-js/,
207-
/node_modules[\\\/]webpack[\\\/]buildin/,
240+
// \\ for Windows, / for macOS and Linux
241+
/node_modules[\\/]core-js/,
242+
/node_modules[\\/]webpack[\\/]buildin/,
208243
],
209244
"presets": [
210245
"@babel/preset-env"

0 commit comments

Comments
 (0)