@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/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # @rspack/core
2
2
 
3
+ ## 0.0.4
4
+
5
+ ### Patch Changes
6
+
7
+ - d466288: add process_assets hook
8
+ - Updated dependencies [d466288]
9
+ - @rspack/binding@0.0.5
10
+ - @rspack/dev-server@0.0.4
11
+ - @rspack/plugin-postcss@0.0.4
12
+
3
13
  ## 0.0.3
4
14
 
5
15
  ### Patch Changes
package/example/basic.ts CHANGED
@@ -7,7 +7,35 @@ const rspack = new Rspack({
7
7
  main: path.resolve(__dirname, "../../../examples/postcss/index.js")
8
8
  },
9
9
  context: path.resolve(__dirname, "../../../examples/postcss"),
10
- plugins: ["html"],
10
+ plugins: [
11
+ {
12
+ name: "test",
13
+ apply(compiler) {
14
+ compiler.hooks.done.tap("done1", () => {
15
+ console.log("done1");
16
+ });
17
+ compiler.hooks.done.tap("done2", () => {
18
+ console.log("done2");
19
+ });
20
+ compiler.hooks.compilation.tap("compilation", compilation => {
21
+ compilation.hooks.processAssets.tapPromise(
22
+ "processAssets1",
23
+ async assets => {
24
+ for (const value of Object.values(assets)) {
25
+ console.log("value:", value.buffer(), value.source());
26
+ }
27
+ compilation.emitAsset("test.js", {
28
+ source: "hello world"
29
+ });
30
+ compilation.updateAsset("main.js", {
31
+ source: "hello main"
32
+ });
33
+ }
34
+ );
35
+ });
36
+ }
37
+ }
38
+ ],
11
39
  module: {
12
40
  rules: [
13
41
  {
package/package.json CHANGED
@@ -1,21 +1,35 @@
1
1
  {
2
2
  "name": "@rspack/core",
3
- "main": "./dist/index.js",
4
- "version": "0.0.3",
3
+ "version": "0.0.4",
5
4
  "bin": {
6
5
  "rspack": "./bin.js"
7
6
  },
8
- "types": "./dist/index.d.ts",
7
+ "main": "./lib/index.js",
8
+ "types": "./lib/index.d.ts",
9
9
  "devDependencies": {
10
10
  "@types/node": "^18.6.3",
11
11
  "tsm": "^2.2.2",
12
- "typescript": "4.7.3"
12
+ "ts-node": "10.9.1",
13
+ "typescript": "4.7.3",
14
+ "@types/ws": "8.5.3",
15
+ "@types/connect": "3.4.35",
16
+ "uvu": "0.5.6",
17
+ "@rspack/core": "0.0.4",
18
+ "@types/webpack-sources": "3.2.0"
13
19
  },
14
20
  "dependencies": {
15
- "@rspack/binding": "0.0.4",
16
- "commander": "^9.4.0",
17
- "@rspack/dev-server": "^0.0.3",
18
- "@rspack/plugin-postcss": "^0.0.3"
21
+ "@rspack/binding": "0.0.5",
22
+ "commander": "9.4.0",
23
+ "ws": "8.8.1",
24
+ "connect": "3.7.0",
25
+ "chokidar": "3.5.3",
26
+ "open": "8.4.0",
27
+ "clipanion": "3.2.0-rc.11",
28
+ "@rspack/dev-server": "^0.0.4",
29
+ "sirv": "2.0.2",
30
+ "@rspack/plugin-postcss": "^0.0.4",
31
+ "tapable": "2.2.1",
32
+ "webpack-sources": "3.2.3"
19
33
  },
20
34
  "optionalDependencies": {
21
35
  "@tmp-sass-embedded/darwin-arm64": "1.54.4",
@@ -27,8 +41,10 @@
27
41
  "@tmp-sass-embedded/win32-x64": "1.54.4"
28
42
  },
29
43
  "scripts": {
30
- "build": "rm -rf dist/ && tsc -p tsconfig.json",
31
- "dev": "tsc -p tsconfig.json -w",
32
- "example": "node -r tsm ./example/basic.ts"
33
- }
44
+ "build": "rm -rf lib/ && tsc",
45
+ "dev": "tsc -w",
46
+ "example": "node -r ts-node/register ./example/basic.ts",
47
+ "test": "uvu -r tsm tests"
48
+ },
49
+ "readme": "# rspack\n"
34
50
  }
package/src/bin/index.ts CHANGED
File without changes
@@ -0,0 +1,5 @@
1
+ import type { RawBuiltins } from "@rspack/binding";
2
+
3
+ export type Builtins = RawBuiltins;
4
+
5
+ export type ResolvedBuiltins = RawBuiltins | any[];
@@ -0,0 +1,3 @@
1
+ export type Context = string;
2
+
3
+ export type ResolvedContext = string;
@@ -0,0 +1,3 @@
1
+ export type Define = Record<string, string>;
2
+
3
+ export type ResolvedDefine = Record<string, string>;
@@ -0,0 +1,32 @@
1
+ import path from "node:path";
2
+
3
+ export interface Dev {
4
+ port?: number;
5
+ static?: {
6
+ directory?: string;
7
+ };
8
+ }
9
+
10
+ export interface ResolvedDev {
11
+ port: number;
12
+ static: {
13
+ directory: string;
14
+ };
15
+ }
16
+
17
+ interface ResolveDevConfigContext {
18
+ context: string;
19
+ }
20
+
21
+ export function resolveDevOptions(
22
+ devConfig: Dev = {},
23
+ context: ResolveDevConfigContext
24
+ ): ResolvedDev {
25
+ return {
26
+ port: devConfig.port ?? 8080,
27
+ static: {
28
+ directory:
29
+ devConfig.static?.directory ?? path.resolve(context.context, "dist")
30
+ }
31
+ };
32
+ }
@@ -0,0 +1,3 @@
1
+ export type Entry = Record<string, string>;
2
+
3
+ export type ResolvedEntry = Record<string, string>;
@@ -0,0 +1,3 @@
1
+ export type External = Record<string, string>;
2
+
3
+ export type ResolvedExternal = Record<string, string>;
@@ -0,0 +1,80 @@
1
+ import type { Context, ResolvedContext } from "./context";
2
+ import type { Define, ResolvedDefine } from "./define";
3
+ import type { Dev, ResolvedDev } from "./dev";
4
+ import type { Entry, ResolvedEntry } from "./entry";
5
+ import type { External, ResolvedExternal } from "./external";
6
+ import type { Mode, ResolvedMode } from "./mode";
7
+ import type { Module, ResolvedModule } from "./module";
8
+ import type { Plugin } from "./plugin";
9
+ import type { ResolvedTarget, Target } from "./target";
10
+ import type { Output, ResolvedOutput } from "./output";
11
+ import { resolveTargetOptions } from "./target";
12
+ import { resolveOutputOptions } from "./output";
13
+ import { resolveDevOptions } from "./dev";
14
+ import { resolveModuleOptions } from "./module";
15
+ import { Builtins, ResolvedBuiltins } from "./builtins";
16
+ import { Resolve, ResolvedResolve } from "./resolve";
17
+
18
+ export type Asset = {
19
+ source: string;
20
+ };
21
+ export type Assets = Record<string, Asset>;
22
+
23
+ export interface RspackOptions {
24
+ entry?: Entry;
25
+ context?: Context;
26
+ plugins?: Plugin[];
27
+ dev?: Dev;
28
+ module?: Module;
29
+ define?: Define;
30
+ target?: Target;
31
+ mode?: Mode;
32
+ external?: External;
33
+ output?: Output;
34
+ builtins: Builtins;
35
+ resolve: Resolve;
36
+ }
37
+
38
+ export interface ResolvedRspackOptions {
39
+ entry: ResolvedEntry;
40
+ context: ResolvedContext;
41
+ plugins: Plugin[];
42
+ dev: ResolvedDev;
43
+ module: ResolvedModule;
44
+ define: ResolvedDefine;
45
+ target: ResolvedTarget;
46
+ mode: ResolvedMode;
47
+ external: ResolvedExternal;
48
+ output: ResolvedOutput;
49
+ builtins: ResolvedBuiltins;
50
+ resolve: ResolvedResolve;
51
+ }
52
+
53
+ export function resolveOptions(config: RspackOptions): ResolvedRspackOptions {
54
+ const context = config.context ?? process.cwd();
55
+ const mode = config.mode ?? "development";
56
+ const dev = resolveDevOptions(config.dev, { context });
57
+ const entry = config.entry ?? {};
58
+ const output = resolveOutputOptions(config.output);
59
+ const define = config.define ?? {};
60
+ const target = resolveTargetOptions(config.target);
61
+ const external = config.external ?? {};
62
+ const plugins = config.plugins ?? [];
63
+ const builtins = config.builtins ?? [];
64
+ const resolve = config.resolve ?? {};
65
+ const module = resolveModuleOptions(config.module);
66
+ return {
67
+ context,
68
+ mode,
69
+ dev,
70
+ entry,
71
+ output,
72
+ define,
73
+ target,
74
+ external,
75
+ plugins,
76
+ builtins,
77
+ module,
78
+ resolve
79
+ };
80
+ }
@@ -0,0 +1,2 @@
1
+ export type Mode = string;
2
+ export type ResolvedMode = string;
@@ -0,0 +1,240 @@
1
+ import type { RawModuleRuleUse, RawModuleRule } from "@rspack/binding";
2
+ import assert from "node:assert";
3
+
4
+ export interface ModuleRule {
5
+ test?: RawModuleRule["test"];
6
+ resource?: RawModuleRule["resource"];
7
+ resourceQuery?: RawModuleRule["resourceQuery"];
8
+ uses?: ModuleRuleUse[];
9
+ type?: RawModuleRule["type"];
10
+ }
11
+
12
+ export interface Module {
13
+ rules?: ModuleRule[];
14
+ parser?: {
15
+ dataUrlCondition?: {
16
+ maxSize?: number;
17
+ };
18
+ };
19
+ }
20
+
21
+ interface ResolvedModuleRule {
22
+ test?: RawModuleRule["test"];
23
+ resource?: RawModuleRule["resource"];
24
+ resourceQuery?: RawModuleRule["resourceQuery"];
25
+ uses?: RawModuleRuleUse[];
26
+ type?: RawModuleRule["type"];
27
+ }
28
+
29
+ export interface ResolvedModule {
30
+ rules: ResolvedModuleRule[];
31
+ parser?: {
32
+ dataUrlCondition: {
33
+ maxSize: number;
34
+ };
35
+ };
36
+ }
37
+
38
+ interface LoaderContextInternal {
39
+ // 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.
40
+ source: number[];
41
+ resource: String;
42
+ resourcePath: String;
43
+ resourceQuery: String | null;
44
+ resourceFragment: String | null;
45
+ }
46
+
47
+ interface LoaderResult {
48
+ content: Buffer | string;
49
+ meta: Buffer | string;
50
+ }
51
+
52
+ interface LoaderThreadsafeResult {
53
+ id: number;
54
+ p: LoaderResultInternal | null | undefined;
55
+ }
56
+
57
+ interface LoaderResultInternal {
58
+ content: number[];
59
+ meta: number[];
60
+ }
61
+
62
+ interface LoaderContext
63
+ extends Pick<
64
+ LoaderContextInternal,
65
+ "resource" | "resourcePath" | "resourceQuery" | "resourceFragment"
66
+ > {
67
+ source: {
68
+ getCode(): string;
69
+ getBuffer(): Buffer;
70
+ };
71
+ }
72
+
73
+ const toBuffer = (bufLike: string | Buffer): Buffer => {
74
+ if (Buffer.isBuffer(bufLike)) {
75
+ return bufLike;
76
+ } else if (typeof bufLike === "string") {
77
+ return Buffer.from(bufLike);
78
+ }
79
+
80
+ throw new Error("Buffer or string expected");
81
+ };
82
+
83
+ interface LoaderThreadsafeContext {
84
+ id: number;
85
+ p: LoaderContextInternal;
86
+ }
87
+
88
+ function composeJsUse(uses: ModuleRuleUse[]): RawModuleRuleUse | null {
89
+ if (!uses.length) {
90
+ return null;
91
+ }
92
+
93
+ async function loader(err: any, data: Buffer): Promise<Buffer> {
94
+ if (err) {
95
+ throw err;
96
+ }
97
+
98
+ const loaderThreadsafeContext: LoaderThreadsafeContext = JSON.parse(
99
+ data.toString("utf-8")
100
+ );
101
+
102
+ const { p: payload, id } = loaderThreadsafeContext;
103
+
104
+ const loaderContextInternal: LoaderContextInternal = {
105
+ source: payload.source,
106
+ resourcePath: payload.resourcePath,
107
+ resourceQuery: payload.resourceQuery,
108
+ resource: payload.resource,
109
+ resourceFragment: payload.resourceFragment
110
+ };
111
+
112
+ let sourceBuffer = Buffer.from(loaderContextInternal.source);
113
+ let meta = Buffer.from("");
114
+ // Loader is executed from right to left
115
+ for (const use of uses) {
116
+ assert("loader" in use);
117
+ const loaderContext = {
118
+ ...loaderContextInternal,
119
+ source: {
120
+ getCode(): string {
121
+ return sourceBuffer.toString("utf-8");
122
+ },
123
+ getBuffer(): Buffer {
124
+ return sourceBuffer;
125
+ }
126
+ },
127
+ getOptions() {
128
+ return use.options;
129
+ }
130
+ };
131
+
132
+ let loaderResult: LoaderResult;
133
+ if (
134
+ (loaderResult = await Promise.resolve().then(() =>
135
+ use.loader.apply(loaderContext, [loaderContext])
136
+ ))
137
+ ) {
138
+ const content = loaderResult.content;
139
+ meta = meta.length > 0 ? meta : toBuffer(loaderResult.meta);
140
+ sourceBuffer = toBuffer(content);
141
+ }
142
+ }
143
+
144
+ const loaderResultPayload: LoaderResultInternal = {
145
+ content: [...sourceBuffer],
146
+ meta: [...meta]
147
+ };
148
+
149
+ const loaderThreadsafeResult: LoaderThreadsafeResult = {
150
+ id: id,
151
+ p: loaderResultPayload
152
+ };
153
+ return Buffer.from(JSON.stringify(loaderThreadsafeResult), "utf-8");
154
+ }
155
+ loader.displayName = `NodeLoaderAdapter(${uses
156
+ .map(item => {
157
+ assert("loader" in item);
158
+ return item.loader.displayName || item.loader.name || "unknown-loader";
159
+ })
160
+ .join(" -> ")})`;
161
+ return {
162
+ loader
163
+ };
164
+ }
165
+
166
+ interface JsLoader {
167
+ (this: LoaderContext, loaderContext: LoaderContext):
168
+ | Promise<LoaderResult | void>
169
+ | LoaderResult
170
+ | void;
171
+ displayName?: string;
172
+ }
173
+
174
+ type BuiltinLoader = string;
175
+
176
+ type ModuleRuleUse =
177
+ | {
178
+ builtinLoader: BuiltinLoader;
179
+ options?: unknown;
180
+ }
181
+ | {
182
+ loader: JsLoader;
183
+ options?: unknown;
184
+ };
185
+
186
+ export function createRawModuleRuleUses(
187
+ uses: ModuleRuleUse[]
188
+ ): RawModuleRuleUse[] {
189
+ return createRawModuleRuleUsesImpl([...uses].reverse());
190
+ }
191
+
192
+ function createRawModuleRuleUsesImpl(
193
+ uses: ModuleRuleUse[]
194
+ ): RawModuleRuleUse[] {
195
+ if (!uses.length) {
196
+ return [];
197
+ }
198
+
199
+ const index = uses.findIndex(use => "builtinLoader" in use);
200
+ if (index < 0) {
201
+ return [composeJsUse(uses)];
202
+ }
203
+
204
+ const before = uses.slice(0, index);
205
+ const after = uses.slice(index + 1);
206
+ return [
207
+ composeJsUse(before),
208
+ createNativeUse(uses[index]),
209
+ ...createRawModuleRuleUsesImpl(after)
210
+ ].filter((item): item is RawModuleRuleUse => Boolean(item));
211
+ }
212
+
213
+ function createNativeUse(use: ModuleRuleUse): RawModuleRuleUse {
214
+ assert("builtinLoader" in use);
215
+
216
+ if (use.builtinLoader === "sass-loader") {
217
+ (use.options ??= {} as any).__exePath = require.resolve(
218
+ `@tmp-sass-embedded/${process.platform}-${
219
+ process.arch
220
+ }/dart-sass-embedded/dart-sass-embedded${
221
+ process.platform === "win32" ? ".bat" : ""
222
+ }`
223
+ );
224
+ }
225
+
226
+ return {
227
+ builtinLoader: use.builtinLoader,
228
+ options: JSON.stringify(use.options)
229
+ };
230
+ }
231
+
232
+ export function resolveModuleOptions(module: Module = {}): ResolvedModule {
233
+ const rules = (module.rules ?? []).map(rule => ({
234
+ ...rule,
235
+ uses: createRawModuleRuleUses(rule.uses || [])
236
+ }));
237
+ return {
238
+ rules
239
+ };
240
+ }
@@ -0,0 +1,29 @@
1
+ export interface Output {
2
+ path?: string;
3
+ publicPath?: string;
4
+ assetModuleFilename?: string;
5
+ filename?: string;
6
+ chunkFilename?: string;
7
+ uniqueName?: string;
8
+ }
9
+
10
+ // TODO: fix it
11
+ export interface ResolvedOutput {
12
+ path?: string;
13
+ publicPath?: string;
14
+ assetModuleFilename?: string;
15
+ filename?: string;
16
+ chunkFilename?: string;
17
+ uniqueName?: string;
18
+ }
19
+
20
+ export function resolveOutputOptions(output: Output = {}): ResolvedOutput {
21
+ return {
22
+ path: output.path,
23
+ publicPath: output.publicPath,
24
+ chunkFilename: output.chunkFilename,
25
+ filename: output.publicPath,
26
+ assetModuleFilename: output.assetModuleFilename,
27
+ uniqueName: output.uniqueName
28
+ };
29
+ }
@@ -0,0 +1,6 @@
1
+ import Rspack from "..";
2
+
3
+ export interface Plugin {
4
+ name: string;
5
+ apply(compiler: Rspack): void;
6
+ }
@@ -0,0 +1,6 @@
1
+ export type Resolve = {
2
+ preferRelative?: boolean;
3
+ };
4
+ export type ResolvedResolve = {
5
+ preferRelative?: boolean;
6
+ };
@@ -0,0 +1,28 @@
1
+ type TargetItem =
2
+ | "web"
3
+ | "webworker"
4
+ | "browserslist"
5
+ | "es3"
6
+ | "es5"
7
+ | "es2015"
8
+ | "es2016"
9
+ | "es2017"
10
+ | "es2018"
11
+ | "es2019"
12
+ | "es2020"
13
+ | "es2021"
14
+ | "es2022";
15
+
16
+ export type Target = TargetItem | TargetItem[] | false;
17
+ export type ResolvedTarget = TargetItem[];
18
+
19
+ export function resolveTargetOptions(target: Target = "web"): ResolvedTarget {
20
+ if (!target) {
21
+ return [];
22
+ }
23
+ if (!Array.isArray(target)) {
24
+ return [target];
25
+ }
26
+
27
+ return target;
28
+ }