@rspack/core 0.1.8 → 0.1.10

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