babel-loader 10.0.0 → 10.1.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 +1 -1
- package/lib/Error.js +13 -0
- package/lib/cache.js +92 -25
- package/lib/index.js +80 -4
- package/lib/injectCaller.js +10 -2
- package/lib/serialize.js +16 -3
- package/lib/transform.js +27 -4
- package/package.json +19 -8
- package/CHANGELOG.md +0 -169
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
|
|
16
16
|
<h1 align="center">Babel Loader</h1>
|
|
17
17
|
|
|
18
|
-
This package allows transpiling JavaScript files using [Babel](https://github.com/babel/babel)
|
|
18
|
+
This package allows transpiling JavaScript files using [Babel](https://github.com/babel/babel) together with [webpack](https://github.com/webpack/webpack) or [Rspack](https://github.com/web-infra-dev/rspack).
|
|
19
19
|
|
|
20
20
|
**Note**: Issues with the output should be reported on the Babel [Issues](https://github.com/babel/babel/issues) tracker.
|
|
21
21
|
|
package/lib/Error.js
CHANGED
|
@@ -1,4 +1,14 @@
|
|
|
1
|
+
// @ts-check
|
|
1
2
|
const STRIP_FILENAME_RE = /^[^:]+: /;
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @typedef { Error & { hideStack?: boolean, codeFrame?: string } } BabelLoaderError
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Format the error for display.
|
|
9
|
+
* @param {BabelLoaderError} err
|
|
10
|
+
* @returns {BabelLoaderError}
|
|
11
|
+
*/
|
|
2
12
|
const format = err => {
|
|
3
13
|
if (err instanceof SyntaxError) {
|
|
4
14
|
err.name = "SyntaxError";
|
|
@@ -12,6 +22,9 @@ const format = err => {
|
|
|
12
22
|
return err;
|
|
13
23
|
};
|
|
14
24
|
class LoaderError extends Error {
|
|
25
|
+
/**
|
|
26
|
+
* @param {BabelLoaderError} err
|
|
27
|
+
*/
|
|
15
28
|
constructor(err) {
|
|
16
29
|
super();
|
|
17
30
|
const {
|
package/lib/cache.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// @ts-check
|
|
1
2
|
/**
|
|
2
3
|
* Filesystem Cache
|
|
3
4
|
*
|
|
@@ -7,6 +8,7 @@
|
|
|
7
8
|
* @see https://github.com/babel/babel-loader/issues/34
|
|
8
9
|
* @see https://github.com/babel/babel-loader/pull/41
|
|
9
10
|
*/
|
|
11
|
+
const nodeModule = require("node:module");
|
|
10
12
|
const os = require("os");
|
|
11
13
|
const path = require("path");
|
|
12
14
|
const zlib = require("zlib");
|
|
@@ -26,16 +28,45 @@ const {
|
|
|
26
28
|
} = process;
|
|
27
29
|
const transform = require("./transform");
|
|
28
30
|
const serialize = require("./serialize");
|
|
31
|
+
/**
|
|
32
|
+
* @typedef {object} FileSystemInfoEntry
|
|
33
|
+
* @property {number} safeTime
|
|
34
|
+
* @property {number} timestamp
|
|
35
|
+
*/
|
|
36
|
+
/**
|
|
37
|
+
* @typedef {object} WebpackLogger
|
|
38
|
+
* @property {function(string): void} debug
|
|
39
|
+
* @property {function(string): void} info
|
|
40
|
+
* @property {function(string): void} warn
|
|
41
|
+
* @property {function(string): void} error
|
|
42
|
+
*/
|
|
43
|
+
/**
|
|
44
|
+
* @typedef {object} WebpackHash
|
|
45
|
+
* @property {(data: string | Buffer, inputEncoding?: string) => WebpackHash} update
|
|
46
|
+
* @property {(encoding?: string) => string | Buffer} digest
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @type {string | null}
|
|
51
|
+
*/
|
|
29
52
|
let defaultCacheDirectory = null;
|
|
30
53
|
const gunzip = promisify(zlib.gunzip);
|
|
31
54
|
const gzip = promisify(zlib.gzip);
|
|
55
|
+
const findRootPackageJSON = () => {
|
|
56
|
+
if (nodeModule.findPackageJSON) {
|
|
57
|
+
return nodeModule.findPackageJSON("..", __filename);
|
|
58
|
+
} else {
|
|
59
|
+
// todo: remove this fallback when dropping support for Node.js < 22.14
|
|
60
|
+
return findUpSync("package.json");
|
|
61
|
+
}
|
|
62
|
+
};
|
|
32
63
|
|
|
33
64
|
/**
|
|
34
65
|
* Read the contents from the compressed file.
|
|
35
66
|
*
|
|
36
67
|
* @async
|
|
37
|
-
* @
|
|
38
|
-
* @
|
|
68
|
+
* @param {string} filename
|
|
69
|
+
* @param {boolean} compress
|
|
39
70
|
*/
|
|
40
71
|
const read = async function (filename, compress) {
|
|
41
72
|
const data = await readFile(filename + (compress ? ".gz" : ""));
|
|
@@ -45,11 +76,10 @@ const read = async function (filename, compress) {
|
|
|
45
76
|
|
|
46
77
|
/**
|
|
47
78
|
* Write contents into a compressed file.
|
|
48
|
-
*
|
|
49
79
|
* @async
|
|
50
|
-
* @
|
|
51
|
-
* @
|
|
52
|
-
* @
|
|
80
|
+
* @param {string} filename
|
|
81
|
+
* @param {boolean} compress
|
|
82
|
+
* @param {any} result
|
|
53
83
|
*/
|
|
54
84
|
const write = async function (filename, compress, result) {
|
|
55
85
|
const content = JSON.stringify(result);
|
|
@@ -59,16 +89,23 @@ const write = async function (filename, compress, result) {
|
|
|
59
89
|
|
|
60
90
|
/**
|
|
61
91
|
* Build the filename for the cached file
|
|
62
|
-
*
|
|
63
|
-
* @
|
|
64
|
-
* @
|
|
65
|
-
*
|
|
66
|
-
* @return {
|
|
92
|
+
* @param {string} source File source code
|
|
93
|
+
* @param {string} identifier Unique identifier to bust cache
|
|
94
|
+
* @param {Object} options Options used
|
|
95
|
+
* @param {WebpackHash} hash Hash function returned by `LoaderContext.utils.createHash`
|
|
96
|
+
* @return {string}
|
|
67
97
|
*/
|
|
68
98
|
const filename = function (source, identifier, options, hash) {
|
|
69
99
|
hash.update(serialize([options, source, identifier]));
|
|
70
100
|
return hash.digest("hex") + ".json";
|
|
71
101
|
};
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Add timestamps to external dependencies.
|
|
105
|
+
* @async
|
|
106
|
+
* @param {import("./transform").TransformResult["externalDependencies"]} externalDependencies
|
|
107
|
+
* @param {(filename: string) => Promise<FileSystemInfoEntry>} getFileTimestamp
|
|
108
|
+
*/
|
|
72
109
|
const addTimestamps = async function (externalDependencies, getFileTimestamp) {
|
|
73
110
|
for (const depAndEmptyTimestamp of externalDependencies) {
|
|
74
111
|
try {
|
|
@@ -82,6 +119,14 @@ const addTimestamps = async function (externalDependencies, getFileTimestamp) {
|
|
|
82
119
|
}
|
|
83
120
|
}
|
|
84
121
|
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Check if any external dependencies have been modified.
|
|
125
|
+
* @async
|
|
126
|
+
* @param {import("./transform").TransformResult["externalDependencies"]} externalDepsWithTimestamp
|
|
127
|
+
* @param {(filename: string) => Promise<FileSystemInfoEntry>} getFileTimestamp
|
|
128
|
+
* @returns {Promise<boolean>}
|
|
129
|
+
*/
|
|
85
130
|
const areExternalDependenciesModified = async function (externalDepsWithTimestamp, getFileTimestamp) {
|
|
86
131
|
for (const depAndTimestamp of externalDepsWithTimestamp) {
|
|
87
132
|
const [dep, timestamp] = depAndTimestamp;
|
|
@@ -100,9 +145,18 @@ const areExternalDependenciesModified = async function (externalDepsWithTimestam
|
|
|
100
145
|
|
|
101
146
|
/**
|
|
102
147
|
* Handle the cache
|
|
103
|
-
*
|
|
104
|
-
* @
|
|
105
|
-
* @
|
|
148
|
+
* @async
|
|
149
|
+
* @param {string} directory
|
|
150
|
+
* @param {Object} params
|
|
151
|
+
* @param {string} params.source The source code to transform.
|
|
152
|
+
* @param {import(".").NormalizedOptions} [params.options] Options used for transformation.
|
|
153
|
+
* @param {string} params.cacheIdentifier Unique identifier to bust cache.
|
|
154
|
+
* @param {string} [params.cacheDirectory] Directory to store cached files.
|
|
155
|
+
* @param {boolean} [params.cacheCompression] Whether to compress cached files.
|
|
156
|
+
* @param {WebpackHash} params.hash Hash function to use for the cache filename.
|
|
157
|
+
* @param {(filename: string) => Promise<FileSystemInfoEntry>} params.getFileTimestamp - Function to get file timestamps.
|
|
158
|
+
* @param {WebpackLogger} params.logger
|
|
159
|
+
* @returns {Promise<null | import("./transform").TransformResult>}
|
|
106
160
|
*/
|
|
107
161
|
const handleCache = async function (directory, params) {
|
|
108
162
|
const {
|
|
@@ -127,7 +181,7 @@ const handleCache = async function (directory, params) {
|
|
|
127
181
|
}
|
|
128
182
|
logger.debug(`discarded cache file '${file}' due to changes in external dependencies`);
|
|
129
183
|
} catch {
|
|
130
|
-
//
|
|
184
|
+
// continue if cache can't be read
|
|
131
185
|
logger.debug(`discarded cache as it can not be read`);
|
|
132
186
|
}
|
|
133
187
|
const fallback = typeof cacheDirectory !== "string" && directory !== os.tmpdir();
|
|
@@ -150,6 +204,10 @@ const handleCache = async function (directory, params) {
|
|
|
150
204
|
// return it to the user asap and write it in cache
|
|
151
205
|
logger.debug(`applying Babel transform`);
|
|
152
206
|
const result = await transform(source, options);
|
|
207
|
+
if (!result) {
|
|
208
|
+
logger.debug(`no result from Babel transform, skipping cache write`);
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
153
211
|
await addTimestamps(result.externalDependencies, getFileTimestamp);
|
|
154
212
|
try {
|
|
155
213
|
logger.debug(`writing result to cache file '${file}'`);
|
|
@@ -166,14 +224,17 @@ const handleCache = async function (directory, params) {
|
|
|
166
224
|
|
|
167
225
|
/**
|
|
168
226
|
* Retrieve file from cache, or create a new one for future reads
|
|
169
|
-
*
|
|
170
227
|
* @async
|
|
171
|
-
* @param
|
|
172
|
-
* @param
|
|
173
|
-
* @param
|
|
174
|
-
* @param
|
|
175
|
-
* @param
|
|
176
|
-
* @param
|
|
228
|
+
* @param {object} params
|
|
229
|
+
* @param {string} params.cacheDirectory Directory to store cached files.
|
|
230
|
+
* @param {string} params.cacheIdentifier Unique identifier to bust cache.
|
|
231
|
+
* @param {boolean} params.cacheCompression Whether compressing cached files.
|
|
232
|
+
* @param {string} params.source Original contents of the file to be cached.
|
|
233
|
+
* @param {import(".").NormalizedOptions} params.options Options to be given to the transform function.
|
|
234
|
+
* @param {function} params.transform Transform function to apply to the file.
|
|
235
|
+
* @param {WebpackHash} params.hash Hash function to use for the cache filename.
|
|
236
|
+
* @param {function(string): Promise<FileSystemInfoEntry>} params.getFileTimestamp Function to get file timestamps.
|
|
237
|
+
* @param {WebpackLogger} params.logger Logger instance.
|
|
177
238
|
*
|
|
178
239
|
* @example
|
|
179
240
|
*
|
|
@@ -189,7 +250,7 @@ const handleCache = async function (directory, params) {
|
|
|
189
250
|
* });
|
|
190
251
|
*/
|
|
191
252
|
|
|
192
|
-
module.exports = async function (params) {
|
|
253
|
+
module.exports = async function cache(params) {
|
|
193
254
|
let directory;
|
|
194
255
|
if (typeof params.cacheDirectory === "string") {
|
|
195
256
|
directory = params.cacheDirectory;
|
|
@@ -199,13 +260,19 @@ module.exports = async function (params) {
|
|
|
199
260
|
}
|
|
200
261
|
return await handleCache(directory, params);
|
|
201
262
|
};
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Find the cache directory for babel-loader.
|
|
266
|
+
* @param {string} name "babel-loader"
|
|
267
|
+
* @returns {string}
|
|
268
|
+
*/
|
|
202
269
|
function findCacheDir(name) {
|
|
203
270
|
if (env.CACHE_DIR && !["true", "false", "1", "0"].includes(env.CACHE_DIR)) {
|
|
204
271
|
return path.join(env.CACHE_DIR, name);
|
|
205
272
|
}
|
|
206
|
-
const rootPkgJSONPath =
|
|
273
|
+
const rootPkgJSONPath = findRootPackageJSON();
|
|
207
274
|
if (rootPkgJSONPath) {
|
|
208
|
-
return path.join(rootPkgJSONPath, "node_modules", ".cache", name);
|
|
275
|
+
return path.join(path.dirname(rootPkgJSONPath), "node_modules", ".cache", name);
|
|
209
276
|
}
|
|
210
277
|
return os.tmpdir();
|
|
211
278
|
}
|
package/lib/index.js
CHANGED
|
@@ -1,3 +1,37 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* @typedef {object} LoaderOnlyOptions
|
|
4
|
+
* @property {string} [cacheDirectory] Directory to store cached files.
|
|
5
|
+
* @property {string} [cacheIdentifier] Unique identifier to bust cache.
|
|
6
|
+
* @property {boolean} [cacheCompression] Whether to compress cached files.
|
|
7
|
+
* @property {string} [customize] The absolute path of a file that exports a BabelLoaderWrapper.
|
|
8
|
+
* @property {Array<string>} [metadataSubscribers] Names of subscribers registered in the loader context.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @typedef {import("webpack").LoaderContext<LoaderOptions>} BabelLoaderContext
|
|
13
|
+
* @typedef {string} BabelLoaderSource Parameters<import("webpack").LoaderDefinitionFunction>[0]
|
|
14
|
+
* @typedef {string} BabelLoaderInputSourceMap Parameters<import("webpack").LoaderDefinitionFunction>[1]
|
|
15
|
+
*
|
|
16
|
+
* @todo Consider exporting these types from @babel/core
|
|
17
|
+
* @typedef {Awaited<ReturnType<import("@babel/core").loadPartialConfigAsync>>} PartialConfig
|
|
18
|
+
* @typedef {PartialConfig['options']} NormalizedOptions
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @typedef {(babel: typeof import("@babel/core")) => BabelOverrideHooks} BabelLoaderWrapper
|
|
23
|
+
* @typedef {object} BabelOverrideHooks
|
|
24
|
+
* @property {(this: BabelLoaderContext, loaderOptions: LoaderOptions, params: { source: BabelLoaderSource, map: BabelLoaderInputSourceMap }) => Promise<{ custom: any, loader: LoaderOptions }>} customOptions
|
|
25
|
+
* @property {(this: BabelLoaderContext, config: PartialConfig, params: { source: BabelLoaderSource, map: BabelLoaderInputSourceMap, customOptions: any }) => Promise<PartialConfig['options']>} config
|
|
26
|
+
* @property {(this: BabelLoaderContext, result: import("./transform").TransformResult, params: { source: BabelLoaderSource, map: BabelLoaderInputSourceMap, customOptions: any, config: PartialConfig, options: PartialConfig['options'] }) => Promise<import("./transform").TransformResult>} result
|
|
27
|
+
*/
|
|
28
|
+
/**
|
|
29
|
+
* @typedef {import("@babel/core").InputOptions & LoaderOnlyOptions} LoaderOptions
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @type {import("@babel/core")}
|
|
34
|
+
*/
|
|
1
35
|
let babel;
|
|
2
36
|
try {
|
|
3
37
|
babel = require("@babel/core");
|
|
@@ -19,31 +53,64 @@ const {
|
|
|
19
53
|
const cache = require("./cache");
|
|
20
54
|
const transform = require("./transform");
|
|
21
55
|
const injectCaller = require("./injectCaller");
|
|
22
|
-
const schema = require("./schema");
|
|
56
|
+
const schema = require("./schema.json");
|
|
23
57
|
const {
|
|
24
58
|
isAbsolute
|
|
25
59
|
} = require("path");
|
|
26
60
|
const {
|
|
27
61
|
promisify
|
|
28
62
|
} = require("util");
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Invoke a metadata subscriber registered in the loader context.
|
|
66
|
+
* @param {string} subscriber
|
|
67
|
+
* @param {unknown} metadata
|
|
68
|
+
* @param {import("webpack").LoaderContext<LoaderOptions>} context
|
|
69
|
+
*/
|
|
29
70
|
function subscribe(subscriber, metadata, context) {
|
|
71
|
+
// @ts-expect-error subscriber is a custom function
|
|
30
72
|
if (context[subscriber]) {
|
|
73
|
+
// @ts-expect-error subscriber is a custom function
|
|
31
74
|
context[subscriber](metadata);
|
|
32
75
|
}
|
|
33
76
|
}
|
|
34
77
|
module.exports = makeLoader();
|
|
35
78
|
module.exports.custom = makeLoader;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @param {BabelLoaderWrapper} [callback]
|
|
82
|
+
*/
|
|
36
83
|
function makeLoader(callback) {
|
|
37
84
|
const overrides = callback ? callback(babel) : undefined;
|
|
38
|
-
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @this {BabelLoaderContext}
|
|
88
|
+
* @param {BabelLoaderSource} source
|
|
89
|
+
* @param {BabelLoaderInputSourceMap} inputSourceMap
|
|
90
|
+
*/
|
|
91
|
+
const webpackLoader = function (source, inputSourceMap) {
|
|
39
92
|
// Make the loader async
|
|
40
93
|
const callback = this.async();
|
|
41
|
-
loader.call(this, source, inputSourceMap, overrides).then(
|
|
94
|
+
loader.call(this, source, inputSourceMap, overrides).then(
|
|
95
|
+
// @ts-expect-error (FixMe): Argument of type 'string | EncodedSourceMap' is not assignable to parameter of type 'string | Buffer<ArrayBufferLike>'.
|
|
96
|
+
args => callback(null, ...args), err => callback(err));
|
|
42
97
|
};
|
|
98
|
+
return webpackLoader;
|
|
43
99
|
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Babel loader
|
|
103
|
+
* @this {BabelLoaderContext}
|
|
104
|
+
* @param {BabelLoaderSource} source The source code to transform
|
|
105
|
+
* @param {BabelLoaderInputSourceMap} inputSourceMap
|
|
106
|
+
* @param {BabelOverrideHooks} overrides
|
|
107
|
+
* @returns
|
|
108
|
+
*/
|
|
44
109
|
async function loader(source, inputSourceMap, overrides) {
|
|
45
110
|
const filename = this.resourcePath;
|
|
46
111
|
const logger = this.getLogger("babel-loader");
|
|
112
|
+
|
|
113
|
+
// @ts-expect-error TS does not treat schema.json/properties/cacheDirectory/type as a constant string literal
|
|
47
114
|
let loaderOptions = this.getOptions(schema);
|
|
48
115
|
if (loaderOptions.customize != null) {
|
|
49
116
|
if (!isAbsolute(loaderOptions.customize)) {
|
|
@@ -133,10 +200,19 @@ async function loader(source, inputSourceMap, overrides) {
|
|
|
133
200
|
cacheCompression = true,
|
|
134
201
|
metadataSubscribers = []
|
|
135
202
|
} = loaderOptions;
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* @type {import("./transform").TransformResult}
|
|
206
|
+
*/
|
|
136
207
|
let result;
|
|
137
208
|
if (cacheDirectory) {
|
|
138
209
|
logger.debug("cache is enabled");
|
|
139
|
-
const getFileTimestamp = promisify(
|
|
210
|
+
const getFileTimestamp = promisify(
|
|
211
|
+
/**
|
|
212
|
+
* @param {string} path
|
|
213
|
+
* @param {(err: import("webpack").WebpackError | null, fileTimestamp: import("./cache").FileSystemInfoEntry) => void} cb
|
|
214
|
+
*/
|
|
215
|
+
(path, cb) => {
|
|
140
216
|
this._compilation.fileSystemInfo.getFileTimestamp(path, cb);
|
|
141
217
|
});
|
|
142
218
|
const hash = this.utils.createHash(this._compilation.outputOptions.hashFunction);
|
package/lib/injectCaller.js
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* Inject babel-loader caller information into the Babel options.
|
|
4
|
+
* @param {import("@babel/core").InputOptions} opts
|
|
5
|
+
* @param {string} target
|
|
6
|
+
* @returns {import("@babel/core").InputOptions}
|
|
7
|
+
*/
|
|
1
8
|
module.exports = function injectCaller(opts, target) {
|
|
2
|
-
return
|
|
9
|
+
return {
|
|
10
|
+
...opts,
|
|
3
11
|
caller: Object.assign({
|
|
4
12
|
name: "babel-loader",
|
|
5
13
|
// Provide plugins with insight into webpack target.
|
|
@@ -13,5 +21,5 @@ module.exports = function injectCaller(opts, target) {
|
|
|
13
21
|
// flag isn't enabled.
|
|
14
22
|
supportsTopLevelAwait: true
|
|
15
23
|
}, opts.caller)
|
|
16
|
-
}
|
|
24
|
+
};
|
|
17
25
|
};
|
package/lib/serialize.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// @ts-check
|
|
1
2
|
var objToString = Object.prototype.toString;
|
|
2
3
|
var objKeys = Object.getOwnPropertyNames;
|
|
3
4
|
|
|
@@ -5,7 +6,7 @@ var objKeys = Object.getOwnPropertyNames;
|
|
|
5
6
|
* A custom Babel options serializer
|
|
6
7
|
*
|
|
7
8
|
* Intentional deviation from JSON.stringify:
|
|
8
|
-
* 1. Object properties are sorted before
|
|
9
|
+
* 1. Object properties are sorted before serializing
|
|
9
10
|
* 2. The output is NOT a valid JSON: e.g.
|
|
10
11
|
* The output does not enquote strings, which means a JSON-like string '{"a":1}'
|
|
11
12
|
* will share the same result with an JS object { a: 1 }. This is not an issue
|
|
@@ -13,8 +14,8 @@ var objKeys = Object.getOwnPropertyNames;
|
|
|
13
14
|
* 3. Only 20% slower than the native JSON.stringify on V8
|
|
14
15
|
*
|
|
15
16
|
* This function is a fork from https://github.com/nickyout/fast-stable-stringify
|
|
16
|
-
* @param {
|
|
17
|
-
* @param {
|
|
17
|
+
* @param {unknown} val Babel options
|
|
18
|
+
* @param {boolean} isArrayProp
|
|
18
19
|
* @returns serialized Babel options
|
|
19
20
|
*/
|
|
20
21
|
function serialize(val, isArrayProp) {
|
|
@@ -29,17 +30,22 @@ function serialize(val, isArrayProp) {
|
|
|
29
30
|
case "object":
|
|
30
31
|
if (val === null) {
|
|
31
32
|
return null;
|
|
33
|
+
// @ts-expect-error
|
|
32
34
|
} else if (val.toJSON && typeof val.toJSON === "function") {
|
|
35
|
+
// @ts-expect-error toJSON has been checked above
|
|
33
36
|
return serialize(val.toJSON(), isArrayProp);
|
|
34
37
|
} else {
|
|
35
38
|
toStr = objToString.call(val);
|
|
36
39
|
if (toStr === "[object Array]") {
|
|
37
40
|
str = "[";
|
|
41
|
+
// @ts-expect-error val is an array
|
|
38
42
|
max = val.length - 1;
|
|
39
43
|
for (i = 0; i < max; i++) {
|
|
44
|
+
// @ts-expect-error val is an array
|
|
40
45
|
str += serialize(val[i], true) + ",";
|
|
41
46
|
}
|
|
42
47
|
if (max > -1) {
|
|
48
|
+
// @ts-expect-error val is an array
|
|
43
49
|
str += serialize(val[i], true);
|
|
44
50
|
}
|
|
45
51
|
return str + "]";
|
|
@@ -51,6 +57,7 @@ function serialize(val, isArrayProp) {
|
|
|
51
57
|
i = 0;
|
|
52
58
|
while (i < max) {
|
|
53
59
|
key = keys[i];
|
|
60
|
+
// @ts-expect-error key must index val
|
|
54
61
|
propVal = serialize(val[key], false);
|
|
55
62
|
if (propVal !== undefined) {
|
|
56
63
|
if (str) {
|
|
@@ -71,9 +78,15 @@ function serialize(val, isArrayProp) {
|
|
|
71
78
|
case "string":
|
|
72
79
|
return val;
|
|
73
80
|
default:
|
|
81
|
+
// @ts-expect-error val must be a number because of the toJSON check above
|
|
74
82
|
return isFinite(val) ? val : null;
|
|
75
83
|
}
|
|
76
84
|
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @param {unknown} val
|
|
88
|
+
* @returns {string | undefined}
|
|
89
|
+
*/
|
|
77
90
|
module.exports = function (val) {
|
|
78
91
|
var returnVal = serialize(val, false);
|
|
79
92
|
if (returnVal !== undefined) {
|
package/lib/transform.js
CHANGED
|
@@ -1,13 +1,31 @@
|
|
|
1
|
+
// @ts-check
|
|
1
2
|
const babel = require("@babel/core");
|
|
2
3
|
const {
|
|
3
4
|
promisify
|
|
4
5
|
} = require("util");
|
|
5
6
|
const LoaderError = require("./Error");
|
|
6
|
-
const
|
|
7
|
-
|
|
7
|
+
const babelTransform = babel.transformAsync ?? promisify(babel.transform);
|
|
8
|
+
/**
|
|
9
|
+
* @typedef {Object} AmendedTransformResult
|
|
10
|
+
* @property {[string, number?][]} externalDependencies
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {Omit<import("@babel/core").FileResult, "externalDependencies" | "options"> & AmendedTransformResult} TransformResult
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* Transform the source code using Babel.
|
|
17
|
+
* @async
|
|
18
|
+
* @param {string} source The source code to transform.
|
|
19
|
+
* @param {import("@babel/core").InputOptions} options The Babel options.
|
|
20
|
+
* @returns {Promise<null | TransformResult>} The transformed result or null if no transformation is needed.
|
|
21
|
+
*/
|
|
22
|
+
module.exports = async function transform(source, options) {
|
|
23
|
+
/**
|
|
24
|
+
* @type {import("@babel/core").FileResult}
|
|
25
|
+
*/
|
|
8
26
|
let result;
|
|
9
27
|
try {
|
|
10
|
-
result = await
|
|
28
|
+
result = await babelTransform(source, options);
|
|
11
29
|
} catch (err) {
|
|
12
30
|
throw err.message && err.codeFrame ? new LoaderError(err) : err;
|
|
13
31
|
}
|
|
@@ -36,7 +54,12 @@ module.exports = async function (source, options) {
|
|
|
36
54
|
metadata,
|
|
37
55
|
sourceType,
|
|
38
56
|
// Convert it from a Set to an Array to make it JSON-serializable.
|
|
39
|
-
externalDependencies: Array.from(externalDependencies || [],
|
|
57
|
+
externalDependencies: Array.from(externalDependencies || [],
|
|
58
|
+
/**
|
|
59
|
+
* @param {string} dep
|
|
60
|
+
* @returns {[string, number?]}
|
|
61
|
+
*/
|
|
62
|
+
dep => [dep]).sort()
|
|
40
63
|
};
|
|
41
64
|
};
|
|
42
65
|
module.exports.version = babel.version;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "babel-loader",
|
|
3
|
-
"version": "10.
|
|
3
|
+
"version": "10.1.0",
|
|
4
4
|
"description": "babel module loader for webpack",
|
|
5
5
|
"files": [
|
|
6
6
|
"lib"
|
|
@@ -13,14 +13,24 @@
|
|
|
13
13
|
"find-up": "^5.0.0"
|
|
14
14
|
},
|
|
15
15
|
"peerDependencies": {
|
|
16
|
-
"@babel/core": "^7.12.0",
|
|
16
|
+
"@babel/core": "^7.12.0 || ^8.0.0-beta.1",
|
|
17
|
+
"@rspack/core": "^1.0.0 || ^2.0.0-0",
|
|
17
18
|
"webpack": ">=5.61.0"
|
|
18
19
|
},
|
|
20
|
+
"peerDependenciesMeta": {
|
|
21
|
+
"@rspack/core": {
|
|
22
|
+
"optional": true
|
|
23
|
+
},
|
|
24
|
+
"webpack": {
|
|
25
|
+
"optional": true
|
|
26
|
+
}
|
|
27
|
+
},
|
|
19
28
|
"devDependencies": {
|
|
20
|
-
"@babel/cli": "^
|
|
21
|
-
"@babel/core": "^
|
|
22
|
-
"@babel/eslint-parser": "^
|
|
23
|
-
"@babel/preset-env": "^
|
|
29
|
+
"@babel/cli": "^8.0.0-beta.1",
|
|
30
|
+
"@babel/core": "^8.0.0-beta.1",
|
|
31
|
+
"@babel/eslint-parser": "^8.0.0-beta.1",
|
|
32
|
+
"@babel/preset-env": "^8.0.0-beta.1",
|
|
33
|
+
"@rspack/core": "^1.7.5",
|
|
24
34
|
"c8": "^10.1.2",
|
|
25
35
|
"eslint": "^9.6.0",
|
|
26
36
|
"eslint-config-prettier": "^9.1.0",
|
|
@@ -29,6 +39,7 @@
|
|
|
29
39
|
"husky": "^9.1.5",
|
|
30
40
|
"lint-staged": "^15.2.9",
|
|
31
41
|
"prettier": "^3.0.0",
|
|
42
|
+
"typescript": "^5.8.3",
|
|
32
43
|
"webpack": "^5.93.0"
|
|
33
44
|
},
|
|
34
45
|
"scripts": {
|
|
@@ -38,7 +49,7 @@
|
|
|
38
49
|
"lint": "eslint src test",
|
|
39
50
|
"precommit": "lint-staged",
|
|
40
51
|
"prepublish": "yarn run clean && yarn run build",
|
|
41
|
-
"preversion": "yarn run test",
|
|
52
|
+
"preversion": "yarn tsc && yarn run test",
|
|
42
53
|
"test": "yarn run lint && yarn run build --source-maps && c8 yarn run test-only",
|
|
43
54
|
"test-only": "node --test test/**/*.test.js"
|
|
44
55
|
},
|
|
@@ -98,4 +109,4 @@
|
|
|
98
109
|
]
|
|
99
110
|
},
|
|
100
111
|
"packageManager": "yarn@3.6.4"
|
|
101
|
-
}
|
|
112
|
+
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
For changes in version v7.0.0 and up please go to [release](https://github.com/babel/babel-loader/releases)
|
|
4
|
-
|
|
5
|
-
# Old Changelog
|
|
6
|
-
|
|
7
|
-
## v6.4.1
|
|
8
|
-
|
|
9
|
-
### 🐛 Bug Fix
|
|
10
|
-
|
|
11
|
-
- Fixed reset of BABEL_ENV when options.forceEnv is used (#420) @nikopavlica
|
|
12
|
-
|
|
13
|
-
## v6.4.0
|
|
14
|
-
|
|
15
|
-
### 🚀 New Feature
|
|
16
|
-
|
|
17
|
-
- added metadata passing from babel to webpack, which is currently used by react-intl (#398) @Ognian
|
|
18
|
-
|
|
19
|
-
## v6.3.2
|
|
20
|
-
|
|
21
|
-
### 😢 Regression
|
|
22
|
-
|
|
23
|
-
- `forceEnv` was interfering with regular environment handling
|
|
24
|
-
|
|
25
|
-
## v6.3.1
|
|
26
|
-
|
|
27
|
-
### 🐛 Bug Fix
|
|
28
|
-
|
|
29
|
-
- The new `forceEnv` options wasn't working as expected (#379) @chrisvasz
|
|
30
|
-
|
|
31
|
-
## v6.3.0
|
|
32
|
-
|
|
33
|
-
### 🚀 New Feature
|
|
34
|
-
|
|
35
|
-
- Add new config option `forceEnv` (#368) @moimael
|
|
36
|
-
|
|
37
|
-
Allow to override BABEL_ENV/NODE_ENV at loader-level. Useful for isomorphic applications which have separate babel config for client and server.
|
|
38
|
-
|
|
39
|
-
### 🐛 Bug Fix
|
|
40
|
-
|
|
41
|
-
- Update loader-utils dependency to ^0.2.16 to fix compatibility with webpack 2 (#371) @leonaves
|
|
42
|
-
|
|
43
|
-
### 💅 Polish
|
|
44
|
-
|
|
45
|
-
- Improve FS caching to do less sync calls which improves performance slightly (#375) @akx
|
|
46
|
-
|
|
47
|
-
## v6.2.10
|
|
48
|
-
|
|
49
|
-
Support for webpack 2.2-rc has been added in this release
|
|
50
|
-
|
|
51
|
-
### 🐛 Bug Fix
|
|
52
|
-
|
|
53
|
-
- If cache directory not writable, try to fallback to tmpdir before failing
|
|
54
|
-
|
|
55
|
-
## v6.2.9
|
|
56
|
-
|
|
57
|
-
### 😢 Regression
|
|
58
|
-
|
|
59
|
-
Source maps on windows did not work correctly with v6.2.8.
|
|
60
|
-
Thanks @josephst
|
|
61
|
-
|
|
62
|
-
### 🏠 Internal
|
|
63
|
-
|
|
64
|
-
- Add AppVeyor to run tests on windows @danez
|
|
65
|
-
- Fix tests on windows (#343) @danez
|
|
66
|
-
|
|
67
|
-
## v6.2.8
|
|
68
|
-
|
|
69
|
-
### 🐛 Bug Fix
|
|
70
|
-
|
|
71
|
-
- gzipped files should have `.gz` as the extension, not `.gzip` (#326) @bjornstar
|
|
72
|
-
- fix options.sourceFileName gennerate bug (#260) @creeperyang
|
|
73
|
-
|
|
74
|
-
### 📝 Documentation
|
|
75
|
-
|
|
76
|
-
- Update README docs for cacheDirectory's actual behaviour (#245) @sohkai
|
|
77
|
-
- updates docs re: transform-runtime (#197) @gbrassey
|
|
78
|
-
|
|
79
|
-
### 🏠 Internal
|
|
80
|
-
|
|
81
|
-
- Use eslint and nyc (#321) @danez
|
|
82
|
-
- Adjust travis config (#320) @danez
|
|
83
|
-
- Use babel to compile babel-loader (#319) @danez
|
|
84
|
-
|
|
85
|
-
## v6.2.7
|
|
86
|
-
|
|
87
|
-
### 😢 Regression
|
|
88
|
-
|
|
89
|
-
Fallback to `os.tmpdir()` if no cachedir found (#318) (fixes #317) @danez
|
|
90
|
-
|
|
91
|
-
Fixes an issue with v6.2.6 when using `babel-loader` as a global package.
|
|
92
|
-
|
|
93
|
-
## v6.2.6
|
|
94
|
-
|
|
95
|
-
### 🐛 Bug Fix
|
|
96
|
-
|
|
97
|
-
- Use standard cache dir as default `cacheDirectory` (#301) @fson
|
|
98
|
-
|
|
99
|
-
Use the common cache directory, `./node_modules/.cache/babel-loader`, as the default cache directory (when the cacheDirectory setting is enabled).
|
|
100
|
-
|
|
101
|
-
```js
|
|
102
|
-
query: {
|
|
103
|
-
cacheDirectory: true
|
|
104
|
-
}
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
## v6.2.5
|
|
108
|
-
|
|
109
|
-
- Don't show the call stack for a Babel error (such as when you have a syntax error)
|
|
110
|
-
|
|
111
|
-
<img width="415" alt="screenshot 2016-08-15 15 24 37" src="https://cloud.githubusercontent.com/assets/30594/17664401/727ba098-62fc-11e6-9f12-42da0cf47f14.png">
|
|
112
|
-
|
|
113
|
-
- resolve the .babelrc relative to the file path rather than the cwd (current working directory).
|
|
114
|
-
|
|
115
|
-
* fix: more concise formatting for Babel errors (#287) (Andrey Popp)
|
|
116
|
-
* fix(resolve-rc): resolve-rc relative file path (#253) (Luke Page)
|
|
117
|
-
* add babel-core and preset-2015 to dev dependencies (#273) (timse)
|
|
118
|
-
* chore(docs): add issue and pr templates (#280) (Joshua Wiens)
|
|
119
|
-
* chore(docs): fix badge formatting (Joshua Wiens)
|
|
120
|
-
* chore(ci): expand travis testing (#278) (Joshua Wiens)
|
|
121
|
-
* Update README: add env vars to cacheIdentifier (#267) (Dominik Ferber)
|
|
122
|
-
* add npm badge [skip ci] (Henry Zhu)
|
|
123
|
-
* update [skip ci] (Henry Zhu)
|
|
124
|
-
* remove jsx references as well [skip ci] (Henry Zhu)
|
|
125
|
-
* Save the transform to devDependencies (Ray Booysen)
|
|
126
|
-
* Remove 'react' preset (Jake Rios)
|
|
127
|
-
* Removed babel-preset-react from README.md (Ben Stephenson)
|
|
128
|
-
|
|
129
|
-
## v6.2.4
|
|
130
|
-
* change allowed peer deps (all webpack 2 beta versions)
|
|
131
|
-
|
|
132
|
-
## v6.2.3
|
|
133
|
-
* change allowed peer deps (2.0.7-beta)
|
|
134
|
-
|
|
135
|
-
## v6.2.2
|
|
136
|
-
* Update peerDependencies to accept webpack 2 [#208](https://github.com/babel/babel-loader/pull/208)
|
|
137
|
-
* Remove duplicated dependencies
|
|
138
|
-
|
|
139
|
-
## v6.2.0
|
|
140
|
-
* Pass true filenames [#106](https://github.com/babel/babel-loader/issues/106)
|
|
141
|
-
* Remove babel-core from devDependencies
|
|
142
|
-
|
|
143
|
-
## v6.1.0
|
|
144
|
-
|
|
145
|
-
* Merge [PR #146](https://github.com/babel/babel-loader/pull/146) Set source file name relative to options.sourceRoot
|
|
146
|
-
* Merge [PR #136](https://github.com/babel/babel-loader/pull/136) use container-based infrastructure for faster build
|
|
147
|
-
* Merge [PR #121](https://github.com/babel/babel-loader/pull/121) Make babelrc configurable
|
|
148
|
-
* Merge [PR #113](https://github.com/babel/babel-loader/pull/113) Include BABEL_ENV || NODE_ENV in cacheIdentifier
|
|
149
|
-
|
|
150
|
-
## v6.0.1
|
|
151
|
-
|
|
152
|
-
* Update to babel v6.
|
|
153
|
-
|
|
154
|
-
## v5.3.1
|
|
155
|
-
|
|
156
|
-
* Merge [PR #85](https://github.com/babel/babel-loader/pull/85) - Don't override sourcemap if sourcesContent already exists.
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
## v5.3.1
|
|
160
|
-
|
|
161
|
-
* Merge [PR #82](https://github.com/babel/babel-loader/pull/82) - Fallback global options to empty object to avoid conflicts with object-assign polyfill.
|
|
162
|
-
|
|
163
|
-
## v5.3.0
|
|
164
|
-
|
|
165
|
-
* Merge [PR #79](https://github.com/babel/babel-loader/pull/79) - This should allow babel-loader to work with [enhanced-require](https://github.com/webpack/enhanced-require).
|
|
166
|
-
|
|
167
|
-
## v5.2.0
|
|
168
|
-
|
|
169
|
-
* Include `.babelrc` file into the `cacheIdentifier` if it exists
|