@rspack/core 0.0.3 → 0.0.7

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 (57) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/example/basic.ts +29 -1
  3. package/{dist → lib}/bin/index.d.ts +0 -0
  4. package/{dist → lib}/bin/index.js +0 -0
  5. package/{dist → lib}/build.d.ts +0 -0
  6. package/{dist → lib}/build.js +0 -0
  7. package/lib/config/builtins.d.ts +3 -0
  8. package/lib/config/builtins.js +2 -0
  9. package/lib/config/context.d.ts +2 -0
  10. package/lib/config/context.js +2 -0
  11. package/lib/config/define.d.ts +2 -0
  12. package/lib/config/define.js +2 -0
  13. package/lib/config/dev.d.ts +17 -0
  14. package/lib/config/dev.js +17 -0
  15. package/lib/config/entry.d.ts +2 -0
  16. package/lib/config/entry.js +2 -0
  17. package/lib/config/external.d.ts +2 -0
  18. package/lib/config/external.js +2 -0
  19. package/lib/config/index.d.ts +45 -0
  20. package/lib/config/index.js +37 -0
  21. package/lib/config/mode.d.ts +2 -0
  22. package/lib/config/mode.js +2 -0
  23. package/{dist/index.d.ts → lib/config/module.d.ts} +41 -29
  24. package/lib/config/module.js +121 -0
  25. package/lib/config/output.d.ts +17 -0
  26. package/lib/config/output.js +14 -0
  27. package/lib/config/plugin.d.ts +5 -0
  28. package/lib/config/plugin.js +2 -0
  29. package/lib/config/resolve.d.ts +6 -0
  30. package/lib/config/resolve.js +2 -0
  31. package/lib/config/target.d.ts +5 -0
  32. package/lib/config/target.js +13 -0
  33. package/lib/index.d.ts +39 -0
  34. package/lib/index.js +154 -0
  35. package/{dist → lib}/server/index.d.ts +0 -0
  36. package/{dist → lib}/server/index.js +0 -0
  37. package/package.json +26 -11
  38. package/src/config/builtins.ts +5 -0
  39. package/src/config/context.ts +3 -0
  40. package/src/config/define.ts +3 -0
  41. package/src/config/dev.ts +32 -0
  42. package/src/config/entry.ts +3 -0
  43. package/src/config/external.ts +3 -0
  44. package/src/config/index.ts +80 -0
  45. package/src/config/mode.ts +2 -0
  46. package/src/config/module.ts +240 -0
  47. package/src/config/output.ts +29 -0
  48. package/src/config/plugin.ts +6 -0
  49. package/src/config/resolve.ts +6 -0
  50. package/src/config/target.ts +28 -0
  51. package/src/index.ts +107 -219
  52. package/tests/config.test.ts +40 -0
  53. package/tsconfig.json +6 -5
  54. package/dist/config.d.ts +0 -43
  55. package/dist/config.js +0 -25
  56. package/dist/index.js +0 -184
  57. package/src/config.ts +0 -66
package/src/index.ts CHANGED
@@ -1,208 +1,16 @@
1
1
  export * from "./build";
2
-
3
2
  import * as binding from "@rspack/binding";
