Skip to content

Commit

Permalink
refactor: cleanup JavaScript API (#6831)
Browse files Browse the repository at this point in the history
feat: init
  • Loading branch information
h-a-n-a authored Jun 18, 2024
1 parent 553f785 commit 66dcc09
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 61 deletions.
11 changes: 4 additions & 7 deletions packages/rspack/etc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ export class Chunk {
// (undocumented)
static __from_binding(chunk: JsChunk, compilation: JsCompilation): Chunk;
// (undocumented)
__internal_inner_ukey(): number;
__internal_innerUkey(): number;
// (undocumented)
auxiliaryFiles: Array<string>;
// (undocumented)
Expand Down Expand Up @@ -905,9 +905,9 @@ export class ChunkGroup {
// (undocumented)
static __from_binding(chunk: JsChunkGroup, compilation: JsCompilation): ChunkGroup;
// (undocumented)
__internal_inner_compilation(): JsCompilation;
__internal_innerCompilation(): JsCompilation;
// (undocumented)
__internal_inner_ukey(): number;
__internal_innerUkey(): number;
// (undocumented)
get chunks(): Chunk[];
// (undocumented)
Expand Down Expand Up @@ -983,7 +983,7 @@ export class Compilation {
__internal__pushNativeDiagnostics(diagnostics: ExternalObject<"Diagnostic[]">): void;
// @internal
__internal__setAssetSource(filename: string, source: Source): void;
// (undocumented)
// @internal
__internal_getInner(): JsCompilation;
get assets(): Record<string, Source>;
// (undocumented)
Expand Down Expand Up @@ -5026,8 +5026,6 @@ export class Module {
// (undocumented)
originalSource(): Source | null;
// (undocumented)
_originalSource?: Source;
// (undocumented)
rawRequest?: string;
// (undocumented)
request?: string;
Expand Down Expand Up @@ -11771,7 +11769,6 @@ type Rules = Rule[] | Rule;
// @public (undocumented)
class RuleSetCompiler {
constructor();
// (undocumented)
builtinReferences: Map<string, any>;
// (undocumented)
references: Map<string, any>;
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack/src/Chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export class Chunk {
);
}

__internal_inner_ukey() {
__internal_innerUkey() {
return this.#inner.__inner_ukey;
}
}
10 changes: 5 additions & 5 deletions packages/rspack/src/ChunkGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ export class ChunkGraph {

getChunkModules(chunk: Chunk): Module[] {
return __chunk_graph_inner_get_chunk_modules(
chunk.__internal_inner_ukey(),
chunk.__internal_innerUkey(),
this.compilation.__internal_getInner()
).map(m => Module.__from_binding(m, this.compilation));
}

getChunkModulesIterable(chunk: Chunk): Iterable<Module> {
return new Set(
__chunk_graph_inner_get_chunk_modules(
chunk.__internal_inner_ukey(),
chunk.__internal_innerUkey(),
this.compilation.__internal_getInner()
).map(m => Module.__from_binding(m, this.compilation))
);
Expand All @@ -31,7 +31,7 @@ export class ChunkGraph {
getChunkEntryModulesIterable(chunk: Chunk): Iterable<Module> {
return new Set(
__chunk_graph_inner_get_chunk_entry_modules(
chunk.__internal_inner_ukey(),
chunk.__internal_innerUkey(),
this.compilation.__internal_getInner()
).map(m => Module.__from_binding(m, this.compilation))
);
Expand All @@ -40,7 +40,7 @@ export class ChunkGraph {
getChunkEntryDependentChunksIterable(chunk: Chunk): Iterable<Chunk> {
return new Set(
__chunk_graph_inner_get_chunk_entry_dependent_chunks_iterable(
chunk.__internal_inner_ukey(),
chunk.__internal_innerUkey(),
this.compilation.__internal_getInner()
).map(c =>
Chunk.__from_binding(c, this.compilation.__internal_getInner())
Expand All @@ -54,7 +54,7 @@ export class ChunkGraph {
): Iterable<Module> {
return new Set(
__chunk_graph_inner_get_chunk_modules_iterable_by_source_type(
chunk.__internal_inner_ukey(),
chunk.__internal_innerUkey(),
sourceType,
this.compilation.__internal_getInner()
).map(m => Module.__from_binding(m, this.compilation))
Expand Down
16 changes: 8 additions & 8 deletions packages/rspack/src/ChunkGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import { Chunk } from "./Chunk";

export class ChunkGroup {
#inner: JsChunkGroup;
#inner_compilation: JsCompilation;
#innerCompilation: JsCompilation;

static __from_binding(chunk: JsChunkGroup, compilation: JsCompilation) {
return new ChunkGroup(chunk, compilation);
}

protected constructor(inner: JsChunkGroup, compilation: JsCompilation) {
this.#inner = inner;
this.#inner_compilation = compilation;
this.#innerCompilation = compilation;
}

getFiles(): string[] {
Expand All @@ -35,15 +35,15 @@ export class ChunkGroup {
return this.#inner.__inner_parents.map(parent => {
const cg = __chunk_group_inner_get_chunk_group(
parent,
this.#inner_compilation
this.#innerCompilation
);
return ChunkGroup.__from_binding(cg, this.#inner_compilation);
return ChunkGroup.__from_binding(cg, this.#innerCompilation);
});
}

get chunks(): Chunk[] {
return this.#inner.chunks.map(c =>
Chunk.__from_binding(c, this.#inner_compilation)
Chunk.__from_binding(c, this.#innerCompilation)
);
}

Expand All @@ -55,11 +55,11 @@ export class ChunkGroup {
return this.#inner.name;
}

__internal_inner_ukey() {
__internal_innerUkey() {
return this.#inner.__inner_ukey;
}

__internal_inner_compilation() {
return this.#inner_compilation;
__internal_innerCompilation() {
return this.#innerCompilation;
}
}
5 changes: 5 additions & 0 deletions packages/rspack/src/Compilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,11 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
.map(c => Chunk.__from_binding(c, this.#inner));
}

/**
* Note: This is not a webpack public API, maybe removed in future.
*
* @internal
*/
__internal_getInner() {
return this.#inner;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/rspack/src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import {
import { rspack } from "./index";
import * as liteTapable from "./lite-tapable";
import ResolverFactory = require("./ResolverFactory");
import { ThreadsafeWritableNodeFS } from "./FileSystem";
import ConcurrentCompilationError from "./error/ConcurrentCompilationError";
import { ThreadsafeWritableNodeFS } from "./fileSystem";
import Cache = require("./lib/Cache");
import CacheFacade = require("./lib/CacheFacade");
import { Source } from "webpack-sources";
Expand Down Expand Up @@ -1114,7 +1114,7 @@ class Compiler {
rawOptions,
this.#builtinPlugins,
this.#registers,
ThreadsafeWritableNodeFS.__into_binding(this.outputFileSystem!)
ThreadsafeWritableNodeFS.__to_binding(this.outputFileSystem!)
);

callback(null, this.#instance);
Expand Down
29 changes: 0 additions & 29 deletions packages/rspack/src/ContextModuleFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import * as liteTapable from "./lite-tapable";

export class ContextModuleFactory {
hooks: {
// TODO: second param resolveData
// resolveForScheme: HookMap<
// AsyncSeriesBailHook<[ResourceDataWithData], true | void>
// >;
beforeResolve: liteTapable.AsyncSeriesWaterfallHook<
[ContextModuleFactoryBeforeResolveResult],
ContextModuleFactoryBeforeResolveResult | void
Expand All @@ -21,33 +17,8 @@ export class ContextModuleFactory {
};
constructor() {
this.hooks = {
// /** @type {AsyncSeriesBailHook<[ResolveData], Module | false | void>} */
// resolve: new AsyncSeriesBailHook(["resolveData"]),
// /** @type {HookMap<AsyncSeriesBailHook<[ResourceDataWithData, ResolveData], true | void>>} */
// resolveForScheme: new HookMap(
// () => new AsyncSeriesBailHook(["resourceData"])
// ),
// /** @type {HookMap<AsyncSeriesBailHook<[ResourceDataWithData, ResolveData], true | void>>} */
// resolveInScheme: new HookMap(
// () => new AsyncSeriesBailHook(["resourceData", "resolveData"])
// ),
// /** @type {AsyncSeriesBailHook<[ResolveData], Module>} */
// factorize: new AsyncSeriesBailHook(["resolveData"]),
// /** @type {AsyncSeriesBailHook<[ResolveData], false | void>} */
beforeResolve: new liteTapable.AsyncSeriesWaterfallHook(["resolveData"]),
afterResolve: new liteTapable.AsyncSeriesWaterfallHook(["resolveData"])
// /** @type {AsyncSeriesBailHook<[ResolveData["createData"], ResolveData], Module | void>} */
// createModule: new AsyncSeriesBailHook(["createData", "resolveData"]),
// /** @type {SyncWaterfallHook<[Module, ResolveData["createData"], ResolveData], Module>} */
// module: new SyncWaterfallHook(["module", "createData", "resolveData"]),
// createParser: new HookMap(() => new SyncBailHook(["parserOptions"])),
// parser: new HookMap(() => new SyncHook(["parser", "parserOptions"])),
// createGenerator: new HookMap(
// () => new SyncBailHook(["generatorOptions"])
// ),
// generator: new HookMap(
// () => new SyncHook(["generator", "generatorOptions"])
// )
};
}
}
6 changes: 3 additions & 3 deletions packages/rspack/src/Entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export class Entrypoint extends ChunkGroup {

getRuntimeChunk(): Chunk | null {
const c = __entrypoint_inner_get_runtime_chunk(
this.__internal_inner_ukey(),
this.__internal_inner_compilation()
this.__internal_innerUkey(),
this.__internal_innerCompilation()
);
if (c) return Chunk.__from_binding(c, this.__internal_inner_compilation());
if (c) return Chunk.__from_binding(c, this.__internal_innerCompilation());
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ThreadsafeWritableNodeFS implements ThreadsafeNodeFS {
this.removeDirAll = memoizeFn(() => util.promisify(rmrf.bind(null, fs)));
}

static __into_binding(fs?: OutputFileSystem) {
static __to_binding(fs?: OutputFileSystem) {
return new this(fs);
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/rspack/src/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export type ContextModuleFactoryAfterResolveResult =

export class Module {
#inner: JsModule;
_originalSource?: Source;
#originalSource?: Source;

context?: string;
resource?: string;
Expand Down Expand Up @@ -89,12 +89,12 @@ export class Module {
}

originalSource(): Source | null {
if (this._originalSource) return this._originalSource;
if (this.#originalSource) return this.#originalSource;
if (this.#inner.originalSource) {
this._originalSource = JsSource.__from_binding(
this.#originalSource = JsSource.__from_binding(
this.#inner.originalSource
);
return this._originalSource;
return this.#originalSource;
} else {
return null;
}
Expand Down
4 changes: 3 additions & 1 deletion packages/rspack/src/RuleSetCompiler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class RuleSetCompiler {
public references: Map<string, any>;
// builtin references that should be serializable
/**
* builtin references that should be serializable and passed to Rust.
*/
public builtinReferences: Map<string, any>;
constructor() {
this.references = new Map();
Expand Down

2 comments on commit 66dcc09

@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-06-18 553f785) Current Change
10000_development-mode + exec 2.21 s ± 22 ms 2.22 s ± 24 ms +0.05 %
10000_development-mode_hmr + exec 737 ms ± 12 ms 739 ms ± 13 ms +0.27 %
10000_production-mode + exec 2.57 s ± 22 ms 2.57 s ± 26 ms +0.12 %
arco-pro_development-mode + exec 1.94 s ± 68 ms 1.91 s ± 96 ms -1.44 %
arco-pro_development-mode_hmr + exec 441 ms ± 2.1 ms 442 ms ± 1.6 ms +0.15 %
arco-pro_production-mode + exec 3.53 s ± 85 ms 3.6 s ± 177 ms +2.08 %
threejs_development-mode_10x + exec 1.46 s ± 11 ms 1.47 s ± 13 ms +0.41 %
threejs_development-mode_10x_hmr + exec 799 ms ± 3.3 ms 805 ms ± 13 ms +0.79 %
threejs_production-mode_10x + exec 4.76 s ± 33 ms 4.77 s ± 20 ms +0.18 %

@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 ✅ success
compat ✅ success
examples ✅ success

Please sign in to comment.