babel-loader 8.0.5 → 8.2.1

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/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2014-2016 Luís Couto <hello@luiscouto.pt>
1
+ Copyright (c) 2014-2019 Luís Couto <hello@luiscouto.pt>
2
2
 
3
3
  MIT License
4
4
 
package/README.md CHANGED
@@ -2,9 +2,7 @@
2
2
  > Check the [7.x branch](https://github.com/babel/babel-loader/tree/7.x) for docs with Babel v6
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">
@@ -29,12 +27,6 @@ This package allows transpiling JavaScript files using [Babel](https://github.co
29
27
  npm install -D babel-loader @babel/core @babel/preset-env webpack
30
28
  ```
31
29
 
32
- > webpack 4.x | babel-loader 7.x | babel 6.x
33
-
34
- ```bash
35
- npm install -D babel-loader@7 babel-core babel-preset-env webpack
36
- ```
37
-
38
30
  <h2 align="center">Usage</h2>
39
31
 
40
32
  webpack documentation: [Loaders](https://webpack.js.org/loaders/)
@@ -84,13 +76,13 @@ module: {
84
76
 
85
77
  This loader also supports the following loader-specific option:
86
78
 
87
- * `cacheDirectory`: Default `false`. When set, the given directory will be used to cache the results of the loader. Future webpack builds will attempt to read from the cache to avoid needing to run the potentially expensive Babel recompilation process on each run. If the value is blank (`loader: 'babel-loader?cacheDirectory'`) or `true` (`loader: 'babel-loader?cacheDirectory=true'`), the loader will use the default cache directory in `node_modules/.cache/babel-loader` or fallback to the default OS temporary file directory if no `node_modules` folder could be found in any root directory.
79
+ * `cacheDirectory`: Default `false`. When set, the given directory will be used to cache the results of the loader. Future webpack builds will attempt to read from the cache to avoid needing to run the potentially expensive Babel recompilation process on each run. If the value is set to `true` in options (`{cacheDirectory: true}`), the loader will use the default cache directory in `node_modules/.cache/babel-loader` or fallback to the default OS temporary file directory if no `node_modules` folder could be found in any root directory.
88
80
 
89
- * `cacheIdentifier`: Default is a string composed by the `babel-core`'s version, the `babel-loader`'s version, the contents of `.babelrc` file if it exists, and the value of the environment variable `BABEL_ENV` with a fallback to the `NODE_ENV` environment variable. This can be set to a custom value to force cache busting if the identifier changes.
81
+ * `cacheIdentifier`: Default is a string composed by the `@babel/core`'s version, the `babel-loader`'s version, the contents of `.babelrc` file if it exists, and the value of the environment variable `BABEL_ENV` with a fallback to the `NODE_ENV` environment variable. This can be set to a custom value to force cache busting if the identifier changes.
90
82
 
91
83
  * `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.
92
84
 
93
- * `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.
85
+ * `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.
94
86
 
95
87
  ## Troubleshooting
96
88
 
@@ -108,11 +100,11 @@ Babel uses very small helpers for common functions such as `_extend`. By default
108
100
 
109
101
  You can instead require the Babel runtime as a separate module to avoid the duplication.
110
102
 
111
- The following configuration disables automatic per-file runtime injection in Babel, requiring `babel-plugin-transform-runtime` instead and making all helper references use it.
103
+ The following configuration disables automatic per-file runtime injection in Babel, requiring `@babel/plugin-transform-runtime` instead and making all helper references use it.
112
104
 
113
105
  See the [docs](https://babeljs.io/docs/plugins/transform-runtime/) for more information.
114
106
 
115
- **NOTE**: You must run `npm install -D @babel/plugin-transform-runtime` to include this in your project and `babel-runtime` itself as a dependency with `npm install @babel/runtime`.
107
+ **NOTE**: You must run `npm install -D @babel/plugin-transform-runtime` to include this in your project and `@babel/runtime` itself as a dependency with `npm install @babel/runtime`.
116
108
 
117
109
  ```javascript
118
110
  rules: [
@@ -134,7 +126,7 @@ rules: [
134
126
 
135
127
  #### **NOTE**: transform-runtime & custom polyfills (e.g. Promise library)
136
128
 
137
- 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:
129
+ 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:
138
130
 
139
131
  ```javascript
140
132
  // ...
@@ -190,7 +182,7 @@ If you receive this message, it means that you have the npm package `babel` inst
190
182
 
191
183
  webpack then tries to load the `babel` package instead of the `babel-loader`.
192
184
 
193
- To fix this, you should uninstall the npm package `babel`, as it is deprecated in Babel v6. (Instead, install `babel-cli` or `babel-core`.)
185
+ To fix this, you should uninstall the npm package `babel`, as it is deprecated in Babel v6. (Instead, install `@babel/cli` or `@babel/core`.)
194
186
  In the case one of your dependencies is installing `babel` and you cannot uninstall it yourself, use the complete name of the loader in the webpack config:
195
187
  ```javascript
196
188
  {
@@ -199,6 +191,59 @@ In the case one of your dependencies is installing `babel` and you cannot uninst
199
191
  }
200
192
  ```
201
193
 
194
+ ### Exclude libraries that should not be transpiled
195
+
196
+ `core-js` and `webpack/buildin` will cause errors if they are transpiled by Babel.
197
+
198
+ You will need to exclude them form `babel-loader`.
199
+
200
+ ```js
201
+ {
202
+ "loader": "babel-loader",
203
+ "options": {
204
+ "exclude": [
205
+ // \\ for Windows, \/ for Mac OS and Linux
206
+ /node_modules[\\\/]core-js/,
207
+ /node_modules[\\\/]webpack[\\\/]buildin/,
208
+ ],
209
+ "presets": [
210
+ "@babel/preset-env"
211
+ ]
212
+ }
213
+ }
214
+ ```
215
+
216
+ ## Customize config based on webpack target
217
+
218
+ 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.
219
+
220
+ For example, to change the environment targets passed to `@babel/preset-env` based on the webpack target:
221
+
222
+ ```javascript
223
+ // babel.config.js
224
+
225
+ module.exports = api => {
226
+ return {
227
+ plugins: [
228
+ "@babel/plugin-proposal-nullish-coalescing-operator",
229
+ "@babel/plugin-proposal-optional-chaining"
230
+ ],
231
+ presets: [
232
+ [
233
+ "@babel/preset-env",
234
+ {
235
+ useBuiltIns: "entry",
236
+ // caller.target will be the same as the target option from webpack
237
+ targets: api.caller(caller => caller && caller.target === "node")
238
+ ? { node: "current" }
239
+ : { chrome: "58", ie: "11" }
240
+ }
241
+ ]
242
+ ]
243
+ }
244
+ }
245
+ ```
246
+
202
247
  ## Customized Loader
203
248
 
204
249
  `babel-loader` exposes a loader-builder utility that allows users to add custom handling
package/lib/cache.js CHANGED
@@ -23,11 +23,9 @@ 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("util.promisify");
28
+ const promisify = require("pify");
31
29
 
32
30
  const transform = require("./transform"); // Lazily instantiated when needed
33
31
 
@@ -37,17 +35,18 @@ const readFile = promisify(fs.readFile);
37
35
  const writeFile = promisify(fs.writeFile);
38
36
  const gunzip = promisify(zlib.gunzip);
39
37
  const gzip = promisify(zlib.gzip);
40
- const mkdirp = promisify(mkdirpOrig);
38
+
39
+ const makeDir = require("make-dir");
41
40
  /**
42
41
  * Read the contents from the compressed file.
43
42
  *
44
43
  * @async
45
44
  * @params {String} filename
45
+ * @params {Boolean} compress
46
46
  */
47
47
 
48
- const read =
49
- /*#__PURE__*/
50
- function () {
48
+
49
+ const read = /*#__PURE__*/function () {
51
50
  var _ref = _asyncToGenerator(function* (filename, compress) {
52
51
  const data = yield readFile(filename + (compress ? ".gz" : ""));
53
52
  const content = compress ? yield gunzip(data) : data;
@@ -63,13 +62,12 @@ function () {
63
62
  *
64
63
  * @async
65
64
  * @params {String} filename
65
+ * @params {Boolean} compress
66
66
  * @params {String} result
67
67
  */
68
68
 
69
69
 
70
- const write =
71
- /*#__PURE__*/
72
- function () {
70
+ const write = /*#__PURE__*/function () {
73
71
  var _ref2 = _asyncToGenerator(function* (filename, compress, result) {
74
72
  const content = JSON.stringify(result);
75
73
  const data = compress ? yield gzip(content) : content;
@@ -108,9 +106,7 @@ const filename = function (source, identifier, options) {
108
106
  */
109
107
 
110
108
 
111
- const handleCache =
112
- /*#__PURE__*/
113
- function () {
109
+ const handleCache = /*#__PURE__*/function () {
114
110
  var _ref3 = _asyncToGenerator(function* (directory, params) {
115
111
  const {
116
112
  source,
@@ -130,7 +126,7 @@ function () {
130
126
  const fallback = typeof cacheDirectory !== "string" && directory !== os.tmpdir(); // Make sure the directory exists.
131
127
 
132
128
  try {
133
- yield mkdirp(directory);
129
+ yield makeDir(directory);
134
130
  } catch (err) {
135
131
  if (fallback) {
136
132
  return handleCache(os.tmpdir(), params);
@@ -166,38 +162,28 @@ function () {
166
162
  *
167
163
  * @async
168
164
  * @param {Object} params
169
- * @param {String} params.directory Directory to store cached files
170
- * @param {String} params.identifier Unique identifier to bust cache
165
+ * @param {String} params.cacheDirectory Directory to store cached files
166
+ * @param {String} params.cacheIdentifier Unique identifier to bust cache
167
+ * @param {Boolean} params.cacheCompression Whether compressing cached files
171
168
  * @param {String} params.source Original contents of the file to be cached
172
169
  * @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
170
  *
177
171
  * @example
178
172
  *
179
- * cache({
180
- * directory: '.tmp/cache',
181
- * identifier: 'babel-loader-cachefile',
173
+ * const result = await cache({
174
+ * cacheDirectory: '.tmp/cache',
175
+ * cacheIdentifier: 'babel-loader-cachefile',
182
176
  * cacheCompression: false,
183
177
  * source: *source code from file*,
184
178
  * options: {
185
179
  * experimental: true,
186
180
  * runtime: true
187
181
  * },
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
182
  * });
195
183
  */
196
184
 
197
185
 
198
- module.exports =
199
- /*#__PURE__*/
200
- function () {
186
+ module.exports = /*#__PURE__*/function () {
201
187
  var _ref4 = _asyncToGenerator(function* (params) {
202
188
  let directory;
203
189
 
package/lib/index.js CHANGED
@@ -22,7 +22,9 @@ if (/^6\./.test(babel.version)) {
22
22
  throw new Error("\n babel-loader@8 will not work with the '@babel/core@6' bridge package. " + "If you want to use Babel 6.x, install 'babel-loader@7'.");
23
23
  }
24
24
 
25
- const pkg = require("../package.json");
25
+ const {
26
+ version
27
+ } = require("../package.json");
26
28
 
27
29
  const cache = require("./cache");
28
30
 
@@ -30,10 +32,16 @@ const transform = require("./transform");
30
32
 
31
33
  const injectCaller = require("./injectCaller");
32
34
 
33
- const path = require("path");
35
+ const schema = require("./schema");
36
+
37
+ const {
38
+ isAbsolute
39
+ } = require("path");
34
40
 
35
41
  const loaderUtils = require("loader-utils");
36
42
 
43
+ const validateOptions = require("schema-utils");
44
+
37
45
  function subscribe(subscriber, metadata, context) {
38
46
  if (context[subscriber]) {
39
47
  context[subscriber](metadata);
@@ -60,13 +68,16 @@ function _loader() {
60
68
  _loader = _asyncToGenerator(function* (source, inputSourceMap, overrides) {
61
69
  const filename = this.resourcePath;
62
70
  let loaderOptions = loaderUtils.getOptions(this) || {};
71
+ validateOptions(schema, loaderOptions, {
72
+ name: "Babel loader"
73
+ });
63
74
 
64
75
  if (loaderOptions.customize != null) {
65
76
  if (typeof loaderOptions.customize !== "string") {
66
77
  throw new Error("Customized loaders must be implemented as standalone modules.");
67
78
  }
68
79
 
69
- if (!path.isAbsolute(loaderOptions.customize)) {
80
+ if (!isAbsolute(loaderOptions.customize)) {
70
81
  throw new Error("Customized loaders must be passed as absolute paths, since " + "babel-loader has no way to know what they would be relative to.");
71
82
  }
72
83
 
@@ -135,9 +146,13 @@ function _loader() {
135
146
 
136
147
  if (!babel.loadPartialConfig) {
137
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`);
138
- }
149
+ } // babel.loadPartialConfigAsync is available in v7.8.0+
150
+
139
151
 
140
- const config = babel.loadPartialConfig(injectCaller(programmaticOptions));
152
+ const {
153
+ loadPartialConfigAsync = babel.loadPartialConfig
154
+ } = babel;
155
+ const config = yield loadPartialConfigAsync(injectCaller(programmaticOptions, this.target));
141
156
 
142
157
  if (config) {
143
158
  let options = config.options;
@@ -165,7 +180,7 @@ function _loader() {
165
180
  cacheIdentifier = JSON.stringify({
166
181
  options,
167
182
  "@babel/core": transform.version,
168
- "@babel/loader": pkg.version
183
+ "@babel/loader": version
169
184
  }),
170
185
  cacheCompression = true,
171
186
  metadataSubscribers = []
@@ -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,13 @@ 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("util.promisify");
9
+ const promisify = require("pify");
10
10
 
11
11
  const LoaderError = require("./Error");
12
12
 
13
13
  const transform = promisify(babel.transform);
14
14
 
15
- module.exports =
16
- /*#__PURE__*/
17
- function () {
15
+ module.exports = /*#__PURE__*/function () {
18
16
  var _ref = _asyncToGenerator(function* (source, options) {
19
17
  let result;
20
18
 
@@ -26,7 +24,7 @@ function () {
26
24
 
27
25
  if (!result) return null; // We don't return the full result here because some entries are not
28
26
  // 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
27
+ // https://github.com/babel/babel/blob/main/packages/babel-core/src/transformation/index.js
30
28
  // For discussion on this topic see here:
31
29
  // https://github.com/babel/babel-loader/pull/629
32
30
 
package/package.json CHANGED
@@ -1,51 +1,55 @@
1
1
  {
2
2
  "name": "babel-loader",
3
- "version": "8.0.5",
3
+ "version": "8.2.1",
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
- "util.promisify": "^1.0.0"
13
+ "find-cache-dir": "^2.1.0",
14
+ "loader-utils": "^1.4.0",
15
+ "make-dir": "^2.1.0",
16
+ "pify": "^4.0.1",
17
+ "schema-utils": "^2.6.5"
17
18
  },
18
19
  "peerDependencies": {
19
20
  "@babel/core": "^7.0.0",
20
21
  "webpack": ">=2"
21
22
  },
22
23
  "devDependencies": {
23
- "@babel/cli": "^7.2.0",
24
- "@babel/core": "^7.2.0",
25
- "@babel/preset-env": "^7.2.0",
26
- "ava": "1.0.1",
24
+ "@ava/babel": "^1.0.1",
25
+ "@babel/cli": "^7.12.1",
26
+ "@babel/core": "^7.12.3",
27
+ "@babel/preset-env": "^7.12.1",
28
+ "ava": "^3.13.0",
27
29
  "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",
32
- "eslint-config-babel": "^8.0.2",
33
- "eslint-config-prettier": "^3.3.0",
34
- "eslint-plugin-flowtype": "^3.2.0",
30
+ "babel-plugin-istanbul": "^6.0.0",
31
+ "babel-plugin-react-intl": "^8.2.15",
32
+ "cross-env": "^7.0.2",
33
+ "eslint": "^7.13.0",
34
+ "eslint-config-babel": "^9.0.0",
35
+ "eslint-config-prettier": "^6.3.0",
36
+ "eslint-plugin-flowtype": "^5.2.0",
35
37
  "eslint-plugin-prettier": "^3.0.0",
36
- "husky": "^1.2.0",
37
- "lint-staged": "^8.1.0",
38
- "nyc": "^13.1.0",
39
- "prettier": "^1.15.3",
40
- "react": "^16.0.0",
41
- "react-intl": "^2.1.2",
38
+ "husky": "^4.3.0",
39
+ "lint-staged": "^10.5.1",
40
+ "nyc": "^15.1.0",
41
+ "pnp-webpack-plugin": "^1.6.4",
42
+ "prettier": "^2.1.2",
43
+ "react": "^17.0.1",
44
+ "react-intl": "^5.9.4",
42
45
  "react-intl-webpack-plugin": "^0.3.0",
43
- "rimraf": "^2.4.3",
44
- "webpack": "^4.0.0"
46
+ "rimraf": "^3.0.0",
47
+ "semver": "7.3.2",
48
+ "webpack": "^5.4.0"
45
49
  },
46
50
  "scripts": {
47
51
  "clean": "rimraf lib/",
48
- "build": "babel src/ --out-dir lib/",
52
+ "build": "babel src/ --out-dir lib/ --copy-files",
49
53
  "format": "prettier --write --trailing-comma all 'src/**/*.js' 'test/**/*.test.js' 'test/helpers/*.js' && prettier --write --trailing-comma es5 'scripts/*.js'",
50
54
  "lint": "eslint src test",
51
55
  "precommit": "lint-staged",
@@ -90,9 +94,11 @@
90
94
  "!test/fixtures/**/*",
91
95
  "!test/helpers/**/*"
92
96
  ],
93
- "sources": [
94
- "src/**/*.js"
95
- ]
97
+ "babel": {
98
+ "compileAsTests": [
99
+ "test/helpers/**/*"
100
+ ]
101
+ }
96
102
  },
97
103
  "lint-staged": {
98
104
  "scripts/*.js": [
@@ -116,4 +122,4 @@
116
122
  "git add yarn.lock"
117
123
  ]
118
124
  }
119
- }
125
+ }