4
- import type {
5
- ExternalObject,
6
- RspackInternal,
7
- RawModuleRuleUse,
8
- RawModuleRule
9
- } from "@rspack/binding";
10
-
11
- import assert from "node:assert";
12
- import * as Config from "./config";
13
- import type { RspackOptions } from "./config";
14
-
15
- interface ModuleRule {
16
- test?: RawModuleRule["test"];
17
- resource?: RawModuleRule["resource"];
18
- resourceQuery?: RawModuleRule["resourceQuery"];
19
- uses?: ModuleRuleUse[];
20
- type?: RawModuleRule["type"];
21
- }
22
-
23
- type ModuleRuleUse =
24
- | {
25
- builtinLoader: BuiltinLoader;
26
- options?: unknown;
27
- }
28
- | {
29
- loader: JsLoader;
30
- options?: unknown;
31
- };
32
-
33
- interface JsLoader {
34
- (this: LoaderContext, loaderContext: LoaderContext):
35
- | Promise<LoaderResult | void>
36
- | LoaderResult
37
- | void;
38
- displayName?: string;
39
- }
40
-
41
- type BuiltinLoader = string;
42
-
43
- interface LoaderThreadsafeContext {
44
- id: number;
45
- p: LoaderContextInternal;
46
- }
47
-
48
- interface LoaderContextInternal {
49
- // TODO: It's not a good way to do this, we should split the `source` into a separate type and avoid using `serde_json`, but it's a temporary solution.
50
- source: number[];
51
- resource: String;
52
- resourcePath: String;
53
- resourceQuery: String | null;
54
- resourceFragment: String | null;
55
- }
56
-
57
- interface LoaderContext
58
- extends Pick<
59
- LoaderContextInternal,
60
- "resource" | "resourcePath" | "resourceQuery" | "resourceFragment"
61
- > {
62
- source: {
63
- getCode(): string;
64
- getBuffer(): Buffer;
65
- };
66
- }
67
-
68
- interface LoaderResultInternal {
69
- content: number[];
70
- meta: number[];
71
- }
72
-
73
- interface LoaderResult {
74
- content: Buffer | string;
75
- meta: Buffer | string;
76
- }
77
-
78
- interface LoaderThreadsafeResult {
79
- id: number;
80
- p: LoaderResultInternal | null | undefined;
81
- }
82
-
83
- const toBuffer = (bufLike: string | Buffer): Buffer => {
84
- if (Buffer.isBuffer(bufLike)) {
85
- return bufLike;
86
- } else if (typeof bufLike === "string") {
87
- return Buffer.from(bufLike);
88
- }
89
-
90
- throw new Error("Buffer or string expected");
91
- };
92
-
93
- function createRawModuleRuleUses(uses: ModuleRuleUse[]): RawModuleRuleUse[] {
94
- return createRawModuleRuleUsesImpl([...uses].reverse());
95
- }
96
-
97
- function createRawModuleRuleUsesImpl(
98
- uses: ModuleRuleUse[]
99
- ): RawModuleRuleUse[] {
100
- const index = uses.findIndex(use => "builtinLoader" in use);
101
- if (index < 0) {
102
- return [composeJsUse(uses)];
103
- }
104
-
105
- const before = uses.slice(0, index);
106
- const after = uses.slice(index + 1);
107
- return [
108
- composeJsUse(before),
109
- createNativeUse(uses[index]),
110
- ...createRawModuleRuleUsesImpl(after)
111
- ];
112
- }
113
-
114
- function createNativeUse(use: ModuleRuleUse): RawModuleRuleUse {
115
- assert("builtinLoader" in use);
116
-
117
- if (use.builtinLoader === "sass-loader") {
118
- (use.options ??= {} as any).__exePath = require.resolve(
119
- `@tmp-sass-embedded/${process.platform}-${
120
- process.arch
121
- }/dart-sass-embedded/dart-sass-embedded${
122
- process.platform === "win32" ? ".bat" : ""
123
- }`
124
- );
125
- }
126
-
127
- return {
128
- builtinLoader: use.builtinLoader,
129
- options: JSON.stringify(use.options)
130
- };
131
- }
132
-
133
- function composeJsUse(uses: ModuleRuleUse[]): RawModuleRuleUse {
134
- async function loader(err: any, data: Buffer): Promise<Buffer> {
135
- if (err) {
136
- throw err;
137
- }
138
-
139
- const loaderThreadsafeContext: LoaderThreadsafeContext = JSON.parse(
140
- data.toString("utf-8")
141
- );
142
-
143
- const { p: payload, id } = loaderThreadsafeContext;
144
-
145
- const loaderContextInternal: LoaderContextInternal = {
146
- source: payload.source,
147
- resourcePath: payload.resourcePath,
148
- resourceQuery: payload.resourceQuery,
149
- resource: payload.resource,
150
- resourceFragment: payload.resourceFragment
151
- };
152
-
153
- let sourceBuffer = Buffer.from(loaderContextInternal.source);
154
- let meta = Buffer.from("");
155
- // Loader is executed from right to left
156
- for (const use of uses) {
157
- assert("loader" in use);
158
- const loaderContext = {
159
- ...loaderContextInternal,
160
- source: {
161
- getCode(): string {
162
- return sourceBuffer.toString("utf-8");
163
- },
164
- getBuffer(): Buffer {
165
- return sourceBuffer;
166
- }
167
- },
168
- getOptions() {
169
- return use.options;
170
- }
171
- };
172
-
173
- let loaderResult: LoaderResult;
174
- if (
175
- (loaderResult = await Promise.resolve().then(() =>
176
- use.loader.apply(loaderContext, [loaderContext])
177
- ))
178
- ) {
179
- const content = loaderResult.content;
180
- meta = meta.length > 0 ? meta : toBuffer(loaderResult.meta);
181
- sourceBuffer = toBuffer(content);
182
- }
183
- }
184
-
185
- const loaderResultPayload: LoaderResultInternal = {
186
- content: [...sourceBuffer],
187
- meta: [...meta]
188
- };
189
-
190
- const loaderThreadsafeResult: LoaderThreadsafeResult = {
191
- id: id,
192
- p: loaderResultPayload
193
- };
194
- return Buffer.from(JSON.stringify(loaderThreadsafeResult), "utf-8");
195
- }
196
- loader.displayName = `NodeLoaderAdapter(${uses
197
- .map(item => {
198
- assert("loader" in item);
199
- return item.loader.displayName || item.loader.name || "unknown-loader";
200
- })
201
- .join(" -> ")})`;
202
- return {
203
- loader
204
- };
205
- }
3
+ import type { ExternalObject, RspackInternal } from "@rspack/binding";
4
+ import * as tapable from "tapable";
5
+ import {
6
+ RspackOptions,
7
+ ResolvedRspackOptions,
8
+ Assets,
9
+ Asset,
10
+ resolveOptions
11
+ } from "./config";
12
+
13
+ import { RawSource, Source } from "webpack-sources";
206
14
  interface RspackThreadsafeContext<T> {
207
15
  readonly id: number;
208
16
  readonly inner: T;
@@ -218,35 +26,115 @@ const createDummyResult = (id: number): string => {
218
26
  };
219
27
  return JSON.stringify(result);
220
28
  };
