diff --git a/packages/rspack-cli/jest.config.js b/packages/rspack-cli/jest.config.js index 40ee463f5f0..08cd9df59a6 100644 --- a/packages/rspack-cli/jest.config.js +++ b/packages/rspack-cli/jest.config.js @@ -1,14 +1,16 @@ /** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */ const config = { - preset: 'ts-jest', - testEnvironment: '../../scripts/test/patch-node-env.cjs', - testTimeout: process.env.CI ? 200000 : 30000, - testMatch: ['/tests/**/*.test.ts', '/tests/**/*.test.js'], - watchPathIgnorePatterns: ['/tests/.*/dist'], - extensionsToTreatAsEsm: ['.mts'], - globals: {'ts-jest': {tsconfig: '/tests/tsconfig.json'}}, - cache: false, - prettierPath: require.resolve('prettier-2') + preset: "ts-jest", + testEnvironment: "../../scripts/test/patch-node-env.cjs", + testTimeout: process.env.CI ? 200000 : 30000, + testMatch: ["/tests/**/*.test.ts", "/tests/**/*.test.js"], + watchPathIgnorePatterns: ["/tests/.*/dist"], + extensionsToTreatAsEsm: [".mts"], + transform: { + "^.+\\.(ts)?$": ["ts-jest", { tsconfig: "/tests/tsconfig.json" }] + }, + cache: false, + prettierPath: require.resolve("prettier-2") }; module.exports = config; diff --git a/packages/rspack-cli/tests/api/type/js-api-cjs.test.ts b/packages/rspack-cli/tests/api/type/js-api-cjs.test.ts new file mode 100644 index 00000000000..ae0a21b2e6a --- /dev/null +++ b/packages/rspack-cli/tests/api/type/js-api-cjs.test.ts @@ -0,0 +1,31 @@ +import rspackCjsRequire, { + rspack as rspackCjsNamedRequire +} from "@rspack/core"; +import assert from "node:assert"; + +type IsFunction = T extends (...args: any[]) => any ? true : false; + +// https://github.com/web-infra-dev/rspack/issues/8095 +describe("js-api-type should be correct when requiring from @rspack/core", () => { + it("cjs default require", async () => { + // const rspack = require('@rspack/core') + type Truthy = IsFunction; + const truthy: Truthy = true; + truthy; + assert(rspackCjsNamedRequire.BannerPlugin); + assert(typeof rspackCjsNamedRequire === "function"); + const compiler = rspackCjsNamedRequire({}); + assert(compiler); + }); + + it("cjs named require", async () => { + // const { rspack } = require('@rspack/core') + type Truthy = IsFunction; + const truthy: Truthy = true; + truthy; + assert(rspackCjsNamedRequire.BannerPlugin); + assert(typeof rspackCjsNamedRequire === "function"); + const compiler = rspackCjsNamedRequire({}); + assert(compiler); + }); +}); diff --git a/packages/rspack-cli/tests/api/type/js-api-esm.test.mts b/packages/rspack-cli/tests/api/type/js-api-esm.test.mts new file mode 100644 index 00000000000..9dc3efb83fd --- /dev/null +++ b/packages/rspack-cli/tests/api/type/js-api-esm.test.mts @@ -0,0 +1,27 @@ +import rspackEsmDefaultImport, { + rspack as rspackEsmNamedImport +} from "@rspack/core"; +import assert from "node:assert"; + +type IsFunction = T extends (...args: any[]) => any ? true : false; + +// https://github.com/web-infra-dev/rspack/issues/8095 +describe("js-api-type should be correct when importing from @rspack/core", () => { + it("esm default import", async () => { + // rspack has no default export now + type Falsy = IsFunction; + const falsy: Falsy = false; + falsy; + assert(rspackEsmDefaultImport.BannerPlugin); + }); + + it("esm named import", async () => { + type Truthy = IsFunction; + const truthy: Truthy = true; + truthy; + assert(rspackEsmNamedImport.BannerPlugin); + assert(typeof rspackEsmNamedImport === "function"); + const compiler = rspackEsmNamedImport({}); + assert(compiler); + }); +}); diff --git a/packages/rspack-test-tools/package.json b/packages/rspack-test-tools/package.json index 637806b0536..024ee918e55 100644 --- a/packages/rspack-test-tools/package.json +++ b/packages/rspack-test-tools/package.json @@ -24,7 +24,7 @@ "test:hot": "cross-env RSPACK_HOT_TEST=true NO_COLOR=1 node --expose-gc --max-old-space-size=8192 --experimental-vm-modules ../../node_modules/jest-cli/bin/jest --logHeapUsage --colors --config ./jest.config.hot.js --passWithNoTests", "test:diff": "cross-env RSPACK_DIFF=true NO_COLOR=1 node --expose-gc --max-old-space-size=8192 --experimental-vm-modules ../../node_modules/jest-cli/bin/jest --logHeapUsage --colors --config ./jest.config.diff.js --passWithNoTests", "api-extractor": "api-extractor run --verbose", - "api-extractor:ci": "api-extractor run --verbose || diff temp/api.md etc/api.md" + "api-extractor:ci": "api-extractor run --verbose || diff temp/test-tools.api.md etc/test-tools.api.md" }, "files": [ "client", diff --git a/packages/rspack/etc/core.api.md b/packages/rspack/etc/core.api.md index 9beac56531b..6bec0eee20c 100644 --- a/packages/rspack/etc/core.api.md +++ b/packages/rspack/etc/core.api.md @@ -4342,7 +4342,9 @@ type Rspack = typeof rspack_2 & typeof rspackExports & { }; // @public (undocumented) -export const rspack: Rspack; +const rspack: Rspack; +export default rspack; +export { rspack } // @public (undocumented) function rspack_2(options: MultiRspackOptions): MultiCompiler; diff --git a/packages/rspack/package.json b/packages/rspack/package.json index 54d0984866e..bdbe81e1d65 100644 --- a/packages/rspack/package.json +++ b/packages/rspack/package.json @@ -12,6 +12,7 @@ }, "exports": { ".": { + "types": "./dist/index.d.ts", "default": "./dist/index.js" }, "./hot/*": "./hot/*.js", @@ -29,7 +30,7 @@ "prepare-container-runtime": "node ./scripts/prepare-container-runtime.js", "doc-coverage": "node scripts/check-documentation-coverage.mjs", "api-extractor": "api-extractor run --verbose", - "api-extractor:ci": "api-extractor run --verbose || diff temp/api.md etc/api.md" + "api-extractor:ci": "api-extractor run --verbose || diff temp/core.api.md etc/core.api.md" }, "files": [ "dist", diff --git a/packages/rspack/src/index.ts b/packages/rspack/src/index.ts index 351ca4e992b..824b8161892 100644 --- a/packages/rspack/src/index.ts +++ b/packages/rspack/src/index.ts @@ -9,5 +9,6 @@ fn.webpack = fn; const rspack: Rspack = fn; export * from "./exports"; +export default rspack; export { rspack }; module.exports = rspack;