@rspack/core 0.1.7 → 0.1.9

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.
Files changed (89) hide show
  1. package/dist/RuleSetCompiler.d.ts +6 -0
  2. package/dist/RuleSetCompiler.d.ts.map +1 -0
  3. package/dist/RuleSetCompiler.js +10 -0
  4. package/dist/RuleSetCompiler.js.map +1 -0
  5. package/dist/compilation.d.ts +5 -0
  6. package/dist/compilation.d.ts.map +1 -1
  7. package/dist/compilation.js +11 -2
  8. package/dist/compilation.js.map +1 -1
  9. package/dist/compiler.d.ts +11 -0
  10. package/dist/compiler.d.ts.map +1 -1
  11. package/dist/compiler.js +28 -5
  12. package/dist/compiler.js.map +1 -1
  13. package/dist/config/adapter-rule-use.d.ts +64 -6
  14. package/dist/config/adapter-rule-use.d.ts.map +1 -1
  15. package/dist/config/adapter-rule-use.js +45 -392
  16. package/dist/config/adapter-rule-use.js.map +1 -1
  17. package/dist/config/adapter.d.ts +4 -2
  18. package/dist/config/adapter.d.ts.map +1 -1
  19. package/dist/config/adapter.js +69 -9
  20. package/dist/config/adapter.js.map +1 -1
  21. package/dist/config/builtins.d.ts +14 -0
  22. package/dist/config/builtins.d.ts.map +1 -1
  23. package/dist/config/builtins.js +24 -0
  24. package/dist/config/builtins.js.map +1 -1
  25. package/dist/config/defaults.d.ts.map +1 -1
  26. package/dist/config/defaults.js +92 -14
  27. package/dist/config/defaults.js.map +1 -1
  28. package/dist/config/normalization.d.ts.map +1 -1
  29. package/dist/config/normalization.js +13 -1
  30. package/dist/config/normalization.js.map +1 -1
  31. package/dist/config/schema.check.d.ts.map +1 -1
  32. package/dist/config/schema.check.js +1555 -1120
  33. package/dist/config/schema.check.js.map +1 -1
  34. package/dist/config/schema.d.ts +1001 -887
  35. package/dist/config/schema.js +91 -0
  36. package/dist/config/schema.js.map +1 -1
  37. package/dist/config/types.d.ts +49 -3
  38. package/dist/config/types.d.ts.map +1 -1
  39. package/dist/lib/Cache.d.ts +72 -0
  40. package/dist/lib/Cache.d.ts.map +1 -0
  41. package/dist/lib/Cache.js +131 -0
  42. package/dist/lib/Cache.js.map +1 -0
  43. package/dist/lib/CacheFacade.d.ts +192 -0
  44. package/dist/lib/CacheFacade.d.ts.map +1 -0
  45. package/dist/lib/CacheFacade.js +320 -0
  46. package/dist/lib/CacheFacade.js.map +1 -0
  47. package/dist/lib/HookWebpackError.d.ts +47 -0
  48. package/dist/lib/HookWebpackError.d.ts.map +1 -0
  49. package/dist/lib/HookWebpackError.js +83 -0
  50. package/dist/lib/HookWebpackError.js.map +1 -0
  51. package/dist/lib/WebpackError.d.ts +24 -0
  52. package/dist/lib/WebpackError.d.ts.map +1 -0
  53. package/dist/lib/WebpackError.js +35 -0
  54. package/dist/lib/WebpackError.js.map +1 -0
  55. package/dist/lib/cache/getLazyHashedEtag.d.ts +36 -0
  56. package/dist/lib/cache/getLazyHashedEtag.d.ts.map +1 -0
  57. package/dist/lib/cache/getLazyHashedEtag.js +74 -0
  58. package/dist/lib/cache/getLazyHashedEtag.js.map +1 -0
  59. package/dist/lib/cache/mergeEtags.d.ts +12 -0
  60. package/dist/lib/cache/mergeEtags.d.ts.map +1 -0
  61. package/dist/lib/cache/mergeEtags.js +72 -0
  62. package/dist/lib/cache/mergeEtags.js.map +1 -0
  63. package/dist/loader-runner/LoaderLoadingError.d.ts +5 -0
  64. package/dist/loader-runner/LoaderLoadingError.d.ts.map +1 -0
  65. package/dist/loader-runner/LoaderLoadingError.js +19 -0
  66. package/dist/loader-runner/LoaderLoadingError.js.map +1 -0
  67. package/dist/loader-runner/index.d.ts +18 -0
  68. package/dist/loader-runner/index.d.ts.map +1 -0
  69. package/dist/loader-runner/index.js +667 -0
  70. package/dist/loader-runner/index.js.map +1 -0
  71. package/dist/loader-runner/loadLoader.d.ts +3 -0
  72. package/dist/loader-runner/loadLoader.d.ts.map +1 -0
  73. package/dist/loader-runner/loadLoader.js +69 -0
  74. package/dist/loader-runner/loadLoader.js.map +1 -0
  75. package/dist/stats.d.ts.map +1 -1
  76. package/dist/stats.js +2 -2
  77. package/dist/stats.js.map +1 -1
  78. package/dist/util/fake.d.ts +1 -0
  79. package/dist/util/fake.d.ts.map +1 -1
  80. package/dist/util/fake.js +1 -0
  81. package/dist/util/fake.js.map +1 -1
  82. package/dist/util/index.d.ts +3 -0
  83. package/dist/util/index.d.ts.map +1 -1
  84. package/dist/util/index.js +28 -1
  85. package/dist/util/index.js.map +1 -1
  86. package/dist/web/ResolveSwcPlugin.d.ts.map +1 -1
  87. package/dist/web/ResolveSwcPlugin.js +5 -0
  88. package/dist/web/ResolveSwcPlugin.js.map +1 -1
  89. package/package.json +11 -10
