Skip to content

Commit

Permalink
fix(type): rspack function type in CJS require (#8169)
Browse files Browse the repository at this point in the history
* fix: the cjs require type and default type

* chore: update

* chore: update the test case

* chore: update

* chore: update

* chore: update

* chore: update

* chore: update

* chore: update the index

* fix: fix the api-extractor:ci command

* chore: update the type
  • Loading branch information
SoonIter authored Oct 24, 2024
1 parent 6f99ff3 commit 11f20e1
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 12 deletions.
20 changes: 11 additions & 9 deletions packages/rspack-cli/jest.config.js
Original file line number Diff line number Diff line change
@@ -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: ['<rootDir>/tests/**/*.test.ts', '<rootDir>/tests/**/*.test.js'],
watchPathIgnorePatterns: ['<rootDir>/tests/.*/dist'],
extensionsToTreatAsEsm: ['.mts'],
globals: {'ts-jest': {tsconfig: '<rootDir>/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: ["<rootDir>/tests/**/*.test.ts", "<rootDir>/tests/**/*.test.js"],
watchPathIgnorePatterns: ["<rootDir>/tests/.*/dist"],
extensionsToTreatAsEsm: [".mts"],
transform: {
"^.+\\.(ts)?$": ["ts-jest", { tsconfig: "<rootDir>/tests/tsconfig.json" }]
},
cache: false,
prettierPath: require.resolve("prettier-2")
};

module.exports = config;
31 changes: 31 additions & 0 deletions packages/rspack-cli/tests/api/type/js-api-cjs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import rspackCjsRequire, {
rspack as rspackCjsNamedRequire
} from "@rspack/core";
import assert from "node:assert";

type IsFunction<T> = 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<typeof rspackCjsRequire>;
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<typeof rspackCjsNamedRequire>;
const truthy: Truthy = true;
truthy;
assert(rspackCjsNamedRequire.BannerPlugin);
assert(typeof rspackCjsNamedRequire === "function");
const compiler = rspackCjsNamedRequire({});
assert(compiler);
});
});
27 changes: 27 additions & 0 deletions packages/rspack-cli/tests/api/type/js-api-esm.test.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import rspackEsmDefaultImport, {
rspack as rspackEsmNamedImport
} from "@rspack/core";
import assert from "node:assert";

type IsFunction<T> = 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<typeof rspackEsmDefaultImport>;
const falsy: Falsy = false;
falsy;
assert(rspackEsmDefaultImport.BannerPlugin);
});

it("esm named import", async () => {
type Truthy = IsFunction<typeof rspackEsmNamedImport>;
const truthy: Truthy = true;
truthy;
assert(rspackEsmNamedImport.BannerPlugin);
assert(typeof rspackEsmNamedImport === "function");
const compiler = rspackEsmNamedImport({});
assert(compiler);
});
});
2 changes: 1 addition & 1 deletion packages/rspack-test-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion packages/rspack/etc/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion packages/rspack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./hot/*": "./hot/*.js",
Expand All @@ -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",
Expand Down
1 change: 1 addition & 0 deletions packages/rspack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ fn.webpack = fn;
const rspack: Rspack = fn;

export * from "./exports";
export default rspack;
export { rspack };
module.exports = rspack;

2 comments on commit 11f20e1

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-10-24 6f99ff3) Current Change
10000_development-mode + exec 2.11 s ± 48 ms 2.11 s ± 30 ms +0.07 %
10000_development-mode_hmr + exec 668 ms ± 22 ms 671 ms ± 23 ms +0.54 %
10000_production-mode + exec 2.71 s ± 48 ms 2.69 s ± 28 ms -0.52 %
arco-pro_development-mode + exec 1.8 s ± 66 ms 1.77 s ± 73 ms -1.14 %
arco-pro_development-mode_hmr + exec 426 ms ± 1.6 ms 426 ms ± 2.9 ms -0.05 %
arco-pro_production-mode + exec 3.19 s ± 73 ms 3.2 s ± 110 ms +0.24 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.24 s ± 69 ms 3.27 s ± 69 ms +0.99 %
threejs_development-mode_10x + exec 1.61 s ± 17 ms 1.61 s ± 19 ms -0.04 %
threejs_development-mode_10x_hmr + exec 761 ms ± 20 ms 756 ms ± 13 ms -0.62 %
threejs_production-mode_10x + exec 5.01 s ± 29 ms 5.02 s ± 32 ms +0.13 %

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ✅ success
_selftest ✅ success
rspress ✅ success
rslib ✅ success
rsbuild ✅ success
examples ✅ success
devserver ✅ success

Please sign in to comment.