Skip to content

Commit 171dc9e

Browse files
committed
Use extension based on "type" in package.json
1 parent 53fe05c commit 171dc9e

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

packages/code-infra/src/bundlers/tsdown.mjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ export async function build(args, pkgJson) {
6767
loader: {
6868
'.js': 'jsx',
6969
},
70-
fixedExtension: true,
7170
logLevel: args.verbose ? 'info' : 'silent',
7271
tsconfig: tsconfigPath ?? undefined,
7372
sourcemap: false,
@@ -94,7 +93,7 @@ export async function build(args, pkgJson) {
9493
? {
9594
cwd,
9695
tsconfig: tsconfigPath,
97-
emitJs: true,
96+
emitJs: false,
9897
compilerOptions: {
9998
jsx: 'react-jsx',
10099
outDir: 'build',
@@ -154,5 +153,7 @@ export async function build(args, pkgJson) {
154153
);
155154
}
156155
await Promise.all(promises);
157-
await writePkgJson(pkgJson, outChunks);
156+
await writePkgJson(pkgJson, outChunks, {
157+
usePkgType: true,
158+
});
158159
}

packages/code-infra/src/utils/build.mjs

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { globby } from 'globby';
22
import * as fs from 'node:fs/promises';
3-
import * as os from 'node:os';
43
import * as path from 'node:path';
54

65
/**
@@ -296,9 +295,11 @@ export function validatePkgJson(pkgJson) {
296295
/**
297296
* @param {Record<string, any>} basePkgJson
298297
* @param {Record<string, {paths: string[]; isBin?: boolean}>} chunks
298+
* @param {{ usePkgType?: boolean }} [options]
299299
* @returns {Promise<void>}
300300
*/
301-
export async function writePkgJson(basePkgJson, chunks) {
301+
export async function writePkgJson(basePkgJson, chunks, options = {}) {
302+
const usePkgType = options.usePkgType || false;
302303
const cwd = process.cwd();
303304
const outPath = path.join(
304305
cwd,
@@ -311,7 +312,10 @@ export async function writePkgJson(basePkgJson, chunks) {
311312
delete basePkgJson.publishConfig;
312313
delete basePkgJson.exports;
313314
delete basePkgJson.bin;
314-
315+
if (basePkgJson.packageScripts) {
316+
basePkgJson.scripts = basePkgJson.packageScripts;
317+
delete basePkgJson.packageScripts;
318+
}
315319
basePkgJson.sideEffects ??= false;
316320
basePkgJson.type ??= 'commonjs';
317321
const isModule = basePkgJson.type === 'module';
@@ -335,10 +339,14 @@ export async function writePkgJson(basePkgJson, chunks) {
335339
key === 'index'
336340
? '.'
337341
: `./${key.endsWith('/index') ? key.substring(0, key.length - '/index'.length) : key}`;
338-
const requireTypePath = chunk.paths.find((p) => p.endsWith('.d.cts'));
339-
const importTypePath = chunk.paths.find((p) => p.endsWith('.d.mts'));
340-
const requirePath = chunk.paths.find((p) => p.endsWith('.cjs'));
341-
const importPath = chunk.paths.find((p) => p.endsWith('.mjs'));
342+
const cjsExtension = usePkgType && isModule ? '.cjs' : '.js';
343+
const cjsDtsExtension = usePkgType && isModule ? '.d.cts' : '.d.ts';
344+
const mjsExtension = usePkgType && !isModule ? '.mjs' : '.js';
345+
const mjsDtsExtension = usePkgType && !isModule ? '.d.mts' : '.d.ts';
346+
const requireTypePath = chunk.paths.find((p) => p.endsWith(cjsDtsExtension));
347+
const importTypePath = chunk.paths.find((p) => p.endsWith(mjsDtsExtension));
348+
const requirePath = chunk.paths.find((p) => p.endsWith(cjsExtension));
349+
const importPath = chunk.paths.find((p) => p.endsWith(mjsExtension));
342350
newExports[pathKey] = {
343351
require: requirePath
344352
? {
@@ -355,30 +363,38 @@ export async function writePkgJson(basePkgJson, chunks) {
355363
};
356364
if (newExports[pathKey].import) {
357365
newExports[pathKey].default = newExports[pathKey].import;
366+
if (pathKey === '.') {
367+
basePkgJson.module = newExports[pathKey].import.default;
368+
}
358369
delete newExports[pathKey].import;
359370
} else if (newExports[pathKey].require) {
360371
newExports[pathKey].default = newExports[pathKey].require;
372+
if (pathKey === '.') {
373+
basePkgJson.main = newExports[pathKey].require.default;
374+
basePkgJson.types = newExports[pathKey].require.types;
375+
}
361376
delete newExports[pathKey].require;
362377
}
363378
}
364379
});
365380

366381
if (Object.keys(newExports).length) {
382+
const dotExport = newExports['.'];
383+
delete newExports['.'];
367384
basePkgJson.exports = {
368385
'./package.json': './package.json',
386+
...(dotExport ? { '.': dotExport } : {}),
369387
...newExports,
370388
};
371389

372-
if (newExports['.']) {
373-
const mainExport = newExports['.'];
374-
if (typeof mainExport === 'string') {
375-
basePkgJson.main = mainExport;
376-
} else if (typeof mainExport === 'object') {
377-
if (isModule && typeof mainExport.import === 'string') {
378-
basePkgJson.module = mainExport.import;
379-
} else if (!isModule && typeof mainExport.require === 'string') {
380-
basePkgJson.main = mainExport.require;
381-
}
390+
if (dotExport) {
391+
const mainExport = dotExport;
392+
if (mainExport.require) {
393+
basePkgJson.main = mainExport.require.default;
394+
basePkgJson.types = mainExport.require.types;
395+
}
396+
if (mainExport.default) {
397+
basePkgJson.module = mainExport.default.default;
382398
}
383399
}
384400
}
@@ -387,7 +403,7 @@ export async function writePkgJson(basePkgJson, chunks) {
387403
basePkgJson.bin = newBin;
388404
}
389405

390-
await fs.writeFile(outPath, `${JSON.stringify(basePkgJson, null, 2)}${os.EOL}`);
406+
await fs.writeFile(outPath, `${JSON.stringify(basePkgJson, null, 2)}\n`);
391407
}
392408

393409
const TS_CONFIG_PATHS = ['tsconfig.build.json', 'tsconfig.json'];

0 commit comments

Comments
 (0)