29
+ type EmitAssetCallback = (options: { filename: string; asset: Asset }) => void;
30
+ class RspackCompilation {
31
+ #emitAssetCallback: EmitAssetCallback;
32
+ hooks: {
33
+ processAssets: tapable.AsyncSeriesHook<Record<string, Source>>;
34
+ };
35
+ constructor() {
36
+ this.hooks = {
37
+ processAssets: new tapable.AsyncSeriesHook<Record<string, Source>>([
38
+ "assets"
39
+ ])
40
+ };
41
+ }
42
+ /**
43
+ * unsafe to call out of processAssets
44
+ * @param filename
45
+ * @param asset
46
+ */
47
+ updateAsset(filename: string, asset: Asset) {
48
+ this.emitAsset(filename, asset);
49
+ }
50
+ /**
51
+ * unsafe to call out of processAssets
52
+ * @param filename
53
+ * @param asset
54
+ */
55
+ emitAsset(filename: string, asset: Asset) {
56
+ if (!this.#emitAssetCallback) {
57
+ throw new Error("can't call emitAsset outof processAssets hook for now");
58
+ }
59
+ this.#emitAssetCallback({
60
+ filename: filename,
61
+ asset
62
+ });
63
+ }
64
+ async processAssets(err: Error, value: string, emitAsset: any) {
65
+ this.#emitAssetCallback = emitAsset;
66
+ if (err) {
67
+ throw err;
68
+ }
69
+ const context: RspackThreadsafeContext<
70
+ Record<string, { source: string | Buffer }>
71
+ > = JSON.parse(value);
72
+ let content: Record<string, { source: string | Buffer }> =
73
+ context.inner ?? {};
74
+ let assets = {};
75
+ for (const [key, value] of Object.entries(content)) {
76
+ // webpack-sources's type definition is wrong, it actually could accept Buffer type
77
+ let source = value.source;
78
+ if (Array.isArray(value.source)) {
79
+ source = Buffer.from(value.source);
80
+ }
81
+ assets[key] = new RawSource(source as string);
82
+ }
83
+ await this.hooks.processAssets.promise(assets);
84
+ return createDummyResult(context.id);
85
+ }
86
+ }
221
87
  class Rspack {
222
- #instance: ExternalObject<RspackInternal>;
223
88
  #plugins: RspackOptions["plugins"];
224
- constructor(public options: RspackOptions) {
225
- const nativeConfig = Config.User2Native(options);
226
- this.#plugins = options.plugins ?? [];
227
-
228
- this.#instance = binding.newRspack(nativeConfig, {
229
- doneCallback: this.#done.bind(this)
89
+ #instance: ExternalObject<RspackInternal>;
90
+ compilation: RspackCompilation;
91
+ hooks: {
92
+ done: tapable.AsyncSeriesHook<void>;
93
+ compilation: tapable.SyncHook<RspackCompilation>;
94
+ };
95
+ options: ResolvedRspackOptions;
96
+ constructor(options: RspackOptions) {
97
+ this.options = resolveOptions(options);
98
+ // @ts-ignored
99
+ this.#instance = binding.newRspack(this.options, {
100
+ doneCallback: this.#done.bind(this),
101
+ processAssetsCallback: this.#processAssets.bind(this)
230
102
  });
103
+ this.hooks = {
104
+ done: new tapable.AsyncSeriesHook<void>(),
105
+ compilation: new tapable.SyncHook<RspackCompilation>(["compilation"])
106
+ };
107
+ this.#plugins = options.plugins ?? [];
108
+ for (const plugin of this.#plugins) {
109
+ plugin.apply(this);
110
+ }
231
111
  }
