babel-loader 8.2.1 → 8.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  > 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
3
3
 
4
4
  [![NPM Status](https://img.shields.io/npm/v/babel-loader.svg?style=flat)](https://www.npmjs.com/package/babel-loader)
5
5
  [![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
21
21
 
22
22
  <h2 align="center">Install</h2>
23
23
 
24
- > webpack 4.x | babel-loader 8.x | babel 7.x
24
+ > webpack `4.x || 5.x` | babel-loader 8.x | babel 7.x
25
25
 
26
26
  ```bash
27
27
  npm install -D babel-loader @babel/core @babel/preset-env webpack
@@ -38,11 +38,13 @@ module: {
38
38
  rules: [
39
39
  {
40
40
  test: /\.m?js$/,
41
- exclude: /(node_modules|bower_components)/,
41
+ exclude: /node_modules/,
42
42
  use: {
43
43
  loader: 'babel-loader',
44
44
  options: {
45
- presets: ['@babel/preset-env']
45
+ presets: [
46
+ ['@babel/preset-env', { targets: "defaults" }]
47
+ ]
46
48
  }
47
49
  }
48
50
  }
@@ -54,19 +56,21 @@ module: {
54
56
 
55
57
  See the `babel` [options](https://babeljs.io/docs/en/options).
56
58
 
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:
58
60
 
59
61
  ```javascript
60
62
  module: {
61
63
  rules: [
62
64
  {
63
65
  test: /\.m?js$/,
64
- exclude: /(node_modules|bower_components)/,
66
+ exclude: /node_modules/,
65
67
  use: {
66
68
  loader: 'babel-loader',
67
69
  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']
70
74
  }
71
75
  }
72
76
  }
@@ -84,6 +88,8 @@ This loader also supports the following loader-specific option:
84
88
 
85
89
  * `customize`: Default `null`. The path of a module that exports a `custom` callback [like the one that you'd pass to `.custom()`](#customized-loader). Since you already have to make a new file to use this, it is recommended that you instead use `.custom` to create a wrapper loader. Only use this if you _must_ continue using `babel-loader` directly, but still want to customize.
86
90
 
91
+ * `metadataSubscribers`: Default `[]`. Takes an array of context function names. E.g. if you passed ['myMetadataPlugin'], you'd assign a subscriber function to `context.myMetadataPlugin` within your webpack plugin's hooks & that function will be called with `metadata`.
92
+
87
93
  ## Troubleshooting
88
94
 
89
95
  ### babel-loader is slow!
@@ -94,6 +100,35 @@ To exclude `node_modules`, see the `exclude` option in the `loaders` config as d
94
100
 
95
101
  You can also speed up babel-loader by as much as 2x by using the `cacheDirectory` option. This will cache transformations to the filesystem.
96
102
 
103
+ ### Some files in my node_modules are not transpiled for IE 11
104
+
105
+ Although we typically recommend not compiling `node_modules`, you may need to when using libraries that do not support IE 11.
106
+
107
+ 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).
108
+
109
+ ```javascript
110
+ {
111
+ test: /\.m?js$/,
112
+ exclude: {
113
+ and: [/node_modules/], // Exclude libraries in node_modules ...
114
+ not: [
115
+ // Except for a few of them that needs to be transpiled because they use modern syntax
116
+ /unfetch/,
117
+ /d3-array|d3-scale/,
118
+ /@hapi[\\/]joi-date/,
119
+ ]
120
+ },
121
+ use: {
122
+ loader: 'babel-loader',
123
+ options: {
124
+ presets: [
125
+ ['@babel/preset-env', { targets: "ie 11" }]
126
+ ]
127
+ }
128
+ }
129
+ }
130
+ ```
131
+
97
132
  ### Babel is injecting helpers into each file and bloating my code!
98
133
 
99
134
  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 +147,13 @@ rules: [
112
147
  // require the runtime instead of inlining it.
113
148
  {
114
149
  test: /\.m?js$/,
115
- exclude: /(node_modules|bower_components)/,
150
+ exclude: /node_modules/,
116
151
  use: {
117
152
  loader: 'babel-loader',
118
153
  options: {
119
- presets: ['@babel/preset-env'],
154
+ presets: [
155
+ ['@babel/preset-env', { targets: "defaults" }]
156
+ ],
120
157
  plugins: ['@babel/plugin-transform-runtime']
121
158
  }
122
159
  }
@@ -202,9 +239,9 @@ You will need to exclude them form `babel-loader`.
202
239
  "loader": "babel-loader",
203
240
  "options": {
204
241
  "exclude": [
205
- // \\ for Windows, \/ for Mac OS and Linux
206
- /node_modules[\\\/]core-js/,
207
- /node_modules[\\\/]webpack[\\\/]buildin/,
242
+ // \\ for Windows, / for macOS and Linux
243
+ /node_modules[\\/]core-js/,
244
+ /node_modules[\\/]webpack[\\/]buildin/,
208
245
  ],
209
246
  "presets": [
210
247
  "@babel/preset-env"
@@ -213,6 +250,26 @@ You will need to exclude them form `babel-loader`.
213
250
  }
214
251
  ```
215
252
 
253
+ ### Top level function (IIFE) is still arrow (on Webpack 5)
254
+
255
+ That function is injected by Webpack itself _after_ running `babel-loader`. By default Webpack asumes that your target environment supports some ES2015 features, but you can overwrite this behavior using the `output.environment` Webpack option ([documentation](https://webpack.js.org/configuration/output/#outputenvironment)).
256
+
257
+ To avoid the top-level arrow function, you can use `output.environment.arrowFunction`:
258
+
259
+ ```js
260
+ // webpack.config.js
261
+ module.exports = {
262
+ // ...
263
+ output: {
264
+ // ...
265
+ environment: {
266
+ // ...
267
+ arrowFunction: false, // <-- this line does the trick
268
+ },
269
+ },
270
+ };
271
+ ```
272
+
216
273
  ## Customize config based on webpack target
217
274
 
218
275
  Webpack supports bundling multiple [targets](https://webpack.js.org/concepts/targets/). For cases where you may want different Babel configurations for each target (like `web` _and_ `node`), this loader provides a `target` property via Babel's [caller](https://babeljs.io/docs/en/config-files#apicallercb) API.
package/lib/cache.js CHANGED
@@ -25,12 +25,22 @@ const crypto = require("crypto");
25
25
 
26
26
  const findCacheDir = require("find-cache-dir");
27
27
 
28
- const promisify = require("pify");
28
+ const {
29
+ promisify
30
+ } = require("util");
29
31
 
30
32
  const transform = require("./transform"); // Lazily instantiated when needed
31
33
 
32
34
 
33
35
  let defaultCacheDirectory = null;
36
+ let hashType = "md4"; // use md5 hashing if md4 is not available
37
+
38
+ try {
39
+ crypto.createHash(hashType);
40
+ } catch (err) {
41
+ hashType = "md5";
42
+ }
43
+
34
44
  const readFile = promisify(fs.readFile);
35
45
  const writeFile = promisify(fs.writeFile);
36
46
  const gunzip = promisify(zlib.gunzip);
@@ -89,7 +99,7 @@ const write = /*#__PURE__*/function () {
89
99
 
90
100
 
91
101
  const filename = function (source, identifier, options) {
92
- const hash = crypto.createHash("md4");
102
+ const hash = crypto.createHash(hashType);
93
103
  const contents = JSON.stringify({
94
104
  source,
95
105
  options,
package/lib/index.js CHANGED
@@ -67,7 +67,7 @@ function loader(_x, _x2, _x3) {
67
67
  function _loader() {
68
68
  _loader = _asyncToGenerator(function* (source, inputSourceMap, overrides) {
69
69
  const filename = this.resourcePath;
70
- let loaderOptions = loaderUtils.getOptions(this) || {};
70
+ let loaderOptions = loaderUtils.getOptions(this);
71
71
  validateOptions(schema, loaderOptions, {
72
72
  name: "Babel loader"
73
73
  });
@@ -198,12 +198,22 @@ function _loader() {
198
198
  });
199
199
  } else {
200
200
  result = yield transform(source, options);
201
- } // TODO: Babel should really provide the full list of config files that
202
- // were used so that this can also handle files loaded with 'extends'.
201
+ } // Availabe since Babel 7.12
202
+ // https://github.com/babel/babel/pull/11907
203
203
 
204
204
 
205
- if (typeof config.babelrc === "string") {
206
- this.addDependency(config.babelrc);
205
+ if (config.files) {
206
+ config.files.forEach(configFile => this.addDependency(configFile));
207
+ } else {
208
+ // .babelrc.json
209
+ if (typeof config.babelrc === "string") {
210
+ this.addDependency(config.babelrc);
211
+ } // babel.config.js
212
+
213
+
214
+ if (config.config) {
215
+ this.addDependency(config.config);
216
+ }
207
217
  }
208
218
 
209
219
  if (result) {
package/lib/transform.js CHANGED
@@ -6,7 +6,9 @@ function _asyncToGenerator(fn) { return function () { var self = this, args = ar
6
6
 
7
7
  const babel = require("@babel/core");
8
8
 
9
- const promisify = require("pify");
9
+ const {
10
+ promisify
11
+ } = require("util");
10
12
 
11
13
  const LoaderError = require("./Error");
12
14
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "babel-loader",
3
- "version": "8.2.1",
3
+ "version": "8.2.4",
4
4
  "description": "babel module loader for webpack",
5
5
  "files": [
6
6
  "lib"
@@ -10,10 +10,9 @@
10
10
  "node": ">= 8.9"
11
11
  },
12
12
  "dependencies": {
13
- "find-cache-dir": "^2.1.0",
14
- "loader-utils": "^1.4.0",
15
- "make-dir": "^2.1.0",
16
- "pify": "^4.0.1",
13
+ "find-cache-dir": "^3.3.1",
14
+ "loader-utils": "^2.0.0",
15
+ "make-dir": "^3.1.0",
17
16
  "schema-utils": "^2.6.5"
18
17
  },
19
18
  "peerDependencies": {
@@ -45,7 +44,7 @@
45
44
  "react-intl-webpack-plugin": "^0.3.0",
46
45
  "rimraf": "^3.0.0",
47
46
  "semver": "7.3.2",
48
- "webpack": "^5.4.0"
47
+ "webpack": "^5.34.0"
49
48
  },
50
49
  "scripts": {
51
50
  "clean": "rimraf lib/",