@@ -0,0 +1,667 @@
1
+ "use strict";
2
+ /**
3
+ * The following code is modified based on
4
+ * https://github.com/webpack/loader-runner
5
+ *
6
+ * MIT Licensed
7
+ * Author Tobias Koppers @sokra
8
+ * Copyright (c) JS Foundation and other contributors
9
+ * https://github.com/webpack/loader-runner/blob/main/LICENSE
10
+ */
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.runLoader = exports.parsePathQueryFragment = void 0;
13
+ const webpack_sources_1 = require("webpack-sources");
14
+ const adapter_rule_use_1 = require("../config/adapter-rule-use");
15
+ const util_1 = require("../util");
16
+ const identifier_1 = require("../util/identifier");
17
+ const memoize_1 = require("../util/memoize");
18
+ const createHash_1 = require("../util/createHash");
19
+ const loadLoader = require("./loadLoader");
20
+ const PATH_QUERY_FRAGMENT_REGEXP = /^((?:\0.|[^?#\0])*)(\?(?:\0.|[^#\0])*)?(#.*)?$/;
21
+ function parsePathQueryFragment(str) {
22
+ let match = PATH_QUERY_FRAGMENT_REGEXP.exec(str);
23
+ return {
24
+ path: (match === null || match === void 0 ? void 0 : match[1].replace(/\0(.)/g, "$1")) || "",
25
+ query: (match === null || match === void 0 ? void 0 : match[2]) ? match[2].replace(/\0(.)/g, "$1") : "",
26
+ fragment: (match === null || match === void 0 ? void 0 : match[3]) || ""
27
+ };
28
+ }
29
+ exports.parsePathQueryFragment = parsePathQueryFragment;
30
+ function dirname(path) {
31
+ if (path === "/")
32
+ return "/";
33
+ var i = path.lastIndexOf("/");
34
+ var j = path.lastIndexOf("\\");
35
+ var i2 = path.indexOf("/");
36
+ var j2 = path.indexOf("\\");
37
+ var idx = i > j ? i : j;
38
+ var idx2 = i > j ? i2 : j2;
39
+ if (idx < 0)
40
+ return path;
41
+ if (idx === idx2)
42
+ return path.slice(0, idx + 1);
43
+ return path.slice(0, idx);
44
+ }
45
+ function stringifyLoaderObject(o) {
46
+ return o.path + o.query + o.fragment;
47
+ }
48
+ function createLoaderObject(loader, compiler) {
49
+ const obj = {
50
+ path: null,
51
+ query: null,
52
+ fragment: null,
53
+ options: null,
54
+ ident: null,
55
+ normal: null,
56
+ pitch: null,
57
+ raw: null,
58
+ data: null,
59
+ pitchExecuted: false,
60
+ normalExecuted: false
61
+ };
62
+ Object.defineProperty(obj, "request", {
63
+ enumerable: true,
64
+ get: function () {
65
+ return (obj.path.replace(/#/g, "\0#") +
66
+ obj.query.replace(/#/g, "\0#") +
67
+ obj.fragment);
68
+ },
69
+ set: function (value) {
70
+ if (typeof value === "string") {
71
+ let splittedRequest = parsePathQueryFragment(value);
72
+ obj.path = splittedRequest.path;
73
+ obj.query = splittedRequest.query;
74
+ obj.fragment = splittedRequest.fragment;
75
+ if (obj.query.startsWith("??")) {
76
+ const ident = obj.query.slice(2);
77
+ if (ident === "[[missing ident]]") {
78
+ throw new Error("No ident is provided by referenced loader. " +
79
+ "When using a function for Rule.use in config you need to " +
80
+ "provide an 'ident' property for referenced loader options.");
81
+ }
82
+ obj.options = compiler.ruleSet.references.get(ident);
83
+ if (obj.options === undefined) {
84
+ throw new Error("Invalid ident is provided by referenced loader");
85
+ }
86
+ obj.ident = ident;
87
+ }
88
+ else {
89
+ obj.options = undefined;
90
+ obj.ident = undefined;
91
+ }
92
+ }
93
+ else {
94
+ if (!value.loader)
95
+ throw new Error("request should be a string or object with loader and options (" +
96
+ JSON.stringify(value) +
97
+ ")");
98
+ obj.path = value.loader;
99
+ obj.fragment = value.fragment || "";
100
+ obj.type = value.type;
101
+ obj.options = value.options;
102
+ obj.ident = value.ident;
103
+ if (obj.options === null)
104
+ obj.query = "";
105
+ else if (obj.options === undefined)
106
+ obj.query = "";
107
+ else if (typeof obj.options === "string")
108
+ obj.query = "?" + obj.options;
109
+ else if (obj.ident)
110
+ obj.query = "??" + obj.ident;
111
+ else if (typeof obj.options === "object" && obj.options.ident)
112
+ obj.query = "??" + obj.options.ident;
113
+ else
114
+ obj.query = "?" + JSON.stringify(obj.options);
115
+ }
116
+ }
117
+ });
118
+ obj.request = loader;
119
+ if (Object.preventExtensions) {
120
+ Object.preventExtensions(obj);
121
+ }
122
+ return obj;
123
+ }
124
+ function getCurrentLoader(loaderContext, index = loaderContext.loaderIndex) {
125
+ if (loaderContext.loaders &&
126
+ loaderContext.loaders.length &&
127
+ index < loaderContext.loaders.length &&
128
+ index >= 0 &&
129
+ loaderContext.loaders[index]) {
130
+ return loaderContext.loaders[index];
131
+ }
132
+ return null;
133
+ }
134
+ async function runLoader(rawContext, compiler) {
135
+ var _a;
136
+ const resource = rawContext.resource;
137
+ const loaderContext = {};
138
+ //
139
+ const splittedResource = parsePathQueryFragment(resource);
140
+ const resourcePath = splittedResource.path;
141
+ const resourceQuery = splittedResource.query;
142
+ const resourceFragment = splittedResource.fragment;
143
+ const contextDirectory = dirname(resourcePath);
144
+ // execution state
145
+ let isPitching = rawContext.isPitching;
146
+ let cacheable = true;
147
+ let fileDependencies = rawContext.fileDependencies.slice();
148
+ let contextDependencies = rawContext.contextDependencies.slice();
149
+ let missingDependencies = rawContext.missingDependencies.slice();
150
+ let buildDependencies = rawContext.buildDependencies.slice();
151
+ let assetFilenames = rawContext.assetFilenames.slice();
152
+ const loaders = rawContext.currentLoader
153
+ .split("$")
154
+ .map(loader => createLoaderObject(loader, compiler));
155
+ loaderContext.__internal__isPitching = isPitching;
156
+ loaderContext.context = contextDirectory;
157
+ loaderContext.loaderIndex = 0;
158
+ loaderContext.loaders = loaders;
159
+ loaderContext.resourcePath = resourcePath;
160
+ loaderContext.resourceQuery = resourceQuery;
161
+ loaderContext.resourceFragment = resourceFragment;
162
+ loaderContext.cacheable = function (flag) {
163
+ if (flag === false) {
164
+ cacheable = false;
165
+ }
166
+ };
167
+ loaderContext.dependency = loaderContext.addDependency =
168
+ function addDependency(file) {
169
+ fileDependencies.push(file);
170
+ };
171
+ loaderContext.addContextDependency = function addContextDependency(context) {
172
+ contextDependencies.push(context);
173
+ };
174
+ loaderContext.addMissingDependency = function addMissingDependency(context) {
175
+ missingDependencies.push(context);
176
+ };
177
+ loaderContext.getDependencies = function getDependencies() {
178
+ return fileDependencies.slice();
179
+ };
180
+ loaderContext.getContextDependencies = function getContextDependencies() {
181
+ return contextDependencies.slice();
182
+ };
183
+ loaderContext.getMissingDependencies = function getMissingDependencies() {
184
+ return missingDependencies.slice();
185
+ };
186
+ loaderContext.clearDependencies = function clearDependencies() {
187
+ fileDependencies.length = 0;
188
+ contextDependencies.length = 0;
189
+ missingDependencies.length = 0;
190
+ cacheable = true;
191
+ };
192
+ Object.defineProperty(loaderContext, "resource", {
193
+ enumerable: true,
194
+ get: function () {
195
+ if (loaderContext.resourcePath === undefined)
196
+ return undefined;
197
+ return (loaderContext.resourcePath.replace(/#/g, "\0#") +
198
+ loaderContext.resourceQuery.replace(/#/g, "\0#") +
199
+ loaderContext.resourceFragment);
200
+ },
201
+ set: function (value) {
202
+ var splittedResource = value && parsePathQueryFragment(value);
203
+ loaderContext.resourcePath = splittedResource
204
+ ? splittedResource.path
205
+ : undefined;
206
+ loaderContext.resourceQuery = splittedResource
207
+ ? splittedResource.query
208
+ : undefined;
209
+ loaderContext.resourceFragment = splittedResource
210
+ ? splittedResource.fragment
211
+ : undefined;
212
+ }
213
+ });
214
+ Object.defineProperty(loaderContext, "request", {
215
+ enumerable: true,
216
+ get: function () {
217
+ return loaderContext.loaders
218
+ .map(function (o) {
219
+ return o.request;
220
+ })
221
+ .concat(loaderContext.resource || "")
222
+ .join("!");
223
+ }
224
+ });
225
+ Object.defineProperty(loaderContext, "remainingRequest", {
226
+ enumerable: true,
227
+ get: function () {
228
+ if (loaderContext.loaderIndex >= loaderContext.loaders.length - 1 &&
229
+ !loaderContext.resource)
230
+ return "";
231
+ return loaderContext.loaders
232
+ .slice(loaderContext.loaderIndex + 1)
233
+ .map(function (o) {
234
+ return o.request;
235
+ })
236
+ .concat(loaderContext.resource || "")
237
+ .join("!");
238
+ }
239
+ });
240
+ Object.defineProperty(loaderContext, "currentRequest", {
241
+ enumerable: true,
242
+ get: function () {
243
+ return loaderContext.loaders
244
+ .slice(loaderContext.loaderIndex)
245
+ .map(function (o) {
246
+ return o.request;
247
+ })
248
+ .concat(loaderContext.resource || "")
249
+ .join("!");
250
+ }
251
+ });
252
+ Object.defineProperty(loaderContext, "previousRequest", {
253
+ enumerable: true,
254
+ get: function () {
255
+ return loaderContext.loaders
256
+ .slice(0, loaderContext.loaderIndex)
257
+ .map(function (o) {
258
+ return o.request;
259
+ })
260
+ .join("!");
261
+ }
262
+ });
263
+ Object.defineProperty(loaderContext, "query", {
264
+ enumerable: true,
265
+ get: function () {
266
+ var entry = loaderContext.loaders[loaderContext.loaderIndex];
267
+ return entry.options && typeof entry.options === "object"
268
+ ? entry.options
269
+ : entry.query;
270
+ }
271
+ });
272
+ Object.defineProperty(loaderContext, "data", {
273
+ enumerable: true,
274
+ get: function () {
275
+ return loaderContext.loaders[loaderContext.loaderIndex].data;
276
+ }
277
+ });
278
+ loaderContext.version = 2;
279
+ loaderContext.sourceMap = compiler.options.devtool
280
+ ? (0, adapter_rule_use_1.isUseSourceMap)(compiler.options.devtool)
281
+ : false;
282
+ loaderContext.mode = compiler.options.mode;
283
+ loaderContext.hot = (_a = compiler.options.devServer) === null || _a === void 0 ? void 0 : _a.hot;
284
+ const getResolveContext = () => {
285
+ // FIXME: resolve's fileDependencies will includes lots of dir, '/', etc
286
+ return {
287
+ fileDependencies: {
288
+ // @ts-expect-error
289
+ add: d => {
290
+ // loaderContext.addDependency(d)
291
+ }
292
+ },
293
+ contextDependencies: {
294
+ // @ts-expect-error
295
+ add: d => {
296
+ // loaderContext.addContextDependency(d)
297
+ }
298
+ },
299
+ missingDependencies: {
300
+ // @ts-expect-error
301
+ add: d => {
302
+ // loaderContext.addMissingDependency(d)
303
+ }
304
+ }
305
+ };
306
+ };
307
+ const resolver = compiler.resolverFactory.get("normal");
308
+ loaderContext.resolve = function resolve(context, request, callback) {
309
+ resolver.resolve({}, context, request, getResolveContext(), callback);
310
+ };
311
+ // @ts-expect-error TODO
312
+ loaderContext.getResolve = function getResolve(options) {
313
+ const child = options ? resolver.withOptions(options) : resolver;
314
+ return (context, request, callback) => {
315
+ if (callback) {
316
+ child.resolve({}, context, request, getResolveContext(), callback);
317
+ }
318
+ else {
319
+ return new Promise((resolve, reject) => {
320
+ child.resolve({}, context, request, getResolveContext(), (err, result) => {
321
+ if (err)
322
+ reject(err);
323
+ else
324
+ resolve(result);
325
+ });
326
+ });
327
+ }
328
+ };
329
+ };
330
+ loaderContext.getLogger = function getLogger(name) {
331
+ return compiler.getInfrastructureLogger(() => [name, resource].filter(Boolean).join("|"));
332
+ };
333
+ loaderContext.rootContext = compiler.context;
334
+ loaderContext.emitError = function emitError(error) {
335
+ const title = "Module Error";
336
+ const message = error instanceof Error ? (0, util_1.concatErrorMsgAndStack)(error) : error;
337
+ compiler.compilation.pushDiagnostic("error", title, `${message}\n(from: ${stringifyLoaderObject(loaderContext.loaders[loaderContext.loaderIndex])})`);
338
+ };
339
+ loaderContext.emitWarning = function emitWarning(warning) {
340
+ const title = "Module Warning";
341
+ const message = warning instanceof Error ? (0, util_1.concatErrorMsgAndStack)(warning) : warning;
342
+ compiler.compilation.pushDiagnostic("warning", title, `${message}\n(from: ${stringifyLoaderObject(loaderContext.loaders[loaderContext.loaderIndex])})`);
343
+ };
344
+ loaderContext.emitFile = function emitFile(name, content, sourceMap, assetInfo) {
345
+ let source;
346
+ if (sourceMap) {
347
+ if (typeof sourceMap === "string" &&
348
+ (loaderContext.sourceMap ||
349
+ (compiler.options.devtool &&
350
+ (0, adapter_rule_use_1.isUseSimpleSourceMap)(compiler.options.devtool)))) {
351
+ source = new webpack_sources_1.OriginalSource(content, (0, identifier_1.makePathsRelative)(contextDirectory, sourceMap, compiler));
352
+ }
353
+ if (this.sourceMap) {
354
+ source = new webpack_sources_1.SourceMapSource(
355
+ // @ts-expect-error webpack-sources type declaration is wrong
356
+ content, name, (0, identifier_1.makePathsRelative)(contextDirectory, sourceMap, compiler));
357
+ }
358
+ }
359
+ else {
360
+ source = new webpack_sources_1.RawSource(
361
+ // @ts-expect-error webpack-sources type declaration is wrong
362
+ content);
363
+ }
364
+ assetFilenames.push(name),
365
+ // @ts-expect-error
366
+ compiler.compilation.emitAsset(name, source, assetInfo);
367
+ };
368
+ loaderContext.fs = compiler.inputFileSystem;
369
+ const getAbsolutify = (0, memoize_1.memoize)(() => identifier_1.absolutify.bindCache(compiler.root));
370
+ const getAbsolutifyInContext = (0, memoize_1.memoize)(() => identifier_1.absolutify.bindContextCache(contextDirectory, compiler.root));
371
+ const getContextify = (0, memoize_1.memoize)(() => identifier_1.contextify.bindCache(compiler.root));
372
+ const getContextifyInContext = (0, memoize_1.memoize)(() => identifier_1.contextify.bindContextCache(contextDirectory, compiler.root));
373
+ loaderContext.utils = {
374
+ absolutify: (context, request) => {
375
+ return context === contextDirectory
376
+ ? getAbsolutifyInContext()(request)
377
+ : getAbsolutify()(context, request);
378
+ },
379
+ contextify: (context, request) => {
380
+ return context === contextDirectory
381
+ ? getContextifyInContext()(request)
382
+ : getContextify()(context, request);
383
+ },
384
+ createHash: type => {
385
+ return (0, createHash_1.createHash)(type || compiler.compilation.outputOptions.hashFunction);
386
+ }
387
+ };
388
+ loaderContext.addBuildDependency = function addBuildDependency(file) {
389
+ buildDependencies.push(file);
390
+ };
391
+ loaderContext.addDependency = function addDependency(file) {
392
+ fileDependencies.push(file);
393
+ };
394
+ loaderContext.dependency = function dependency(file) {
395
+ fileDependencies.push(file);
396
+ };
397
+ loaderContext.addContextDependency = function addContextDependency(context) {
398
+ contextDependencies.push(context);
399
+ };
400
+ loaderContext.addMissingDependency = function addMissingDependency(missing) {
401
+ missingDependencies.push(missing);
402
+ };
403
+ loaderContext.clearDependencies = function clearDependencies() {
404
+ fileDependencies.length = 0;
405
+ contextDependencies.length = 0;
406
+ missingDependencies.length = 0;
407
+ };
408
+ loaderContext.getDependencies = function getDependencies() {
409
+ return fileDependencies.slice();
410
+ };
411
+ loaderContext.getContextDependencies = function getContextDependencies() {
412
+ return contextDependencies.slice();
413
+ };
414
+ loaderContext.getMissingDependencies = function getMissingDependencies() {
415
+ return missingDependencies.slice();
416
+ };
417
+ loaderContext._compiler = compiler;
418
+ loaderContext._compilation = compiler.compilation;
419
+ loaderContext.getOptions = function (schema) {
420
+ let loader = getCurrentLoader(loaderContext);
421
+ let options = loader === null || loader === void 0 ? void 0 : loader.options;
422
+ if (options === null || options === undefined) {
423
+ options = {};
424
+ }
425
+ if (schema) {
426
+ let name = "Loader";
427
+ let baseDataPath = "options";
428
+ let match;
429
+ if (schema.title && (match = /^(.+) (.+)$/.exec(schema.title))) {
430
+ [, name, baseDataPath] = match;
431
+ }
432
+ const { validate } = require("schema-utils");
433
+ validate(schema, options, {
434
+ name,
435
+ baseDataPath
436
+ });
437
+ }
438
+ return options;
439
+ };
440
+ return new Promise((resolve, reject) => {
441
+ if (isPitching) {
442
+ iteratePitchingLoaders(loaderContext, [], (err, result) => {
443
+ if (err) {
444
+ return reject(err);
445
+ }
446
+ const [content, sourceMap, additionalData] = result;
447
+ resolve({
448
+ content: (0, util_1.isNil)(content) ? undefined : (0, util_1.toBuffer)(content),
449
+ sourceMap: (0, util_1.isNil)(sourceMap)
450
+ ? undefined
451
+ : (0, util_1.toBuffer)(typeof sourceMap === "string"
452
+ ? sourceMap
453
+ : JSON.stringify(sourceMap)),
454
+ additionalData: (0, util_1.isNil)(additionalData)
455
+ ? undefined
456
+ : (0, util_1.toBuffer)(JSON.stringify(additionalData)),
457
+ buildDependencies,
458
+ cacheable,
459
+ fileDependencies,
460
+ contextDependencies,
461
+ missingDependencies,
462
+ assetFilenames,
463
+ isPitching: loaderContext.__internal__isPitching
464
+ });
465
+ });
466
+ }
467
+ else {
468
+ // normal
469
+ loaderContext.loaderIndex = loaderContext.loaders.length - 1;
470
+ iterateNormalLoaders(loaderContext, [
471
+ rawContext.content,
472
+ (0, util_1.isNil)(rawContext.sourceMap)
473
+ ? undefined
474
+ : (0, util_1.toObject)(rawContext.sourceMap),
475
+ (0, util_1.isNil)(rawContext.additionalData)
476
+ ? undefined
477
+ : (0, util_1.toObject)(rawContext.additionalData)
478
+ ], (err, result) => {
479
+ if (err) {
480
+ return reject(err);
481
+ }
482
+ const [content, sourceMap, additionalData] = result;
483
+ resolve({
484
+ content: (0, util_1.isNil)(content) ? undefined : (0, util_1.toBuffer)(content),
485
+ sourceMap: (0, util_1.isNil)(sourceMap)
486
+ ? undefined
487
+ : (0, util_1.toBuffer)(typeof sourceMap === "string"
488
+ ? sourceMap
489
+ : JSON.stringify(sourceMap)),
490
+ additionalData: (0, util_1.isNil)(additionalData)
491
+ ? undefined
492
+ : (0, util_1.toBuffer)(JSON.stringify(additionalData)),
493
+ buildDependencies,
494
+ cacheable,
495
+ fileDependencies,
496
+ contextDependencies,
497
+ missingDependencies,
498
+ assetFilenames,
499
+ isPitching: loaderContext.__internal__isPitching
500
+ });
501
+ });
502
+ }
503
+ });
504
+ }
505
+ exports.runLoader = runLoader;
506
+ function utf8BufferToString(buf) {
507
+ var str = buf.toString("utf-8");
508
+ if (str.charCodeAt(0) === 0xfeff) {
509
+ return str.slice(1);
510
+ }
511
+ else {
512
+ return str;
513
+ }
514
+ }
515
+ function convertArgs(args, raw) {
516
+ if (!raw && Buffer.isBuffer(args[0]))
517
+ args[0] = utf8BufferToString(args[0]);
518
+ else if (raw && typeof args[0] === "string")
519
+ args[0] = Buffer.from(args[0], "utf-8");
520
+ }
521
+ function runSyncOrAsync(fn, context, args, callback) {
522
+ var isSync = true;
523
+ var isDone = false;
524
+ var isError = false; // internal error
525
+ var reportedError = false;
526
+ // @ts-expect-error loader-runner leverages `arguments` to achieve the same functionality.
527
+ context.async = function async() {
528
+ if (isDone) {
529
+ if (reportedError)
530
+ return; // ignore
531
+ throw new Error("async(): The callback was already called.");
532
+ }
533
+ isSync = false;
534
+ return innerCallback;
535
+ };
536
+ var innerCallback = (context.callback = function () {
537
+ if (isDone) {
538
+ if (reportedError)
539
+ return; // ignore
540
+ throw new Error("callback(): The callback was already called.");
541
+ }
542
+ isDone = true;
543
+ isSync = false;
544
+ try {
545
+ callback.apply(null, arguments);
546
+ }
547
+ catch (e) {
548
+ isError = true;
549
+ throw e;
550
+ }
551
+ });
552
+ try {
553
+ var result = (function LOADER_EXECUTION() {
554
+ return fn.apply(context, args);
555
+ })();
556
+ if (isSync) {
557
+ isDone = true;
558
+ if (result === undefined)
559
+ return callback();
560
+ if (result &&
561
+ typeof result === "object" &&
562
+ typeof result.then === "function") {
563
+ return result.then(function (r) {
564
+ callback(null, r);
565
+ }, callback);
566
+ }
567
+ return callback(null, result);
568
+ }
569
+ }
570
+ catch (e) {
571
+ if (isError)
572
+ throw e;
573
+ if (isDone) {
574
+ // loader is already "done", so we cannot use the callback function
575
+ // for better debugging we print the error on the console
576
+ if (e instanceof Error)
577
+ console.error(e.stack);
578
+ else
579
+ console.error(e);
580
+ return;
581
+ }
582
+ isDone = true;
583
+ reportedError = true;
584
+ callback(e);
585
+ }
586
+ }
587
+ function iteratePitchingLoaders(loaderContext, args, callback) {
588
+ // Running out of js loaders, so yield back to rust.
589
+ // Directly callback as we may still have other loaders on the rust side,
590
+ // The difference between rspack loader-runner and webpack loader-runner is
591
+ // that we do not run the loaders in the normal stage if pitching is not successful.
592
+ if (loaderContext.loaderIndex >= loaderContext.loaders.length)
593
+ return callback(null, args);
594
+ var currentLoaderObject = loaderContext.loaders[loaderContext.loaderIndex];
595
+ // iterate
596
+ if (currentLoaderObject.pitchExecuted) {
597
+ loaderContext.loaderIndex++;
598
+ return iteratePitchingLoaders(loaderContext, args, callback);
599
+ }
600
+ // load loader module
601
+ loadLoader(currentLoaderObject, function (err) {
602
+ if (err) {
603
+ loaderContext.cacheable(false);
604
+ return callback(err);
605
+ }
606
+ var fn = currentLoaderObject.pitch;
607
+ currentLoaderObject.pitchExecuted = true;
608
+ if (!fn)
609
+ return iteratePitchingLoaders(loaderContext, args, callback);
610
+ runSyncOrAsync(fn, loaderContext, [
611
+ loaderContext.remainingRequest,
612
+ loaderContext.previousRequest,
613
+ (currentLoaderObject.data = {})
614
+ ], function (err) {
615
+ if (err)
616
+ return callback(err);
617
+ var args = Array.prototype.slice.call(arguments, 1);
618
+ // Determine whether to continue the pitching process based on
619
+ // argument values (as opposed to argument presence) in order
620
+ // to support synchronous and asynchronous usages.
621
+ var hasArg = args.some(function (value) {
622
+ return value !== undefined;
623
+ });
624
+ // If a loader pitched successfully,
625
+ // then It should execute normal loaders too.
626
+ if (hasArg) {
627
+ // Instruct rust side to execute loaders in backwards.
628
+ loaderContext.__internal__isPitching = false;
629
+ loaderContext.loaderIndex--;
630
+ iterateNormalLoaders(loaderContext, args, callback);
631
+ }
632
+ else {
633
+ iteratePitchingLoaders(loaderContext, args, callback);
634
+ }
635
+ });
636
+ });
637
+ }
638
+ function iterateNormalLoaders(loaderContext, args, callback) {
639
+ // JS loaders ends
640
+ if (loaderContext.loaderIndex < 0)
641
+ return callback(null, args);
642
+ var currentLoaderObject = loaderContext.loaders[loaderContext.loaderIndex];
643
+ // iterate
644
+ if (currentLoaderObject.normalExecuted) {
645
+ loaderContext.loaderIndex--;
646
+ return iterateNormalLoaders(loaderContext, args, callback);
647
+ }
648
+ loadLoader(currentLoaderObject, function (err) {
649
+ if (err) {
650
+ loaderContext.cacheable(false);
651
+ return callback(err);
652
+ }
653
+ var fn = currentLoaderObject.normal;
654
+ currentLoaderObject.normalExecuted = true;
655
+ if (!fn) {
656
+ return iterateNormalLoaders(loaderContext, args, callback);
657
+ }
658
+ convertArgs(args, !!currentLoaderObject.raw);
659
+ runSyncOrAsync(fn, loaderContext, args, function (err) {
660
+ if (err)
661
+ return callback(err);
662
+ var args = Array.prototype.slice.call(arguments, 1);
663
+ iterateNormalLoaders(loaderContext, args, callback);
664
+ });
665
+ });
666
+ }
667
+ //# sourceMappingURL=index.js.map