Skip to content

Commit 9699f0d

Browse files
committed
Chore: Solving TypeScript packaging issues
1 parent f1ca15c commit 9699f0d

File tree

15 files changed

+50
-110
lines changed

15 files changed

+50
-110
lines changed

.changeset/tasty-ducks-turn.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
"@mincho-js/css-additional-types": patch
3+
"@mincho-js/transform-to-vanilla": patch
4+
"vite-config-custom": patch
5+
"@mincho-js/integration": patch
6+
"@mincho-js/debug-log": patch
7+
"@mincho-js/esbuild": patch
8+
"@mincho-js/babel": patch
9+
"@mincho-js/react": patch
10+
"@mincho-js/vite": patch
11+
"@mincho-js/css": patch
12+
---
13+
14+
**package**
15+
- Achieve all [Are the types wrong](https://github.com/arethetypeswrong/arethetypeswrong.github.io) using [vite-plugin-dts-build's dual mode](https://github.com/black7375/vite-plugin-dts-build#dual-module-support).

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ build/Release
4242
node_modules/
4343
jspm_packages/
4444

45+
# Package Redirect - Sync with package.json "workspaces"
46+
configs/*/**/package.json
47+
packages/*/**/package.json
48+
examples/*/**/package.json
49+
4550
# Snowpack dependency directory (https://snowpack.dev/)
4651
web_modules/
4752

@@ -90,6 +95,7 @@ out
9095
# Nuxt.js build / generate output
9196
.nuxt
9297
dist
98+
dist-ssr
9399

94100
# Gatsby files
95101
.cache/

configs/vite-config-custom/index.js

Lines changed: 3 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
// @ts-check
22

3-
import { readdir, readFile, writeFile, unlink } from "node:fs/promises";
43
import { resolve, join } from "node:path";
54
import { cwd, env } from "node:process";
65

7-
import { PromisePool } from "@supercharge/promise-pool";
86
import { initConfigBuilder, ViteEnv, PluginBuilder } from "vite-config-builder";
97
import { mergeConfig } from "vite";
108

11-
import { ModuleKind, ModuleResolutionKind } from "typescript";
129
import { externalizeDeps } from "vite-plugin-externalize-deps";
1310
import tsconfigPaths from "vite-tsconfig-paths";
14-
import { dts } from "vite-plugin-dts-build";
11+
import { dtsForEsm, dtsForCjs } from "vite-plugin-dts-build";
1512

1613
// == Main Configs ============================================================
1714
export function NodeConfig(viteConfigEnv, extendConfigs = {}) {
@@ -33,48 +30,24 @@ function NodeBuilder(viteConfigEnv) {
3330
const packageRoot = cwd();
3431
const entryRoot = resolve(packageRoot, "src");
3532
const entryFile = resolve(entryRoot, "index.ts");
36-
const cacheEsmDir = resolve(packageRoot, ".cache", "typescript", "esm");
37-
const cacheCjsDir = resolve(packageRoot, ".cache", "typescript", "cjs");
38-
const outEsmDir = resolve(packageRoot, "dist", "esm");
39-
const outCjsDir = resolve(packageRoot, "dist", "cjs");
4033

4134
const runtimeEnv = getRuntimeEnv();
4235
if (ViteEnv.isProd()) {
4336
if (runtimeEnv === "LOCAL" || runtimeEnv === "PUBLISH") {
4437
plugins.add(
4538
// This is currently a proprietary implementation. You might also like to see
4639
// https://github.com/qmhc/vite-plugin-dts/issues/267
47-
dts({
40+
dtsForEsm({
4841
include: ["src"],
49-
cacheDir: cacheEsmDir,
50-
outDir: outEsmDir,
5142
tsconfigPath: resolve(packageRoot, "tsconfig.lib.json")
5243
})
5344
);
5445
}
5546
if (runtimeEnv === "PUBLISH") {
5647
plugins.add(
57-
dts({
48+
dtsForCjs({
5849
include: ["src"],
59-
cacheDir: cacheCjsDir,
60-
outDir: outCjsDir,
6150
tsconfigPath: resolve(packageRoot, "tsconfig.lib.json"),
62-
compilerOptions: {
63-
module: ModuleKind.CommonJS,
64-
moduleResolution: ModuleResolutionKind.Node10,
65-
outDir: outCjsDir,
66-
declarationDir: outCjsDir,
67-
tsBuildInfoFile: resolve(
68-
packageRoot,
69-
".cache",
70-
"typescript",
71-
"tsbuildinfo-cjs"
72-
)
73-
},
74-
afterBuild: async () => {
75-
// Rename the CommonJS declaration file to .d.cts
76-
await renameDeclarationFiles(outCjsDir);
77-
}
7851
})
7952
);
8053
}
@@ -171,69 +144,6 @@ function initCommonBuilder(viteConfigEnv) {
171144
};
172145
}
173146

174-
async function renameDeclarationFiles(dir) {
175-
try {
176-
const allFiles = await collectDeclarationFiles(dir);
177-
178-
if (allFiles.length === 0) {
179-
return;
180-
}
181-
console.log(`Processing ${allFiles.length} declaration files...`);
182-
183-
const { errors } = await PromisePool.for(allFiles)
184-
.withConcurrency(10)
185-
.process(processCtsFile);
186-
187-
if (errors.length > 0) {
188-
console.error(`${errors.length} files failed to process`);
189-
}
190-
} catch (error) {
191-
console.error(`Error processing: ${error.message}`);
192-
}
193-
}
194-
195-
async function collectDeclarationFiles(dir, fileList = []) {
196-
try {
197-
const fileOrDirs = await readdir(dir, { withFileTypes: true });
198-
const subDirectories = [];
199-
200-
for (const fileOrDir of fileOrDirs) {
201-
const fullPath = join(dir, fileOrDir.name);
202-
203-
if (fileOrDir.isDirectory()) {
204-
subDirectories.push(fullPath);
205-
} else if (fileOrDir.name.endsWith(".d.ts")) {
206-
fileList.push(fullPath);
207-
}
208-
}
209-
210-
if (subDirectories.length > 0) {
211-
await PromisePool.for(subDirectories)
212-
.withConcurrency(8)
213-
.process(async (subDir) => {
214-
await collectDeclarationFiles(subDir, fileList);
215-
});
216-
}
217-
218-
return fileList;
219-
} catch (error) {
220-
console.error(`Error reading directory ${dir}: ${error.message}`);
221-
return fileList;
222-
}
223-
}
224-
225-
async function processCtsFile(fullPath) {
226-
// Change import paths from .js to .cjs
227-
const content = await readFile(fullPath, "utf8");
228-
const importRegex = /from ['"](.+)\.js['"];?$/gm;
229-
const modifiedContent = content.replace(importRegex, "from '$1.cjs'");
230-
231-
// Change file extension from .d.ts to .d.cts
232-
const newPath = fullPath.replace(".d.ts", ".d.cts");
233-
await writeFile(newPath, modifiedContent, "utf8");
234-
await unlink(fullPath);
235-
}
236-
237147
function getRuntimeEnv() {
238148
if (env["PACKAGE_PUBLISH"] === "true") {
239149
return "PUBLISH";

configs/vite-config-custom/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
"devDependencies": {
1010
"prettier-config-custom": "workspace:^",
1111
"vite-config-builder": "^0.0.2",
12-
"vite-plugin-dts-build": "^0.1.3",
12+
"vite-plugin-dts-build": "^0.2.2",
1313
"vite-plugin-externalize-deps": "^0.9.0",
1414
"vite-tsconfig-paths": "^5.1.4"
1515
},
1616
"dependencies": {
17-
"@supercharge/promise-pool": "^3.2.0",
1817
"typescript": "^5.8.3",
1918
"vite": "^6.3.5"
2019
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"task": "yarn turbo run --cache-dir .cache/turbo",
1313
"clean": "yarn g:clean && yarn task clean",
1414
"build": "yarn task build",
15+
"build:full": "PACKAGE_PUBLISH=true yarn task build",
1516
"build:type": "yarn tsc --build",
1617
"lint": "yarn lint:action & yarn task lint",
1718
"lint:action": "yarn run -T eslint '.github/**/*.{js,yaml,yml}' --cache --cache-location .cache/eslint_action",

packages/babel/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
},
3939
"files": [
4040
"dist",
41-
"src"
41+
"src",
42+
"**/package.json"
4243
],
4344
"scripts": {
4445
"clean": "yarn g:clean",

packages/css-additional-types/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
},
4444
"files": [
4545
"README.md",
46-
"dist/"
46+
"dist/",
47+
"**/package.json"
4748
],
4849
"scripts": {
4950
"clean": "yarn g:clean",

packages/css/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@
6868
},
6969
"files": [
7070
"README.md",
71-
"dist/"
71+
"dist/",
72+
"**/package.json"
7273
],
7374
"scripts": {
7475
"clean": "yarn g:clean",

packages/debug-log/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
"files": [
4646
"README.md",
4747
"importMeta.d.ts",
48-
"dist/"
48+
"dist/",
49+
"**/package.json"
4950
],
5051
"scripts": {
5152
"clean": "yarn g:clean",

packages/esbuild/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
},
3939
"files": [
4040
"dist",
41-
"src"
41+
"src",
42+
"**/package.json"
4243
],
4344
"scripts": {
4445
"clean": "yarn g:clean",

0 commit comments

Comments
 (0)