babel-loader 9.1.3 → 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
@@ -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
 
package/lib/cache.js CHANGED
@@ -27,7 +27,7 @@ let hashType = "sha256";
27
27
  // use md5 hashing if sha256 is not available
28
28
  try {
29
29
  crypto.createHash(hashType);
30
- } catch (err) {
30
+ } catch {
31
31
  hashType = "md5";
32
32
  }
33
33
  const gunzip = promisify(zlib.gunzip);
@@ -91,19 +91,25 @@ const handleCache = async function (directory, params) {
91
91
  options = {},
92
92
  cacheIdentifier,
93
93
  cacheDirectory,
94
- cacheCompression
94
+ cacheCompression,
95
+ logger
95
96
  } = params;
96
97
  const file = path.join(directory, filename(source, cacheIdentifier, options));
97
98
  try {
98
99
  // No errors mean that the file was previously cached
99
100
  // we just need to return it
101
+ logger.debug(`reading cache file '${file}'`);
100
102
  return await read(file, cacheCompression);
101
- } catch (err) {}
103
+ } catch {
104
+ // conitnue if cache can't be read
105
+ logger.debug(`discarded cache as it can not be read`);
106
+ }
102
107
  const fallback = typeof cacheDirectory !== "string" && directory !== os.tmpdir();
103
108
 
104
109
  // Make sure the directory exists.
