Skip to content

Commit

Permalink
refactor(typescript): refactor packages/rspack/src/util from js to …
Browse files Browse the repository at this point in the history
…ts and esm (#7246)

refactor: packages/rspack/src/util from js to ts/esm
  • Loading branch information
jithyan authored Jul 21, 2024
1 parent 0110aad commit 1b5fbdf
Show file tree
Hide file tree
Showing 15 changed files with 434 additions and 421 deletions.
17 changes: 11 additions & 6 deletions packages/rspack/etc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ import CacheFacade = require('./lib/CacheFacade');
import type { Callback } from '@rspack/lite-tapable';
import { cleanupGlobalTrace } from '@rspack/binding';
import { Compiler as Compiler_2 } from '../Compiler';
import { default as default_2 } from './util/hash';
import { RawEvalDevToolModulePluginOptions as EvalDevToolModulePluginOptions } from '@rspack/binding';
import { EventEmitter } from 'events';
import { ExternalObject } from '@rspack/binding';
import fs from 'graceful-fs';
import { fs as fs_2 } from 'fs';
import Hash_2 = require('./util/hash');
import Hash_3 = require('../util/hash');
import { HookMap } from '@rspack/lite-tapable';
import type { JsAssetInfo } from '@rspack/binding';
import { JsChunk } from '@rspack/binding';
Expand Down Expand Up @@ -1118,7 +1117,7 @@ export class Compilation {

// @public (undocumented)
type CompilationHooks = {
chunkHash: liteTapable.SyncHook<[Chunk, Hash_3]>;
chunkHash: liteTapable.SyncHook<[Chunk, Hash_2]>;
};

// @public (undocumented)
Expand Down Expand Up @@ -3952,6 +3951,12 @@ class Hash {
update(data: string | Buffer, inputEncoding?: string): Hash;
}

// @public (undocumented)
class Hash_2 {
digest(encoding?: string): string | Buffer;
update(data: string | Buffer, inputEncoding?: string): this;
}

// @public (undocumented)
export type HashDigest = z.infer<typeof hashDigest>;

Expand Down Expand Up @@ -4924,7 +4929,7 @@ export interface LoaderContext<OptionsType = {}> {
utils: {
absolutify: (context: string, request: string) => string;
contextify: (context: string, request: string) => string;
createHash: (algorithm?: string) => Hash_3;
createHash: (algorithm?: string) => Hash_2;
};
// (undocumented)
version: 2;
Expand Down Expand Up @@ -13818,8 +13823,8 @@ const uniqueName: z.ZodString;

// @public (undocumented)
export const util: {
createHash: (algorithm: any) => any;
cleverMerge: <T, O>(first: T, second: O) => T | O | (T & O);
createHash: (algorithm: "debug" | "md4" | "xxhash64" | (string & {}) | "native-md4" | (new () => default_2)) => default_2;
cleverMerge: <First, Second>(first: First, second: Second) => First | Second | (First & Second);
};

// @public (undocumented)
Expand Down
4 changes: 2 additions & 2 deletions packages/rspack/src/Compilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { cutOffLoaderExecution } from "./ErrorHelpers";
import { type CodeGenerationResult, Module } from "./Module";
import type { NormalModuleFactory } from "./NormalModuleFactory";
import type { ResolverFactory } from "./ResolverFactory";
import { JsDiagnostic, type RspackError } from "./RspackError";
import {
Stats,
type StatsAsset,
Expand All @@ -48,10 +49,9 @@ import { StatsPrinter } from "./stats/StatsPrinter";
import { type AssetInfo, JsAssetInfo } from "./util/AssetInfo";
import MergeCaller from "./util/MergeCaller";
import { createFakeCompilationDependencies } from "./util/fake";
import type Hash from "./util/hash";
import { memoizeValue } from "./util/memoize";
import { JsSource } from "./util/source";
import Hash = require("./util/hash");
import { JsDiagnostic, type RspackError } from "./RspackError";
export { type AssetInfo } from "./util/AssetInfo";

export type Assets = Record<string, Source>;
Expand Down
6 changes: 6 additions & 0 deletions packages/rspack/src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,9 @@ class Compiler {
binding.RegisterJsTapKind.CompilationChunkHash,
() => this.#compilation!.hooks.chunkHash,
queried => (chunk: binding.JsChunk) => {
if (!this.options.output.hashFunction) {
throw new Error("'output.hashFunction' cannot be undefined");
}
const hash = createHash(this.options.output.hashFunction);
queried.call(Chunk.__from_binding(chunk, this.#compilation!), hash);
const digestResult = hash.digest(this.options.output.hashDigest);
Expand Down Expand Up @@ -1175,6 +1178,9 @@ class Compiler {
JavascriptModulesPlugin.getCompilationHooks(this.#compilation!)
.chunkHash,
queried => (chunk: binding.JsChunk) => {
if (!this.options.output.hashFunction) {
throw new Error("'output.hashFunction' cannot be undefined");
}
const hash = createHash(this.options.output.hashFunction);
queried.call(Chunk.__from_binding(chunk, this.#compilation!), hash);
const digestResult = hash.digest(this.options.output.hashDigest);
Expand Down
169 changes: 0 additions & 169 deletions packages/rspack/src/ModuleTypeConstants.js

This file was deleted.

Loading

2 comments on commit 1b5fbdf

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-07-21 ff2771c) Current Change
10000_development-mode + exec 2.25 s ± 12 ms 2.26 s ± 25 ms +0.52 %
10000_development-mode_hmr + exec 702 ms ± 6.3 ms 701 ms ± 10 ms -0.13 %
10000_production-mode + exec 2.85 s ± 31 ms 2.85 s ± 28 ms -0.00 %
arco-pro_development-mode + exec 1.89 s ± 44 ms 1.92 s ± 55 ms +1.59 %
arco-pro_development-mode_hmr + exec 436 ms ± 3.3 ms 434 ms ± 1.8 ms -0.36 %
arco-pro_production-mode + exec 3.47 s ± 91 ms 3.5 s ± 83 ms +0.89 %
threejs_development-mode_10x + exec 1.76 s ± 19 ms 1.77 s ± 15 ms +0.23 %
threejs_development-mode_10x_hmr + exec 860 ms ± 6.3 ms 862 ms ± 8 ms +0.27 %
threejs_production-mode_10x + exec 5.73 s ± 21 ms 5.74 s ± 31 ms +0.09 %

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ❌ failure
_selftest ✅ success
nx ✅ success
rspress ✅ success
rsbuild ❌ failure
examples ❌ failure

Please sign in to comment.