Skip to content

Commit

Permalink
feat: add setters for errors and warnings in Compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
inottn committed Oct 14, 2024
1 parent 69e3bc3 commit b2811e2
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
38 changes: 38 additions & 0 deletions packages/rspack-test-tools/tests/errorCases/error-test-set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/** @type {import('../..').TErrorCaseConfig} */
module.exports = {
description: "Testing set errors",
options() {
return {
entry: "./resolve-fail-esm",
plugins: [
compiler => {
compiler.hooks.afterCompile.tap("set errors", compilation => {
compilation.errors = [
new Error("error 1"),
new Error("error 2")
];
});
}
]
};
},
async check(diagnostics) {
expect(diagnostics).toMatchInlineSnapshot(`
Object {
"errors": Array [
Object {
"message": " × Error: error 1\\n │ at xxx\\n │ at xxx\\n │ at xxx\\n │ at xxx\\n │ at xxx\\n │ at xxx\\n",
"moduleTrace": Array [],
"stack": "Error: error 1\\n at Object.fn (<cwd>packages/rspack-test-tools/tests/errorCases/error-test-set.js:<line>:<col>)\\n at next (<cwd>node_modules/.pnpm/@rspack+lite-tapable@1.0.1/node_modules/@rspack/lite-tapable/dist/index.js:<line>:<col>)\\n at AsyncSeriesHook.callAsyncStageRange (<cwd>node_modules/.pnpm/@rspack+lite-tapable@1.0.1/node_modules/@rspack/lite-tapable/dist/index.js:<line>:<col>)\\n at AsyncSeriesHook.callAsync (<cwd>node_modules/.pnpm/@rspack+lite-tapable@1.0.1/node_modules/@rspack/lite-tapable/dist/index.js:<line>:<col>)\\n at <cwd>packages/rspack/dist/index.js:<line>:<col>\\n at <cwd>packages/rspack/dist/index.js:<line>:<col>",
},
Object {
"message": " × Error: error 2\\n │ at xxx\\n │ at xxx\\n │ at xxx\\n │ at xxx\\n │ at xxx\\n │ at xxx\\n",
"moduleTrace": Array [],
"stack": "Error: error 2\\n at Object.fn (<cwd>packages/rspack-test-tools/tests/errorCases/error-test-set.js:<line>:<col>)\\n at next (<cwd>node_modules/.pnpm/@rspack+lite-tapable@1.0.1/node_modules/@rspack/lite-tapable/dist/index.js:<line>:<col>)\\n at AsyncSeriesHook.callAsyncStageRange (<cwd>node_modules/.pnpm/@rspack+lite-tapable@1.0.1/node_modules/@rspack/lite-tapable/dist/index.js:<line>:<col>)\\n at AsyncSeriesHook.callAsync (<cwd>node_modules/.pnpm/@rspack+lite-tapable@1.0.1/node_modules/@rspack/lite-tapable/dist/index.js:<line>:<col>)\\n at <cwd>packages/rspack/dist/index.js:<line>:<col>\\n at <cwd>packages/rspack/dist/index.js:<line>:<col>",
},
],
"warnings": Array [],
}
`);
}
};
38 changes: 38 additions & 0 deletions packages/rspack-test-tools/tests/errorCases/warning-test-set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/** @type {import('../..').TErrorCaseConfig} */
module.exports = {
description: "Testing set warnings",
options() {
return {
entry: "./require.main.require",
plugins: [
compiler => {
compiler.hooks.afterCompile.tap("set warnings", compilation => {
compilation.warnings = [
new Error("warning 1"),
new Error("warning 2")
];
});
}
]
};
},
async check(diagnostics) {
expect(diagnostics).toMatchInlineSnapshot(`
Object {
"errors": Array [],
"warnings": Array [
Object {
"message": " ⚠ Error: warning 1\\n │ at xxx\\n │ at xxx\\n │ at xxx\\n │ at xxx\\n │ at xxx\\n │ at xxx\\n",
"moduleTrace": Array [],
"stack": "Error: warning 1\\n at Object.fn (<cwd>packages/rspack-test-tools/tests/errorCases/warning-test-set.js:<line>:<col>)\\n at next (<cwd>node_modules/.pnpm/@rspack+lite-tapable@1.0.1/node_modules/@rspack/lite-tapable/dist/index.js:<line>:<col>)\\n at AsyncSeriesHook.callAsyncStageRange (<cwd>node_modules/.pnpm/@rspack+lite-tapable@1.0.1/node_modules/@rspack/lite-tapable/dist/index.js:<line>:<col>)\\n at AsyncSeriesHook.callAsync (<cwd>node_modules/.pnpm/@rspack+lite-tapable@1.0.1/node_modules/@rspack/lite-tapable/dist/index.js:<line>:<col>)\\n at <cwd>packages/rspack/dist/index.js:<line>:<col>\\n at <cwd>packages/rspack/dist/index.js:<line>:<col>",
},
Object {
"message": " ⚠ Error: warning 2\\n │ at xxx\\n │ at xxx\\n │ at xxx\\n │ at xxx\\n │ at xxx\\n │ at xxx\\n",
"moduleTrace": Array [],
"stack": "Error: warning 2\\n at Object.fn (<cwd>packages/rspack-test-tools/tests/errorCases/warning-test-set.js:<line>:<col>)\\n at next (<cwd>node_modules/.pnpm/@rspack+lite-tapable@1.0.1/node_modules/@rspack/lite-tapable/dist/index.js:<line>:<col>)\\n at AsyncSeriesHook.callAsyncStageRange (<cwd>node_modules/.pnpm/@rspack+lite-tapable@1.0.1/node_modules/@rspack/lite-tapable/dist/index.js:<line>:<col>)\\n at AsyncSeriesHook.callAsync (<cwd>node_modules/.pnpm/@rspack+lite-tapable@1.0.1/node_modules/@rspack/lite-tapable/dist/index.js:<line>:<col>)\\n at <cwd>packages/rspack/dist/index.js:<line>:<col>\\n at <cwd>packages/rspack/dist/index.js:<line>:<col>",
},
],
}
`);
}
};
2 changes: 2 additions & 0 deletions packages/rspack/etc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,7 @@ export class Compilation {
get entrypoints(): ReadonlyMap<string, Entrypoint>;
// (undocumented)
get errors(): RspackError[];
set errors(errors: RspackError[]);
// (undocumented)
fileDependencies: {
[Symbol.iterator](): Generator<string, void, unknown>;
Expand Down Expand Up @@ -1048,6 +1049,7 @@ export class Compilation {
updateAsset(filename: string, newSourceOrFunction: Source | ((source: Source) => Source), assetInfoUpdateOrFunction?: AssetInfo | ((assetInfo: AssetInfo) => AssetInfo)): void;
// (undocumented)
get warnings(): RspackError[];
set warnings(warnings: RspackError[]);
}

// @public (undocumented)
Expand Down
24 changes: 24 additions & 0 deletions packages/rspack/src/Compilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,18 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
return errors;
}

set errors(errors: RspackError[]) {
const inner = this.#inner;
const length = inner.getErrors().length;
inner.spliceDiagnostic(
0,
length,
errors.map(error => {
return JsRspackDiagnostic.__to_binding(error, JsRspackSeverity.Error);
})
);
}

get warnings(): RspackError[] {
const inner = this.#inner;
type WarnType = Error | RspackError;
Expand Down Expand Up @@ -893,6 +905,18 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
return warnings;
}

set warnings(warnings: RspackError[]) {
const inner = this.#inner;
const length = inner.getWarnings().length;
inner.spliceDiagnostic(
0,
length,
warnings.map(warning => {
return JsRspackDiagnostic.__to_binding(warning, JsRspackSeverity.Warn);
})
);
}

getPath(filename: Filename, data: PathData = {}) {
return this.#inner.getPath(filename, {
...data,
Expand Down

0 comments on commit b2811e2

Please sign in to comment.