105
110
  try {
106
111
  // overwrite directory if exists
112
+ logger.debug(`creating cache folder '${directory}'`);
107
113
  await mkdir(directory, {
108
114
  recursive: true
109
115
  });
@@ -116,12 +122,14 @@ const handleCache = async function (directory, params) {
116
122
 
117
123
  // Otherwise just transform the file
118
124
  // return it to the user asap and write it in cache
125
+ logger.debug(`applying Babel transform`);
119
126
  const result = await transform(source, options);
120
127
 
121
128
  // Do not cache if there are external dependencies,
122
129
  // since they might change and we cannot control it.
123
130
  if (!result.externalDependencies.length) {
124
131
  try {
132
+ logger.debug(`writing result to cache file '${file}'`);
125
133
  await write(file, cacheCompression, result);
126
134
  } catch (err) {
127
135
  if (fallback) {
package/lib/index.js CHANGED
@@ -41,6 +41,7 @@ function makeLoader(callback) {
41
41
  }
42
42
  async function loader(source, inputSourceMap, overrides) {
43
43
  const filename = this.resourcePath;
44
+ const logger = this.getLogger("babel-loader");
44
45
  let loaderOptions = this.getOptions();
45
46
  validateOptions(schema, loaderOptions, {
46
47
  name: "Babel loader"
@@ -55,15 +56,18 @@ async function loader(source, inputSourceMap, overrides) {
55
56
  if (overrides) {
56
57
  throw new Error("babel-loader's 'customize' option is not available when already " + "using a customized babel-loader wrapper.");
57
58
  }
59
+ logger.debug(`loading customize override: '${loaderOptions.customize}'`);
58
60
  let override = require(loaderOptions.customize);
59
61
  if (override.__esModule) override = override.default;
60
62
  if (typeof override !== "function") {
61
63
  throw new Error("Custom overrides must be functions.");
62
64
  }
65
+ logger.debug("applying customize override to @babel/core");
63
66
  overrides = override(babel);
64
67
  }
65
68
  let customOptions;
66
69
  if (overrides && overrides.customOptions) {
70
+ logger.debug("applying overrides customOptions() to loader options");
67
71
  const result = await overrides.customOptions.call(this, loaderOptions, {
68
72
  source,
69
73
  map: inputSourceMap
@@ -79,7 +83,7 @@ async function loader(source, inputSourceMap, overrides) {
79
83
  if (typeof loaderOptions.babelrc === "string") {
80
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");
81
85
  }
82
-
86
+ logger.debug("normalizing loader options");
83
87
  // Standardize on 'sourceMaps' as the key passed through to Webpack, so that
84
88
  // users may safely use either one alongside our default use of
85
89
  // 'this.sourceMap' below without getting error about conflicting aliases.
@@ -106,10 +110,12 @@ async function loader(source, inputSourceMap, overrides) {
106
110
  delete programmaticOptions.cacheIdentifier;
107
111
  delete programmaticOptions.cacheCompression;
108
112
  delete programmaticOptions.metadataSubscribers;
113
+ logger.debug("resolving Babel configs");
109
114
  const config = await babel.loadPartialConfigAsync(injectCaller(programmaticOptions, this.target));
110
115
  if (config) {
111
116
  let options = config.options;
112
117
  if (overrides && overrides.config) {
118
+ logger.debug("applying overrides config() to Babel config");
113
119
  options = await overrides.config.call(this, config, {
114
120
  source,
115
121
  map: inputSourceMap,
@@ -137,20 +143,27 @@ async function loader(source, inputSourceMap, overrides) {
137
143
  } = loaderOptions;
138
144
  let result;
139
145
  if (cacheDirectory) {
146
+ logger.debug("cache is enabled");
140
147
  result = await cache({
141
148
  source,
142
149
  options,
143
150
  transform,
144
151
  cacheDirectory,
145
152
  cacheIdentifier,
146
- cacheCompression
153
+ cacheCompression,
154
+ logger
147
155
  });
148
156
  } else {
157
+ logger.debug("cache is disabled, applying Babel transform");
149
158
  result = await transform(source, options);
150
159
  }
151
- 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
+ });
152
164
  if (result) {
153
165
  if (overrides && overrides.result) {
166
+ logger.debug("applying overrides result() to Babel transform results");
154
167
  result = await overrides.result.call(this, result, {
155
168
  source,
156
169
  map: inputSourceMap,
@@ -165,9 +178,13 @@ async function loader(source, inputSourceMap, overrides) {
165
178
  metadata,
166
179
  externalDependencies
167
180
  } = result;
168
- 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
+ });
169
185
  metadataSubscribers.forEach(subscriber => {
170
186
  subscribe(subscriber, metadata, this);
187
+ logger.debug(`invoked metadata subscriber '${String(subscriber)}'`);
171
188
  });
172
189
  return [code, map];
173
190
  }
@@ -1,6 +1,4 @@
1
- const babel = require("@babel/core");
2
1
  module.exports = function injectCaller(opts, target) {
3
- if (!supportsCallerOption()) return opts;
4
2
  return Object.assign({}, opts, {
5
3
  caller: Object.assign({
6
4
  name: "babel-loader",
@@ -16,26 +14,4 @@ module.exports = function injectCaller(opts, target) {
16
14
  supportsTopLevelAwait: true
17
15
  }, opts.caller)
18
16
  });
19
- };
20
-
21
- // TODO: We can remove this eventually, I'm just adding it so that people have
22
- // a little time to migrate to the newer RCs of @babel/core without getting
23
- // hard-to-diagnose errors about unknown 'caller' options.
24
- let supportsCallerOptionFlag = undefined;
25
- function supportsCallerOption() {
26
- if (supportsCallerOptionFlag === undefined) {
27
- try {
28
- // Rather than try to match the Babel version, we just see if it throws
29
- // when passed a 'caller' flag, and use that to decide if it is supported.
30
- babel.loadPartialConfig({
31
- caller: undefined,
32
- babelrc: false,
33
- configFile: false
34
- });
35
- supportsCallerOptionFlag = true;
36
- } catch (err) {
37
- supportsCallerOptionFlag = false;
38
- }
39
- }
40
- return supportsCallerOptionFlag;
41
- }
17
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "babel-loader",
3
- "version": "9.1.3",
3
+ "version": "9.2.0",
4
4
  "description": "babel module loader for webpack",
5
5
  "files": [
6
6
  "lib"
@@ -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.5.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
  }