babel-loader 8.0.6 → 8.2.2

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,10 +1,8 @@
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
- [![Build Status](https://travis-ci.org/babel/babel-loader.svg?branch=master)](https://travis-ci.org/babel/babel-loader)
6
- [![Build Status](https://ci.appveyor.com/api/projects/status/77y5mk6amwqt0q88/branch/master?svg=true)](https://ci.appveyor.com/project/babel/babel-loader/branch/master)
7
- [![codecov](https://codecov.io/gh/babel/babel-loader/branch/master/graph/badge.svg)](https://codecov.io/gh/babel/babel-loader)
5
+ [![codecov](https://codecov.io/gh/babel/babel-loader/branch/main/graph/badge.svg)](https://codecov.io/gh/babel/babel-loader)
8
6
 
9
7
  <div align="center">
10
8
  <a href="https://github.com/babel/babel">
@@ -23,7 +21,7 @@ This package allows transpiling JavaScript files using [Babel](https://github.co
23
21
 
24
22
  <h2 align="center">Install</h2>
25
23
 
26
- > webpack 4.x | babel-loader 8.x | babel 7.x
24
+ > webpack `4.x || 5.x` | babel-loader 8.x | babel 7.x
27
25
 
28
26
  ```bash
29
27
  npm install -D babel-loader @babel/core @babel/preset-env webpack
@@ -40,11 +38,13 @@ module: {
40
38
  rules: [
41
39
  {
42
40
  test: /\.m?js$/,
43
- exclude: /(node_modules|bower_components)/,
41
+ exclude: /node_modules/,
44
42
  use: {
45
43
  loader: 'babel-loader',
46
44
  options: {
47
- presets: ['@babel/preset-env']
45
+ presets: [
46
+ ['@babel/preset-env', { targets: "defaults" }]
47
+ ]
48
48
  }
49
49
  }
50
50
  }
@@ -56,19 +56,21 @@ module: {
56
56
 
57
57
  See the `babel` [options](https://babeljs.io/docs/en/options).
58
58
 
59
- 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:
60
60
 
61
61
  ```javascript
62
62
  module: {
63
63
  rules: [
64
64
  {
65
65
  test: /\.m?js$/,
66
- exclude: /(node_modules|bower_components)/,
66
+ exclude: /node_modules/,
67
67
  use: {
68
68
  loader: 'babel-loader',
69
69
  options: {
70
- presets: ['@babel/preset-env'],
71
- plugins: ['@babel/plugin-proposal-object-rest-spread']
70
+ presets: [
71
+ ['@babel/preset-env', { targets: "defaults" }]
72
+ ],
73
+ plugins: ['@babel/plugin-proposal-class-properties']
72
74
  }
73
75
  }
74
76
  }
@@ -84,7 +86,7 @@ This loader also supports the following loader-specific option:
84
86
 
85
87
  * `cacheCompression`: Default `true`. When set, each Babel transform output will be compressed with Gzip. If you want to opt-out of cache compression, set it to `false` -- your project may benefit from this if it transpiles thousands of files.
86
88
 
87
- * `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 is you _must_ continue using `babel-loader` directly, but still want to customize.
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.
88
90
 
89
91
  ## Troubleshooting
90
92
 
@@ -96,6 +98,35 @@ To exclude `node_modules`, see the `exclude` option in the `loaders` config as d
96
98
 
97
99
  You can also speed up babel-loader by as much as 2x by using the `cacheDirectory` option. This will cache transformations to the filesystem.
98
100
 
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
+
99
130
  ### Babel is injecting helpers into each file and bloating my code!
100
131
 
101
132
  Babel uses very small helpers for common functions such as `_extend`. By default, this will be added to every file that requires it.
@@ -114,11 +145,13 @@ rules: [
114
145
  // require the runtime instead of inlining it.
115
146
  {
116
147
  test: /\.m?js$/,
117
- exclude: /(node_modules|bower_components)/,
148
+ exclude: /node_modules/,
118
149
  use: {
119
150
  loader: 'babel-loader',
120
151
  options: {
121
- presets: ['@babel/preset-env'],
152
+ presets: [
153
+ ['@babel/preset-env', { targets: "defaults" }]
154
+ ],
122
155
  plugins: ['@babel/plugin-transform-runtime']
123
156
  }
124
157
  }
@@ -128,7 +161,7 @@ rules: [
128
161
 
129
162
  #### **NOTE**: transform-runtime & custom polyfills (e.g. Promise library)
130
163
 
131
- Since [@babel/plugin-transform-runtime](https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-runtime) includes a polyfill that includes a custom [regenerator-runtime](https://github.com/facebook/regenerator/blob/master/packages/regenerator-runtime/runtime.js) and [core-js](https://github.com/zloirock/core-js), the following usual shimming method using `webpack.ProvidePlugin` will not work:
164
+ Since [@babel/plugin-transform-runtime](https://github.com/babel/babel/tree/main/packages/babel-plugin-transform-runtime) includes a polyfill that includes a custom [regenerator-runtime](https://github.com/facebook/regenerator/blob/master/packages/regenerator-runtime/runtime.js) and [core-js](https://github.com/zloirock/core-js), the following usual shimming method using `webpack.ProvidePlugin` will not work:
132
165
 
133
166
  ```javascript
134
167
  // ...
@@ -193,6 +226,59 @@ In the case one of your dependencies is installing `babel` and you cannot uninst
193
226
  }
194
227
  ```
195
228
 
229
+ ### Exclude libraries that should not be transpiled
230
+
231
+ `core-js` and `webpack/buildin` will cause errors if they are transpiled by Babel.
232
+
233
+ You will need to exclude them form `babel-loader`.
234
+
235
+ ```js
236
+ {
237
+ "loader": "babel-loader",
238
+ "options": {
239
+ "exclude": [
240
+ // \\ for Windows, / for macOS and Linux
241
+ /node_modules[\\/]core-js/,
242
+ /node_modules[\\/]webpack[\\/]buildin/,
243
+ ],
244
+ "presets": [
245
+ "@babel/preset-env"
246
+ ]
247
+ }
248
+ }
249
+ ```
250
+
251
+ ## Customize config based on webpack target
252
+
253
+ 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.
254
+
255
+ For example, to change the environment targets passed to `@babel/preset-env` based on the webpack target:
256
+
257
+ ```javascript
258
+ // babel.config.js
259
+
260
+ module.exports = api => {
261
+ return {
262
+ plugins: [
263
+ "@babel/plugin-proposal-nullish-coalescing-operator",
264
+ "@babel/plugin-proposal-optional-chaining"
265
+ ],
266
+ presets: [
267
+ [
268
+ "@babel/preset-env",
269
+ {
270
+ useBuiltIns: "entry",
271
+ // caller.target will be the same as the target option from webpack
272
+ targets: api.caller(caller => caller && caller.target === "node")
273
+ ? { node: "current" }
274
+ : { chrome: "58", ie: "11" }
275
+ }
276
+ ]
277
+ ]
278
+ }
279
+ }
280
+ ```
281
+
196
282
  ## Customized Loader
197
283
 
198
284
  `babel-loader` exposes a loader-builder utility that allows users to add custom handling
package/lib/cache.js CHANGED
@@ -23,11 +23,11 @@ const zlib = require("zlib");
23
23
 
24
24
  const crypto = require("crypto");
25
25
 
26
- const mkdirpOrig = require("mkdirp");
27
-
28
26
  const findCacheDir = require("find-cache-dir");
29
27
 
30
- const promisify = require("pify");
28
+ const {
29
+ promisify
30
+ } = require("util");
31
31
 
32
32
  const transform = require("./transform"); // Lazily instantiated when needed
33
33
 
@@ -37,17 +37,18 @@ const readFile = promisify(fs.readFile);
37
37
  const writeFile = promisify(fs.writeFile);
38
38
  const gunzip = promisify(zlib.gunzip);
39
39
  const gzip = promisify(zlib.gzip);
40
- const mkdirp = promisify(mkdirpOrig);
40
+
41
+ const makeDir = require("make-dir");
41
42
  /**
42
43
  * Read the contents from the compressed file.
43
44
  *
44
45
  * @async
45
46
  * @params {String} filename
47
+ * @params {Boolean} compress
46
48
  */
47
49
 
48
- const read =
49
- /*#__PURE__*/
50
- function () {
50
+
51
+ const read = /*#__PURE__*/function () {
51
52
  var _ref = _asyncToGenerator(function* (filename, compress) {
52
53
  const data = yield readFile(filename + (compress ? ".gz" : ""));
53
54
  const content = compress ? yield gunzip(data) : data;
@@ -63,13 +64,12 @@ function () {
63
64
  *
64
65
  * @async
65
66
  * @params {String} filename
67
+ * @params {Boolean} compress
66
68
  * @params {String} result
67
69
  */
68
70
 
69
71
 
70
- const write =
71
- /*#__PURE__*/
72
- function () {
72
+ const write = /*#__PURE__*/function () {
73
73
  var _ref2 = _asyncToGenerator(function* (filename, compress, result) {
74
74
  const content = JSON.stringify(result);
75
75
  const data = compress ? yield gzip(content) : content;
@@ -108,9 +108,7 @@ const filename = function (source, identifier, options) {
108
108
  */
109
109
 
110
110
 
111
- const handleCache =
112
- /*#__PURE__*/
113
- function () {
111
+ const handleCache = /*#__PURE__*/function () {
114
112
  var _ref3 = _asyncToGenerator(function* (directory, params) {
115
113
  const {
116
114
  source,
@@ -130,7 +128,7 @@ function () {
130
128
  const fallback = typeof cacheDirectory !== "string" && directory !== os.tmpdir(); // Make sure the directory exists.
131
129
 
132
130
  try {
133
- yield mkdirp(directory);
131
+ yield makeDir(directory);
134
132
  } catch (err) {
135
133
  if (fallback) {
136
134
  return handleCache(os.tmpdir(), params);
@@ -166,38 +164,28 @@ function () {
166
164
  *
167
165
  * @async
168
166
  * @param {Object} params
169
- * @param {String} params.directory Directory to store cached files
170
- * @param {String} params.identifier Unique identifier to bust cache
167
+ * @param {String} params.cacheDirectory Directory to store cached files
168
+ * @param {String} params.cacheIdentifier Unique identifier to bust cache
169
+ * @param {Boolean} params.cacheCompression Whether compressing cached files
171
170
  * @param {String} params.source Original contents of the file to be cached
172
171
  * @param {Object} params.options Options to be given to the transform fn
173
- * @param {Function} params.transform Function that will transform the
174
- * original file and whose result will be
175
- * cached
176
172
  *
177
173
  * @example
178
174
  *
179
- * cache({
180
- * directory: '.tmp/cache',
181
- * identifier: 'babel-loader-cachefile',
175
+ * const result = await cache({
176
+ * cacheDirectory: '.tmp/cache',
177
+ * cacheIdentifier: 'babel-loader-cachefile',
182
178
  * cacheCompression: false,
183
179
  * source: *source code from file*,
184
180
  * options: {
185
181
  * experimental: true,
186
182
  * runtime: true
187
183
  * },
188
- * transform: function(source, options) {
189
- * var content = *do what you need with the source*
190
- * return content;
191
- * }
192
- * }, function(err, result) {
193
- *
194
184
  * });
195
185
  */
196
186
 
197
187
 
198
- module.exports =
199
- /*#__PURE__*/
200
- function () {
188
+ module.exports = /*#__PURE__*/function () {
201
189
  var _ref4 = _asyncToGenerator(function* (params) {
202
190
  let directory;
203
191
 
package/lib/index.js CHANGED
@@ -32,12 +32,16 @@ const transform = require("./transform");
32
32
 
33
33
  const injectCaller = require("./injectCaller");
34
34
 
35
+ const schema = require("./schema");
36
+
35
37
  const {
36
38
  isAbsolute
37
39
  } = require("path");
38
40
 
39
41
  const loaderUtils = require("loader-utils");
40
42
 
43
+ const validateOptions = require("schema-utils");
44
+
41
45
  function subscribe(subscriber, metadata, context) {
42
46
  if (context[subscriber]) {
43
47
  context[subscriber](metadata);
@@ -64,6 +68,9 @@ function _loader() {
64
68
  _loader = _asyncToGenerator(function* (source, inputSourceMap, overrides) {
65
69
  const filename = this.resourcePath;
66
70
  let loaderOptions = loaderUtils.getOptions(this) || {};
71
+ validateOptions(schema, loaderOptions, {
72
+ name: "Babel loader"
73
+ });
67
74
 
68
75
  if (loaderOptions.customize != null) {
69
76
  if (typeof loaderOptions.customize !== "string") {
@@ -139,9 +146,13 @@ function _loader() {
139
146
 
140
147
  if (!babel.loadPartialConfig) {
141
148
  throw new Error(`babel-loader ^8.0.0-beta.3 requires @babel/core@7.0.0-beta.41, but ` + `you appear to be using "${babel.version}". Either update your ` + `@babel/core version, or pin you babel-loader version to 8.0.0-beta.2`);
142
- }
149
+ } // babel.loadPartialConfigAsync is available in v7.8.0+
150
+
143
151
 
144
- const config = babel.loadPartialConfig(injectCaller(programmaticOptions));
152
+ const {
153
+ loadPartialConfigAsync = babel.loadPartialConfig
154
+ } = babel;
155
+ const config = yield loadPartialConfigAsync(injectCaller(programmaticOptions, this.target));
145
156
 
146
157
  if (config) {
147
158
  let options = config.options;
@@ -2,14 +2,21 @@
2
2
 
3
3
  const babel = require("@babel/core");
4
4
 
5
- module.exports = function injectCaller(opts) {
5
+ module.exports = function injectCaller(opts, target) {
6
6
  if (!supportsCallerOption()) return opts;
7
7
  return Object.assign({}, opts, {
8
8
  caller: Object.assign({
9
9
  name: "babel-loader",
10
+ // Provide plugins with insight into webpack target.
11
+ // https://github.com/babel/babel-loader/issues/787
12
+ target,
10
13
  // Webpack >= 2 supports ESM and dynamic import.
11
14
  supportsStaticESM: true,
12
- supportsDynamicImport: true
15
+ supportsDynamicImport: true,
16
+ // Webpack 5 supports TLA behind a flag. We enable it by default
17
+ // for Babel, and then webpack will throw an error if the experimental
18
+ // flag isn't enabled.
19
+ supportsTopLevelAwait: true
13
20
  }, opts.caller)
14
21
  });
15
22
  }; // TODO: We can remove this eventually, I'm just adding it so that people have
@@ -0,0 +1,28 @@
1
+ {
2
+ "type": "object",
3
+ "properties": {
4
+ "cacheDirectory": {
5
+ "oneOf": [
6
+ {
7
+ "type": "boolean"
8
+ },
9
+ {
10
+ "type": "string"
11
+ }
12
+ ],
13
+ "default": false
14
+ },
15
+ "cacheIdentifier": {
16
+ "type": "string"
17
+ },
18
+ "cacheCompression": {
19
+ "type": "boolean",
20
+ "default": true
21
+ },
22
+ "customize": {
23
+ "type": "string",
24
+ "default": null
25
+ }
26
+ },
27
+ "additionalProperties": true
28
+ }
package/lib/transform.js CHANGED
@@ -6,15 +6,15 @@ 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
 
13
15
  const transform = promisify(babel.transform);
14
16
 
15
- module.exports =
16
- /*#__PURE__*/
17
- function () {
17
+ module.exports = /*#__PURE__*/function () {
18
18
  var _ref = _asyncToGenerator(function* (source, options) {
19
19
  let result;
20
20
 
@@ -26,7 +26,7 @@ function () {
26
26
 
27
27
  if (!result) return null; // We don't return the full result here because some entries are not
28
28
  // really serializable. For a full list of properties see here:
29
- // https://github.com/babel/babel/blob/master/packages/babel-core/src/transformation/index.js
29
+ // https://github.com/babel/babel/blob/main/packages/babel-core/src/transformation/index.js
30
30
  // For discussion on this topic see here:
31
31
  // https://github.com/babel/babel-loader/pull/629
32
32
 
package/package.json CHANGED
@@ -1,51 +1,54 @@
1
1
  {
2
2
  "name": "babel-loader",
3
- "version": "8.0.6",
3
+ "version": "8.2.2",
4
4
  "description": "babel module loader for webpack",
5
5
  "files": [
6
6
  "lib"
7
7
  ],
8
8
  "main": "lib/index.js",
9
9
  "engines": {
10
- "node": ">= 6.9"
10
+ "node": ">= 8.9"
11
11
  },
12
12
  "dependencies": {
13
- "find-cache-dir": "^2.0.0",
14
- "loader-utils": "^1.0.2",
15
- "mkdirp": "^0.5.1",
16
- "pify": "^4.0.1"
13
+ "find-cache-dir": "^3.3.1",
14
+ "loader-utils": "^1.4.0",
15
+ "make-dir": "^3.1.0",
16
+ "schema-utils": "^2.6.5"
17
17
  },
18
18
  "peerDependencies": {
19
19
  "@babel/core": "^7.0.0",
20
20
  "webpack": ">=2"
21
21
  },
22
22
  "devDependencies": {
23
- "@babel/cli": "^7.2.0",
24
- "@babel/core": "^7.2.0",
25
- "@babel/preset-env": "^7.2.0",
26
- "ava": "^1.1.0",
23
+ "@ava/babel": "^1.0.1",
24
+ "@babel/cli": "^7.12.1",
25
+ "@babel/core": "^7.12.3",
26
+ "@babel/preset-env": "^7.12.1",
27
+ "ava": "^3.13.0",
27
28
  "babel-eslint": "^10.0.1",
28
- "babel-plugin-istanbul": "^5.1.0",
29
- "babel-plugin-react-intl": "^3.0.1",
30
- "cross-env": "^5.2.0",
31
- "eslint": "^5.9.0",
29
+ "babel-plugin-istanbul": "^6.0.0",
30
+ "babel-plugin-react-intl": "^8.2.15",
31
+ "cross-env": "^7.0.2",
32
+ "eslint": "^7.13.0",
32
33
  "eslint-config-babel": "^9.0.0",
33
- "eslint-config-prettier": "^4.1.0",
34
- "eslint-plugin-flowtype": "^3.2.0",
34
+ "eslint-config-prettier": "^6.3.0",
35
+ "eslint-plugin-flowtype": "^5.2.0",
35
36
  "eslint-plugin-prettier": "^3.0.0",
36
- "husky": "^1.2.0",
37
- "lint-staged": "^8.1.0",
38
- "nyc": "^14.1.1",
39
- "prettier": "^1.15.3",
40
- "react": "^16.0.0",
41
- "react-intl": "^2.1.2",
37
+ "husky": "^4.3.0",
38
+ "lint-staged": "^10.5.1",
39
+ "nyc": "^15.1.0",
40
+ "pnp-webpack-plugin": "^1.6.4",
41
+ "prettier": "^2.1.2",
42
+ "react": "^17.0.1",
43
+ "react-intl": "^5.9.4",
42
44
  "react-intl-webpack-plugin": "^0.3.0",
43
- "rimraf": "^2.4.3",
44
- "webpack": "^4.0.0"
45
+ "rimraf": "^3.0.0",
46
+ "semver": "7.3.2",
47
+ "webpack": "^5.4.0"
45
48
  },
46
49
  "scripts": {
47
50
  "clean": "rimraf lib/",
48
- "build": "babel src/ --out-dir lib/",
51
+ "build": "babel src/ --out-dir lib/ --copy-files",
49
52
  "format": "prettier --write --trailing-comma all 'src/**/*.js' 'test/**/*.test.js' 'test/helpers/*.js' && prettier --write --trailing-comma es5 'scripts/*.js'",
50
53
  "lint": "eslint src test",
51
54
  "precommit": "lint-staged",
@@ -90,9 +93,11 @@
90
93
  "!test/fixtures/**/*",
91
94
  "!test/helpers/**/*"
92
95
  ],
93
- "sources": [
94
- "src/**/*.js"
95
- ]
96
+ "babel": {
97
+ "compileAsTests": [
98
+ "test/helpers/**/*"
99
+ ]
100
+ }
96
101
  },
97
102
  "lint-staged": {
98
103
  "scripts/*.js": [
@@ -116,4 +121,4 @@
116
121
  "git add yarn.lock"
117
122
  ]
118
123
  }
119
- }
124
+ }