Skip to content

Commit

Permalink
fix(tscjs): fix typescript in cjs builds by using a separate type imp…
Browse files Browse the repository at this point in the history
…ort (#2935)

Closes #2356

Taking this PR from another project as inspiration:
openapi-ts/openapi-typescript#1360

This should fix ariakit-react typing in Typescript projects using
NodeNext module/moduleresolution by explicitly pointing at a "cts" type
definition file. It should not bother existing use cases but I left
index.d.ts in the cjs module just in case.
  • Loading branch information
djMax authored Oct 13, 2023
1 parent 8c06440 commit 5b6ce47
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 27 deletions.
8 changes: 8 additions & 0 deletions .changeset/2935-violet-wasps-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@ariakit/core": patch
"@ariakit/react": patch
"@ariakit/react-core": patch
"@ariakit/test": patch
---

Fixed TypeScript declaration files in CommonJS projects using `NodeNext` for `moduleResolution`.
20 changes: 12 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/ariakit-react-core/src/utils/store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export function useStoreProps<
if (value === undefined) return;
canSyncValue.current = true;
return sync(store, [key], () => {
if (value === undefined) return;
if (!canSyncValue.current) return;
store.setState(key, value);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/ariakit-react-core/src/utils/system.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function memo<P, T extends React.FC<P>>(
* <Component customProp render={<button />} />
*/
export function createComponent<O extends Options>(
render: (props: Props<O>) => React.ReactNode,
render: (props: Props<O>) => React.ReactElement | null,
) {
const Role = (props: Props<O>, ref: React.Ref<any>) =>
render({ ref, ...props });
Expand All @@ -83,7 +83,7 @@ export function createComponent<O extends Options>(
* <Component customProp render={<button />} />
*/
export function createMemoComponent<O extends Options>(
render: (props: Props<O>) => React.ReactNode,
render: (props: Props<O>) => React.ReactElement | null,
) {
const Role = createComponent(render);
return React.memo(Role) as unknown as typeof Role;
Expand Down
39 changes: 26 additions & 13 deletions scripts/build/build.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { cpSync } from "fs";
import spawn from "cross-spawn";
import fse from "fs-extra";
import { glob } from "glob";
import { build } from "tsup";
import {
cleanBuild,
Expand Down Expand Up @@ -46,21 +47,33 @@ spawn.sync(
{ stdio: "inherit" },
);

fse.copySync(esmDir, cjsDir);
cpSync(esmDir, cjsDir, { recursive: true });

const declarationFiles = glob.sync("**/*.d.ts", {
cwd: cjsDir,
absolute: true,
});

for (const file of declarationFiles) {
const ctsFile = file.replace(/\.d\.ts$/, ".d.cts");
cpSync(file, ctsFile);
}

const builds = /** @type {const} */ ([
{ format: "esm", outDir: esmDir },
{ format: "cjs", outDir: cjsDir },
]);

for (const { format, outDir } of builds) {
await build({
entry,
format,
outDir,
splitting: true,
esbuildOptions(options) {
options.chunkNames = "__chunks/[hash]";
},
});
}
await Promise.all(
builds.map(({ format, outDir }) =>
build({
entry,
format,
outDir,
splitting: true,
esbuildOptions(options) {
options.chunkNames = "__chunks/[hash]";
},
}),
),
);
5 changes: 4 additions & 1 deletion scripts/build/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ export function getPackageJson(rootPath, prod = false) {
path = removeExt(path).replace(sourcePath, "");
return {
import: `./${join(esmDir, path)}.js`,
require: `./${join(cjsDir, path)}.cjs`,
require: {
types: `./${join(cjsDir, path)}.d.cts`,
default: `./${join(cjsDir, path)}.cjs`,
},
};
};

Expand Down
4 changes: 1 addition & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"jsx": "react-jsx",
"types": ["node", "vitest/globals"],
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"incremental": true,
"noEmit": true,
"skipLibCheck": true,
"allowJs": true,
Expand All @@ -22,8 +21,7 @@
"noUnusedParameters": true,
"stripInternal": true,
"noUncheckedIndexedAccess": true,
"verbatimModuleSyntax": true,
"isolatedModules": true
"verbatimModuleSyntax": true
},
"include": [
"**/*.ts",
Expand Down

1 comment on commit 5b6ce47

@vercel
Copy link

@vercel vercel bot commented on 5b6ce47 Oct 13, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

ariakit – ./

ariakit-ariakit.vercel.app
ariakit.org
ariakit-git-main-ariakit.vercel.app
www.ariakit.org

Please sign in to comment.