@rspack/core 0.0.3 → 0.0.4

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/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;
File without changes
File without changes
package/src/config.ts DELETED
@@ -1,66 +0,0 @@
1
- import type { RawOptions } from "@rspack/binding";
2
-
3
- import type { ModuleRule } from ".";
4
- import { createRawModuleRuleUses } from ".";
5
-
6
- export type Plugin = {
7
- name: string;
8
- done?: () => void | Promise<void>;
9
- };
10
-
11
- export interface RspackOptions {
12
- /**
13
- * Entry points of compilation.
14
- */
15
- entry?: RawOptions["entry"];
16
- /**
17
- * An **absolute** path pointed the
18
- */
19
- context?: RawOptions["context"];
20
- /**
21
- * An array of plugins
22
- */
23
- plugins?: Plugin[];
24
- /**
25
- * dev server
26
- */
27
- dev?: {
28
- port?: Number;
29
- static?: {
30
- directory?: string;
31
- };
32
- };
33
- /**
34
- * Module configuration.
35
- */
36
- module?: {
37
- rules?: ModuleRule[];
38
- parser?: RawOptions["module"]["parser"];
39
- };
40
- define?: RawOptions["define"];
41
- target?: RawOptions["target"];
42
- mode?: RawOptions["mode"];
43
- external?: RawOptions["external"];
44
- }
45
-
46
- export function User2Native(config: RspackOptions): RawOptions & {
47
- plugins: Plugin[];
48
- } {
49
- return {
50
- entry: config.entry ?? {},
51
- context: config.context,
52
- define: config.define,
53
- target: config.target,
54
- external: config.external,
55
- plugins: config.plugins ?? [],
56
- module: {
57
- // TODO: support mutliple rules to support `Module Type`
58
- rules: (config?.module?.rules ?? []).map(rule => {
59
- return {
60
- ...rule,
61
- uses: createRawModuleRuleUses(rule.uses || [])
62
- };
63
- })
64
- }
65
- };
66
- }