Skip to content

Commit 4f6c5ba

Browse files
committed
add exports
1 parent d87c6b7 commit 4f6c5ba

File tree

2 files changed

+77
-18
lines changed

2 files changed

+77
-18
lines changed

resources/build-npm.ts

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import ts from 'typescript';
66

77
import { changeExtensionInImportPaths } from './change-extension-in-import-paths.js';
88
import { inlineInvariant } from './inline-invariant.js';
9+
import type { ConditionalExports } from './utils.js';
910
import {
1011
prettify,
1112
readPackageJSON,
@@ -98,18 +99,35 @@ async function buildPackage(outDir: string, isESMOnly: boolean): Promise<void> {
9899
}
99100
}
100101

101-
// Temporary workaround to allow "internal" imports, no grantees provided
102102
packageJSON.exports['./*.js'] = './*.js';
103103
packageJSON.exports['./*'] = './*.js';
104104

105105
packageJSON.publishConfig.tag += '-esm';
106106
packageJSON.version += '+esm';
107107
} else {
108108
delete packageJSON.type;
109-
packageJSON.main = 'index';
109+
packageJSON.main = 'index.js';
110110
packageJSON.module = 'index.mjs';
111-
emitTSFiles({ outDir, module: 'commonjs', extension: '.js' });
111+
packageJSON.types = 'index.d.ts';
112+
113+
const { emittedTSFiles } = emitTSFiles({
114+
outDir,
115+
module: 'commonjs',
116+
extension: '.js',
117+
});
112118
emitTSFiles({ outDir, module: 'es2020', extension: '.mjs' });
119+
120+
packageJSON.exports = {};
121+
for (const filepath of emittedTSFiles) {
122+
if (path.basename(filepath) === 'index.js') {
123+
const relativePath = './' + path.relative('./npmDist', filepath);
124+
packageJSON.exports[path.dirname(relativePath)] =
125+
buildExports(relativePath);
126+
}
127+
}
128+
129+
packageJSON.exports['./*.js'] = buildExports('./*.js');
130+
packageJSON.exports['./*'] = buildExports('./*.js');
113131
}
114132

115133
const packageJsonPath = `./${outDir}/package.json`;
@@ -141,21 +159,31 @@ function emitTSFiles(options: {
141159

142160
const tsHost = ts.createCompilerHost(tsOptions);
143161
tsHost.writeFile = (filepath, body) => {
144-
if (filepath.match(/.js$/) && extension === '.mjs') {
145-
let bodyToWrite = body;
146-
bodyToWrite = bodyToWrite.replace(
147-
'//# sourceMappingURL=graphql.js.map',
148-
'//# sourceMappingURL=graphql.mjs.map',
149-
);
150-
writeGeneratedFile(filepath.replace(/.js$/, extension), bodyToWrite);
151-
} else if (filepath.match(/.js.map$/) && extension === '.mjs') {
152-
writeGeneratedFile(
153-
filepath.replace(/.js.map$/, extension + '.map'),
154-
body,
155-
);
156-
} else {
157-
writeGeneratedFile(filepath, body);
162+
if (extension === '.mjs') {
163+
if (filepath.match(/.js$/)) {
164+
let bodyToWrite = body;
165+
bodyToWrite = bodyToWrite.replace(
166+
'//# sourceMappingURL=graphql.js.map',
167+
'//# sourceMappingURL=graphql.mjs.map',
168+
);
169+
writeGeneratedFile(filepath.replace(/.js$/, extension), bodyToWrite);
170+
return;
171+
}
172+
173+
if (filepath.match(/.js.map$/)) {
174+
writeGeneratedFile(
175+
filepath.replace(/.js.map$/, extension + '.map'),
176+
body,
177+
);
178+
return;
179+
}
180+
181+
if (filepath.match(/.d.ts$/)) {
182+
writeGeneratedFile(filepath.replace(/.d.ts$/, '.d.mts'), body);
183+
return;
184+
}
158185
}
186+
writeGeneratedFile(filepath, body);
159187
};
160188

161189
const tsProgram = ts.createProgram(['src/index.ts'], tsOptions, tsHost);
@@ -172,3 +200,22 @@ function emitTSFiles(options: {
172200
emittedTSFiles: tsResult.emittedFiles.sort((a, b) => a.localeCompare(b)),
173201
};
174202
}
203+
204+
function buildExports(filepath: string): ConditionalExports {
205+
const { dir, name } = path.parse(filepath);
206+
const base = `./${path.join(dir, name)}`;
207+
return {
208+
types: {
209+
module: `${base}.d.mts`,
210+
'module-sync': `${base}.d.mts`,
211+
node: `${base}.d.ts`,
212+
require: `${base}.d.ts`,
213+
default: `${base}.d.mts`,
214+
},
215+
module: `${base}.mjs`,
216+
'module-sync': `${base}.mjs`,
217+
node: `${base}.js`,
218+
require: `${base}.js`,
219+
default: `${base}.mjs`,
220+
};
221+
}

resources/utils.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ interface PackageJSON {
234234
repository?: { url?: string };
235235
scripts?: { [name: string]: string };
236236
type?: string;
237-
exports: { [path: string]: string };
237+
exports: { [path: string]: string | ConditionalExports };
238238
types?: string;
239239
typesVersions: { [ranges: string]: { [path: string]: Array<string> } };
240240
devDependencies?: { [name: string]: string };
@@ -245,6 +245,18 @@ interface PackageJSON {
245245
module?: string;
246246
}
247247

248+
export interface ConditionalExports extends BaseExports {
249+
types: BaseExports;
250+
}
251+
252+
interface BaseExports {
253+
module: string;
254+
'module-sync': string;
255+
node: string;
256+
require: string;
257+
default: string;
258+
}
259+
248260
export function readPackageJSON(
249261
dirPath: string = localRepoPath(),
250262
): PackageJSON {

0 commit comments

Comments
 (0)