babel-loader 10.0.0 → 10.1.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/README.md +1 -1
- package/lib/Error.js +13 -0
- package/lib/cache.js +83 -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 +18 -7
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
|
*
|
|
@@ -26,6 +27,27 @@ const {
|
|
|
26
27
|
} = process;
|
|
27
28
|
const transform = require("./transform");
|
|
28
29
|
const serialize = require("./serialize");
|
|
30
|
+
/**
|
|
31
|
+
* @typedef {object} FileSystemInfoEntry
|
|
32
|
+
* @property {number} safeTime
|
|
33
|
+
* @property {number} timestamp
|
|
34
|
+
*/
|
|
35
|
+
/**
|
|
36
|
+
* @typedef {object} WebpackLogger
|
|
37
|
+
* @property {function(string): void} debug
|
|
38
|
+
* @property {function(string): void} info
|
|
39
|
+
* @property {function(string): void} warn
|
|
40
|
+
* @property {function(string): void} error
|
|
41
|
+
*/
|
|
42
|
+
/**
|
|
43
|
+
* @typedef {object} WebpackHash
|
|
44
|
+
* @property {(data: string | Buffer, inputEncoding?: string) => WebpackHash} update
|
|
45
|
+
* @property {(encoding?: string) => string | Buffer} digest
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @type {string | null}
|
|
50
|
+
*/
|
|
29
51
|
let defaultCacheDirectory = null;
|
|
30
52
|
const gunzip = promisify(zlib.gunzip);
|
|
31
53
|
const gzip = promisify(zlib.gzip);
|
|
@@ -34,8 +56,8 @@ const gzip = promisify(zlib.gzip);
|
|
|
34
56
|
* Read the contents from the compressed file.
|
|
35
57
|
*
|
|
36
58
|
* @async
|
|
37
|
-
* @
|
|
38
|
-
* @
|
|
59
|
+
* @param {string} filename
|
|
60
|
+
* @param {boolean} compress
|
|
39
61
|
*/
|
|
40
62
|
const read = async function (filename, compress) {
|
|
41
63
|
const data = await readFile(filename + (compress ? ".gz" : ""));
|
|
@@ -45,11 +67,10 @@ const read = async function (filename, compress) {
|
|
|
45
67
|
|
|
46
68
|
/**
|
|
47
69
|
* Write contents into a compressed file.
|
|
48
|
-
*
|
|
49
70
|
* @async
|
|
50
|
-
* @
|
|
51
|
-
* @
|
|
52
|
-
* @
|
|
71
|
+
* @param {string} filename
|
|
72
|
+
* @param {boolean} compress
|
|
73
|
+
* @param {any} result
|
|
53
74
|
*/
|
|
54
75
|
const write = async function (filename, compress, result) {
|
|
55
76
|
const content = JSON.stringify(result);
|
|
@@ -59,16 +80,23 @@ const write = async function (filename, compress, result) {
|
|
|
59
80
|
|
|
60
81
|
/**
|
|
61
82
|
* Build the filename for the cached file
|
|
62
|
-
*
|
|
63
|
-
* @
|
|
64
|
-
* @
|
|
65
|
-
*
|
|
66
|
-
* @return {
|
|
83
|
+
* @param {string} source File source code
|
|
84
|
+
* @param {string} identifier Unique identifier to bust cache
|
|
85
|
+
* @param {Object} options Options used
|
|
86
|
+
* @param {WebpackHash} hash Hash function returned by `LoaderContext.utils.createHash`
|
|
87
|
+
* @return {string}
|
|
67
88
|
*/
|
|
68
89
|
const filename = function (source, identifier, options, hash) {
|
|
69
90
|
hash.update(serialize([options, source, identifier]));
|
|
70
91
|
return hash.digest("hex") + ".json";
|
|
71
92
|
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Add timestamps to external dependencies.
|
|
96
|
+
* @async
|
|
97
|
+
* @param {import("./transform").TransformResult["externalDependencies"]} externalDependencies
|
|
98
|
+
* @param {(filename: string) => Promise<FileSystemInfoEntry>} getFileTimestamp
|
|
99
|
+
*/
|
|
72
100
|
const addTimestamps = async function (externalDependencies, getFileTimestamp) {
|
|
73
101
|
for (const depAndEmptyTimestamp of externalDependencies) {
|
|
74
102
|
try {
|
|
@@ -82,6 +110,14 @@ const addTimestamps = async function (externalDependencies, getFileTimestamp) {
|
|
|
82
110
|
}
|
|
83
111
|
}
|
|
84
112
|
};
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Check if any external dependencies have been modified.
|
|
116
|
+
* @async
|
|
117
|
+
* @param {import("./transform").TransformResult["externalDependencies"]} externalDepsWithTimestamp
|
|
118
|
+
* @param {(filename: string) => Promise<FileSystemInfoEntry>} getFileTimestamp
|
|
119
|
+
* @returns {Promise<boolean>}
|
|
120
|
+
*/
|
|
85
121
|
const areExternalDependenciesModified = async function (externalDepsWithTimestamp, getFileTimestamp) {
|
|
86
122
|
for (const depAndTimestamp of externalDepsWithTimestamp) {
|
|
87
123
|
const [dep, timestamp] = depAndTimestamp;
|
|
@@ -100,9 +136,18 @@ const areExternalDependenciesModified = async function (externalDepsWithTimestam
|
|
|
100
136
|
|
|
101
137
|
/**
|
|
102
138
|
* Handle the cache
|
|
103
|
-
*
|
|
104
|
-
* @
|
|
105
|
-
* @
|
|
139
|
+
* @async
|
|
140
|
+
* @param {string} directory
|
|
141
|
+
* @param {Object} params
|
|
142
|
+
* @param {string} params.source The source code to transform.
|
|
143
|
+
* @param {import(".").NormalizedOptions} [params.options] Options used for transformation.
|
|
144
|
+
* @param {string} params.cacheIdentifier Unique identifier to bust cache.
|
|
145
|
+
* @param {string} [params.cacheDirectory] Directory to store cached files.
|
|
146
|
+
* @param {boolean} [params.cacheCompression] Whether to compress cached files.
|
|
147
|
+
* @param {WebpackHash} params.hash Hash function to use for the cache filename.
|
|
148
|
+
* @param {(filename: string) => Promise<FileSystemInfoEntry>} params.getFileTimestamp - Function to get file timestamps.
|
|
149
|
+
* @param {WebpackLogger} params.logger
|
|
150
|
+
* @returns {Promise<null | import("./transform").TransformResult>}
|
|
106
151
|
*/
|
|
107
152
|
const handleCache = async function (directory, params) {
|
|
108
153
|
const {
|
|
@@ -127,7 +172,7 @@ const handleCache = async function (directory, params) {
|
|
|
127
172
|
}
|
|
128
173
|
logger.debug(`discarded cache file '${file}' due to changes in external dependencies`);
|
|
129
174
|
} catch {
|
|
130
|
-
//
|
|
175
|
+
// continue if cache can't be read
|
|
131
176
|
logger.debug(`discarded cache as it can not be read`);
|
|
132
177
|
}
|
|
133
178
|
const fallback = typeof cacheDirectory !== "string" && directory !== os.tmpdir();
|
|
@@ -150,6 +195,10 @@ const handleCache = async function (directory, params) {
|
|
|
150
195
|
// return it to the user asap and write it in cache
|
|
151
196
|
logger.debug(`applying Babel transform`);
|
|
152
197
|
const result = await transform(source, options);
|
|
198
|
+
if (!result) {
|
|
199
|
+
logger.debug(`no result from Babel transform, skipping cache write`);
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
153
202
|
await addTimestamps(result.externalDependencies, getFileTimestamp);
|
|
154
203
|
try {
|
|
155
204
|
logger.debug(`writing result to cache file '${file}'`);
|
|
@@ -166,14 +215,17 @@ const handleCache = async function (directory, params) {
|
|
|
166
215
|
|
|
167
216
|
/**
|
|
168
217
|
* Retrieve file from cache, or create a new one for future reads
|
|
169
|
-
*
|
|
170
218
|
* @async
|
|
171
|
-
* @param
|
|
172
|
-
* @param
|
|
173
|
-
* @param
|
|
174
|
-
* @param
|
|
175
|
-
* @param
|
|
176
|
-
* @param
|
|
219
|
+
* @param {object} params
|
|
220
|
+
* @param {string} params.cacheDirectory Directory to store cached files.
|
|
221
|
+
* @param {string} params.cacheIdentifier Unique identifier to bust cache.
|
|
222
|
+
* @param {boolean} params.cacheCompression Whether compressing cached files.
|
|
223
|
+
* @param {string} params.source Original contents of the file to be cached.
|
|
224
|
+
* @param {import(".").NormalizedOptions} params.options Options to be given to the transform function.
|
|
225
|
+
* @param {function} params.transform Transform function to apply to the file.
|
|
226
|
+
* @param {WebpackHash} params.hash Hash function to use for the cache filename.
|
|
227
|
+
* @param {function(string): Promise<FileSystemInfoEntry>} params.getFileTimestamp Function to get file timestamps.
|
|
228
|
+
* @param {WebpackLogger} params.logger Logger instance.
|
|
177
229
|
*
|
|
178
230
|
* @example
|
|
179
231
|
*
|
|
@@ -189,7 +241,7 @@ const handleCache = async function (directory, params) {
|
|
|
189
241
|
* });
|
|
190
242
|
*/
|
|
191
243
|
|
|
192
|
-
module.exports = async function (params) {
|
|
244
|
+
module.exports = async function cache(params) {
|
|
193
245
|
let directory;
|
|
194
246
|
if (typeof params.cacheDirectory === "string") {
|
|
195
247
|
directory = params.cacheDirectory;
|
|
@@ -199,13 +251,19 @@ module.exports = async function (params) {
|
|
|
199
251
|
}
|
|
200
252
|
return await handleCache(directory, params);
|
|
201
253
|
};
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Find the cache directory for babel-loader.
|
|
257
|
+
* @param {string} name "babel-loader"
|
|
258
|
+
* @returns {string}
|
|
259
|
+
*/
|
|
202
260
|
function findCacheDir(name) {
|
|
203
261
|
if (env.CACHE_DIR && !["true", "false", "1", "0"].includes(env.CACHE_DIR)) {
|
|
204
262
|
return path.join(env.CACHE_DIR, name);
|
|
205
263
|
}
|
|
206
|
-
const rootPkgJSONPath =
|
|
264
|
+
const rootPkgJSONPath = findUpSync("package.json");
|
|
207
265
|
if (rootPkgJSONPath) {
|
|
208
|
-
return path.join(rootPkgJSONPath, "node_modules", ".cache", name);
|
|
266
|
+
return path.join(path.dirname(rootPkgJSONPath), "node_modules", ".cache", name);
|
|
209
267
|
}
|
|
210
268
|
return os.tmpdir();
|
|
211
269
|
}
|
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.1",
|
|
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
|
},
|