Skip to content

Commit

Permalink
feat: deprecate compiler apis
Browse files Browse the repository at this point in the history
  • Loading branch information
h-a-n-a committed May 10, 2024
1 parent 4b01ca0 commit 68854af
Show file tree
Hide file tree
Showing 33 changed files with 1,178 additions and 828 deletions.
10 changes: 5 additions & 5 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1435,10 +1435,10 @@ export interface RegisterJsTaps {
export function runBuiltinLoader(builtin: string, options: string | undefined | null, loaderContext: JsLoaderContext): Promise<JsLoaderContext>

export interface ThreadsafeNodeFS {
writeFile: (name: string, content: Buffer) => void
removeFile: (name: string) => void
mkdir: (name: string) => void
mkdirp: (name: string) => string | void
removeDirAll: (name: string) => string | void
writeFile: (name: string, content: Buffer) => Promise<void> | void
removeFile: (name: string) => Promise<void> | void
mkdir: (name: string) => Promise<void> | void
mkdirp: (name: string) => Promise<string | void> | string | void
removeDirAll: (name: string) => Promise<string | void> | string | void
}

10 changes: 5 additions & 5 deletions crates/rspack_fs_node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ cfg_async! {

#[napi(object, object_to_js = false, js_name = "ThreadsafeNodeFS")]
pub struct ThreadsafeNodeFS {
#[napi(ts_type = "(name: string, content: Buffer) => void")]
#[napi(ts_type = "(name: string, content: Buffer) => Promise<void> | void")]
pub write_file: ThreadsafeFunction<(String, Buffer), ()>,
#[napi(ts_type = "(name: string) => void")]
#[napi(ts_type = "(name: string) => Promise<void> | void")]
pub remove_file: ThreadsafeFunction<String, ()>,
#[napi(ts_type = "(name: string) => void")]
#[napi(ts_type = "(name: string) => Promise<void> | void")]
pub mkdir: ThreadsafeFunction<String, ()>,
#[napi(ts_type = "(name: string) => string | void")]
#[napi(ts_type = "(name: string) => Promise<string | void> | string | void")]
pub mkdirp: ThreadsafeFunction<String, Either<String, ()>>,
#[napi(ts_type = "(name: string) => string | void")]
#[napi(ts_type = "(name: string) => Promise<string | void> | string | void")]
pub remove_dir_all: ThreadsafeFunction<String, Either<String, ()>>,
}
}
6 changes: 5 additions & 1 deletion packages/rspack-cli/tests/build/issue-6359/rspack.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const path = require("path");
const { WEBPACK_SERVE } = process.env;
module.exports = /** @type {import('@rspack/cli').Configuration} */ {
mode: "production",
entry: "./entry.js",
output: { clean: true },
output: {
clean: true,
path: path.resolve(__dirname, "dist")
},
plugins: [
{
apply(compiler) {
Expand Down
4 changes: 2 additions & 2 deletions packages/rspack-dev-server/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ export function getRspackMemoryAssets(
: // @ts-expect-error
path.slice(1);
const buffer =
compiler.getAsset(filename) ??
compiler._lastCompilation?.getAsset(filename) ??
(() => {
const { index } = rdm.context.options;
const indexValue =
typeof index === "undefined" || typeof index === "boolean"
? "index.html"
: index;
return compiler.getAsset(filename + indexValue);
return compiler._lastCompilation?.getAsset(filename + indexValue);
})();
if (!buffer) {
return next();
Expand Down
4 changes: 2 additions & 2 deletions packages/rspack-dev-server/tests/normalizeOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe("normalize options snapshot", () => {
});
const server = new RspackDevServer({}, compiler);
await server.start();
const hmrPlugins = compiler.builtinPlugins.filter(
const hmrPlugins = compiler.__internal__builtinPlugins.filter(
p => p.name === "HotModuleReplacementPlugin"
);
expect(hmrPlugins.length).toBe(1);
Expand Down Expand Up @@ -149,7 +149,7 @@ async function getAdditionEntries(

const server = new RspackDevServer(serverConfig, compiler);
await server.start();
const entries = compiler.builtinPlugins
const entries = compiler.__internal__builtinPlugins
.filter(p => p.name === "EntryPlugin")
.map(p => p.options)
.reduce<Object>((acc, cur: any) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/rspack-test-tools/etc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,8 @@ export interface IStatsAPITaskProcessorOptions<T extends ECompilerType> {
// (undocumented)
check?: (stats: TCompilerStats<T>, compiler: TCompiler<T>) => Promise<void>;
// (undocumented)
compiler?: (context: ITestContext, compiler: TCompiler<T>) => Promise<void>;
// (undocumented)
compilerType: T;
// (undocumented)
cwd?: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack-test-tools/src/processor/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class SnapshotProcessor<
);
}
const compilation =
(c as RspackCompiler).compilation ||
(c as RspackCompiler)._lastCompilation ||
(
c as WebpackCompiler & {
_lastCompilation: WebpackCompilation;
Expand Down
4 changes: 3 additions & 1 deletion packages/rspack-test-tools/src/processor/stats-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface IStatsAPITaskProcessorOptions<T extends ECompilerType> {
name: string;
cwd?: string;
compilerType: T;
compiler?: (context: ITestContext, compiler: TCompiler<T>) => Promise<void>;
build?: (context: ITestContext, compiler: TCompiler<T>) => Promise<void>;
check?: (stats: TCompilerStats<T>, compiler: TCompiler<T>) => Promise<void>;
}
Expand All @@ -28,7 +29,8 @@ export class StatsAPITaskProcessor<
options: _statsAPIOptions.options,
build: _statsAPIOptions.build,
compilerType: _statsAPIOptions.compilerType,
name: _statsAPIOptions.name
name: _statsAPIOptions.name,
compiler: _statsAPIOptions.compiler
});
}

Expand Down
60 changes: 30 additions & 30 deletions packages/rspack-test-tools/tests/errorCases/error-test-shift.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
/** @type {import('../..').TErrorCaseConfig} */
module.exports = {
description: "Testing proxy methods on errors: test shift&unshift",
options() {
return {
entry: "./resolve-fail-esm",
plugins: [
compiler => {
compiler.hooks.afterCompile.tap(
"test shift and unshift",
compilation => {
compilation.errors.shift();
compilation.errors.unshift("test unshift");
}
);
}
]
};
},
async check(diagnostics) {
expect(diagnostics).toMatchInlineSnapshot(`
Object {
"errors": Array [
Object {
"formatted": " × test unshift\\n",
"message": " × test unshift\\n",
},
],
"warnings": Array [],
}
`);
}
description: "Testing proxy methods on errors: test shift&unshift",
options() {
return {
entry: "./resolve-fail-esm",
plugins: [
compiler => {
compiler.hooks.afterCompile.tap(
"test shift and unshift",
compilation => {
compilation.errors.shift();
compilation.errors.unshift("test unshift");
}
);
}
]
};
},
async check(diagnostics) {
expect(diagnostics).toMatchInlineSnapshot(`
Object {
"errors": Array [
Object {
"formatted": " × test unshift\\n",
"message": " × test unshift\\n",
},
],
"warnings": Array [],
}
`);
}
};
52 changes: 26 additions & 26 deletions packages/rspack-test-tools/tests/errorCases/error-test-splice-1.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
/** @type {import('../..').TErrorCaseConfig} */
module.exports = {
description: "Testing proxy methods on errors: test splice 1",
options() {
return {
entry: "./resolve-fail-esm",
plugins: [
compiler => {
compiler.hooks.afterCompile.tap("test splice", compilation => {
compilation.errors.splice(0, 1, "test splice");
});
}
]
};
},
async check(diagnostics) {
expect(diagnostics).toMatchInlineSnapshot(`
Object {
"errors": Array [
Object {
"formatted": " × test splice\\n",
"message": " × test splice\\n",
},
],
"warnings": Array [],
}
`);
}
description: "Testing proxy methods on errors: test splice 1",
options() {
return {
entry: "./resolve-fail-esm",
plugins: [
compiler => {
compiler.hooks.afterCompile.tap("test splice", compilation => {
compilation.errors.splice(0, 1, "test splice");
});
}
]
};
},
async check(diagnostics) {
expect(diagnostics).toMatchInlineSnapshot(`
Object {
"errors": Array [
Object {
"formatted": " × test splice\\n",
"message": " × test splice\\n",
},
],
"warnings": Array [],
}
`);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ module.exports = {
"errors": Array [],
"warnings": Array [
Object {
"formatted": " ⚠ Error: test push\\n │ at <cwd>packages/rspack-test-tools/tests/errorCases/warning-test-push.js:10:33\\n │ at Hook.eval [as callAsync] (eval at create (<cwd>node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:9:1)\\n │ at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (<cwd>node_modules/tapable/lib/Hook.js:18:14)\\n │ at <cwd>packages/rspack/dist/Compiler.js:419:41\\n │ at <cwd>packages/rspack/dist/Compiler.js:751:65\\n",
"message": " ⚠ Error: test push\\n │ at <cwd>packages/rspack-test-tools/tests/errorCases/warning-test-push.js:10:33\\n │ at Hook.eval [as callAsync] (eval at create (<cwd>node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:9:1)\\n │ at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (<cwd>node_modules/tapable/lib/Hook.js:18:14)\\n │ at <cwd>packages/rspack/dist/Compiler.js:419:41\\n │ at <cwd>packages/rspack/dist/Compiler.js:751:65\\n",
"formatted": " ⚠ Error: test push\\n │ at <cwd>packages/rspack-test-tools/tests/errorCases/warning-test-push.js:10:33\\n │ at Hook.eval [as callAsync] (eval at create (<cwd>node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:9:1)\\n │ at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (<cwd>node_modules/tapable/lib/Hook.js:18:14)\\n │ at <cwd>packages/rspack/dist/Compiler.js:466:41\\n │ at <cwd>packages/rspack/dist/Compiler.js:533:65\\n",
"message": " ⚠ Error: test push\\n │ at <cwd>packages/rspack-test-tools/tests/errorCases/warning-test-push.js:10:33\\n │ at Hook.eval [as callAsync] (eval at create (<cwd>node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:9:1)\\n │ at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (<cwd>node_modules/tapable/lib/Hook.js:18:14)\\n │ at <cwd>packages/rspack/dist/Compiler.js:466:41\\n │ at <cwd>packages/rspack/dist/Compiler.js:533:65\\n",
},
Object {
"formatted": " ⚠ Module parse warning:\\n ╰─▶ ⚠ Module parse failed: require.main.require() is not supported by Rspack.\\n ╭────\\n 1 │ require.main.require('./file');\\n · ──────────────────────────────\\n ╰────\\n \\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ module.exports = {
"errors": Array [],
"warnings": Array [
Object {
"formatted": " ⚠ Error: test unshift\\n │ at <cwd>packages/rspack-test-tools/tests/errorCases/warning-test-shift.js:13:37\\n │ at Hook.eval [as callAsync] (eval at create (<cwd>node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:9:1)\\n │ at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (<cwd>node_modules/tapable/lib/Hook.js:18:14)\\n │ at <cwd>packages/rspack/dist/Compiler.js:419:41\\n │ at <cwd>packages/rspack/dist/Compiler.js:751:65\\n",
"message": " ⚠ Error: test unshift\\n │ at <cwd>packages/rspack-test-tools/tests/errorCases/warning-test-shift.js:13:37\\n │ at Hook.eval [as callAsync] (eval at create (<cwd>node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:9:1)\\n │ at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (<cwd>node_modules/tapable/lib/Hook.js:18:14)\\n │ at <cwd>packages/rspack/dist/Compiler.js:419:41\\n │ at <cwd>packages/rspack/dist/Compiler.js:751:65\\n",
"formatted": " ⚠ Error: test unshift\\n │ at <cwd>packages/rspack-test-tools/tests/errorCases/warning-test-shift.js:13:37\\n │ at Hook.eval [as callAsync] (eval at create (<cwd>node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:9:1)\\n │ at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (<cwd>node_modules/tapable/lib/Hook.js:18:14)\\n │ at <cwd>packages/rspack/dist/Compiler.js:466:41\\n │ at <cwd>packages/rspack/dist/Compiler.js:533:65\\n",
"message": " ⚠ Error: test unshift\\n │ at <cwd>packages/rspack-test-tools/tests/errorCases/warning-test-shift.js:13:37\\n │ at Hook.eval [as callAsync] (eval at create (<cwd>node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:9:1)\\n │ at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (<cwd>node_modules/tapable/lib/Hook.js:18:14)\\n │ at <cwd>packages/rspack/dist/Compiler.js:466:41\\n │ at <cwd>packages/rspack/dist/Compiler.js:533:65\\n",
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ module.exports = {
"errors": Array [],
"warnings": Array [
Object {
"formatted": " ⚠ Error: test splice\\n │ at <cwd>packages/rspack-test-tools/tests/errorCases/warning-test-splice-1.js:10:41\\n │ at Hook.eval [as callAsync] (eval at create (<cwd>node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:9:1)\\n │ at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (<cwd>node_modules/tapable/lib/Hook.js:18:14)\\n │ at <cwd>packages/rspack/dist/Compiler.js:419:41\\n │ at <cwd>packages/rspack/dist/Compiler.js:751:65\\n",
"message": " ⚠ Error: test splice\\n │ at <cwd>packages/rspack-test-tools/tests/errorCases/warning-test-splice-1.js:10:41\\n │ at Hook.eval [as callAsync] (eval at create (<cwd>node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:9:1)\\n │ at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (<cwd>node_modules/tapable/lib/Hook.js:18:14)\\n │ at <cwd>packages/rspack/dist/Compiler.js:419:41\\n │ at <cwd>packages/rspack/dist/Compiler.js:751:65\\n",
"formatted": " ⚠ Error: test splice\\n │ at <cwd>packages/rspack-test-tools/tests/errorCases/warning-test-splice-1.js:10:41\\n │ at Hook.eval [as callAsync] (eval at create (<cwd>node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:9:1)\\n │ at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (<cwd>node_modules/tapable/lib/Hook.js:18:14)\\n │ at <cwd>packages/rspack/dist/Compiler.js:466:41\\n │ at <cwd>packages/rspack/dist/Compiler.js:533:65\\n",
"message": " ⚠ Error: test splice\\n │ at <cwd>packages/rspack-test-tools/tests/errorCases/warning-test-splice-1.js:10:41\\n │ at Hook.eval [as callAsync] (eval at create (<cwd>node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:9:1)\\n │ at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (<cwd>node_modules/tapable/lib/Hook.js:18:14)\\n │ at <cwd>packages/rspack/dist/Compiler.js:466:41\\n │ at <cwd>packages/rspack/dist/Compiler.js:533:65\\n",
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ module.exports = {
"errors": Array [],
"warnings": Array [
Object {
"formatted": " ⚠ Error: test splice\\n │ at <cwd>packages/rspack-test-tools/tests/errorCases/warning-test-splice-2.js:10:41\\n │ at Hook.eval [as callAsync] (eval at create (<cwd>node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:9:1)\\n │ at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (<cwd>node_modules/tapable/lib/Hook.js:18:14)\\n │ at <cwd>packages/rspack/dist/Compiler.js:419:41\\n │ at <cwd>packages/rspack/dist/Compiler.js:751:65\\n",
"message": " ⚠ Error: test splice\\n │ at <cwd>packages/rspack-test-tools/tests/errorCases/warning-test-splice-2.js:10:41\\n │ at Hook.eval [as callAsync] (eval at create (<cwd>node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:9:1)\\n │ at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (<cwd>node_modules/tapable/lib/Hook.js:18:14)\\n │ at <cwd>packages/rspack/dist/Compiler.js:419:41\\n │ at <cwd>packages/rspack/dist/Compiler.js:751:65\\n",
"formatted": " ⚠ Error: test splice\\n │ at <cwd>packages/rspack-test-tools/tests/errorCases/warning-test-splice-2.js:10:41\\n │ at Hook.eval [as callAsync] (eval at create (<cwd>node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:9:1)\\n │ at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (<cwd>node_modules/tapable/lib/Hook.js:18:14)\\n │ at <cwd>packages/rspack/dist/Compiler.js:466:41\\n │ at <cwd>packages/rspack/dist/Compiler.js:533:65\\n",
"message": " ⚠ Error: test splice\\n │ at <cwd>packages/rspack-test-tools/tests/errorCases/warning-test-splice-2.js:10:41\\n │ at Hook.eval [as callAsync] (eval at create (<cwd>node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:9:1)\\n │ at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (<cwd>node_modules/tapable/lib/Hook.js:18:14)\\n │ at <cwd>packages/rspack/dist/Compiler.js:466:41\\n │ at <cwd>packages/rspack/dist/Compiler.js:533:65\\n",
},
Object {
"formatted": " ⚠ Module parse warning:\\n ╰─▶ ⚠ Module parse failed: require.main.require() is not supported by Rspack.\\n ╭────\\n 1 │ require.main.require('./file');\\n · ──────────────────────────────\\n ╰────\\n \\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe("Output", () => {
});

it("should be cleared the build directory", done => {
const outputDist = "dist/output";
const outputDist = path.resolve(__dirname, "../js/legacyTest");
compile(
"./a",
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = {
});
},
async check(_, compiler) {
const stats = new Stats(compiler.compilation).toString({
const stats = new Stats(compiler._lastCompilation).toString({
all: false,
logging: "verbose"
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = {
});
},
async check(_, compiler) {
const stats = new Stats(compiler.compilation).toString({
const stats = new Stats(compiler._lastCompilation).toString({
all: false,
logging: "verbose"
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { createFsFromVolume, Volume } = require("memfs")
let statsJson;

class TestPlugin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class TestPlugin {
apply(compiler) {
compiler.hooks.thisCompilation.tap("custom", compilation => {
compilation.hooks.optimizeModules.tap("test plugin", () => {
stats = compiler.compilation.getStats().toJson({});
stats = compiler._lastCompilation.getStats().toJson({});
});
});
}
Expand Down
Loading

0 comments on commit 68854af

Please sign in to comment.