Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(type): rspack function type in cjs require #8169

Merged
merged 11 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, {
chenjiahan marked this conversation as resolved.
Show resolved Hide resolved
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;
Loading