Skip to content

Commit a22c132

Browse files
committed
fix: fixed dts only out generation, fixed compatibility mode if the value is true
1 parent 306d20d commit a22c132

File tree

4 files changed

+225
-40
lines changed

4 files changed

+225
-40
lines changed

packages/packem/__tests__/intigration/__snapshots__/typescript.test.ts.snap

+113
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,119 @@ export type { NativeStringTypes };
440440
"
441441
`;
442442
443+
exports[`packem typescript > should compile a .d.ts file into .d.cts and .d.mts > .d.ts type code output 1`] = `
444+
"declare global {
445+
interface String {
446+
447+
}
448+
}
449+
450+
type NativeStringTypes = string;
451+
452+
export type { NativeStringTypes };
453+
"
454+
`;
455+
456+
exports[`packem typescript > should compile a .d.ts file into .d.cts and .d.mts, with other exports > .d.cts type code output 1`] = `
457+
"declare global {
458+
interface String {
459+
460+
}
461+
}
462+
463+
type NativeStringTypes = string;
464+
465+
export type { NativeStringTypes };
466+
"
467+
`;
468+
469+
exports[`packem typescript > should compile a .d.ts file into .d.cts and .d.mts, with other exports > .d.mts type code output 1`] = `
470+
"declare global {
471+
interface String {
472+
473+
}
474+
}
475+
476+
type NativeStringTypes = string;
477+
478+
export type { NativeStringTypes };
479+
"
480+
`;
481+
482+
exports[`packem typescript > should compile a .d.ts file into .d.cts and .d.mts, with other exports > .d.ts type code output 1`] = `
483+
"declare global {
484+
interface String {
485+
486+
}
487+
}
488+
489+
type NativeStringTypes = string;
490+
491+
export type { NativeStringTypes };
492+
"
493+
`;
494+
495+
exports[`packem typescript > should compile a .d.ts file into .d.cts and .d.mts, with other exports > cjs content 1`] = `
496+
"'use strict';
497+
498+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
499+
500+
const isUpperCode = new Uint8Array(128);
501+
const isLowerCode = new Uint8Array(128);
502+
const isDigitCode = new Uint8Array(128);
503+
for (let index = 0; index < 128; index++) {
504+
isUpperCode[index] = index >= 65 && index <= 90 ? 1 : 0;
505+
isLowerCode[index] = index >= 97 && index <= 122 ? 1 : 0;
506+
isDigitCode[index] = index >= 48 && index <= 57 ? 1 : 0;
507+
}
508+
509+
exports.isDigitCode = isDigitCode;
510+
exports.isLowerCode = isLowerCode;
511+
exports.isUpperCode = isUpperCode;
512+
"
513+
`;
514+
515+
exports[`packem typescript > should compile a .d.ts file into .d.cts and .d.mts, with other exports > d.cts content 1`] = `
516+
"declare const isUpperCode: Uint8Array<ArrayBuffer>;
517+
declare const isLowerCode: Uint8Array<ArrayBuffer>;
518+
declare const isDigitCode: Uint8Array<ArrayBuffer>;
519+
520+
export { isDigitCode, isLowerCode, isUpperCode };
521+
"
522+
`;
523+
524+
exports[`packem typescript > should compile a .d.ts file into .d.cts and .d.mts, with other exports > d.mts content 1`] = `
525+
"declare const isUpperCode: Uint8Array<ArrayBuffer>;
526+
declare const isLowerCode: Uint8Array<ArrayBuffer>;
527+
declare const isDigitCode: Uint8Array<ArrayBuffer>;
528+
529+
export { isDigitCode, isLowerCode, isUpperCode };
530+
"
531+
`;
532+
533+
exports[`packem typescript > should compile a .d.ts file into .d.cts and .d.mts, with other exports > d.ts content 1`] = `
534+
"declare const isUpperCode: Uint8Array<ArrayBuffer>;
535+
declare const isLowerCode: Uint8Array<ArrayBuffer>;
536+
declare const isDigitCode: Uint8Array<ArrayBuffer>;
537+
538+
export { isDigitCode, isLowerCode, isUpperCode };
539+
"
540+
`;
541+
542+
exports[`packem typescript > should compile a .d.ts file into .d.cts and .d.mts, with other exports > mjs content 1`] = `
543+
"const isUpperCode = new Uint8Array(128);
544+
const isLowerCode = new Uint8Array(128);
545+
const isDigitCode = new Uint8Array(128);
546+
for (let index = 0; index < 128; index++) {
547+
isUpperCode[index] = index >= 65 && index <= 90 ? 1 : 0;
548+
isLowerCode[index] = index >= 97 && index <= 122 ? 1 : 0;
549+
isDigitCode[index] = index >= 48 && index <= 57 ? 1 : 0;
550+
}
551+
552+
export { isDigitCode, isLowerCode, isUpperCode };
553+
"
554+
`;
555+
443556
exports[`packem typescript > should compile only a type file > .d.ts type code output 1`] = `
444557
"declare global {
445558
interface String {

packages/packem/__tests__/intigration/typescript.test.ts

+102-2
Original file line numberDiff line numberDiff line change
@@ -2953,7 +2953,7 @@ declare global {
29532953
});
29542954

