From e80a50d36c7c4f7eb72339336cb0093788a788ca Mon Sep 17 00:00:00 2001 From: Dolan Date: Fri, 6 Dec 2024 13:44:42 +0000 Subject: [PATCH] =?UTF-8?q?#2846=20Type=20error=20when=20importing=20docx?= =?UTF-8?q?=20in=20CJS=20file=20with=20TypeScript=20modu=E2=80=A6=20(#2883?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * #2846 Type error when importing docx in CJS file with TypeScript moduleResolution set to node16 * Adhere to publint --- .gitignore | 3 +-- eslint.config.ts | 2 +- package.json | 20 ++++++++++++-------- vite.config.ts | 39 ++++++++++++++++++--------------------- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index 2ec3d77b21e..4233d232997 100644 --- a/.gitignore +++ b/.gitignore @@ -33,8 +33,7 @@ node_modules .node_repl_history # build -build -build-tests +dist # Documentation docs/api/ diff --git a/eslint.config.ts b/eslint.config.ts index 8733c814a1b..03101357d04 100644 --- a/eslint.config.ts +++ b/eslint.config.ts @@ -10,7 +10,7 @@ import tsEslint from "typescript-eslint"; const config: Linter.Config[] = [ { - ignores: ["**/vite.config.ts", "**/build/**", "**/coverage/**", "**/*.js", "eslint.config.ts", "**/demo/**", "**/scripts/**"], + ignores: ["**/vite.config.ts", "**/dist/**", "**/coverage/**", "**/*.js", "eslint.config.ts", "**/demo/**", "**/scripts/**"], }, eslint.configs.recommended, importPlugin.flatConfigs.recommended, diff --git a/package.json b/package.json index 1e496e0fd70..5b413eb67c5 100644 --- a/package.json +++ b/package.json @@ -3,19 +3,23 @@ "version": "9.1.0", "description": "Easily generate .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.", "type": "module", - "main": "build/index.umd.js", - "module": "./build/index.mjs", - "types": "./build/index.d.ts", + "main": "dist/index.umd.cjs", + "module": "./dist/index.mjs", + "types": "./dist/index.d.ts", "exports": { ".": { - "require": "./build/index.cjs", - "types": "./build/index.d.ts", - "import": "./build/index.mjs", - "default": "./build/index.mjs" + "import": { + "types": "./dist/index.d.ts", + "default": "./dist/index.mjs" + }, + "require": { + "types": "./dist/index.d.cts", + "default": "./dist/index.cjs" + } } }, "files": [ - "build" + "dist" ], "scripts": { "build": "tsc && vite build", diff --git a/vite.config.ts b/vite.config.ts index f39fd8080fb..b60c84a0d37 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -3,12 +3,18 @@ import dts from "vite-plugin-dts"; import { nodePolyfills } from "vite-plugin-node-polyfills"; import tsconfigPaths from "vite-tsconfig-paths"; import { configDefaults, defineConfig } from "vitest/config"; +import { copyFileSync } from "node:fs"; export default defineConfig({ plugins: [ tsconfigPaths(), dts({ - rollupTypes: true + rollupTypes: true, + afterBuild: () => { + // https://github.com/dolanmiu/docx/pull/2883 + // To pass publint - `npx publint@latest` + copyFileSync("dist/index.d.ts", "dist/index.d.cts"); + }, }), nodePolyfills({ exclude: [], @@ -34,7 +40,7 @@ export default defineConfig({ name: "docx", fileName: (d) => { if (d === "umd") { - return "index.umd.js"; + return "index.umd.cjs"; } if (d === "cjs") { @@ -53,7 +59,7 @@ export default defineConfig({ }, formats: ["iife", "es", "cjs", "umd"], }, - outDir: resolve(__dirname, "build"), + outDir: resolve(__dirname, "dist"), commonjsOptions: { include: [/node_modules/], }, @@ -71,25 +77,16 @@ export default defineConfig({ }, exclude: [ ...configDefaults.exclude, - '**/build/**', - '**/demo/**', - '**/docs/**', - '**/scripts/**', - '**/src/**/index.ts', - '**/src/**/types.ts', - '**/*.spec.ts', + "**/dist/**", + "**/demo/**", + "**/docs/**", + "**/scripts/**", + "**/src/**/index.ts", + "**/src/**/types.ts", + "**/*.spec.ts", ], }, - include: [ - '**/src/**/*.spec.ts', - '**/packages/**/*.spec.ts' - ], - exclude: [ - ...configDefaults.exclude, - '**/build/**', - '**/demo/**', - '**/docs/**', - '**/scripts/**' - ], + include: ["**/src/**/*.spec.ts", "**/packages/**/*.spec.ts"], + exclude: [...configDefaults.exclude, "**/build/**", "**/demo/**", "**/docs/**", "**/scripts/**"], }, });