232
112
  async #done(err: Error, value: string) {
233
- const context: RspackThreadsafeContext<void> = JSON.parse(value);
234
- for (const plugin of this.#plugins) {
235
- await plugin.done?.();
113
+ if (err) {
114
+ throw err;
236
115
  }
116
+ const context: RspackThreadsafeContext<void> = JSON.parse(value);
117
+ await this.hooks.done.promise();
237
118
  return createDummyResult(context.id);
238
119
  }
120
+ async #processAssets(err: Error, value: string, emitAsset: any) {
121
+ return this.compilation.processAssets(err, value, emitAsset);
122
+ }
123
+ #newCompilation() {
124
+ const compilation = new RspackCompilation();
125
+ this.compilation = compilation;
126
+ this.hooks.compilation.call(compilation);
127
+ return compilation;
128
+ }
239
129
  async build() {
130
+ const compilation = this.#newCompilation();
240
131
  const stats = await binding.build(this.#instance);
241
132
  return stats;
242
133
  }
243
-
244
- async rebuild() {
245
- const stats = await binding.rebuild(this.#instance);
134
+ async rebuild(changeFiles: string[]) {
135
+ const stats = await binding.rebuild(this.#instance, changeFiles);
246
136
  return stats;
247
137
  }
248
138
  }
249
-
250
- export { Rspack, createRawModuleRuleUses };
251
- export type { ModuleRule };
139
+ export { Rspack };
252
140
  export default Rspack;
@@ -0,0 +1,40 @@
1
+ import { test } from "uvu";
2
+ import * as assert from "uvu/assert";
3
+ import { Rspack } from "@rspack/core";
4
+ import path from "path";
5
+
6
+ test("default config snapshot", () => {
7
+ const resolvedOptions = new Rspack({}).options;
8
+
9
+ assert.equal(resolvedOptions.context, process.cwd());
10
+ assert.equal(
11
+ resolvedOptions.dev.static.directory,
12
+ path.resolve(process.cwd(), "./dist")
13
+ );
14
+
15
+ // TypeScript will throw `The operand of a 'delete' operator must be optional`.
16
+ // But we remove these configurations with absolute paths.
17
+ // @ts-expect-error
18
+ delete resolvedOptions.context;
19
+ // @ts-expect-error
20
+ delete resolvedOptions.dev.static.directory;
21
+
22
+ assert.snapshot(
23
+ JSON.stringify(resolvedOptions),
24
+ JSON.stringify({
25
+ mode: "development",
26
+ dev: { port: 8080, static: {} },
27
+ entry: {},
28
+ output: {},
29
+ define: {},
30
+ target: ["web"],
31
+ external: {},
32
+ plugins: [],
33
+ builtins: [],
34
+ module: { rules: [] },
35
+ resolve: {}
36
+ })
37
+ );
38
+ });
39
+
40
+ test.run();
package/tsconfig.json CHANGED
@@ -4,10 +4,11 @@
4
4
  "target": "ES2018",
5
5
  "moduleResolution": "node",
6
6
  "esModuleInterop": true,
7
- "outDir": "dist",
7
+ "outDir": "lib",
8
8
  "declaration": true
9
9
  },
10
- "include": [
11
- "src"
12
- ]
13
- }
10
+ "include": ["src"],
11
+ "ts-node": {
12
+ "transpileOnly": true
13
+ }
14
+ }
package/dist/config.d.ts DELETED
@@ -1,43 +0,0 @@
1
- import type { RawOptions } from "@rspack/binding";
2
- import type { ModuleRule } from ".";
3
- export declare type Plugin = {
4
- name: string;
5
- done?: () => void | Promise<void>;
6
- };
7
- export interface RspackOptions {
8
- /**
9
- * Entry points of compilation.
10
- */
11
- entry?: RawOptions["entry"];
12
- /**
13
- * An **absolute** path pointed the
14
- */
15
- context?: RawOptions["context"];
16
- /**
17
- * An array of plugins
18
- */
19
- plugins?: Plugin[];
20
- /**
21
- * dev server
22
- */
23
- dev?: {
24
- port?: Number;
25
- static?: {
26
- directory?: string;
27
- };
28
- };
29
- /**
30
- * Module configuration.
31
- */
32
- module?: {
33
- rules?: ModuleRule[];
34
- parser?: RawOptions["module"]["parser"];
35
- };
36
- define?: RawOptions["define"];
37
- target?: RawOptions["target"];
38
- mode?: RawOptions["mode"];
39
- external?: RawOptions["external"];
40
- }
41
- export declare function User2Native(config: RspackOptions): RawOptions & {
42
- plugins: Plugin[];
43
- };
package/dist/config.js DELETED
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.User2Native = void 0;
4
- const _1 = require(".");
5
- function User2Native(config) {
6
- var _a, _b, _c, _d;
7
- return {
8
- entry: (_a = config.entry) !== null && _a !== void 0 ? _a : {},
9
- context: config.context,
10
- define: config.define,
11
- target: config.target,
12
- external: config.external,
13
- plugins: (_b = config.plugins) !== null && _b !== void 0 ? _b : [],
14
- module: {
15
- // TODO: support mutliple rules to support `Module Type`
16
- rules: ((_d = (_c = config === null || config === void 0 ? void 0 : config.module) === null || _c === void 0 ? void 0 : _c.rules) !== null && _d !== void 0 ? _d : []).map(rule => {
17
- return {
18
- ...rule,
19
- uses: (0, _1.createRawModuleRuleUses)(rule.uses || [])
20
- };
21
- })
22
- }
23
- };
24
- }
25
- exports.User2Native = User2Native;
package/dist/index.js DELETED
@@ -1,184 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
19
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
20
- };
21
- var __importStar = (this && this.__importStar) || function (mod) {
22
- if (mod && mod.__esModule) return mod;
23
- var result = {};
24
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
25
- __setModuleDefault(result, mod);
26
- return result;
27
- };
28
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
29
- if (kind === "m") throw new TypeError("Private method is not writable");
30
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
31
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
32
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
33
- };
34
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
35
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
36
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
37
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
38
- };
39
- var __importDefault = (this && this.__importDefault) || function (mod) {
40
- return (mod && mod.__esModule) ? mod : { "default": mod };
41
- };
42
- var _Rspack_instances, _Rspack_instance, _Rspack_plugins, _Rspack_done;
43
- Object.defineProperty(exports, "__esModule", { value: true });
44
- exports.createRawModuleRuleUses = exports.Rspack = void 0;
45
- __exportStar(require("./build"), exports);
46
- const binding = __importStar(require("@rspack/binding"));
47
- const node_assert_1 = __importDefault(require("node:assert"));
48
- const Config = __importStar(require("./config"));
49
- const toBuffer = (bufLike) => {
50
- if (Buffer.isBuffer(bufLike)) {
51
- return bufLike;
52
- }
53
- else if (typeof bufLike === "string") {
54
- return Buffer.from(bufLike);
55
- }
56
- throw new Error("Buffer or string expected");
57
- };
58
- function createRawModuleRuleUses(uses) {
59
- return createRawModuleRuleUsesImpl([...uses].reverse());
60
- }
61
- exports.createRawModuleRuleUses = createRawModuleRuleUses;
62
- function createRawModuleRuleUsesImpl(uses) {
63
- const index = uses.findIndex(use => "builtinLoader" in use);
64
- if (index < 0) {
65
- return [composeJsUse(uses)];
66
- }
67
- const before = uses.slice(0, index);
68
- const after = uses.slice(index + 1);
69
- return [
70
- composeJsUse(before),
71
- createNativeUse(uses[index]),
72
- ...createRawModuleRuleUsesImpl(after)
73
- ];
74
- }
75
- function createNativeUse(use) {
76
- var _a;
77
- (0, node_assert_1.default)("builtinLoader" in use);
78
- if (use.builtinLoader === "sass-loader") {
79
- ((_a = use.options) !== null && _a !== void 0 ? _a : (use.options = {})).__exePath = require.resolve(`@tmp-sass-embedded/${process.platform}-${process.arch}/dart-sass-embedded/dart-sass-embedded${process.platform === "win32" ? ".bat" : ""}`);
80
- }
81
- return {
82
- builtinLoader: use.builtinLoader,
83
- options: JSON.stringify(use.options)
84
- };
85
- }
86
- function composeJsUse(uses) {
87
- async function loader(err, data) {
88
- if (err) {
89
- throw err;
90
- }
91
- const loaderThreadsafeContext = JSON.parse(data.toString("utf-8"));
92
- const { p: payload, id } = loaderThreadsafeContext;
93
- const loaderContextInternal = {
94
- source: payload.source,
95
- resourcePath: payload.resourcePath,
96
- resourceQuery: payload.resourceQuery,
97
- resource: payload.resource,
98
- resourceFragment: payload.resourceFragment
99
- };
100
- let sourceBuffer = Buffer.from(loaderContextInternal.source);
101
- let meta = Buffer.from("");
102
- // Loader is executed from right to left
103
- for (const use of uses) {
104
- (0, node_assert_1.default)("loader" in use);
105
- const loaderContext = {
106
- ...loaderContextInternal,
107
- source: {
108
- getCode() {
109
- return sourceBuffer.toString("utf-8");
110
- },
111
- getBuffer() {
112
- return sourceBuffer;
113
- }
114
- },
115
- getOptions() {
116
- return use.options;
117
- }
118
- };
119
- let loaderResult;
120
- if ((loaderResult = await Promise.resolve().then(() => use.loader.apply(loaderContext, [loaderContext])))) {
121
- const content = loaderResult.content;
122
- meta = meta.length > 0 ? meta : toBuffer(loaderResult.meta);
123
- sourceBuffer = toBuffer(content);
124
- }
125
- }
126
- const loaderResultPayload = {
127
- content: [...sourceBuffer],
128
- meta: [...meta]
129
- };
130
- const loaderThreadsafeResult = {
131
- id: id,
132
- p: loaderResultPayload
133
- };
134
- return Buffer.from(JSON.stringify(loaderThreadsafeResult), "utf-8");
135
- }
136
- loader.displayName = `NodeLoaderAdapter(${uses
137
- .map(item => {
138
- (0, node_assert_1.default)("loader" in item);
139
- return item.loader.displayName || item.loader.name || "unknown-loader";
140
- })
141
- .join(" -> ")})`;
142
- return {
143
- loader
144
- };
145
- }
146
- const createDummyResult = (id) => {
147
- const result = {
148
- id,
149
- inner: null
150
- };
151
- return JSON.stringify(result);
152
- };
153
- class Rspack {
154
- constructor(options) {
155
- var _a;
156
- this.options = options;
157
- _Rspack_instances.add(this);
158
- _Rspack_instance.set(this, void 0);
159
- _Rspack_plugins.set(this, void 0);
160
- const nativeConfig = Config.User2Native(options);
161
- __classPrivateFieldSet(this, _Rspack_plugins, (_a = options.plugins) !== null && _a !== void 0 ? _a : [], "f");
162
- __classPrivateFieldSet(this, _Rspack_instance, binding.newRspack(nativeConfig, {
163
- doneCallback: __classPrivateFieldGet(this, _Rspack_instances, "m", _Rspack_done).bind(this)
164
- }), "f");
165
- }
166
- async build() {
167
- const stats = await binding.build(__classPrivateFieldGet(this, _Rspack_instance, "f"));
168
- return stats;
169
- }
170
- async rebuild() {
171
- const stats = await binding.rebuild(__classPrivateFieldGet(this, _Rspack_instance, "f"));
172
- return stats;
173
- }
174
- }
175
- exports.Rspack = Rspack;
176
- _Rspack_instance = new WeakMap(), _Rspack_plugins = new WeakMap(), _Rspack_instances = new WeakSet(), _Rspack_done = async function _Rspack_done(err, value) {
177
- var _a;
178
- const context = JSON.parse(value);
179
- for (const plugin of __classPrivateFieldGet(this, _Rspack_plugins, "f")) {
180
- await ((_a = plugin.done) === null || _a === void 0 ? void 0 : _a.call(plugin));
181
- }
182
- return createDummyResult(context.id);
183
- };
184
- exports.default = Rspack;