Skip to content

Commit 5256e31

Browse files
sduduzogtobiasdiez
andauthored
fix!: resolve path resolve issues (on windows) and types for virtual modules (#68)
* fix: multimatch returns an array, so we check for length instead * chore: use variables to keep template names * fix: ensure the glob pattern is correct for project * fix: set types as ambient * chore: only add declaration types to nuxt.d.ts * fix: bug from incomplete name * fix: tsconfig path issues on windows, ultimately any other machine * fix: use correct aliases * chore: resolve conflicts and pr commentary * chore: one more debug log * chore: fix lint issues * fix: add defu as a dependency * minor cleanup * fix linter --------- Co-authored-by: Tobias Diez <[email protected]>
1 parent 4356e16 commit 5256e31

File tree

6 files changed

+28
-34
lines changed

6 files changed

+28
-34
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"Codecov",
44
"codegen",
55
"Corepack",
6+
"defu",
67
"multimatch",
78
"nuxi",
89
"nuxt",

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"@graphql-tools/graphql-file-loader": "^8.0.0",
3939
"@graphql-tools/load": "^8.0.0",
4040
"@nuxt/kit": "^3.6.2",
41+
"defu": "^6.1.2",
4142
"multimatch": "^6.0.0"
4243
},
4344
"peerDependencies": {

playground/app.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
<pre>{{ schema }}</pre>
55
</template>
66

7-
<script setup>
8-
import { schema } from '#graphql/schema'
7+
<script lang="ts" setup>
8+
const schema = await $fetch('/api/GetSchema')
99
</script>

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/graphql-server.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
export const schema: string
1+
declare module '#graphql/schema' {
2+
const schema: string
3+
}

src/module.ts

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ import {
55
updateTemplates,
66
useLogger,
77
} from '@nuxt/kit'
8-
import { relative } from 'path'
8+
import { defu } from 'defu'
99
import { CodeGenConfig, createResolverTypeDefs } from './codegen'
1010
import { createSchemaImport } from './schema-loader'
1111
import multimatch from 'multimatch'
12-
import { resolve as resolvePath } from 'path'
1312
import { Nuxt } from '@nuxt/schema'
14-
import { pathToFileURL } from 'url'
1513

1614
export interface ModuleOptions {
1715
schema: string | string[]
@@ -24,24 +22,18 @@ const logger = useLogger('graphql/server')
2422
// logger.level = 5
2523

2624
function setAlias(nuxt: Nuxt, alias: string, path: string) {
27-
// workaround for https://github.com/nuxt/nuxt/issues/19453
28-
if (process.env.NODE_ENV === 'development') {
29-
// rollup needs a file URL
30-
nuxt.options.alias[alias] = pathToFileURL(
31-
resolvePath(nuxt.options.buildDir, path),
32-
).href
33-
34-
// vite needs a path
35-
nuxt.hooks.hook('vite:extendConfig', (config) => {
36-
config.resolve = config.resolve || {}
37-
config.resolve.alias = {
38-
...config.resolve.alias,
39-
[alias]: path,
40-
}
41-
})
42-
} else {
25+
nuxt.hook('nitro:config', (nitroConfig) => {
26+
// Workaround for https://github.com/nuxt/nuxt/issues/19453
27+
nitroConfig.externals = defu(
28+
typeof nitroConfig.externals === 'object' ? nitroConfig.externals : {},
29+
{
30+
inline: [path],
31+
},
32+
)
33+
nitroConfig.alias = nitroConfig.alias || {}
34+
nitroConfig.alias[alias] = path
4335
nuxt.options.alias[alias] = path
44-
}
36+
})
4537
}
4638

4739
export default defineNuxtModule<ModuleOptions>({
@@ -71,8 +63,8 @@ export default defineNuxtModule<ModuleOptions>({
7163
},
7264
write: true,
7365
})
74-
logger.debug(`GraphQL schema registered at ${schemaPath}`)
7566
setAlias(nuxt, '#graphql/schema', schemaPath)
67+
logger.debug(`GraphQL schema registered at ${schemaPath}`)
7668

7769
// Create types in build dir
7870
const { dst: typeDefPath } = addTemplate({
@@ -91,18 +83,12 @@ export default defineNuxtModule<ModuleOptions>({
9183
)
9284
},
9385
})
86+
setAlias(nuxt, '#graphql/resolver', resolverTypeDefPath)
9487

9588
// Add types to `nuxt.d.ts`
96-
nuxt.hook('prepare:types', ({ tsConfig }) => {
97-
tsConfig.compilerOptions = tsConfig.compilerOptions || { paths: [] }
98-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access --- seems to be a eslint bug
99-
tsConfig.compilerOptions.paths['#graphql/schema'] = [
100-
relative(nuxt.options.rootDir, typeDefPath),
101-
]
102-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access --- seems to be a eslint bug
103-
tsConfig.compilerOptions.paths['#graphql/resolver'] = [
104-
relative(nuxt.options.rootDir, resolverTypeDefPath),
105-
]
89+
nuxt.hook('prepare:types', ({ references }) => {
90+
// no need to add the resolver types here, since they are exported from the corresponding alias
91+
references.push({ path: typeDefPath })
10692
})
10793

10894
// HMR support for schema files
@@ -117,6 +103,7 @@ export default defineNuxtModule<ModuleOptions>({
117103
resolve(nuxt.options.srcDir, pattern),
118104
)
119105
: resolve(nuxt.options.srcDir, options.schema)
106+
logger.debug('Checking if the file changed matches ', schema)
120107
if (multimatch(absolutePath, schema).length > 0) {
121108
logger.debug('Schema changed', absolutePath)
122109

0 commit comments

Comments
 (0)