forked from graphql-nexus/nexus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: better esm support, remove top-level node imports (graphql-nexu…
…s#1112) * test for esm/esbuild use of nexus * refactor: add nodeImports, remove top-level references * Don't minify the esbuild test script * A bit more explicit of a check on the process being node
- Loading branch information
Showing
11 changed files
with
263 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ examples/*/dist | |
website/static/playground-dist | ||
yarn-error.log | ||
coverage/* | ||
tests/esm/out/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export function nodeImports() { | ||
const fs = require('fs') as typeof import('fs') | ||
const path = require('path') as typeof import('path') | ||
return { | ||
fs, | ||
path, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,75 @@ | ||
import * as path from 'path' | ||
import type { BuilderConfigInput } from './builder' | ||
import type { ConfiguredTypegen } from './core' | ||
import { nodeImports } from './node' | ||
import type { TypegenMetadataConfig } from './typegenMetadata' | ||
import { assertAbsolutePath, getOwnPackage, isProductionStage } from './utils' | ||
|
||
/** Normalizes the builder config into the config we need for typegen */ | ||
export function resolveTypegenConfig(config: BuilderConfigInput): TypegenMetadataConfig { | ||
const { | ||
outputs, | ||
shouldGenerateArtifacts = Boolean(!process.env.NODE_ENV || process.env.NODE_ENV !== 'production'), | ||
...rest | ||
} = config | ||
const { outputs, shouldGenerateArtifacts = defaultShouldGenerateArtifacts(), ...rest } = config | ||
|
||
const defaultSDLFilePath = path.join(process.cwd(), 'schema.graphql') | ||
function getOutputPaths() { | ||
const defaultSDLFilePath = nodeImports().path.join(process.cwd(), 'schema.graphql') | ||
|
||
let typegenFilePath: ConfiguredTypegen | null = null | ||
let sdlFilePath: string | null = null | ||
let typegenFilePath: ConfiguredTypegen | null = null | ||
let sdlFilePath: string | null = null | ||
|
||
if (outputs === undefined) { | ||
if (isProductionStage()) { | ||
sdlFilePath = defaultSDLFilePath | ||
} | ||
} else if (outputs === true) { | ||
sdlFilePath = defaultSDLFilePath | ||
} else if (typeof outputs === 'object') { | ||
if (outputs.schema === true) { | ||
if (outputs === undefined) { | ||
if (isProductionStage()) { | ||
sdlFilePath = defaultSDLFilePath | ||
} | ||
} else if (outputs === true) { | ||
sdlFilePath = defaultSDLFilePath | ||
} else if (typeof outputs.schema === 'string') { | ||
sdlFilePath = assertAbsolutePath(outputs.schema, 'outputs.schema') | ||
} else if (outputs.schema === undefined && isProductionStage()) { | ||
} | ||
// handle typegen configuration | ||
if (typeof outputs.typegen === 'string') { | ||
typegenFilePath = { | ||
outputPath: assertAbsolutePath(outputs.typegen, 'outputs.typegen'), | ||
} else if (typeof outputs === 'object') { | ||
if (outputs.schema === true) { | ||
sdlFilePath = defaultSDLFilePath | ||
} else if (typeof outputs.schema === 'string') { | ||
sdlFilePath = assertAbsolutePath(outputs.schema, 'outputs.schema') | ||
} else if (outputs.schema === undefined && isProductionStage()) { | ||
} | ||
} else if (typeof outputs.typegen === 'object') { | ||
typegenFilePath = { | ||
...outputs.typegen, | ||
outputPath: assertAbsolutePath(outputs.typegen.outputPath, 'outputs.typegen.outputPath'), | ||
} as ConfiguredTypegen | ||
if (outputs.typegen.globalsPath) { | ||
typegenFilePath.globalsPath = assertAbsolutePath( | ||
outputs.typegen.globalsPath, | ||
'outputs.typegen.globalsPath' | ||
) | ||
// handle typegen configuration | ||
if (typeof outputs.typegen === 'string') { | ||
typegenFilePath = { | ||
outputPath: assertAbsolutePath(outputs.typegen, 'outputs.typegen'), | ||
} | ||
} else if (typeof outputs.typegen === 'object') { | ||
typegenFilePath = { | ||
...outputs.typegen, | ||
outputPath: assertAbsolutePath(outputs.typegen.outputPath, 'outputs.typegen.outputPath'), | ||
} as ConfiguredTypegen | ||
if (outputs.typegen.globalsPath) { | ||
typegenFilePath.globalsPath = assertAbsolutePath( | ||
outputs.typegen.globalsPath, | ||
'outputs.typegen.globalsPath' | ||
) | ||
} | ||
} | ||
} else if (outputs !== false) { | ||
console.warn( | ||
`You should specify a configuration value for outputs in Nexus' makeSchema. ` + | ||
`Provide one to remove this warning.` | ||
) | ||
} | ||
return { | ||
typegenFilePath, | ||
sdlFilePath, | ||
} | ||
} else if (outputs !== false) { | ||
console.warn( | ||
`You should specify a configuration value for outputs in Nexus' makeSchema. ` + | ||
`Provide one to remove this warning.` | ||
) | ||
} | ||
|
||
return { | ||
...rest, | ||
nexusSchemaImportId: getOwnPackage().name, | ||
outputs: { | ||
typegen: shouldGenerateArtifacts ? typegenFilePath : null, | ||
schema: shouldGenerateArtifacts ? sdlFilePath : null, | ||
typegen: shouldGenerateArtifacts ? getOutputPaths().typegenFilePath : null, | ||
schema: shouldGenerateArtifacts ? getOutputPaths().sdlFilePath : null, | ||
}, | ||
} | ||
} | ||
|
||
function defaultShouldGenerateArtifacts() { | ||
return Boolean( | ||
typeof process === 'object' && | ||
typeof process.cwd === 'function' && | ||
(!process.env.NODE_ENV || process.env.NODE_ENV !== 'production') | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { GraphQLSchema } from 'graphql' | ||
import { makeSchema, queryType } from '../../dist' | ||
|
||
const schema = makeSchema({ | ||
types: [ | ||
queryType({ | ||
definition(t) { | ||
t.boolean('ok') | ||
}, | ||
}), | ||
], | ||
}) | ||
|
||
if (!(schema instanceof GraphQLSchema)) { | ||
throw new Error('Not a schema') | ||
} |
Oops, something went wrong.