Skip to content

Commit

Permalink
fix(nuxt): use simpler paths
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Aug 16, 2022
1 parent 44863e4 commit 2caf85e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
62 changes: 33 additions & 29 deletions packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,64 +51,68 @@ export default defineNuxtModule<ModuleOptions>({
// avoid unwanted behavior with different package managers
const schemaOrgPath = dirname(await resolvePath(Pkg))

const moduleRuntime = resolve('./runtime')
nuxt.options.build.transpile.push(...[moduleRuntime, RuntimeDir])
const moduleRuntimeDir = resolve('./runtime')
nuxt.options.build.transpile.push(...[moduleRuntimeDir, RuntimeDir])

if (typeof moduleOptions.client === 'undefined')
moduleOptions.client = !!nuxt.options.dev

// fallback clears schema on route change
if (!moduleOptions.client)
addPlugin(resolve(moduleRuntime, 'plugin-fallback.client'))
addPlugin(resolve(moduleRuntimeDir, 'plugin-fallback.client'))

addPlugin({
src: resolve(moduleRuntime, 'plugin'),
src: resolve(moduleRuntimeDir, 'plugin'),
mode: moduleOptions.client ? 'all' : 'server',
})

// might need this again
nuxt.options.alias[Pkg] = schemaOrgPath
// set the alias for the types
nuxt.options.alias['#vueuse/schema-org/provider'] = await resolvePath(`${schemaOrgPath}/providers/full`)
nuxt.options.alias['#vueuse/schema-org/runtime'] = await resolvePath(`${schemaOrgPath}/runtime`)

nuxt.hook('vite:extendConfig', (config, { isClient }) => {
config.optimizeDeps = config.optimizeDeps || {}
config.optimizeDeps.exclude = config.optimizeDeps.exclude || []
config.optimizeDeps.exclude.push(...[`${schemaOrgPath}/runtime`, Pkg])

config.plugins = config.plugins || []
config.plugins.push(SchemaOrgVitePlugin({
mock: !moduleOptions.client && isClient,
full: moduleOptions.full,
}))
})
const nuxtSchemaComposablesRuntime = `${moduleRuntimeDir}/composables`

addTemplate({
filename: 'nuxt-schema-org-config.mjs',
getContents: () => `export default ${JSON.stringify(moduleOptions)}`,
})

const componentPath = await resolvePath('@vueuse/schema-org/components')
for (const component of schemaOrgComponents) {
await addComponent({
name: component,
export: component,
chunkName: 'schema-org-components',
filePath: componentPath,
})
}

nuxt.hooks.hook('autoImports:sources', (autoImports) => {
autoImports.unshift({
from: `${moduleRuntime}/schema-org-runtime`,
from: nuxtSchemaComposablesRuntime,
imports: [
'injectSchemaOrg',
'useSchemaOrg',
],
})

autoImports.unshift({
from: '#vueuse/schema-org/provider',
imports: [
...RootSchemas
.map(schema => [`define${schema}`])
.flat(),
],
})
})

schemaOrgComponents.forEach((component) => {
addComponent({
name: component,
export: component,
chunkName: 'schema-org-components',
filePath: '#vueuse/schema-org/runtime',
})
nuxt.hooks.hook('vite:extendConfig', (config, { isClient }) => {
config.optimizeDeps = config.optimizeDeps || {}
config.optimizeDeps.exclude = config.optimizeDeps.exclude || []
config.optimizeDeps.exclude.push(...[`${schemaOrgPath}/runtime`, Pkg])

config.plugins = config.plugins || []
config.plugins.push(SchemaOrgVitePlugin({
mock: !moduleOptions.client && isClient,
full: moduleOptions.full,
runtimePath: nuxtSchemaComposablesRuntime,
}))
})
},
}) as NuxtModule<ModuleOptions>
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useNuxtApp } from '#app'

export * from '#vueuse/schema-org/provider'
export { useSchemaOrg } from '#vueuse/schema-org/runtime'
export { useSchemaOrg } from '@vueuse/schema-org/runtime'

export function injectSchemaOrg() {
const nuxtApp = useNuxtApp()
Expand Down

0 comments on commit 2caf85e

Please sign in to comment.