import assert from 'node:assert';
import fs from 'node:fs';
import path from 'node:path';
import ts from 'typescript';
import {
changeExtensionInImportPaths,
changeExtensionInImportPathsInBundle,
} from './change-extension-in-import-paths.ts';
import { inlineInvariant } from './inline-invariant.ts';
import { stripPrivateDeclarations } from './strip-private-declarations.ts';
import type { PlatformConditionalExports } from './utils.ts';
import {
buildCJSDevModeStub,
buildESMDevModeStub,
localRepoPath,
prettify,
readPackageJSON,
readTSConfig,
showDirStats,
writeGeneratedFile,
} from './utils.ts';
const devTypeFiles = [
['.ts', '.js'],
['.mts', '.mjs'],
] as const;
const outputDirectory = path.resolve(
process.argv[2] ?? localRepoPath('npmDist'),
);
console.log(`\n${outputDirectory}`);
await buildPackage(outputDirectory);
showDirStats(outputDirectory);
async function buildPackage(outDir: string): Promise<void> {
const devDir = path.join(outDir, '__dev__');
fs.rmSync(outDir, { recursive: true, force: true });
fs.mkdirSync(outDir);
fs.mkdirSync(devDir);
fs.copyFileSync(localRepoPath('LICENSE'), path.join(outDir, 'LICENSE'));
fs.copyFileSync(localRepoPath('README.md'), path.join(outDir, 'README.md'));
const packageJSON = readPackageJSON();
delete packageJSON.private;
delete packageJSON.scripts;
delete packageJSON.devDependencies;
assert(packageJSON.types === undefined, 'Unexpected "types" in package.json');
const supportedTSVersions = Object.keys(packageJSON.typesVersions);
assert(
supportedTSVersions.length === 1,
'Property "typesVersions" should have exactly one key.',
);
const notSupportedTSVersionFile = 'NotSupportedTSVersion.d.ts';
fs.writeFileSync(
path.join(outDir, notSupportedTSVersionFile),
`"Package 'graphql' support only TS versions that are ${supportedTSVersions[0]}".`,
);
packageJSON.typesVersions = {
...packageJSON.typesVersions,
'*': { '*': [notSupportedTSVersionFile] },
};
delete packageJSON.type;
packageJSON.main = 'index.js';
packageJSON.module = 'index.mjs';
packageJSON.types = 'index.d.ts';
const { emittedTSFiles } = emitTSFiles({
outDir,
module: 'commonjs',
moduleResolution: 'node10',
extension: '.js',
});
emitTSFiles({ outDir, extension: '.mjs' });
packageJSON.exports = {};
for (const prodFile of emittedTSFiles) {
const { dir, base, name, ext } = path.parse(prodFile);
if (ext === '.map') {
continue;
}
const relativePathToProd = relativeModulePath(prodFile, outDir);
const { name: innerName, ext: innerExt } = path.parse(name);
if (innerExt === '.d') {
const relativePathAndName = relativeModulePath(
outDir,
`${dir}/${innerName}`,
);
for (const [typeExt, targetExt] of devTypeFiles) {
const line = `export * from '${relativePathToProd}/${relativePathAndName}${targetExt}';`;
writeGeneratedFile(
path.join(devDir, path.relative(outDir, `${dir}/${name}${typeExt}`)),
line,
);
}
continue;
}
const relativePathAndName = relativeModulePath(outDir, `${dir}/${name}`);
writeGeneratedFile(
path.join(devDir, path.relative(outDir, `${dir}/${name}.js`)),
buildCJSDevModeStub(
`${relativePathToProd}/devMode.js`,
`${relativePathToProd}/${relativePathAndName}.js`,
),
);
writeGeneratedFile(
path.join(devDir, path.relative(outDir, `${dir}/${name}.mjs`)),
buildESMDevModeStub(
`${relativePathToProd}/devMode.mjs`,
`${relativePathToProd}/${relativePathAndName}.mjs`,
),
);
if (base === 'index.js') {
const dirname = path.posix.dirname(relativePathAndName);
packageJSON.exports[dirname === '.' ? dirname : `./${dirname}`] = {
development: buildPlatformConditionalExports(
'./__dev__',
relativePathAndName,
),
default: buildPlatformConditionalExports('.', relativePathAndName),
};
}
}
const globEntryPoints = {
development: buildPlatformConditionalExports('./__dev__', '*'),
default: buildPlatformConditionalExports('.', '*'),
};
packageJSON.exports['./*.js'] = globEntryPoints;
packageJSON.exports['./*'] = globEntryPoints;
packageJSON.sideEffects = ['__dev__/*'];
const packageJsonPath = path.join(outDir, 'package.json');
const prettified = await prettify(
packageJsonPath,
JSON.stringify(packageJSON),
);
writeGeneratedFile(packageJsonPath, prettified);
}
function emitTSFiles(options: {
outDir: string;
module?: string;
moduleResolution?: string;
extension: string;
}): {
emittedTSFiles: ReadonlyArray<string>;
} {
const { extension, ...rest } = options;
const rootFiles = [localRepoPath('src/index.ts')];
const jsOptions = readTSConfig({
...rest,
noEmit: false,
declaration: false,
listEmittedFiles: true,
removeComments: true,
});
const dtsOptions = readTSConfig({
...rest,
noEmit: false,
declaration: true,
declarationDir: rest.outDir,
emitDeclarationOnly: true,
listEmittedFiles: true,
});
const writeFile = (filepath: string, body: string) => {
if (extension === '.mjs') {
if (filepath.match(/.js$/)) {
let bodyToWrite = body;
bodyToWrite = bodyToWrite.replace(
/(\/\/# sourceMappingURL=.+)\.js\.map(\r?\n?)$/,
'$1.mjs.map$2',
);
writeGeneratedFile(filepath.replace(/.js$/, extension), bodyToWrite);
return;
}
if (filepath.match(/.js.map$/)) {
const sourceMap = JSON.parse(body);
sourceMap.file = sourceMap.file.replace(/\.js$/, extension);
writeGeneratedFile(
filepath.replace(/.js.map$/, extension + '.map'),
JSON.stringify(sourceMap),
);
return;
}
if (filepath.match(/.d.ts$/)) {
writeGeneratedFile(filepath.replace(/.d.ts$/, '.d.mts'), body);
return;
}
}
writeGeneratedFile(filepath, body);
};
const jsHost = ts.createCompilerHost(jsOptions);
jsHost.getCurrentDirectory = localRepoPath;
jsHost.writeFile = writeFile;
const jsProgram = ts.createProgram(rootFiles, jsOptions, jsHost);
const jsResult = jsProgram.emit(undefined, undefined, undefined, undefined, {
before: [changeExtensionInImportPaths({ extension })],
after: [inlineInvariant],
});
const dtsHost = ts.createCompilerHost(dtsOptions);
dtsHost.getCurrentDirectory = localRepoPath;
dtsHost.writeFile = writeFile;
const dtsProgram = ts.createProgram(rootFiles, dtsOptions, dtsHost);
const dtsResult = dtsProgram.emit(
undefined,
undefined,
undefined,
undefined,
{
afterDeclarations: [
stripPrivateDeclarations(),
changeExtensionInImportPathsInBundle({ extension }),
],
},
);
assert(
!jsResult.emitSkipped && !dtsResult.emitSkipped,
'Fail to generate `*.d.ts` files, please run `npm run check`',
);
assert(jsResult.emittedFiles != null);
assert(dtsResult.emittedFiles != null);
return {
emittedTSFiles: [...jsResult.emittedFiles, ...dtsResult.emittedFiles].sort(
(a, b) => a.localeCompare(b),
),
};
}
function buildPlatformConditionalExports(
dir: string,
name: string,
): PlatformConditionalExports {
const base = `./${path.posix.join(dir, name)}`;
return {
module: `${base}.mjs`,
bun: `${base}.mjs`,
'module-sync': `${base}.mjs`,
node: `${base}.js`,
require: `${base}.js`,
default: `${base}.mjs`,
};
}
function relativeModulePath(from: string, to: string): string {
return path.relative(from, to).split(path.sep).join('/');
}