Skip to content

Commit

Permalink
Merge pull request #132 from piglovesyou/fix-issue-118
Browse files Browse the repository at this point in the history
Pick up only correspoinding dtsContents for target `.tsx`s
  • Loading branch information
piglovesyou authored Aug 2, 2020
2 parents edbcf43 + 9e87b45 commit 5dbb38b
Show file tree
Hide file tree
Showing 29 changed files with 679 additions and 586 deletions.
14 changes: 9 additions & 5 deletions src/gen.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import globby from 'globby';
import createExecContext from './lib/exec-context';
import fullGenerate, {
CodegenContext,
SkippedContext,
Expand All @@ -13,8 +14,8 @@ import { rimraf } from './lib/file';
async function removeOldTsxCaches(
cwd: string,
config: ConfigTypes,
codegenContext: CodegenContext,
skippedContext: SkippedContext,
codegenContext: CodegenContext[],
skippedContext: SkippedContext[],
) {
const cacheDir = getCacheFullDir(cwd, config.cacheDir);
const validTsxCaches = [
Expand All @@ -28,13 +29,16 @@ async function removeOldTsxCaches(
for (const tsx of oldTsxPaths) await rimraf(tsx);
}

export default async function gen(commandOpts: CommandOpts): Promise<void> {
export default async function gen({
cwd,
configFilePath,
}: CommandOpts): Promise<void> {
logUpdate(PRINT_PREFIX + 'Running graphql-codegen...');

const { cwd, configFilePath } = commandOpts;
const [config, configHash] = await loadConfig(cwd, configFilePath);
const execContext = createExecContext(cwd, config, configHash);

const [generated, skipped] = await fullGenerate(cwd, config, configHash);
const [generated, skipped] = await fullGenerate(execContext);

await removeOldTsxCaches(cwd, config, generated, skipped);

Expand Down
7 changes: 4 additions & 3 deletions src/jestTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createTransformer, getCacheKey as getBabelCacheKey } from 'babel-jest';
import graphQLTransformer from 'jest-transform-graphql';
import { join as pathJoin, relative as pathRelative } from 'path';
import { readFileSync } from 'fs';
import createExecContext from './lib/exec-context';
import { loadConfigSync } from './lib/load-config';
import { createPaths } from './lib/paths';

Expand All @@ -23,7 +24,8 @@ const jestTransformer: Transformer = {
},
process(input, filePath, jestConfig, transformOptions) {
const { rootDir } = jestConfig;
const [config] = loadConfigSync(rootDir);
const [config, configHash] = loadConfigSync(rootDir);
const execContext = createExecContext(rootDir, config, configHash);
const fileSchema = config.schema as string;
const schemaFullPath = pathJoin(rootDir, fileSchema);

Expand All @@ -37,9 +39,8 @@ const jestTransformer: Transformer = {
}

const { tsxFullPath } = createPaths(
rootDir,
execContext,
pathRelative(rootDir, filePath),
config.cacheDir,
);

const tsxContent = readFileSync(tsxFullPath, 'utf-8');
Expand Down
4 changes: 2 additions & 2 deletions src/lib/create-codegen-opts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { ConfigTypes } from './types';

export type PartialCodegenOpts = Pick<Types.GenerateOptions, 'config'>;

export default async function createCodegenOpts(
export default function createCodegenOpts(
config: ConfigTypes,
): Promise<PartialCodegenOpts> {
): PartialCodegenOpts {
return {
config: {
withHOC: false, // True by default
Expand Down
52 changes: 36 additions & 16 deletions src/lib/dts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import makeDir from 'make-dir';
import path from 'path';
import slash from 'slash';
import {
createCompilerHost,
createProgram,
Expand All @@ -10,7 +11,9 @@ import {
findConfigFile,
readConfigFile,
} from 'typescript';
import { ExecContext } from './exec-context';
import { withHash, writeFile } from './file';
import { CodegenContext } from './full-generate';
import { ConfigTypes } from './types';

const essentialCompilerOptions: CompilerOptions = {
Expand All @@ -20,9 +23,9 @@ const essentialCompilerOptions: CompilerOptions = {
noEmit: false,
};

function resolveCompilerOptions(configObj: ConfigTypes) {
const fileName = configObj.TSConfigFile || 'tsconfig.json';
const configPath = findConfigFile(process.cwd(), sys.fileExists, fileName);
function resolveCompilerOptions(cwd: string, { TSConfigFile }: ConfigTypes) {
const fileName = TSConfigFile || 'tsconfig.json';
const configPath = findConfigFile(cwd, sys.fileExists, fileName);
let compilerOptions = essentialCompilerOptions;

if (configPath != null) {
Expand All @@ -32,7 +35,7 @@ function resolveCompilerOptions(configObj: ConfigTypes) {
if (config != null) {
const settings = convertCompilerOptionsFromJson(
{ ...config['compilerOptions'], ...essentialCompilerOptions },
process.cwd(),
cwd,
);
if (settings.errors.length > 0) {
console.log(settings.errors);
Expand All @@ -51,16 +54,29 @@ function resolveCompilerOptions(configObj: ConfigTypes) {
}

export function genDts(
tsxFullPaths: string[],
configObj: ConfigTypes,
{ cwd, config }: ExecContext,
codegenContext: CodegenContext[],
): string[] {
const compilerOptions = resolveCompilerOptions(configObj);
const compilerOptions = resolveCompilerOptions(cwd, config);
const tsxFullPaths = codegenContext.map(({ tsxFullPath }) =>
slash(tsxFullPath),
);
const tsxFullPathSet = new Set(tsxFullPaths);

const compilerHost = createCompilerHost(compilerOptions);

const dtsContents: string[] = [];
compilerHost.writeFile = (name, dtsContent) => {
// XXX: How to improve memory usage?
compilerHost.writeFile = (
name,
dtsContent,
writeByteOrderMark,
onError,
sourceFiles,
) => {
// TypeScript can write `d.ts`s of submodules imported from `.tsx`s.
// We only pick up `.d.ts`s for `.tsx` entry points.
const [{ fileName }] = sourceFiles!;
if (!tsxFullPathSet.has(fileName)) return;
dtsContents.push(dtsContent);
};

Expand Down Expand Up @@ -94,19 +110,23 @@ export function genDts(
throw new Error('Failed to generate .d.ts.');
}

if (codegenContext.length !== dtsContents.length) {
throw new Error(
`Never supposed to be here. Please make an issue on GitHub.`,
);
}

return dtsContents;
}

export async function processGenDts(
dtsFullPath: string,
tsxFullPath: string,
gqlRelPath: string,
sourceHash: string,
config: ConfigTypes,
execContext: ExecContext,
codegenContext: CodegenContext,
) {
const { dtsFullPath, gqlHash } = codegenContext;
await makeDir(path.dirname(dtsFullPath));
const [dtsContent] = await genDts([tsxFullPath], config);
const [dtsContent] = await genDts(execContext, [codegenContext]);
if (!dtsContent) throw new Error(`Generate ${dtsFullPath} fails.`);
await writeFile(dtsFullPath, withHash(sourceHash, dtsContent));
await writeFile(dtsFullPath, withHash(gqlHash, dtsContent));
return dtsContent;
}
27 changes: 27 additions & 0 deletions src/lib/exec-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import createCodegenOpts, { PartialCodegenOpts } from './create-codegen-opts';
import { getCacheFullDir } from './paths';
import { ConfigTypes } from './types';

export type ExecContext = {
cwd: string;
config: ConfigTypes;
configHash: string;
codegenOpts: PartialCodegenOpts;
cacheFullDir: string;
};

export default function createExecContext(
cwd: string,
config: ConfigTypes,
configHash: string,
) {
const codegenOpts = createCodegenOpts(config);
const cacheFullDir = getCacheFullDir(cwd, config.cacheDir);
return {
cwd,
config,
codegenOpts,
configHash,
cacheFullDir,
};
}
Loading

0 comments on commit 5dbb38b

Please sign in to comment.