From d83e34f52407f78060543de9b91fecc8e010add9 Mon Sep 17 00:00:00 2001 From: neverland Date: Sat, 14 Mar 2026 21:18:47 +0800 Subject: [PATCH 1/2] refactor: migrate from webpack to rspack --- README.md | 68 +-- jest.config.js | 1 - package.json | 8 +- pnpm-lock.yaml | 129 ++++- scripts/globalSetup.js | 7 +- src/index.js | 19 +- src/utils/getPaths.js | 29 +- src/utils/setupHooks.js | 34 +- src/utils/setupOutputFileSystem.js | 7 +- src/utils/setupWriteToDisk.js | 9 +- test/__snapshots__/logging.test.js | 387 +++++++++++++ .../logging.test.js.snap.webpack5 | 540 ------------------ test/helpers/getCompiler.js | 8 +- test/helpers/runner.js | 14 +- test/helpers/snapshotResolver.js | 11 +- test/middleware.test.js | 2 +- ...st.js.snap.webpack5 => setupHooks.test.js} | 0 ...snap.webpack5 => setupWriteToDisk.test.js} | 0 types/index.d.ts | 34 +- types/utils/getPaths.d.ts | 23 +- types/utils/setupHooks.d.ts | 20 +- types/utils/setupOutputFileSystem.d.ts | 8 +- types/utils/setupWriteToDisk.d.ts | 15 +- 23 files changed, 674 insertions(+), 699 deletions(-) create mode 100644 test/__snapshots__/logging.test.js delete mode 100644 test/__snapshots__/logging.test.js.snap.webpack5 rename test/utils/__snapshots__/{setupHooks.test.js.snap.webpack5 => setupHooks.test.js} (100%) rename test/utils/__snapshots__/{setupWriteToDisk.test.js.snap.webpack5 => setupWriteToDisk.test.js} (100%) diff --git a/README.md b/README.md index 00d997e..2f98d15 100644 --- a/README.md +++ b/README.md @@ -51,9 +51,9 @@ bun add -D @rspack/dev-middleware ```js const middleware = require("@rspack/dev-middleware"); const express = require("express"); -const webpack = require("webpack"); +const { rspack } = require("@rspack/core"); -const compiler = webpack({ +const compiler = rspack({ // webpack options }); @@ -261,12 +261,12 @@ This option also accepts a `Function` value, which can be used to filter which f The function follows the same premise as [`Array#filter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) in which a return value of `false` _will not_ write the file, and a return value of `true` _will_ write the file to disk. eg. ```js -const webpack = require("webpack"); +const { rspack } = require("@rspack/core"); const configuration = { /* Webpack configuration */ }; -const compiler = webpack(configuration); +const compiler = rspack(configuration); middleware(compiler, { writeToDisk: (filePath) => /superman\.css$/.test(filePath), @@ -289,12 +289,12 @@ This can be done simply by using `path.join`: const path = require("node:path"); const mkdirp = require("mkdirp"); const myOutputFileSystem = require("my-fs"); -const webpack = require("webpack"); +const { rspack } = require("@rspack/core"); myOutputFileSystem.join = path.join.bind(path); // no need to bind myOutputFileSystem.mkdirp = mkdirp.bind(mkdirp); // no need to bind -const compiler = webpack({ +const compiler = rspack({ /* Webpack configuration */ }); @@ -306,12 +306,12 @@ middleware(compiler, { outputFileSystem: myOutputFileSystem }); Allows to set up a callback to change the response data. ```js -const webpack = require("webpack"); +const { rspack } = require("@rspack/core"); const configuration = { /* Webpack configuration */ }; -const compiler = webpack(configuration); +const compiler = rspack(configuration); middleware(compiler, { // Note - if you send the `Range` header you will have `ReadStream` @@ -344,9 +344,9 @@ A function executed once the middleware has stopped watching. ```js const middleware = require("@rspack/dev-middleware"); const express = require("express"); -const webpack = require("webpack"); +const { rspack } = require("@rspack/core"); -const compiler = webpack({ +const compiler = rspack({ /* Webpack configuration */ }); @@ -379,9 +379,9 @@ A function executed once the middleware has invalidated. ```js const middleware = require("@rspack/dev-middleware"); const express = require("express"); -const webpack = require("webpack"); +const { rspack } = require("@rspack/core"); -const compiler = webpack({ +const compiler = rspack({ /* Webpack configuration */ }); @@ -419,9 +419,9 @@ If the bundle is valid at the time of calling, the callback is executed immediat ```js const middleware = require("@rspack/dev-middleware"); const express = require("express"); -const webpack = require("webpack"); +const { rspack } = require("@rspack/core"); -const compiler = webpack({ +const compiler = rspack({ /* Webpack configuration */ }); @@ -453,9 +453,9 @@ URL for the requested file. ```js const middleware = require("@rspack/dev-middleware"); const express = require("express"); -const webpack = require("webpack"); +const { rspack } = require("@rspack/core"); -const compiler = webpack({ +const compiler = rspack({ /* Webpack configuration */ }); @@ -484,9 +484,9 @@ But there is a solution to avoid it - mount the middleware to a non-root route, ```js const middleware = require("@rspack/dev-middleware"); const express = require("express"); -const webpack = require("webpack"); +const { rspack } = require("@rspack/core"); -const compiler = webpack({ +const compiler = rspack({ // webpack options }); @@ -526,9 +526,9 @@ Example Implementation: const middleware = require("@rspack/dev-middleware"); const express = require("express"); const isObject = require("is-object"); -const webpack = require("webpack"); +const { rspack } = require("@rspack/core"); -const compiler = webpack({ +const compiler = rspack({ /* Webpack configuration */ }); @@ -614,10 +614,10 @@ Examples of use with other servers will follow here. const http = require("node:http"); const devMiddleware = require("@rspack/dev-middleware"); const connect = require("connect"); -const webpack = require("webpack"); +const { rspack } = require("@rspack/core"); const webpackConfig = require("./webpack.config.js"); -const compiler = webpack(webpackConfig); +const compiler = rspack(webpackConfig); const devMiddlewareOptions = { // options }; @@ -635,10 +635,10 @@ const http = require("node:http"); const devMiddleware = require("@rspack/dev-middleware"); const finalhandler = require("finalhandler"); const Router = require("router"); -const webpack = require("webpack"); +const { rspack } = require("@rspack/core"); const webpackConfig = require("./webpack.config.js"); -const compiler = webpack(webpackConfig); +const compiler = rspack(webpackConfig); const devMiddlewareOptions = { // options }; @@ -660,10 +660,10 @@ server.listen(3000); ```js const devMiddleware = require("@rspack/dev-middleware"); const express = require("express"); -const webpack = require("webpack"); +const { rspack } = require("@rspack/core"); const webpackConfig = require("./webpack.config.js"); -const compiler = webpack(webpackConfig); +const compiler = rspack(webpackConfig); const devMiddlewareOptions = { // options }; @@ -679,10 +679,10 @@ app.listen(3000, () => console.log("Example app listening on port 3000!")); ```js const middleware = require("@rspack/dev-middleware"); const Koa = require("koa"); -const webpack = require("webpack"); +const { rspack } = require("@rspack/core"); const webpackConfig = require("./webpack.simple.config"); -const compiler = webpack(webpackConfig); +const compiler = rspack(webpackConfig); const devMiddlewareOptions = { // options }; @@ -698,10 +698,10 @@ app.listen(3000); ```js const Hapi = require("@hapi/hapi"); const devMiddleware = require("@rspack/dev-middleware"); -const webpack = require("webpack"); +const { rspack } = require("@rspack/core"); const webpackConfig = require("./webpack.config.js"); -const compiler = webpack(webpackConfig); +const compiler = rspack(webpackConfig); const devMiddlewareOptions = {}; const server = Hapi.server({ port: 3000, host: "localhost" }); @@ -732,10 +732,10 @@ Fastify interop will require the use of `fastify-express` instead of `middie` fo ```js const devMiddleware = require("@rspack/dev-middleware"); const fastify = require("fastify")(); -const webpack = require("webpack"); +const { rspack } = require("@rspack/core"); const webpackConfig = require("./webpack.config.js"); -const compiler = webpack(webpackConfig); +const compiler = rspack(webpackConfig); const devMiddlewareOptions = { // options }; @@ -751,10 +751,10 @@ await fastify.listen(3000); import { serve } from "@hono/node-server"; import devMiddleware from "@rspack/dev-middleware"; import { Hono } from "hono"; -import webpack from "webpack"; +import { rspack } from "@rspack/core"; import webpackConfig from "./webpack.config.js"; -const compiler = webpack(webpackConfig); +const compiler = rspack(webpackConfig); const devMiddlewareOptions = { // options }; diff --git a/jest.config.js b/jest.config.js index 3cf3fc3..886defe 100644 --- a/jest.config.js +++ b/jest.config.js @@ -6,5 +6,4 @@ module.exports = { testMatch: ["**/test/**/*.test.js"], setupFilesAfterEnv: ["/setupTest.js"], globalSetup: "/scripts/globalSetup.js", - snapshotResolver: "./test/helpers/snapshotResolver.js", }; diff --git a/package.json b/package.json index 77fc7b0..ceca051 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "range-parser": "^1.2.1" }, "devDependencies": { + "@rspack/core": "2.0.0-beta.5", "@babel/cli": "^7.16.7", "@babel/core": "^7.16.7", "@babel/preset-env": "^7.16.7", @@ -79,14 +80,13 @@ "prettier": "^3.6.0", "router": "^2.2.0", "supertest": "^7.0.0", - "typescript": "^5.3.3", - "webpack": "^5.101.0" + "typescript": "^5.3.3" }, "peerDependencies": { - "webpack": "^5.0.0" + "@rspack/core": "^2.0.0-0" }, "peerDependenciesMeta": { - "webpack": { + "@rspack/core": { "optional": true } }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 455df2f..0e85886 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -42,6 +42,9 @@ importers: '@hono/node-server': specifier: ^1.12.0 version: 1.19.11(hono@4.12.7) + '@rspack/core': + specifier: 2.0.0-beta.5 + version: 2.0.0-beta.5 '@types/connect': specifier: ^3.4.35 version: 3.4.38 @@ -117,9 +120,6 @@ importers: typescript: specifier: ^5.3.3 version: 5.9.3 - webpack: - specifier: ^5.101.0 - version: 5.105.4 packages: @@ -1306,6 +1306,9 @@ packages: '@napi-rs/wasm-runtime@0.2.12': resolution: {integrity: sha512-ZVWUcfwY4E/yPitQJl481FjFo3K22D6qF0DuFH6Y/nbnE11GY5uguDxZMGXPQ8WQ0128MXQD7TnfHyK4oWoIJQ==} + '@napi-rs/wasm-runtime@1.0.7': + resolution: {integrity: sha512-SeDnOO0Tk7Okiq6DbXmmBODgOAb9dp9gjlphokTUxmt8U3liIP1ZsozBahH69j/RJv+Rfs6IwUKHTgQYJ/HBAw==} + '@nicolo-ribaudo/chokidar-2@2.1.8-no-fsevents.3': resolution: {integrity: sha512-s88O1aVtXftvp5bCPB7WnmXc5IwOZZ7YPuwNPt+GtOOXpPvad1LfbmjYv+qII7zP6RU2QGnqve27dnLycEnyEQ==} @@ -1339,6 +1342,70 @@ packages: resolution: {integrity: sha512-QNqXyfVS2wm9hweSYD2O7F0G06uurj9kZ96TRQE5Y9hU7+tgdZwIkbAKc5Ocy1HxEY2kuDQa6cQ1WRs/O5LFKA==} engines: {node: ^12.20.0 || ^14.18.0 || >=16.0.0} + '@rspack/binding-darwin-arm64@2.0.0-beta.5': + resolution: {integrity: sha512-PWV/fItbDY9ySPub2YT+DynZLaXeODfzd24SMykUDCfk5U8P/4sK7+7MB8HEN9PeLiM8+iTjHN/XuXti5DzgZA==} + cpu: [arm64] + os: [darwin] + + '@rspack/binding-darwin-x64@2.0.0-beta.5': + resolution: {integrity: sha512-RfaI0Tf+efGblR2mgqzjKzFxN/NwDqnKsV14YxiwLDxbHFFBy7qSrbeLN+E3xapEaw9x0uTnzcUQDHjekkgXIA==} + cpu: [x64] + os: [darwin] + + '@rspack/binding-linux-arm64-gnu@2.0.0-beta.5': + resolution: {integrity: sha512-Ydbdzji/IFTjPbB5MioBvxsGCpt9uY5W7x9e9c0qadZ5i2wExWjgEoGp0ALiWKPvkNaQo5CAu/0MEwf3C+kf9g==} + cpu: [arm64] + os: [linux] + + '@rspack/binding-linux-arm64-musl@2.0.0-beta.5': + resolution: {integrity: sha512-HG1rRjypO2ShKGDbLJA8fqr/DWayo+YbNL3H/aBcv+it02TD2j8j9N2jyKwNpjp4LoMboDtFLOiKjmsLTMf3lQ==} + cpu: [arm64] + os: [linux] + + '@rspack/binding-linux-x64-gnu@2.0.0-beta.5': + resolution: {integrity: sha512-R6KCdMwnSXgz8qLZROxpYwSmq7/sLuyvL52TrzCNkMUIl6crfbEfSUAFQGcxMdy6lq5qX8Xir67TzTGMs7fNhw==} + cpu: [x64] + os: [linux] + + '@rspack/binding-linux-x64-musl@2.0.0-beta.5': + resolution: {integrity: sha512-McRRtUYjcbGgIrPEJ4doRAdVhg28Y4Y6Bx1FVnIYWvwvZPQvUPT2DHsCnYcnP4P5hp531Oa3ISFz0oU3w10FAA==} + cpu: [x64] + os: [linux] + + '@rspack/binding-wasm32-wasi@2.0.0-beta.5': + resolution: {integrity: sha512-4CDqMjcm+d/eTBa35aIAodJnZvy7diYK5n16Dx4erYl+qFfLLSIb/OFP9r+1MFBzWWMRjLORuQUwOMFaGc4m2Q==} + cpu: [wasm32] + + '@rspack/binding-win32-arm64-msvc@2.0.0-beta.5': + resolution: {integrity: sha512-11RH36VV8rFfImqQ3DiAmYfzAxof3T6xUqjf9JZSJYIavH4R4iDBqLaCBCkuTtdZAUl/Ujv3ziKpQ/bm/mqV/A==} + cpu: [arm64] + os: [win32] + + '@rspack/binding-win32-ia32-msvc@2.0.0-beta.5': + resolution: {integrity: sha512-XPqt2o2gLmhhEtrG4FgJ8KVNkbJPgGOwbfn3iz5+XjKcmC0ZCvQ1muRIQrhwfNeKaReLoWScFkam5gGcbTK7Gw==} + cpu: [ia32] + os: [win32] + + '@rspack/binding-win32-x64-msvc@2.0.0-beta.5': + resolution: {integrity: sha512-Tx9OsnK+GTArCA1dxGnY3EAxjGTr1WSOPs24d0JlFSMpc8AOoDXu5YojE21K6dXnYxBwwb1aVc+aRg3ipS27uQ==} + cpu: [x64] + os: [win32] + + '@rspack/binding@2.0.0-beta.5': + resolution: {integrity: sha512-gep96+L6yaul0nMUS3RD7w2GkHlx5tgoxvAQ7/zJvI3xrd4UaRY+pnAtfTAr7sBt+y7YQZKHIPvZvHS5omvouQ==} + + '@rspack/core@2.0.0-beta.5': + resolution: {integrity: sha512-7qylDEpBxhuoByPjXvKWZYeWSze+mQ54SuU5X9L8EcIjY22rWe/NcmyVHBkbUt5XC1cKP+nrCZMp9eNCBq3/Bg==} + engines: {node: ^20.19.0 || >=22.12.0} + peerDependencies: + '@module-federation/runtime-tools': ^0.24.1 || ^2.0.0 + '@swc/helpers': '>=0.5.1' + peerDependenciesMeta: + '@module-federation/runtime-tools': + optional: true + '@swc/helpers': + optional: true + '@sinclair/typebox@0.34.48': resolution: {integrity: sha512-kKJTNuK3AQOrgjjotVxMrCn1sUJwM76wMszfq1kdU4uYVJjvEWuFQ6HgvLt4Xz3fSmZlTOxJ/Ie13KnIcWQXFA==} @@ -5449,6 +5516,13 @@ snapshots: '@tybys/wasm-util': 0.10.1 optional: true + '@napi-rs/wasm-runtime@1.0.7': + dependencies: + '@emnapi/core': 1.8.1 + '@emnapi/runtime': 1.8.1 + '@tybys/wasm-util': 0.10.1 + optional: true + '@nicolo-ribaudo/chokidar-2@2.1.8-no-fsevents.3': optional: true @@ -5477,6 +5551,55 @@ snapshots: '@pkgr/core@0.2.9': {} + '@rspack/binding-darwin-arm64@2.0.0-beta.5': + optional: true + + '@rspack/binding-darwin-x64@2.0.0-beta.5': + optional: true + + '@rspack/binding-linux-arm64-gnu@2.0.0-beta.5': + optional: true + + '@rspack/binding-linux-arm64-musl@2.0.0-beta.5': + optional: true + + '@rspack/binding-linux-x64-gnu@2.0.0-beta.5': + optional: true + + '@rspack/binding-linux-x64-musl@2.0.0-beta.5': + optional: true + + '@rspack/binding-wasm32-wasi@2.0.0-beta.5': + dependencies: + '@napi-rs/wasm-runtime': 1.0.7 + optional: true + + '@rspack/binding-win32-arm64-msvc@2.0.0-beta.5': + optional: true + + '@rspack/binding-win32-ia32-msvc@2.0.0-beta.5': + optional: true + + '@rspack/binding-win32-x64-msvc@2.0.0-beta.5': + optional: true + + '@rspack/binding@2.0.0-beta.5': + optionalDependencies: + '@rspack/binding-darwin-arm64': 2.0.0-beta.5 + '@rspack/binding-darwin-x64': 2.0.0-beta.5 + '@rspack/binding-linux-arm64-gnu': 2.0.0-beta.5 + '@rspack/binding-linux-arm64-musl': 2.0.0-beta.5 + '@rspack/binding-linux-x64-gnu': 2.0.0-beta.5 + '@rspack/binding-linux-x64-musl': 2.0.0-beta.5 + '@rspack/binding-wasm32-wasi': 2.0.0-beta.5 + '@rspack/binding-win32-arm64-msvc': 2.0.0-beta.5 + '@rspack/binding-win32-ia32-msvc': 2.0.0-beta.5 + '@rspack/binding-win32-x64-msvc': 2.0.0-beta.5 + + '@rspack/core@2.0.0-beta.5': + dependencies: + '@rspack/binding': 2.0.0-beta.5 + '@sinclair/typebox@0.34.48': {} '@sindresorhus/merge-streams@2.3.0': {} diff --git a/scripts/globalSetup.js b/scripts/globalSetup.js index 8448a31..d79d20c 100644 --- a/scripts/globalSetup.js +++ b/scripts/globalSetup.js @@ -1,5 +1,6 @@ -const { version } = require("webpack"); +const { version } = require("@rspack/core"); -module.exports = () => +module.exports = () => { // eslint-disable-next-line no-console - console.log(`\n Running tests for webpack @${version} \n`); + console.log(`\n Running tests for rspack @${version} \n`); +}; diff --git a/src/index.js b/src/index.js index 9827a63..001d552 100644 --- a/src/index.js +++ b/src/index.js @@ -9,11 +9,11 @@ const setupWriteToDisk = require("./utils/setupWriteToDisk"); const noop = () => {}; -/** @typedef {import("webpack").Compiler} Compiler */ -/** @typedef {import("webpack").MultiCompiler} MultiCompiler */ -/** @typedef {import("webpack").Configuration} Configuration */ -/** @typedef {import("webpack").Stats} Stats */ -/** @typedef {import("webpack").MultiStats} MultiStats */ +/** @typedef {import("@rspack/core").Compiler} Compiler */ +/** @typedef {import("@rspack/core").MultiCompiler} MultiCompiler */ +/** @typedef {import("@rspack/core").Configuration} Configuration */ +/** @typedef {import("@rspack/core").Stats} Stats */ +/** @typedef {import("@rspack/core").MultiStats} MultiStats */ /** @typedef {import("fs").ReadStream} ReadStream */ /** @@ -39,6 +39,10 @@ const noop = () => {}; * @typedef {NonNullable} WatchOptions */ +/** + * @typedef {boolean | Configuration["devServer"] | undefined} DevServerOption + */ + /** * @typedef {Compiler["watching"]} Watching */ @@ -48,10 +52,11 @@ const noop = () => {}; */ /** - * @typedef {import("webpack").OutputFileSystem & { createReadStream?: import("fs").createReadStream, statSync: import("fs").statSync, readFileSync: import("fs").readFileSync }} OutputFileSystem + * @typedef {import("@rspack/core").OutputFileSystem & { createReadStream?: import("fs").createReadStream, statSync: import("fs").statSync, readFileSync: import("fs").readFileSync }} OutputFileSystem */ /** @typedef {ReturnType} Logger */ +/** @typedef {{ close(callback: (err?: Error | null | undefined) => void): void }} ClosableWatching */ /** * @callback Callback @@ -284,7 +289,7 @@ function wdm(compiler, options = {}) { }; instance.close = (callback = noop) => { - filledContext.watching.close(callback); + /** @type {ClosableWatching} */ (filledContext.watching).close(callback); }; instance.context = filledContext; diff --git a/src/utils/getPaths.js b/src/utils/getPaths.js index 2d6b0e4..1a4177d 100644 --- a/src/utils/getPaths.js +++ b/src/utils/getPaths.js @@ -1,7 +1,8 @@ -/** @typedef {import("webpack").Compiler} Compiler */ -/** @typedef {import("webpack").Stats} Stats */ -/** @typedef {import("webpack").MultiStats} MultiStats */ -/** @typedef {import("webpack").Asset} Asset */ +/** @typedef {import("@rspack/core").Compiler} Compiler */ +/** @typedef {import("@rspack/core").Stats} Stats */ +/** @typedef {import("@rspack/core").MultiStats} MultiStats */ +/** @typedef {import("@rspack/core").Asset} Asset */ +/** @typedef {import("../index.js").DevServerOption} DevServerOption */ /** @typedef {import("../index.js").IncomingMessage} IncomingMessage */ /** @typedef {import("../index.js").ServerResponse} ServerResponse */ @@ -9,7 +10,7 @@ * @template {IncomingMessage} Request * @template {ServerResponse} Response * @param {import("../index.js").FilledContext} context context - * @returns {{ outputPath: string, publicPath: string, assetsInfo: Asset["info"] }[]} paths + * @returns {{ outputPath: string, publicPath: string, assetsInfo: Map | undefined }[]} paths */ function getPaths(context) { const { stats, options } = context; @@ -20,11 +21,14 @@ function getPaths(context) { (stats).stats ? /** @type {MultiStats} */ (stats).stats : [/** @type {Stats} */ (stats)]; - /** @type {{ outputPath: string, publicPath: string, assetsInfo: Asset["info"] }[]} */ + /** @type {{ outputPath: string, publicPath: string, assetsInfo: Map | undefined }[]} */ const publicPaths = []; for (const { compilation } of childStats) { - if (compilation.options.devServer === false) { + if ( + /** @type {DevServerOption} */ + (compilation.options.devServer) === false + ) { continue; } @@ -33,15 +37,20 @@ function getPaths(context) { compilation.outputOptions.path || "", ); const publicPath = options.publicPath - ? compilation.getPath(options.publicPath) + ? compilation.getPath(/** @type {any} */ (options.publicPath)) : compilation.outputOptions.publicPath - ? compilation.getPath(compilation.outputOptions.publicPath) + ? compilation.getPath( + /** @type {any} */ (compilation.outputOptions.publicPath), + ) : ""; + const assetsInfo = new Map( + compilation.getAssets().map((asset) => [asset.name, asset.info]), + ); publicPaths.push({ outputPath, publicPath, - assetsInfo: compilation.assetsInfo, + assetsInfo, }); } diff --git a/src/utils/setupHooks.js b/src/utils/setupHooks.js index f9b14c8..cfe7bf4 100644 --- a/src/utils/setupHooks.js +++ b/src/utils/setupHooks.js @@ -1,8 +1,8 @@ -/** @typedef {import("webpack").Configuration} Configuration */ -/** @typedef {import("webpack").Compiler} Compiler */ -/** @typedef {import("webpack").MultiCompiler} MultiCompiler */ -/** @typedef {import("webpack").Stats} Stats */ -/** @typedef {import("webpack").MultiStats} MultiStats */ +/** @typedef {import("@rspack/core").Configuration} Configuration */ +/** @typedef {import("@rspack/core").Compiler} Compiler */ +/** @typedef {import("@rspack/core").MultiCompiler} MultiCompiler */ +/** @typedef {import("@rspack/core").Stats} Stats */ +/** @typedef {import("@rspack/core").MultiStats} MultiStats */ /** @typedef {import("../index.js").IncomingMessage} IncomingMessage */ /** @typedef {import("../index.js").ServerResponse} ServerResponse */ @@ -108,21 +108,6 @@ function setupHooks(context) { (childStatsOptions) => { childStatsOptions = normalizeStatsOptions(childStatsOptions); - if (typeof childStatsOptions.colors === "undefined") { - const [firstCompiler] = - /** @type {MultiCompiler} */ - (compiler).compilers; - - // TODO remove `colorette` and set minimum supported webpack version is `5.101.0` - childStatsOptions.colors = - typeof firstCompiler.webpack !== "undefined" && - typeof firstCompiler.webpack.cli !== "undefined" && - typeof firstCompiler.webpack.cli.isColorSupported === - "function" - ? firstCompiler.webpack.cli.isColorSupported() - : require("colorette").isColorSupported; - } - return childStatsOptions; }, ); @@ -132,14 +117,7 @@ function setupHooks(context) { ); if (typeof statsOptions.colors === "undefined") { - const { compiler } = /** @type {{ compiler: Compiler }} */ (context); - // TODO remove `colorette` and set minimum supported webpack version is `5.101.0` - statsOptions.colors = - typeof compiler.webpack !== "undefined" && - typeof compiler.webpack.cli !== "undefined" && - typeof compiler.webpack.cli.isColorSupported === "function" - ? compiler.webpack.cli.isColorSupported() - : require("colorette").isColorSupported; + statsOptions.colors = require("colorette").isColorSupported; } } diff --git a/src/utils/setupOutputFileSystem.js b/src/utils/setupOutputFileSystem.js index 29f48a7..569c6b7 100644 --- a/src/utils/setupOutputFileSystem.js +++ b/src/utils/setupOutputFileSystem.js @@ -1,6 +1,7 @@ const memfs = require("memfs"); -/** @typedef {import("webpack").MultiCompiler} MultiCompiler */ +/** @typedef {import("@rspack/core").MultiCompiler} MultiCompiler */ +/** @typedef {import("../index.js").DevServerOption} DevServerOption */ /** @typedef {import("../index.js").IncomingMessage} IncomingMessage */ /** @typedef {import("../index.js").ServerResponse} ServerResponse */ @@ -33,7 +34,7 @@ function setupOutputFileSystem(context) { (context.compiler).compilers.find( (item) => Object.hasOwn(item.options, "devServer") && - item.options.devServer !== false, + /** @type {DevServerOption} */ (item.options.devServer) !== false, ); ({ outputFileSystem } = @@ -50,7 +51,7 @@ function setupOutputFileSystem(context) { (context.compiler).compilers || [context.compiler]; for (const compiler of compilers) { - if (compiler.options.devServer === false) { + if (/** @type {DevServerOption} */ (compiler.options.devServer) === false) { continue; } diff --git a/src/utils/setupWriteToDisk.js b/src/utils/setupWriteToDisk.js index f8c8bd4..fa0ce5d 100644 --- a/src/utils/setupWriteToDisk.js +++ b/src/utils/setupWriteToDisk.js @@ -1,9 +1,10 @@ const fs = require("node:fs"); const path = require("node:path"); -/** @typedef {import("webpack").Compiler} Compiler */ -/** @typedef {import("webpack").MultiCompiler} MultiCompiler */ -/** @typedef {import("webpack").Compilation} Compilation */ +/** @typedef {import("@rspack/core").Compiler} Compiler */ +/** @typedef {import("@rspack/core").MultiCompiler} MultiCompiler */ +/** @typedef {import("@rspack/core").Compilation} Compilation */ +/** @typedef {import("../index.js").DevServerOption} DevServerOption */ /** @typedef {import("../index.js").IncomingMessage} IncomingMessage */ /** @typedef {import("../index.js").ServerResponse} ServerResponse */ @@ -21,7 +22,7 @@ function setupWriteToDisk(context) { (context.compiler).compilers || [context.compiler]; for (const compiler of compilers) { - if (compiler.options.devServer === false) { + if (/** @type {DevServerOption} */ (compiler.options.devServer) === false) { continue; } diff --git a/test/__snapshots__/logging.test.js b/test/__snapshots__/logging.test.js new file mode 100644 index 0000000..30e6c97 --- /dev/null +++ b/test/__snapshots__/logging.test.js @@ -0,0 +1,387 @@ +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing + +exports[`logging should logging an error in "watch" method: stderr 1`] = `"Error: Watch error"`; + +exports[`logging should logging an warning: stderr 1`] = `""`; + +exports[`logging should logging an warning: stdout 1`] = ` +"WARNING in ⚠ Warning + + +Rspack compiled with 1 warning" +`; + +exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #2: stderr 1`] = `""`; + +exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #2: stdout 1`] = ` +"broken: +ERROR in ./broken.js +× Module parse failed: +╰─▶ × JavaScript parse error: Expected ';', '}' or +╭──── +1 │ 1()2()3() +· ─ +╰──── + +help: +You may need an appropriate loader to handle this file type. + + +broken (Rspack 2.0.0-beta.5) compiled with 1 error in x ms + +warning: +WARNING in ⚠ Warning + + +warning (Rspack 2.0.0-beta.5) compiled with 1 warning in x ms + +success: +success (Rspack 2.0.0-beta.5) compiled successfully in x ms" +`; + +exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #3: stderr 1`] = `""`; + +exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #3: stdout 1`] = ` +"ERROR in ./broken.js +× Module parse failed: +╰─▶ × JavaScript parse error: Expected ';', '}' or +╭──── +1 │ 1()2()3() +· ─ +╰──── + +help: +You may need an appropriate loader to handle this file type. + + +Rspack 2.0.0-beta.5 compiled with 1 error in x ms + +WARNING in ⚠ Warning + + +Rspack 2.0.0-beta.5 compiled with 1 warning in x ms + +Rspack 2.0.0-beta.5 compiled successfully in x ms" +`; + +exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #4: stderr 1`] = `""`; + +exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #4: stdout 1`] = ` +"ERROR in ./broken.js +× Module parse failed: +╰─▶ × JavaScript parse error: Expected ';', '}' or +╭──── +1 │ 1()2()3() +· ─ +╰──── + +help: +You may need an appropriate loader to handle this file type. + + +Rspack 2.0.0-beta.5 compiled with 1 error in x ms + +WARNING in ⚠ Warning + + +Rspack 2.0.0-beta.5 compiled with 1 warning in x ms + +asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) +asset bundle.js x KiB [emitted] (name: main) +asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)" +`; + +exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #5: stderr 1`] = `""`; + +exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #5: stdout 1`] = ` +"Rspack 2.0.0-beta.5 compiled successfully in x ms + +Rspack 2.0.0-beta.5 compiled successfully in x ms" +`; + +exports[`logging should logging in multi-compiler and respect the "stats" option from configuration: stderr 1`] = `""`; + +exports[`logging should logging in multi-compiler and respect the "stats" option from configuration: stdout 1`] = ` +"ERROR in ./broken.js +× Module parse failed: +╰─▶ × JavaScript parse error: Expected ';', '}' or +╭──── +1 │ 1()2()3() +· ─ +╰──── + +help: +You may need an appropriate loader to handle this file type. + + +Rspack 2.0.0-beta.5 compiled with 1 error in x ms + +WARNING in ⚠ Warning + + +Rspack 2.0.0-beta.5 compiled with 1 warning in x ms + +Rspack 2.0.0-beta.5 compiled successfully in x ms" +`; + +exports[`logging should logging on successfully build and respect colors #2: stderr 1`] = `""`; + +exports[`logging should logging on successfully build and respect colors #2: stdout 1`] = `"Rspack 2.0.0-beta.5 compiled successfully in x ms"`; + +exports[`logging should logging on successfully build and respect colors: stderr 1`] = `""`; + +exports[`logging should logging on successfully build and respect colors: stdout 1`] = `"Rspack 2.0.0-beta.5 compiled successfully in x ms"`; + +exports[`logging should logging on successfully build and respect the "NO_COLOR" env: stderr 1`] = `""`; + +exports[`logging should logging on successfully build and respect the "NO_COLOR" env: stdout 1`] = `"Rspack 2.0.0-beta.5 compiled successfully in x ms"`; + +exports[`logging should logging on successfully build and respect the "stats" option from configuration with custom object value: stderr 1`] = `""`; + +exports[`logging should logging on successfully build and respect the "stats" option from configuration with custom object value: stdout 1`] = ` +"asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) +asset bundle.js x KiB [emitted] (name: main) +asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)" +`; + +exports[`logging should logging on successfully build and respect the "stats" option from configuration with the "false" value: stderr 1`] = `""`; + +exports[`logging should logging on successfully build and respect the "stats" option from configuration with the "false" value: stdout 1`] = `""`; + +exports[`logging should logging on successfully build and respect the "stats" option from configuration with the "minimal" value: stderr 1`] = `""`; + +exports[`logging should logging on successfully build and respect the "stats" option from configuration with the "minimal" value: stdout 1`] = ` +"x assets +x modules +Rspack 2.0.0-beta.5 compiled successfully in x ms" +`; + +exports[`logging should logging on successfully build and respect the "stats" option from configuration with the "none" value: stderr 1`] = `""`; + +exports[`logging should logging on successfully build and respect the "stats" option from configuration with the "none" value: stdout 1`] = `""`; + +exports[`logging should logging on successfully build and respect the "stats" option from configuration with the "true" value: stderr 1`] = `""`; + +exports[`logging should logging on successfully build and respect the "stats" option from configuration with the "true" value: stdout 1`] = `"Rspack 2.0.0-beta.5 compiled successfully in x ms"`; + +exports[`logging should logging on successfully build and respect the "stats" option from configuration with the "verbose" value: stderr 1`] = `""`; + +exports[`logging should logging on successfully build and respect the "stats" option from configuration with the "verbose" value: stdout 1`] = ` +"PublicPath: auto +asset svg.svg x KiB ({main}) [emitted] [from: svg.svg] (auxiliary name: main) +asset bundle.js x KiB {main} [emitted] (name: main) +asset index.html x bytes ({main}) [emitted] [from: index.html] (auxiliary name: main) +Entrypoint main x KiB = bundle.js +chunk {main} (runtime: main) bundle.js (xxxx) x bytes (xxxx) x KiB (xxxx) [entry] [rendered] +> ./foo.js main +./foo.js x bytes {main} [depth 0] [built] [code generated] +[used exports unknown] +entry ./foo.js +./index.html x bytes {main} [depth 1] [dependent] [built] [code generated] +[exports: default] +[used exports unknown] +cjs require ./index.html [./foo.js] ./foo.js 4:9-23 +./svg.svg x bytes {main} [depth 1] [dependent] [built] [code generated] +[exports: default] +[used exports unknown] +cjs require ./svg.svg [./foo.js] ./foo.js 3:9-20 + + +LOG from xxx" +`; + +exports[`logging should logging on successfully build in multi-compiler mode: stderr 1`] = `""`; + +exports[`logging should logging on successfully build in multi-compiler mode: stdout 1`] = ` +"Rspack 2.0.0-beta.5 compiled successfully in x ms + +Rspack 2.0.0-beta.5 compiled successfully in x ms" +`; + +exports[`logging should logging on successfully build using the "stats" option for middleware with object value and no colors: stderr 1`] = `""`; + +exports[`logging should logging on successfully build using the "stats" option for middleware with object value and no colors: stdout 1`] = ` +"asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) +asset bundle.js x KiB [emitted] (name: main) +asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)" +`; + +exports[`logging should logging on successfully build using the "stats" option for middleware with object value: stderr 1`] = `""`; + +exports[`logging should logging on successfully build using the "stats" option for middleware with object value: stdout 1`] = ` +"asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) +asset bundle.js x KiB [emitted] (name: main) +asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)" +`; + +exports[`logging should logging on successfully build using the "stats" option for middleware with the "false" value: stderr 1`] = `""`; + +exports[`logging should logging on successfully build using the "stats" option for middleware with the "false" value: stdout 1`] = `""`; + +exports[`logging should logging on successfully build using the "stats" option for middleware with the "none" value: stderr 1`] = `""`; + +exports[`logging should logging on successfully build using the "stats" option for middleware with the "none" value: stdout 1`] = `""`; + +exports[`logging should logging on successfully build using the "stats" option for middleware with the "normal" value: stderr 1`] = `""`; + +exports[`logging should logging on successfully build using the "stats" option for middleware with the "normal" value: stdout 1`] = `"Rspack 2.0.0-beta.5 compiled successfully in x ms"`; + +exports[`logging should logging on successfully build using the "stats" option for middleware with the "true" value: stderr 1`] = `""`; + +exports[`logging should logging on successfully build using the "stats" option for middleware with the "true" value: stdout 1`] = `"Rspack 2.0.0-beta.5 compiled successfully in x ms"`; + +exports[`logging should logging on successfully build using the "stats" option for middleware with the "verbose" value: stderr 1`] = `""`; + +exports[`logging should logging on successfully build using the "stats" option for middleware with the "verbose" value: stdout 1`] = ` +"PublicPath: auto +asset svg.svg x KiB ({main}) [emitted] [from: svg.svg] (auxiliary name: main) +asset bundle.js x KiB {main} [emitted] (name: main) +asset index.html x bytes ({main}) [emitted] [from: index.html] (auxiliary name: main) +Entrypoint main x KiB = bundle.js +chunk {main} (runtime: main) bundle.js (xxxx) x bytes (xxxx) x KiB (xxxx) [entry] [rendered] +> ./foo.js main +./foo.js x bytes {main} [depth 0] [built] [code generated] +[used exports unknown] +entry ./foo.js +./index.html x bytes {main} [depth 1] [dependent] [built] [code generated] +[exports: default] +[used exports unknown] +cjs require ./index.html [./foo.js] ./foo.js 4:9-23 +./svg.svg x bytes {main} [depth 1] [dependent] [built] [code generated] +[exports: default] +[used exports unknown] +cjs require ./svg.svg [./foo.js] ./foo.js 3:9-20 + + +LOG from xxx" +`; + +exports[`logging should logging on successfully build using the "stats" option for middleware with the object value and colors: stderr 1`] = `""`; + +exports[`logging should logging on successfully build using the "stats" option for middleware with the object value and colors: stdout 1`] = ` +"asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) +asset bundle.js x KiB [emitted] (name: main) +asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)" +`; + +exports[`logging should logging on successfully build when the 'stats' doesn't exist: stderr 1`] = `""`; + +exports[`logging should logging on successfully build when the 'stats' doesn't exist: stdout 1`] = `"Rspack 2.0.0-beta.5 compiled successfully in x ms"`; + +exports[`logging should logging on successfully build: stderr 1`] = `""`; + +exports[`logging should logging on successfully build: stdout 1`] = `"Rspack 2.0.0-beta.5 compiled successfully in x ms"`; + +exports[`logging should logging on successfully multi-compiler build using the "stats" option for middleware with object value and colors: stderr 1`] = `""`; + +exports[`logging should logging on successfully multi-compiler build using the "stats" option for middleware with object value and colors: stdout 1`] = ` +"asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) +asset bundle.js x KiB [emitted] (name: main) +asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main) + +asset bundle.js x bytes [emitted] (name: main)" +`; + +exports[`logging should logging on successfully multi-compiler build using the "stats" option for middleware with object value and no colors: stderr 1`] = `""`; + +exports[`logging should logging on successfully multi-compiler build using the "stats" option for middleware with object value and no colors: stdout 1`] = ` +"asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) +asset bundle.js x KiB [emitted] (name: main) +asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main) + +asset bundle.js x bytes [emitted] (name: main)" +`; + +exports[`logging should logging on successfully multi-compiler build using the "stats" option for middleware with the "false" value: stderr 1`] = `""`; + +exports[`logging should logging on successfully multi-compiler build using the "stats" option for middleware with the "false" value: stdout 1`] = `""`; + +exports[`logging should logging on successfully multi-compiler build using the "stats" option for middleware with the "normal" value: stderr 1`] = `""`; + +exports[`logging should logging on successfully multi-compiler build using the "stats" option for middleware with the "normal" value: stdout 1`] = ` +"Rspack 2.0.0-beta.5 compiled successfully in x ms + +Rspack 2.0.0-beta.5 compiled successfully in x ms" +`; + +exports[`logging should logging on successfully multi-compiler build using the "stats" option for middleware with the "true" value: stderr 1`] = `""`; + +exports[`logging should logging on successfully multi-compiler build using the "stats" option for middleware with the "true" value: stdout 1`] = ` +"Rspack 2.0.0-beta.5 compiled successfully in x ms + +Rspack 2.0.0-beta.5 compiled successfully in x ms" +`; + +exports[`logging should logging on successfully multi-compiler build using the "stats" option for middleware with the object value: stderr 1`] = `""`; + +exports[`logging should logging on successfully multi-compiler build using the "stats" option for middleware with the object value: stdout 1`] = ` +"asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) +asset bundle.js x KiB [emitted] (name: main) +asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main) + +asset bundle.js x bytes [emitted] (name: main)" +`; + +exports[`logging should logging on unsuccessful build in multi-compiler: stderr 1`] = `""`; + +exports[`logging should logging on unsuccessful build in multi-compiler: stdout 1`] = ` +"ERROR in ./broken.js +× Module parse failed: +╰─▶ × JavaScript parse error: Expected ';', '}' or +╭──── +1 │ 1()2()3() +· ─ +╰──── + +help: +You may need an appropriate loader to handle this file type. + + +Rspack compiled with 1 error + +ERROR in ./broken.js +× Module parse failed: +╰─▶ × JavaScript parse error: Expected ';', '}' or +╭──── +1 │ 1()2()3() +· ─ +╰──── + +help: +You may need an appropriate loader to handle this file type. + + +Rspack compiled with 1 error" +`; + +exports[`logging should logging on unsuccessful build: stderr 1`] = `""`; + +exports[`logging should logging on unsuccessful build: stdout 1`] = ` +"ERROR in ./broken.js +× Module parse failed: +╰─▶ × JavaScript parse error: Expected ';', '}' or +╭──── +1 │ 1()2()3() +· ─ +╰──── + +help: +You may need an appropriate loader to handle this file type. + + +Rspack compiled with 1 error" +`; + +exports[`logging should logging warnings in multi-compiler mode: stderr 1`] = `""`; + +exports[`logging should logging warnings in multi-compiler mode: stdout 1`] = ` +"WARNING in ⚠ Warning + + +Rspack compiled with 1 warning + +WARNING in ⚠ Warning + + +Rspack compiled with 1 warning" +`; diff --git a/test/__snapshots__/logging.test.js.snap.webpack5 b/test/__snapshots__/logging.test.js.snap.webpack5 deleted file mode 100644 index e5a367d..0000000 --- a/test/__snapshots__/logging.test.js.snap.webpack5 +++ /dev/null @@ -1,540 +0,0 @@ -// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing - -exports[`logging should logging an error in "watch" method: stderr 1`] = `"Error: Watch error"`; - -exports[`logging should logging an warning: stderr 1`] = `""`; - -exports[`logging should logging an warning: stdout 1`] = ` -"WARNING in Warning - -webpack compiled with 1 warning" -`; - -exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #2: stderr 1`] = `""`; - -exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #2: stdout 1`] = ` -"broken: -asset bundle.js x KiB [emitted] (name: main) -./broken.js x bytes [built] [code generated] [1 error] - -ERROR in ./broken.js 1:3 -Module parse failed: Unexpected token (1:3) -You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders -> 1()2()3() -| - -broken (webpack x.x.x) compiled with 1 error in x ms - -warning: -asset bundle.js x KiB [emitted] (name: main) -./warning.js x bytes [built] [code generated] - -WARNING in Warning - -warning (webpack x.x.x) compiled with 1 warning in x ms - -success: -asset bundle.js x KiB [emitted] (name: main) -asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) -asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main) -runtime modules x bytes x modules -cacheable modules x bytes -./foo.js x bytes [built] [code generated] -./svg.svg x bytes [built] [code generated] -./index.html x bytes [built] [code generated] -success (webpack x.x.x) compiled successfully in x ms" -`; - -exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #3: stderr 1`] = `""`; - -exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #3: stdout 1`] = ` -"asset bundle.js x KiB [emitted] (name: main) -./broken.js x bytes [built] [code generated] [1 error] - -ERROR in ./broken.js 1:3 -Module parse failed: Unexpected token (1:3) -You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders -> 1()2()3() -| - -webpack x.x.x compiled with 1 error in x ms - -asset bundle.js x KiB [emitted] (name: main) -./warning.js x bytes [built] [code generated] - -WARNING in Warning - -webpack x.x.x compiled with 1 warning in x ms - -asset bundle.js x KiB [emitted] (name: main) -asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) -asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main) -runtime modules x bytes x modules -cacheable modules x bytes -./foo.js x bytes [built] [code generated] -./svg.svg x bytes [built] [code generated] -./index.html x bytes [built] [code generated] -webpack x.x.x compiled successfully in x ms" -`; - -exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #4: stderr 1`] = `""`; - -exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #4: stdout 1`] = ` -"asset bundle.js x KiB [emitted] (name: main) -./broken.js x bytes [built] [code generated] [1 error] - -ERROR in ./broken.js 1:3 -Module parse failed: Unexpected token (1:3) -You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders -> 1()2()3() -| - -webpack x.x.x compiled with 1 error in x ms - -asset bundle.js x KiB [emitted] (name: main) -./warning.js x bytes [built] [code generated] - -WARNING in Warning - -webpack x.x.x compiled with 1 warning in x ms - -asset bundle.js x KiB [emitted] (name: main) -asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) -asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)" -`; - -exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #5: stderr 1`] = `""`; - -exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #5: stdout 1`] = ` -"asset bundle.js x KiB [emitted] (name: main) -./bar.js x bytes [built] [code generated] -webpack x.x.x compiled successfully in x ms - -asset bundle.js x KiB [emitted] (name: main) -asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) -asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main) -runtime modules x bytes x modules -cacheable modules x bytes -./foo.js x bytes [built] [code generated] -./svg.svg x bytes [built] [code generated] -./index.html x bytes [built] [code generated] -webpack x.x.x compiled successfully in x ms" -`; - -exports[`logging should logging in multi-compiler and respect the "stats" option from configuration: stderr 1`] = `""`; - -exports[`logging should logging in multi-compiler and respect the "stats" option from configuration: stdout 1`] = ` -"asset bundle.js x KiB [emitted] (name: main) -./broken.js x bytes [built] [code generated] [1 error] - -ERROR in ./broken.js 1:3 -Module parse failed: Unexpected token (1:3) -You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders -> 1()2()3() -| - -webpack x.x.x compiled with 1 error in x ms - -asset bundle.js x KiB [emitted] (name: main) -./warning.js x bytes [built] [code generated] - -WARNING in Warning - -webpack x.x.x compiled with 1 warning in x ms - -asset bundle.js x KiB [emitted] (name: main) -asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) -asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main) -runtime modules x bytes x modules -cacheable modules x bytes -./foo.js x bytes [built] [code generated] -./svg.svg x bytes [built] [code generated] -./index.html x bytes [built] [code generated] -webpack x.x.x compiled successfully in x ms" -`; - -exports[`logging should logging on successfully build and respect colors #2: stderr 1`] = `""`; - -exports[`logging should logging on successfully build and respect colors #2: stdout 1`] = ` -"asset bundle.js x KiB [emitted] (name: main) -asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) -asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main) -runtime modules x KiB x modules -cacheable modules x bytes -./foo.js x bytes [built] [code generated] -./svg.svg x bytes [built] [code generated] -./index.html x bytes [built] [code generated] -webpack x.x.x compiled successfully in x ms" -`; - -exports[`logging should logging on successfully build and respect colors: stderr 1`] = `""`; - -exports[`logging should logging on successfully build and respect colors: stdout 1`] = ` -"asset bundle.js x KiB [emitted] (name: main) -asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) -asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main) -runtime modules x KiB x modules -cacheable modules x bytes -./foo.js x bytes [built] [code generated] -./svg.svg x bytes [built] [code generated] -./index.html x bytes [built] [code generated] -webpack x.x.x compiled successfully in x ms" -`; - -exports[`logging should logging on successfully build and respect the "NO_COLOR" env: stderr 1`] = `""`; - -exports[`logging should logging on successfully build and respect the "NO_COLOR" env: stdout 1`] = ` -"asset bundle.js x KiB [emitted] (name: main) -asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) -asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main) -runtime modules x KiB x modules -cacheable modules x bytes -./foo.js x bytes [built] [code generated] -./svg.svg x bytes [built] [code generated] -./index.html x bytes [built] [code generated] -webpack x.x.x compiled successfully in x ms" -`; - -exports[`logging should logging on successfully build and respect the "stats" option from configuration with custom object value: stderr 1`] = `""`; - -exports[`logging should logging on successfully build and respect the "stats" option from configuration with custom object value: stdout 1`] = ` -"asset bundle.js x KiB [emitted] (name: main) -asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) -asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)" -`; - -exports[`logging should logging on successfully build and respect the "stats" option from configuration with the "false" value: stderr 1`] = `""`; - -exports[`logging should logging on successfully build and respect the "stats" option from configuration with the "false" value: stdout 1`] = `""`; - -exports[`logging should logging on successfully build and respect the "stats" option from configuration with the "minimal" value: stderr 1`] = `""`; - -exports[`logging should logging on successfully build and respect the "stats" option from configuration with the "minimal" value: stdout 1`] = ` -"x assets -x modules -webpack x.x.x compiled successfully in x ms" -`; - -exports[`logging should logging on successfully build and respect the "stats" option from configuration with the "none" value: stderr 1`] = `""`; - -exports[`logging should logging on successfully build and respect the "stats" option from configuration with the "none" value: stdout 1`] = `""`; - -exports[`logging should logging on successfully build and respect the "stats" option from configuration with the "true" value: stderr 1`] = `""`; - -exports[`logging should logging on successfully build and respect the "stats" option from configuration with the "true" value: stdout 1`] = ` -"asset bundle.js x KiB [emitted] (name: main) -asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) -asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main) -runtime modules x KiB x modules -cacheable modules x bytes -./foo.js x bytes [built] [code generated] -./svg.svg x bytes [built] [code generated] -./index.html x bytes [built] [code generated] -webpack x.x.x compiled successfully in x ms" -`; - -exports[`logging should logging on successfully build and respect the "stats" option from configuration with the "verbose" value: stderr 1`] = `""`; - -exports[`logging should logging on successfully build and respect the "stats" option from configuration with the "verbose" value: stdout 1`] = ` -"PublicPath: auto -asset bundle.js x KiB {main} [emitted] (name: main) -asset svg.svg x KiB ({main}) [emitted] [from: svg.svg] (auxiliary name: main) -asset index.html x bytes ({main}) [emitted] [from: index.html] (auxiliary name: main) -Entrypoint main x KiB (x KiB) = bundle.js 2 auxiliary assets -chunk {main} (runtime: main) bundle.js (xxxx) x bytes (xxxx) x KiB (xxxx) [entry] [rendered] -> ./foo.js main -runtime modules x KiB -webpack/runtime/define property getters x bytes {main} [code generated] -[no exports] -[used exports unknown] -webpack/runtime/global x bytes {main} [code generated] -[no exports] -[used exports unknown] -webpack/runtime/hasOwnProperty shorthand x bytes {main} [code generated] -[no exports] -[used exports unknown] -webpack/runtime/make namespace object x bytes {main} [code generated] -[no exports] -[used exports unknown] -webpack/runtime/publicPath x KiB {main} [code generated] -[no exports] -[used exports unknown] -cacheable modules x bytes -./foo.js x bytes {main} [depth 0] [built] [code generated] -[used exports unknown] -entry ./foo.js main -./index.html x bytes {main} [depth 1] [dependent] [built] [code generated] -[exports: default] -[used exports unknown] -cjs require ./index.html [./foo.js] 4:0-23 -./svg.svg x bytes {main} [depth 1] [dependent] [built] [code generated] -[exports: default] -[used exports unknown] -cjs require ./svg.svg [./foo.js] 3:0-20 - - -LOG from xxx" -`; - -exports[`logging should logging on successfully build in multi-compiler mode: stderr 1`] = `""`; - -exports[`logging should logging on successfully build in multi-compiler mode: stdout 1`] = ` -"asset bundle.js x KiB [emitted] (name: main) -asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) -asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main) -runtime modules x bytes x modules -cacheable modules x bytes -./foo.js x bytes [built] [code generated] -./svg.svg x bytes [built] [code generated] -./index.html x bytes [built] [code generated] -webpack x.x.x compiled successfully in x ms - -asset bundle.js x KiB [emitted] (name: main) -./bar.js x bytes [built] [code generated] -webpack x.x.x compiled successfully in x ms" -`; - -exports[`logging should logging on successfully build using the "stats" option for middleware with object value and no colors: stderr 1`] = `""`; - -exports[`logging should logging on successfully build using the "stats" option for middleware with object value and no colors: stdout 1`] = ` -"asset bundle.js x KiB [emitted] (name: main) -asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) -asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)" -`; - -exports[`logging should logging on successfully build using the "stats" option for middleware with object value: stderr 1`] = `""`; - -exports[`logging should logging on successfully build using the "stats" option for middleware with object value: stdout 1`] = ` -"asset bundle.js x KiB [emitted] (name: main) -asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) -asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)" -`; - -exports[`logging should logging on successfully build using the "stats" option for middleware with the "false" value: stderr 1`] = `""`; - -exports[`logging should logging on successfully build using the "stats" option for middleware with the "false" value: stdout 1`] = `""`; - -exports[`logging should logging on successfully build using the "stats" option for middleware with the "none" value: stderr 1`] = `""`; - -exports[`logging should logging on successfully build using the "stats" option for middleware with the "none" value: stdout 1`] = `""`; - -exports[`logging should logging on successfully build using the "stats" option for middleware with the "normal" value: stderr 1`] = `""`; - -exports[`logging should logging on successfully build using the "stats" option for middleware with the "normal" value: stdout 1`] = ` -"asset bundle.js x KiB [emitted] (name: main) -asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) -asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main) -runtime modules x KiB x modules -cacheable modules x bytes -./foo.js x bytes [built] [code generated] -./svg.svg x bytes [built] [code generated] -./index.html x bytes [built] [code generated] -webpack x.x.x compiled successfully in x ms" -`; - -exports[`logging should logging on successfully build using the "stats" option for middleware with the "true" value: stderr 1`] = `""`; - -exports[`logging should logging on successfully build using the "stats" option for middleware with the "true" value: stdout 1`] = ` -"asset bundle.js x KiB [emitted] (name: main) -asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) -asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main) -runtime modules x KiB x modules -cacheable modules x bytes -./foo.js x bytes [built] [code generated] -./svg.svg x bytes [built] [code generated] -./index.html x bytes [built] [code generated] -webpack x.x.x compiled successfully in x ms" -`; - -exports[`logging should logging on successfully build using the "stats" option for middleware with the "verbose" value: stderr 1`] = `""`; - -exports[`logging should logging on successfully build using the "stats" option for middleware with the "verbose" value: stdout 1`] = ` -"PublicPath: auto -asset bundle.js x KiB {main} [emitted] (name: main) -asset svg.svg x KiB ({main}) [emitted] [from: svg.svg] (auxiliary name: main) -asset index.html x bytes ({main}) [emitted] [from: index.html] (auxiliary name: main) -Entrypoint main x KiB (x KiB) = bundle.js 2 auxiliary assets -chunk {main} (runtime: main) bundle.js (xxxx) x bytes (xxxx) x KiB (xxxx) [entry] [rendered] -> ./foo.js main -runtime modules x KiB -webpack/runtime/define property getters x bytes {main} [code generated] -[no exports] -[used exports unknown] -webpack/runtime/global x bytes {main} [code generated] -[no exports] -[used exports unknown] -webpack/runtime/hasOwnProperty shorthand x bytes {main} [code generated] -[no exports] -[used exports unknown] -webpack/runtime/make namespace object x bytes {main} [code generated] -[no exports] -[used exports unknown] -webpack/runtime/publicPath x KiB {main} [code generated] -[no exports] -[used exports unknown] -cacheable modules x bytes -./foo.js x bytes {main} [depth 0] [built] [code generated] -[used exports unknown] -entry ./foo.js main -./index.html x bytes {main} [depth 1] [dependent] [built] [code generated] -[exports: default] -[used exports unknown] -cjs require ./index.html [./foo.js] 4:0-23 -./svg.svg x bytes {main} [depth 1] [dependent] [built] [code generated] -[exports: default] -[used exports unknown] -cjs require ./svg.svg [./foo.js] 3:0-20 - - -LOG from xxx" -`; - -exports[`logging should logging on successfully build using the "stats" option for middleware with the object value and colors: stderr 1`] = `""`; - -exports[`logging should logging on successfully build using the "stats" option for middleware with the object value and colors: stdout 1`] = ` -"asset bundle.js x KiB [emitted] (name: main) -asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) -asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)" -`; - -exports[`logging should logging on successfully build when the 'stats' doesn't exist: stderr 1`] = `""`; - -exports[`logging should logging on successfully build when the 'stats' doesn't exist: stdout 1`] = ` -"asset bundle.js x KiB [emitted] (name: main) -asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) -asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main) -runtime modules x KiB x modules -cacheable modules x bytes -./foo.js x bytes [built] [code generated] -./svg.svg x bytes [built] [code generated] -./index.html x bytes [built] [code generated] -webpack x.x.x compiled successfully in x ms" -`; - -exports[`logging should logging on successfully build: stderr 1`] = `""`; - -exports[`logging should logging on successfully build: stdout 1`] = ` -"asset bundle.js x KiB [emitted] (name: main) -asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) -asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main) -runtime modules x KiB x modules -cacheable modules x bytes -./foo.js x bytes [built] [code generated] -./svg.svg x bytes [built] [code generated] -./index.html x bytes [built] [code generated] -webpack x.x.x compiled successfully in x ms" -`; - -exports[`logging should logging on successfully multi-compiler build using the "stats" option for middleware with object value and colors: stderr 1`] = `""`; - -exports[`logging should logging on successfully multi-compiler build using the "stats" option for middleware with object value and colors: stdout 1`] = ` -"asset bundle.js x KiB [emitted] (name: main) -asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) -asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main) - -asset bundle.js x KiB [emitted] (name: main)" -`; - -exports[`logging should logging on successfully multi-compiler build using the "stats" option for middleware with object value and no colors: stderr 1`] = `""`; - -exports[`logging should logging on successfully multi-compiler build using the "stats" option for middleware with object value and no colors: stdout 1`] = ` -"asset bundle.js x KiB [emitted] (name: main) -asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) -asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main) - -asset bundle.js x KiB [emitted] (name: main)" -`; - -exports[`logging should logging on successfully multi-compiler build using the "stats" option for middleware with the "false" value: stderr 1`] = `""`; - -exports[`logging should logging on successfully multi-compiler build using the "stats" option for middleware with the "false" value: stdout 1`] = `""`; - -exports[`logging should logging on successfully multi-compiler build using the "stats" option for middleware with the "normal" value: stderr 1`] = `""`; - -exports[`logging should logging on successfully multi-compiler build using the "stats" option for middleware with the "normal" value: stdout 1`] = ` -"asset bundle.js x KiB [emitted] (name: main) -asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) -asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main) -runtime modules x bytes x modules -cacheable modules x bytes -./foo.js x bytes [built] [code generated] -./svg.svg x bytes [built] [code generated] -./index.html x bytes [built] [code generated] -webpack x.x.x compiled successfully in x ms - -asset bundle.js x KiB [emitted] (name: main) -./bar.js x bytes [built] [code generated] -webpack x.x.x compiled successfully in x ms" -`; - -exports[`logging should logging on successfully multi-compiler build using the "stats" option for middleware with the "true" value: stderr 1`] = `""`; - -exports[`logging should logging on successfully multi-compiler build using the "stats" option for middleware with the "true" value: stdout 1`] = ` -"asset bundle.js x KiB [emitted] (name: main) -asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) -asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main) -runtime modules x bytes x modules -cacheable modules x bytes -./foo.js x bytes [built] [code generated] -./svg.svg x bytes [built] [code generated] -./index.html x bytes [built] [code generated] -webpack x.x.x compiled successfully in x ms - -asset bundle.js x KiB [emitted] (name: main) -./bar.js x bytes [built] [code generated] -webpack x.x.x compiled successfully in x ms" -`; - -exports[`logging should logging on successfully multi-compiler build using the "stats" option for middleware with the object value: stderr 1`] = `""`; - -exports[`logging should logging on successfully multi-compiler build using the "stats" option for middleware with the object value: stdout 1`] = ` -"asset bundle.js x KiB [emitted] (name: main) -asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main) -asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main) - -asset bundle.js x KiB [emitted] (name: main)" -`; - -exports[`logging should logging on unsuccessful build in multi-compiler: stderr 1`] = `""`; - -exports[`logging should logging on unsuccessful build in multi-compiler: stdout 1`] = ` -"ERROR in ./broken.js 1:3 -Module parse failed: Unexpected token (1:3) -You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders -> 1()2()3() -| - -webpack compiled with 1 error - -ERROR in ./broken.js 1:3 -Module parse failed: Unexpected token (1:3) -You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders -> 1()2()3() -| - -webpack compiled with 1 error" -`; - -exports[`logging should logging on unsuccessful build: stderr 1`] = `""`; - -exports[`logging should logging on unsuccessful build: stdout 1`] = ` -"ERROR in ./broken.js 1:3 -Module parse failed: Unexpected token (1:3) -You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders -> 1()2()3() -| - -webpack compiled with 1 error" -`; - -exports[`logging should logging warnings in multi-compiler mode: stderr 1`] = `""`; - -exports[`logging should logging warnings in multi-compiler mode: stdout 1`] = ` -"WARNING in Warning - -webpack compiled with 1 warning - -WARNING in Warning - -webpack compiled with 1 warning" -`; diff --git a/test/helpers/getCompiler.js b/test/helpers/getCompiler.js index 3ce0550..3255be0 100644 --- a/test/helpers/getCompiler.js +++ b/test/helpers/getCompiler.js @@ -1,16 +1,16 @@ -import webpack from "webpack"; +const { rspack } = require("@rspack/core"); import defaultConfig from "../fixtures/webpack.config"; -/** @typedef {import("webpack").Configuration} Configuration */ -/** @typedef {import("webpack").Compiler} Compiler */ +/** @typedef {import("@rspack/core").Configuration} Configuration */ +/** @typedef {import("@rspack/core").Compiler} Compiler */ /** * @param {Configuration} config config * @returns {Compiler} compiler */ function getCompiler(config) { - return webpack(config || defaultConfig); + return rspack(config || defaultConfig); } export default getCompiler; diff --git a/test/helpers/runner.js b/test/helpers/runner.js index 13fba0c..a36ffd9 100755 --- a/test/helpers/runner.js +++ b/test/helpers/runner.js @@ -2,7 +2,7 @@ const merge = require("deepmerge"); const express = require("express"); -const webpack = require("webpack"); +const { rspack } = require("@rspack/core"); const middleware = require("../../dist"); const defaultConfig = require("../fixtures/webpack.config"); @@ -33,7 +33,7 @@ fillConfigEntries("WMC_", configMiddlewareEntries); /** * @param {string} name name - * @returns {import("webpack").Configuration | import("webpack").Configuration[]} configuration + * @returns {import("@rspack/core").Configuration | import("@rspack/core").Configuration[]} configuration */ function getWebpackConfig(name) { try { @@ -44,8 +44,8 @@ function getWebpackConfig(name) { } /** - * @param {import("webpack").Configuration[]} data data - * @returns {import("webpack").Configuration} configuration + * @param {import("@rspack/core").Configuration[]} data data + * @returns {import("@rspack/core").Configuration} configuration */ function createConfig(data) { /** @@ -57,8 +57,8 @@ function createConfig(data) { } /** - * @param {import("webpack").Configuration[]} arr arr - * @returns {import("webpack").Configuration} result + * @param {import("@rspack/core").Configuration[]} arr arr + * @returns {import("@rspack/core").Configuration} result */ function reduceObject(arr) { if (arr.length > 1) { @@ -89,7 +89,7 @@ if (Array.isArray(config)) { config.parallelism = 1; } -const compiler = webpack(config); +const compiler = rspack(config); if (process.env.WEBPACK_BREAK_WATCH) { compiler.watch = function watch() { diff --git a/test/helpers/snapshotResolver.js b/test/helpers/snapshotResolver.js index 12d4a4e..ec57752 100644 --- a/test/helpers/snapshotResolver.js +++ b/test/helpers/snapshotResolver.js @@ -1,21 +1,14 @@ const path = require("node:path"); -const webpack = require("webpack"); - -const [webpackVersion] = webpack.version; -const snapshotExtension = `.snap.webpack${webpackVersion}`; - module.exports = { resolveSnapshotPath: (testPath) => path.join( path.dirname(testPath), "__snapshots__", - `${path.basename(testPath)}${snapshotExtension}`, + `${path.basename(testPath)}`, ), resolveTestPath: (snapshotPath) => - snapshotPath - .replace(`${path.sep}__snapshots__`, "") - .slice(0, -snapshotExtension.length), + snapshotPath.replace(`${path.sep}__snapshots__`, ""), testPathForConsistencyCheck: path.join( "consistency_check", "__tests__", diff --git a/test/middleware.test.js b/test/middleware.test.js index 8d676b4..7e2922e 100644 --- a/test/middleware.test.js +++ b/test/middleware.test.js @@ -15,7 +15,7 @@ import mime from "mime-types"; import router from "router"; import request from "supertest"; -import { Stats } from "webpack"; +const { Stats } = require("@rspack/core"); import middleware from "../src"; diff --git a/test/utils/__snapshots__/setupHooks.test.js.snap.webpack5 b/test/utils/__snapshots__/setupHooks.test.js similarity index 100% rename from test/utils/__snapshots__/setupHooks.test.js.snap.webpack5 rename to test/utils/__snapshots__/setupHooks.test.js diff --git a/test/utils/__snapshots__/setupWriteToDisk.test.js.snap.webpack5 b/test/utils/__snapshots__/setupWriteToDisk.test.js similarity index 100% rename from test/utils/__snapshots__/setupWriteToDisk.test.js.snap.webpack5 rename to test/utils/__snapshots__/setupWriteToDisk.test.js diff --git a/types/index.d.ts b/types/index.d.ts index 025dd20..e21a085 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,9 +1,9 @@ export = wdm; -/** @typedef {import("webpack").Compiler} Compiler */ -/** @typedef {import("webpack").MultiCompiler} MultiCompiler */ -/** @typedef {import("webpack").Configuration} Configuration */ -/** @typedef {import("webpack").Stats} Stats */ -/** @typedef {import("webpack").MultiStats} MultiStats */ +/** @typedef {import("@rspack/core").Compiler} Compiler */ +/** @typedef {import("@rspack/core").MultiCompiler} MultiCompiler */ +/** @typedef {import("@rspack/core").Configuration} Configuration */ +/** @typedef {import("@rspack/core").Stats} Stats */ +/** @typedef {import("@rspack/core").MultiStats} MultiStats */ /** @typedef {import("fs").ReadStream} ReadStream */ /** * @typedef {object} ExtendedServerResponse @@ -21,6 +21,9 @@ export = wdm; /** * @typedef {NonNullable} WatchOptions */ +/** + * @typedef {boolean | Configuration["devServer"] | undefined} DevServerOption + */ /** * @typedef {Compiler["watching"]} Watching */ @@ -28,9 +31,10 @@ export = wdm; * @typedef {ReturnType} MultiWatching */ /** - * @typedef {import("webpack").OutputFileSystem & { createReadStream?: import("fs").createReadStream, statSync: import("fs").statSync, readFileSync: import("fs").readFileSync }} OutputFileSystem + * @typedef {import("@rspack/core").OutputFileSystem & { createReadStream?: import("fs").createReadStream, statSync: import("fs").statSync, readFileSync: import("fs").readFileSync }} OutputFileSystem */ /** @typedef {ReturnType} Logger */ +/** @typedef {{ close(callback: (err?: Error | null | undefined) => void): void }} ClosableWatching */ /** * @callback Callback * @param {(Stats | MultiStats)=} stats @@ -179,10 +183,12 @@ declare namespace wdm { EXPECTED_FUNCTION, NextFunction, WatchOptions, + DevServerOption, Watching, MultiWatching, OutputFileSystem, Logger, + ClosableWatching, Callback, ResponseData, ModifyResponseData, @@ -257,11 +263,11 @@ declare function honoWrapper< compiler: Compiler | MultiCompiler, options?: Options | undefined, ): (ctx: EXPECTED_ANY, next: EXPECTED_FUNCTION) => Promise | void; -type Compiler = import("webpack").Compiler; -type MultiCompiler = import("webpack").MultiCompiler; -type Configuration = import("webpack").Configuration; -type Stats = import("webpack").Stats; -type MultiStats = import("webpack").MultiStats; +type Compiler = import("@rspack/core").Compiler; +type MultiCompiler = import("@rspack/core").MultiCompiler; +type Configuration = import("@rspack/core").Configuration; +type Stats = import("@rspack/core").Stats; +type MultiStats = import("@rspack/core").MultiStats; type ReadStream = import("fs").ReadStream; type ExtendedServerResponse = { /** @@ -281,14 +287,18 @@ type EXPECTED_ANY = any; type EXPECTED_FUNCTION = Function; type NextFunction = (err?: EXPECTED_ANY | undefined) => void; type WatchOptions = NonNullable; +type DevServerOption = boolean | Configuration["devServer"] | undefined; type Watching = Compiler["watching"]; type MultiWatching = ReturnType; -type OutputFileSystem = import("webpack").OutputFileSystem & { +type OutputFileSystem = import("@rspack/core").OutputFileSystem & { createReadStream?: typeof import("fs").createReadStream; statSync: import("fs").StatSyncFn; readFileSync: typeof import("fs").readFileSync; }; type Logger = ReturnType; +type ClosableWatching = { + close(callback: (err?: Error | null | undefined) => void): void; +}; type Callback = (stats?: (Stats | MultiStats) | undefined) => any; type ResponseData = { /** diff --git a/types/utils/getPaths.d.ts b/types/utils/getPaths.d.ts index f8269fb..d6d3972 100644 --- a/types/utils/getPaths.d.ts +++ b/types/utils/getPaths.d.ts @@ -1,15 +1,16 @@ export = getPaths; -/** @typedef {import("webpack").Compiler} Compiler */ -/** @typedef {import("webpack").Stats} Stats */ -/** @typedef {import("webpack").MultiStats} MultiStats */ -/** @typedef {import("webpack").Asset} Asset */ +/** @typedef {import("@rspack/core").Compiler} Compiler */ +/** @typedef {import("@rspack/core").Stats} Stats */ +/** @typedef {import("@rspack/core").MultiStats} MultiStats */ +/** @typedef {import("@rspack/core").Asset} Asset */ +/** @typedef {import("../index.js").DevServerOption} DevServerOption */ /** @typedef {import("../index.js").IncomingMessage} IncomingMessage */ /** @typedef {import("../index.js").ServerResponse} ServerResponse */ /** * @template {IncomingMessage} Request * @template {ServerResponse} Response * @param {import("../index.js").FilledContext} context context - * @returns {{ outputPath: string, publicPath: string, assetsInfo: Asset["info"] }[]} paths + * @returns {{ outputPath: string, publicPath: string, assetsInfo: Map | undefined }[]} paths */ declare function getPaths< Request extends IncomingMessage, @@ -19,7 +20,7 @@ declare function getPaths< ): { outputPath: string; publicPath: string; - assetsInfo: Asset["info"]; + assetsInfo: Map | undefined; }[]; declare namespace getPaths { export { @@ -27,13 +28,15 @@ declare namespace getPaths { Stats, MultiStats, Asset, + DevServerOption, IncomingMessage, ServerResponse, }; } -type Compiler = import("webpack").Compiler; -type Stats = import("webpack").Stats; -type MultiStats = import("webpack").MultiStats; -type Asset = import("webpack").Asset; +type Compiler = import("@rspack/core").Compiler; +type Stats = import("@rspack/core").Stats; +type MultiStats = import("@rspack/core").MultiStats; +type Asset = import("@rspack/core").Asset; +type DevServerOption = import("../index.js").DevServerOption; type IncomingMessage = import("../index.js").IncomingMessage; type ServerResponse = import("../index.js").ServerResponse; diff --git a/types/utils/setupHooks.d.ts b/types/utils/setupHooks.d.ts index 1ec0e48..173cff1 100644 --- a/types/utils/setupHooks.d.ts +++ b/types/utils/setupHooks.d.ts @@ -1,9 +1,9 @@ export = setupHooks; -/** @typedef {import("webpack").Configuration} Configuration */ -/** @typedef {import("webpack").Compiler} Compiler */ -/** @typedef {import("webpack").MultiCompiler} MultiCompiler */ -/** @typedef {import("webpack").Stats} Stats */ -/** @typedef {import("webpack").MultiStats} MultiStats */ +/** @typedef {import("@rspack/core").Configuration} Configuration */ +/** @typedef {import("@rspack/core").Compiler} Compiler */ +/** @typedef {import("@rspack/core").MultiCompiler} MultiCompiler */ +/** @typedef {import("@rspack/core").Stats} Stats */ +/** @typedef {import("@rspack/core").MultiStats} MultiStats */ /** @typedef {import("../index.js").IncomingMessage} IncomingMessage */ /** @typedef {import("../index.js").ServerResponse} ServerResponse */ /** @typedef {Configuration["stats"]} StatsOptions */ @@ -37,11 +37,11 @@ declare namespace setupHooks { StatsObjectOptions, }; } -type Configuration = import("webpack").Configuration; -type Compiler = import("webpack").Compiler; -type MultiCompiler = import("webpack").MultiCompiler; -type Stats = import("webpack").Stats; -type MultiStats = import("webpack").MultiStats; +type Configuration = import("@rspack/core").Configuration; +type Compiler = import("@rspack/core").Compiler; +type MultiCompiler = import("@rspack/core").MultiCompiler; +type Stats = import("@rspack/core").Stats; +type MultiStats = import("@rspack/core").MultiStats; type IncomingMessage = import("../index.js").IncomingMessage; type ServerResponse = import("../index.js").ServerResponse; type StatsOptions = Configuration["stats"]; diff --git a/types/utils/setupOutputFileSystem.d.ts b/types/utils/setupOutputFileSystem.d.ts index eabb467..2bd3765 100644 --- a/types/utils/setupOutputFileSystem.d.ts +++ b/types/utils/setupOutputFileSystem.d.ts @@ -1,5 +1,6 @@ export = setupOutputFileSystem; -/** @typedef {import("webpack").MultiCompiler} MultiCompiler */ +/** @typedef {import("@rspack/core").MultiCompiler} MultiCompiler */ +/** @typedef {import("../index.js").DevServerOption} DevServerOption */ /** @typedef {import("../index.js").IncomingMessage} IncomingMessage */ /** @typedef {import("../index.js").ServerResponse} ServerResponse */ /** @@ -17,8 +18,9 @@ declare function setupOutputFileSystem< >, ): void; declare namespace setupOutputFileSystem { - export { MultiCompiler, IncomingMessage, ServerResponse }; + export { MultiCompiler, DevServerOption, IncomingMessage, ServerResponse }; } -type MultiCompiler = import("webpack").MultiCompiler; +type MultiCompiler = import("@rspack/core").MultiCompiler; +type DevServerOption = import("../index.js").DevServerOption; type IncomingMessage = import("../index.js").IncomingMessage; type ServerResponse = import("../index.js").ServerResponse; diff --git a/types/utils/setupWriteToDisk.d.ts b/types/utils/setupWriteToDisk.d.ts index 8a237c4..c692727 100644 --- a/types/utils/setupWriteToDisk.d.ts +++ b/types/utils/setupWriteToDisk.d.ts @@ -1,7 +1,8 @@ export = setupWriteToDisk; -/** @typedef {import("webpack").Compiler} Compiler */ -/** @typedef {import("webpack").MultiCompiler} MultiCompiler */ -/** @typedef {import("webpack").Compilation} Compilation */ +/** @typedef {import("@rspack/core").Compiler} Compiler */ +/** @typedef {import("@rspack/core").MultiCompiler} MultiCompiler */ +/** @typedef {import("@rspack/core").Compilation} Compilation */ +/** @typedef {import("../index.js").DevServerOption} DevServerOption */ /** @typedef {import("../index.js").IncomingMessage} IncomingMessage */ /** @typedef {import("../index.js").ServerResponse} ServerResponse */ /** @@ -23,12 +24,14 @@ declare namespace setupWriteToDisk { Compiler, MultiCompiler, Compilation, + DevServerOption, IncomingMessage, ServerResponse, }; } -type Compiler = import("webpack").Compiler; -type MultiCompiler = import("webpack").MultiCompiler; -type Compilation = import("webpack").Compilation; +type Compiler = import("@rspack/core").Compiler; +type MultiCompiler = import("@rspack/core").MultiCompiler; +type Compilation = import("@rspack/core").Compilation; +type DevServerOption = import("../index.js").DevServerOption; type IncomingMessage = import("../index.js").IncomingMessage; type ServerResponse = import("../index.js").ServerResponse; From 127c767fe097ce2c9728db6267ee2271ee936d2b Mon Sep 17 00:00:00 2001 From: neverland Date: Sat, 14 Mar 2026 21:18:53 +0800 Subject: [PATCH 2/2] fix: snap --- test/helpers/snapshotResolver.js | 17 ------ .../__snapshots__/setupHooks.test.js.snap | 9 +++ .../setupWriteToDisk.test.js.snap | 55 +++++++++++++++++++ 3 files changed, 64 insertions(+), 17 deletions(-) delete mode 100644 test/helpers/snapshotResolver.js create mode 100644 test/utils/__snapshots__/setupHooks.test.js.snap create mode 100644 test/utils/__snapshots__/setupWriteToDisk.test.js.snap diff --git a/test/helpers/snapshotResolver.js b/test/helpers/snapshotResolver.js deleted file mode 100644 index ec57752..0000000 --- a/test/helpers/snapshotResolver.js +++ /dev/null @@ -1,17 +0,0 @@ -const path = require("node:path"); - -module.exports = { - resolveSnapshotPath: (testPath) => - path.join( - path.dirname(testPath), - "__snapshots__", - `${path.basename(testPath)}`, - ), - resolveTestPath: (snapshotPath) => - snapshotPath.replace(`${path.sep}__snapshots__`, ""), - testPathForConsistencyCheck: path.join( - "consistency_check", - "__tests__", - "example.test.js", - ), -}; diff --git a/test/utils/__snapshots__/setupHooks.test.js.snap b/test/utils/__snapshots__/setupHooks.test.js.snap new file mode 100644 index 0000000..bf6a22b --- /dev/null +++ b/test/utils/__snapshots__/setupHooks.test.js.snap @@ -0,0 +1,9 @@ +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing + +exports[`setupHooks handles multi compiler 1`] = `[]`; + +exports[`setupHooks handles multi compiler 2`] = `[]`; + +exports[`setupHooks handles multi compiler 3`] = `[]`; + +exports[`setupHooks sets state, then logs stats and handles callbacks on nextTick from done hook 1`] = `[]`; diff --git a/test/utils/__snapshots__/setupWriteToDisk.test.js.snap b/test/utils/__snapshots__/setupWriteToDisk.test.js.snap new file mode 100644 index 0000000..a7a3642 --- /dev/null +++ b/test/utils/__snapshots__/setupWriteToDisk.test.js.snap @@ -0,0 +1,55 @@ +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing + +exports[`setupWriteToDisk tries to create directories and write file if not filtered out with mkdir error 1`] = ` +[ + [ + "Child "name": Unable to write "/target/path" directory to disk: +error1", + ], +] +`; + +exports[`setupWriteToDisk tries to create directories and write file if not filtered out with mkdir error 2`] = `[]`; + +exports[`setupWriteToDisk tries to create directories and write file if not filtered out with mkdir error 3`] = ` +[ + [ + "error1", + ], +] +`; + +exports[`setupWriteToDisk tries to create directories and write file if not filtered out with no write errors 1`] = `[]`; + +exports[`setupWriteToDisk tries to create directories and write file if not filtered out with no write errors 2`] = ` +[ + [ + "Child "name": Asset written to disk: "/target/path/file"", + ], +] +`; + +exports[`setupWriteToDisk tries to create directories and write file if not filtered out with no write errors 3`] = ` +[ + [], +] +`; + +exports[`setupWriteToDisk tries to create directories and write file if not filtered out with writeFile error 1`] = ` +[ + [ + "Child "name": Unable to write "/target/path/file" asset to disk: +error2", + ], +] +`; + +exports[`setupWriteToDisk tries to create directories and write file if not filtered out with writeFile error 2`] = `[]`; + +exports[`setupWriteToDisk tries to create directories and write file if not filtered out with writeFile error 3`] = ` +[ + [ + "error2", + ], +] +`;