29552955
it("should compile a .d.ts file into .d.cts and .d.mts", async () => {
2956-
expect.assertions(4);
2956+
expect.assertions(5);
29572957

29582958
await writeFile(`${temporaryDirectoryPath}/src/native-string-types.d.ts`, `
29592959
declare global {
@@ -2987,7 +2987,83 @@ export type NativeStringTypes = string;
29872987
const binProcess = await execPackemSync("build", [], {
29882988
cwd: temporaryDirectoryPath,
29892989
});
2990-
console.log(binProcess.stdout);
2990+
2991+
expect(binProcess.stderr).toBe("");
2992+
expect(binProcess.exitCode).toBe(0);
2993+
2994+
const dCtsContent = await readFile(`${temporaryDirectoryPath}/dist/native-string-types.d.cts`);
2995+
2996+
expect(dCtsContent).toMatchSnapshot(".d.cts type code output");
2997+
2998+
const dMtsContent = await readFile(`${temporaryDirectoryPath}/dist/native-string-types.d.mts`);
2999+
3000+
expect(dMtsContent).toMatchSnapshot(".d.mts type code output");
3001+
3002+
const dTsContent = await readFile(`${temporaryDirectoryPath}/dist/native-string-types.d.ts`);
3003+
3004+
expect(dTsContent).toMatchSnapshot(".d.ts type code output");
3005+
});
3006+
3007+
it("should compile a .d.ts file into .d.cts and .d.mts, with other exports", async () => {
3008+
expect.assertions(10);
3009+
3010+
await writeFile(`${temporaryDirectoryPath}/src/native-string-types.d.ts`, `
3011+
declare global {
3012+
interface String {
3013+
3014+
}
3015+
}
3016+
3017+
export type NativeStringTypes = string;
3018+
`);
3019+
await writeFile(`${temporaryDirectoryPath}/src/index.ts`, `// Fast lookup tables for performance optimization
3020+
const isUpperCode = new Uint8Array(128);
3021+
const isLowerCode = new Uint8Array(128);
3022+
const isDigitCode = new Uint8Array(128);
3023+
3024+
// Initialize lookup tables once
3025+
for (let index = 0; index < 128; index++) {
3026+
isUpperCode[index] = index >= 65 && index <= 90 ? 1 : 0; // A-Z
3027+
isLowerCode[index] = index >= 97 && index <= 122 ? 1 : 0; // a-z
3028+
isDigitCode[index] = index >= 48 && index <= 57 ? 1 : 0; // 0-9
3029+
}
3030+
3031+
export { isUpperCode, isLowerCode, isDigitCode };
3032+
`);
3033+
3034+
await installPackage(temporaryDirectoryPath, "typescript");
3035+
await createTsConfig(temporaryDirectoryPath);
3036+
await createPackageJson(temporaryDirectoryPath, {
3037+
devDependencies: {
3038+
typescript: "*",
3039+
},
3040+
exports: {
3041+
"./index": {
3042+
import: {
3043+
default: "./index.mjs",
3044+
types: "./index.d.mts",
3045+
},
3046+
require: {
3047+
default: "./index.cjs",
3048+
types: "./index.d.cts",
3049+
},
3050+
},
3051+
"./native-string-types": {
3052+
import: {
3053+
types: "./native-string-types.d.mts",
3054+
},
3055+
require: {
3056+
types: "./native-string-types.d.cts",
3057+
},
3058+
}
3059+
},
3060+
});
3061+
await createPackemConfig(temporaryDirectoryPath);
3062+
3063+
const binProcess = await execPackemSync("build", [], {
3064+
cwd: temporaryDirectoryPath,
3065+
});
3066+
29913067
expect(binProcess.stderr).toBe("");
29923068
expect(binProcess.exitCode).toBe(0);
29933069

@@ -2998,5 +3074,29 @@ console.log(binProcess.stdout);
29983074
const dMtsContent = await readFile(`${temporaryDirectoryPath}/dist/native-string-types.d.mts`);
29993075

30003076
expect(dMtsContent).toMatchSnapshot(".d.mts type code output");
3077+
3078+
const dTsContent = await readFile(`${temporaryDirectoryPath}/dist/native-string-types.d.ts`);
3079+
3080+
expect(dTsContent).toMatchSnapshot(".d.ts type code output");
3081+
3082+
const cjsIndexContent = await readFile(`${temporaryDirectoryPath}/dist/index.cjs`);
3083+
3084+
expect(cjsIndexContent).toMatchSnapshot("cjs content");
3085+
3086+
const mjsIndexContent = await readFile(`${temporaryDirectoryPath}/dist/index.mjs`);
3087+
3088+
expect(mjsIndexContent).toMatchSnapshot("mjs content");
3089+
3090+
const dTsIndexContent = await readFile(`${temporaryDirectoryPath}/dist/index.d.ts`);
3091+
3092+
expect(dTsIndexContent).toMatchSnapshot("d.ts content");
3093+
3094+
const dCtsIndexContent = await readFile(`${temporaryDirectoryPath}/dist/index.d.cts`);
3095+
3096+
expect(dCtsIndexContent).toMatchSnapshot("d.cts content");
3097+
3098+
const dMtsIndexContent = await readFile(`${temporaryDirectoryPath}/dist/index.d.mts`);
3099+
3100+
expect(dMtsIndexContent).toMatchSnapshot("d.mts content");
30013101
});
30023102
});

packages/packem/src/packem/build.ts

+9-37
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ interface BuilderProperties {
248248
subDirectory: string;
249249
}
250250

251+
const DTS_REGEX = /\.d\.[mc]?ts$/;
252+
251253
/**
252254
* Prepares Rollup configuration for both JavaScript/TypeScript builds and type definition builds.
253255
* Processes entries and generates appropriate builder configurations.
@@ -330,12 +332,9 @@ const prepareRollupConfig = (
330332
const esmEntries: BuildEntry[] = [];
331333
const cjsEntries: BuildEntry[] = [];
332334
const dtsEntries: BuildEntry[] = [];
333-
const dtsOnlyEntries: BuildEntry[] = [];
334335

335336
for (const entry of entries) {
336-
if (/\.d\.[cm]?ts$/.test(entry.input)) {
337-
dtsOnlyEntries.push(entry);
338-
} else if (entry.cjs && entry.esm) {
337+
if (entry.cjs && entry.esm) {
339338
esmAndCjsEntries.push(entry);
340339
} else if (entry.cjs) {
341340
cjsEntries.push(entry);
@@ -353,7 +352,7 @@ const prepareRollupConfig = (
353352
...environmentRuntimeContext.options,
354353
emitCJS: true,
355354
emitESM: true,
356-
entries: esmAndCjsEntries,
355+
entries: [...esmAndCjsEntries].filter((entry) => !DTS_REGEX.test(entry.input)),
357356
minify,
358357
rollup: {
359358
...environmentRuntimeContext.options.rollup,
@@ -375,7 +374,7 @@ const prepareRollupConfig = (
375374
}
376375

377376
if (context.options.declaration) {
378-
const typedEntries = adjustedEsmAndCjsContext.options.entries.filter((entry) => entry.declaration);
377+
const typedEntries = [...esmAndCjsEntries].filter((entry) => entry.declaration);
379378

380379
if (typedEntries.length > 0) {
381380
typeBuilders.add({
@@ -400,7 +399,7 @@ const prepareRollupConfig = (
400399
...environmentRuntimeContext.options,
401400
emitCJS: false,
402401
emitESM: true,
403-
entries: esmEntries,
402+
entries: [...esmEntries].filter((entry) => !DTS_REGEX.test(entry.input)),
404403
minify,
405404
rollup: {
406405
...environmentRuntimeContext.options.rollup,
@@ -422,7 +421,7 @@ const prepareRollupConfig = (
422421
}
423422

424423
if (context.options.declaration) {
425-
const typedEntries = adjustedEsmContext.options.entries.filter((entry) => entry.declaration);
424+
const typedEntries = [...esmEntries].filter((entry) => entry.declaration);
426425

427426
if (typedEntries.length > 0) {
428427
typeBuilders.add({
@@ -447,7 +446,7 @@ const prepareRollupConfig = (
447446
...environmentRuntimeContext.options,
448447
emitCJS: true,
449448
emitESM: false,
450-
entries: cjsEntries,
449+
entries: [...cjsEntries].filter((entry) => !DTS_REGEX.test(entry.input)),
451450
minify,
452451
rollup: {
453452
...environmentRuntimeContext.options.rollup,
@@ -469,7 +468,7 @@ const prepareRollupConfig = (
469468
}
470469

471470
if (context.options.declaration) {
472-
const typedEntries = adjustedCjsContext.options.entries.filter((entry) => entry.declaration);
471+
const typedEntries = [...cjsEntries].filter((entry) => entry.declaration);
473472

474473
if (typedEntries.length > 0) {
475474
typeBuilders.add({
@@ -515,33 +514,6 @@ const prepareRollupConfig = (
515514
subDirectory,
516515
});
517516
}
518-
519-
if (environmentRuntimeContext.options.declaration && dtsOnlyEntries.length > 0) {
520-
typeBuilders.add({
521-
context: {
522-
...environmentRuntimeContext,
523-
options: {
524-
...environmentRuntimeContext.options,
525-
entries: dtsOnlyEntries,
526-
minify,
527-
rollup: {
528-
...environmentRuntimeContext.options.rollup,
529-
replace: environmentRuntimeContext.options.rollup.replace
530-
? {
531-
...environmentRuntimeContext.options.rollup.replace,
532-
values: {
533-
...environmentRuntimeContext.options.rollup.replace.values,
534-
...replaceValues,
535-
},
536-
}
537-
: false,
538-
},
539-
},
540-
},
541-
fileCache,
542-
subDirectory,
543-
});
544-
}
545517
}
546518
}
547519

packages/packem/src/rollup/build-types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const buildTypes = async (context: BuildContext, fileCache: FileCache, subDirect
5858
}
5959

6060
// .d.ts for node10 compatibility (TypeScript version < 4.7)
61-
if (context.options.declaration === "compatible") {
61+
if (context.options.declaration === true || context.options.declaration === "compatible") {
6262
await typesBuild.write({
6363
chunkFileNames: (chunk) => getChunkFilename(chunk, "d.ts"),
6464
dir: resolve(context.options.rootDir, context.options.outDir),

0 commit comments

Comments
 (0)