babel-loader 9.1.2 → 9.2.0

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
@@ -41,7 +41,7 @@ Within your webpack configuration object, you'll need to add the babel-loader to
41
41
  module: {
42
42
  rules: [
43
43
  {
44
- test: /\.m?js$/,
44
+ test: /\.(?:js|mjs|cjs)$/,
45
45
  exclude: /node_modules/,
46
46
  use: {
47
47
  loader: 'babel-loader',
@@ -66,7 +66,7 @@ You can pass options to the loader by using the [`options`](https://webpack.js.o
66
66
  module: {
67
67
  rules: [
68
68
  {
69
- test: /\.m?js$/,
69
+ test: /\.(?:js|mjs|cjs)$/,
70
70
  exclude: /node_modules/,
71
71
  use: {
72
72
  loader: 'babel-loader',
@@ -82,11 +82,17 @@ module: {
82
82
  }
83
83
  ```
84
84
 
85
+ The `options` passed here will be [merged](https://babeljs.io/docs/configuration#how-babel-merges-config-items) with Babel config files, e.g. `babel.config.js` or `.babelrc`.
86
+
85
87
  This loader also supports the following loader-specific option:
86
88
 
87
89
  * `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
90
 
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.
91
+ * `cacheIdentifier`: Default is a string composed by
92
+ - the `@babel/core`'s version and the `babel-loader`'s version
93
+ - the [merged](https://babeljs.io/docs/configuration#how-babel-merges-config-items) [Babel config](https://babeljs.io/docs/config-files), including options passed to `babel-loader` and the contents of `babel.config.js` or `.babelrc` file if they exist
94
+ - the value of the environment variable `BABEL_ENV` with a fallback to the `NODE_ENV` environment variable.
95
+ This can be set to a custom value to force cache busting if the identifier changes.
90
96
 
91
97
  * `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
98
 
@@ -106,13 +112,13 @@ You can also speed up babel-loader by as much as 2x by using the `cacheDirectory
106
112
 
107
113
  ### Some files in my node_modules are not transpiled for IE 11
108
114
 
109
- Although we typically recommend not compiling `node_modules`, you may need to when using libraries that do not support IE 11.
115
+ Although we typically recommend not compiling `node_modules`, you may need to when using libraries that do not support IE 11 or any legacy targets.
110
116
 
111
117
  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).
112
118
 
113
119
  ```javascript
114
120
  {
115
- test: /\.m?js$/,
121
+ test: /\.(?:js|mjs|cjs)$/,
116
122
  exclude: {
117
123
  and: [/node_modules/], // Exclude libraries in node_modules ...
118
124
  not: [
@@ -150,7 +156,7 @@ rules: [
150
156
  // the 'transform-runtime' plugin tells Babel to
151
157
  // require the runtime instead of inlining it.
152
158
  {
153
- test: /\.m?js$/,
159
+ test: /\.(?:js|mjs|cjs)$/,
154
160
  exclude: /node_modules/,
155
161
  use: {
156
162
  loader: 'babel-loader',
@@ -216,7 +222,7 @@ require('./app');
216
222
  If you receive this message, it means that you have the npm package `babel` installed and are using the short notation of the loader in the webpack config (which is not valid anymore as of webpack 2.x):
217
223
  ```javascript
218
224
  {
219
- test: /\.m?js$/,
225
+ test: /\.(?:js|mjs|cjs)$/,
220
226
  loader: 'babel',
221
227
  }
222
228
  ```
@@ -227,7 +233,7 @@ To fix this, you should uninstall the npm package `babel`, as it is deprecated i
227
233
  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:
228
234
  ```javascript
229
235
  {
230
- test: /\.m?js$/,
236
+ test: /\.(?:js|mjs|cjs)$/,
231
237
  loader: 'babel-loader',
232
238
  }
233
239
  ```
@@ -323,7 +329,8 @@ your `custom` callback function.
323
329
  ```js
324
330
  // Export from "./my-custom-loader.js" or whatever you want.
325
331
  module.exports = require("babel-loader").custom(babel => {
326
- function myPlugin() {
332
+ // Extract the custom options in the custom plugin
333
+ function myPlugin(api, { opt1, opt2 }) {
327
334
  return {
328
335
  visitor: {},
329
336
  };
@@ -342,7 +349,7 @@ module.exports = require("babel-loader").custom(babel => {
342
349
  },
343
350
 
344
351
  // Passed Babel's 'PartialConfig' object.
345
- config(cfg) {
352
+ config(cfg, { customOptions }) {
346
353
  if (cfg.hasFilesystemConfig()) {
347
354
  // Use the normal config
348
355
  return cfg.options;
@@ -353,8 +360,8 @@ module.exports = require("babel-loader").custom(babel => {
353
360
  plugins: [
354
361
  ...(cfg.options.plugins || []),
355
362
 
356
- // Include a custom plugin in the options.
357
- myPlugin,
363
+ // Include a custom plugin in the options and passing it the customOptions object.
364
+ [myPlugin, customOptions],
358
365
  ],
359
366
  };
360
367
  },
@@ -389,7 +396,7 @@ Given the loader's options, split custom options out of `babel-loader`'s
389
396
  options.
390
397
 
391
398
 
392
- ### `config(cfg: PartialConfig): Object`
399
+ ### `config(cfg: PartialConfig, options: { source, customOptions }): Object`
393
400
 
394
401
  Given Babel's `PartialConfig` object, return the `options` object that should
395
402
  be passed to `babel.transform`.
package/lib/Error.js CHANGED
@@ -1,5 +1,3 @@
1
- "use strict";
2
-
3
1
  const STRIP_FILENAME_RE = /^[^:]+: /;
4
2
  const format = err => {
5
3
  if (err instanceof SyntaxError) {
package/lib/cache.js CHANGED
@@ -1,5 +1,3 @@
1
- "use strict";
2
-
3
1
  /**
4
2
  * Filesystem Cache
5
3
  *
@@ -13,7 +11,6 @@ const os = require("os");
13
11
  const path = require("path");
14
12
  const zlib = require("zlib");
15
13
  const crypto = require("crypto");
16
- const findCacheDir = require("find-cache-dir");
17
14
  const {
18
15
  promisify
19
16
  } = require("util");
@@ -22,6 +19,7 @@ const {
22
19
  writeFile,
23
20
  mkdir
24
21
  } = require("fs/promises");
22
+ const findCacheDirP = import("find-cache-dir");
25
23
  const transform = require("./transform");
26
24
  // Lazily instantiated when needed
27
25
  let defaultCacheDirectory = null;
@@ -29,7 +27,7 @@ let hashType = "sha256";
29
27
  // use md5 hashing if sha256 is not available
30
28
  try {
31
29
  crypto.createHash(hashType);
32
- } catch (err) {
30
+ } catch {
33
31
  hashType = "md5";
34
32
  }
35
33
  const gunzip = promisify(zlib.gunzip);
@@ -93,19 +91,25 @@ const handleCache = async function (directory, params) {
93
91
  options = {},
94
92
  cacheIdentifier,
95
93
  cacheDirectory,
96
- cacheCompression
94
+ cacheCompression,
95
+ logger
97
96
  } = params;
98
97
  const file = path.join(directory, filename(source, cacheIdentifier, options));
99
98
  try {
100
99
  // No errors mean that the file was previously cached
101
100
  // we just need to return it
101
+ logger.debug(`reading cache file '${file}'`);
102
102
  return await read(file, cacheCompression);
103
- } catch (err) {}
103
+ } catch {
104
+ // conitnue if cache can't be read
105
+ logger.debug(`discarded cache as it can not be read`);
106
+ }
104
107
  const fallback = typeof cacheDirectory !== "string" && directory !== os.tmpdir();
105
108
 
106
109
  // Make sure the directory exists.
107
110
  try {
108
111
  // overwrite directory if exists
112
+ logger.debug(`creating cache folder '${directory}'`);
109
113
  await mkdir(directory, {
110
114
  recursive: true
111
115
  });
@@ -118,12 +122,14 @@ const handleCache = async function (directory, params) {
118
122
 
119
123
  // Otherwise just transform the file
120
124
  // return it to the user asap and write it in cache
125
+ logger.debug(`applying Babel transform`);
121
126
  const result = await transform(source, options);
122
127
 
123
128
  // Do not cache if there are external dependencies,
124
129
  // since they might change and we cannot control it.
125
130
  if (!result.externalDependencies.length) {
126
131
  try {
132
+ logger.debug(`writing result to cache file '${file}'`);
127
133
  await write(file, cacheCompression, result);
128
134
  } catch (err) {
129
135
  if (fallback) {
@@ -167,6 +173,9 @@ module.exports = async function (params) {
167
173
  directory = params.cacheDirectory;
168
174
  } else {
169
175
  if (defaultCacheDirectory === null) {
176
+ const {
177
+ default: findCacheDir
178
+ } = await findCacheDirP;
170
179
  defaultCacheDirectory = findCacheDir({
171
180
  name: "babel-loader"
172
181
  }) || os.tmpdir();
package/lib/index.js CHANGED
@@ -1,5 +1,3 @@
1
- "use strict";
2
-
3
1
  let babel;
4
2
  try {
5
3
  babel = require("@babel/core");
@@ -43,6 +41,7 @@ function makeLoader(callback) {
43
41
  }
44
42
  async function loader(source, inputSourceMap, overrides) {
45
43
  const filename = this.resourcePath;
44
+ const logger = this.getLogger("babel-loader");
46
45
  let loaderOptions = this.getOptions();
47
46
  validateOptions(schema, loaderOptions, {
48
47
  name: "Babel loader"
@@ -57,15 +56,18 @@ async function loader(source, inputSourceMap, overrides) {
57
56
  if (overrides) {
58
57
  throw new Error("babel-loader's 'customize' option is not available when already " + "using a customized babel-loader wrapper.");
59
58
  }
59
+ logger.debug(`loading customize override: '${loaderOptions.customize}'`);
60
60
  let override = require(loaderOptions.customize);
61
61
  if (override.__esModule) override = override.default;
62
62
  if (typeof override !== "function") {
63
63
  throw new Error("Custom overrides must be functions.");
64
64
  }
65
+ logger.debug("applying customize override to @babel/core");
65
66
  overrides = override(babel);
66
67
  }
67
68
  let customOptions;
68
69
  if (overrides && overrides.customOptions) {
70
+ logger.debug("applying overrides customOptions() to loader options");
69
71
  const result = await overrides.customOptions.call(this, loaderOptions, {
70
72
  source,
71
73
  map: inputSourceMap
@@ -81,7 +83,7 @@ async function loader(source, inputSourceMap, overrides) {
81
83
  if (typeof loaderOptions.babelrc === "string") {
82
84
  console.warn("The option `babelrc` should not be set to a string anymore in the babel-loader config. " + "Please update your configuration and set `babelrc` to true or false.\n" + "If you want to specify a specific babel config file to inherit config from " + "please use the `extends` option.\nFor more information about this options see " + "https://babeljs.io/docs/core-packages/#options");
83
85
  }
84
-
86
+ logger.debug("normalizing loader options");
85
87
  // Standardize on 'sourceMaps' as the key passed through to Webpack, so that
86
88
  // users may safely use either one alongside our default use of
87
89
  // 'this.sourceMap' below without getting error about conflicting aliases.
@@ -108,10 +110,12 @@ async function loader(source, inputSourceMap, overrides) {
108
110
  delete programmaticOptions.cacheIdentifier;
109
111
  delete programmaticOptions.cacheCompression;
110
112
  delete programmaticOptions.metadataSubscribers;
113
+ logger.debug("resolving Babel configs");
111
114
  const config = await babel.loadPartialConfigAsync(injectCaller(programmaticOptions, this.target));
112
115
  if (config) {
113
116
  let options = config.options;
114
117
  if (overrides && overrides.config) {
118
+ logger.debug("applying overrides config() to Babel config");
115
119
  options = await overrides.config.call(this, config, {
116
120
  source,
117
121
  map: inputSourceMap,
@@ -139,20 +143,27 @@ async function loader(source, inputSourceMap, overrides) {
139
143
  } = loaderOptions;
140
144
  let result;
141
145
  if (cacheDirectory) {
146
+ logger.debug("cache is enabled");
142
147
  result = await cache({
143
148
  source,
144
149
  options,
145
150
  transform,
146
151
  cacheDirectory,
147
152
  cacheIdentifier,
148
- cacheCompression
153
+ cacheCompression,
154
+ logger
149
155
  });
150
156
  } else {
157
+ logger.debug("cache is disabled, applying Babel transform");
151
158
  result = await transform(source, options);
152
159
  }
153
- config.files.forEach(configFile => this.addDependency(configFile));
160
+ config.files.forEach(configFile => {
161
+ this.addDependency(configFile);
162
+ logger.debug(`added '${configFile}' to webpack dependencies`);
163
+ });
154
164
  if (result) {
155
165
  if (overrides && overrides.result) {
166
+ logger.debug("applying overrides result() to Babel transform results");
156
167
  result = await overrides.result.call(this, result, {
157
168
  source,
158
169
  map: inputSourceMap,
@@ -167,9 +178,13 @@ async function loader(source, inputSourceMap, overrides) {
167
178
  metadata,
168
179
  externalDependencies
169
180
  } = result;
170
- externalDependencies == null ? void 0 : externalDependencies.forEach(dep => this.addDependency(dep));
181
+ externalDependencies?.forEach(dep => {
182
+ this.addDependency(dep);
183
+ logger.debug(`added '${dep}' to webpack dependencies`);
184
+ });
171
185
  metadataSubscribers.forEach(subscriber => {
172
186
  subscribe(subscriber, metadata, this);
187
+ logger.debug(`invoked metadata subscriber '${String(subscriber)}'`);
173
188
  });
174
189
  return [code, map];
175
190
  }
@@ -1,8 +1,4 @@
1
- "use strict";
2
-
3
- const babel = require("@babel/core");
4
1
  module.exports = function injectCaller(opts, target) {
5
- if (!supportsCallerOption()) return opts;
6
2
  return Object.assign({}, opts, {
7
3
  caller: Object.assign({
8
4
  name: "babel-loader",
@@ -18,26 +14,4 @@ module.exports = function injectCaller(opts, target) {
18
14
  supportsTopLevelAwait: true
19
15
  }, opts.caller)
20
16
  });
21
- };
22
-
23
- // TODO: We can remove this eventually, I'm just adding it so that people have
24
- // a little time to migrate to the newer RCs of @babel/core without getting
25
- // hard-to-diagnose errors about unknown 'caller' options.
26
- let supportsCallerOptionFlag = undefined;
27
- function supportsCallerOption() {
28
- if (supportsCallerOptionFlag === undefined) {
29
- try {
30
- // Rather than try to match the Babel version, we just see if it throws
31
- // when passed a 'caller' flag, and use that to decide if it is supported.
32
- babel.loadPartialConfig({
33
- caller: undefined,
34
- babelrc: false,
35
- configFile: false
36
- });
37
- supportsCallerOptionFlag = true;
38
- } catch (err) {
39
- supportsCallerOptionFlag = false;
40
- }
41
- }
42
- return supportsCallerOptionFlag;
43
- }
17
+ };
package/lib/transform.js CHANGED
@@ -1,5 +1,3 @@
1
- "use strict";
2
-
3
1
  const babel = require("@babel/core");
4
2
  const {
5
3
  promisify
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "babel-loader",
3
- "version": "9.1.2",
3
+ "version": "9.2.0",
4
4
  "description": "babel module loader for webpack",
5
5
  "files": [
6
6
  "lib"
@@ -10,7 +10,7 @@
10
10
  "node": ">= 14.15.0"
11
11
  },
12
12
  "dependencies": {
13
- "find-cache-dir": "^3.3.2",
13
+ "find-cache-dir": "^4.0.0",
14
14
  "schema-utils": "^4.0.0"
15
15
  },
16
16
  "peerDependencies": {
@@ -19,41 +19,34 @@
19
19
  },
20
20
  "devDependencies": {
21
21
  "@ava/babel": "^1.0.1",
22
- "@babel/cli": "^7.19.3",
23
- "@babel/core": "^7.19.6",
24
- "@babel/preset-env": "^7.19.4",
22
+ "@babel/cli": "^7.23.0",
23
+ "@babel/core": "^7.23.3",
24
+ "@babel/eslint-parser": "^7.23.3",
25
+ "@babel/preset-env": "^7.23.3",
25
26
  "ava": "^3.13.0",
26
- "babel-eslint": "^10.1.0",
27
- "babel-plugin-istanbul": "^6.1.1",
28
- "babel-plugin-react-intl": "^8.2.25",
29
- "cross-env": "^7.0.2",
30
- "eslint": "^7.13.0",
31
- "eslint-config-babel": "^9.0.0",
32
- "eslint-config-prettier": "^6.3.0",
33
- "eslint-plugin-flowtype": "^5.2.0",
34
- "eslint-plugin-prettier": "^3.0.0",
35
- "husky": "^4.3.0",
36
- "lint-staged": "^10.5.1",
37
- "nyc": "^15.1.0",
38
- "pnp-webpack-plugin": "^1.6.4",
39
- "prettier": "^2.1.2",
40
- "react": "^17.0.1",
41
- "react-intl": "^5.9.4",
42
- "react-intl-webpack-plugin": "^0.3.0",
43
- "rimraf": "^3.0.0",
44
- "semver": "7.3.2",
45
- "webpack": "^5.74.0"
27
+ "c8": "^8.0.0",
28
+ "eslint": "^9.6.0",
29
+ "eslint-config-prettier": "^9.1.0",
30
+ "eslint-plugin-prettier": "^5.1.3",
31
+ "globals": "^15.8.0",
32
+ "husky": "^8.0.3",
33
+ "lint-staged": "^13.2.3",
34
+ "prettier": "^3.0.0",
35
+ "webpack": "^5.89.0"
46
36
  },
47
37
  "scripts": {
48
- "clean": "rimraf lib/",
38
+ "clean": "node ./scripts/rimraf.mjs lib",
49
39
  "build": "babel src/ --out-dir lib/ --copy-files",
50
40
  "format": "prettier --write --trailing-comma all 'src/**/*.js' 'test/**/*.test.js' 'test/helpers/*.js' && prettier --write --trailing-comma es5 'scripts/*.js'",
51
41
  "lint": "eslint src test",
52
42
  "precommit": "lint-staged",
53
43
  "prepublish": "yarn run clean && yarn run build",
54
44
  "preversion": "yarn run test",
55
- "test": "yarn run lint && cross-env BABEL_ENV=test yarn run build && yarn run test-only",
56
- "test-only": "nyc ava"
45
+ "test": "yarn run lint && yarn run build --source-maps && c8 yarn run test-only",
46
+ "test-only": "ava"
47
+ },
48
+ "resolutions": {
49
+ "minipass": "6.0.2"
57
50
  },
58
51
  "repository": {
59
52
  "type": "git",
@@ -119,7 +112,5 @@
119
112
  "git add yarn.lock"
120
113
  ]
121
114
  },
122
- "resolutions": {
123
- "nyc/node-preload": "0.2.0"
124
- }
115
+ "packageManager": "yarn@3.6.4"
125
116
  }