Skip to content

Commit

Permalink
fix: ensure runtime does not get bundled when mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Aug 16, 2022
1 parent 28d9d70 commit 4c702fa
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 66 deletions.
43 changes: 13 additions & 30 deletions packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
defineNuxtModule,
} from '@nuxt/kit'
import type { MetaInput } from 'schema-org-graph-js'
import { RootSchemas, schemaOrgComponents } from '@vueuse/schema-org'
import { AliasProvider, AliasRuntime, schemaOrgAutoImports, schemaOrgComponents } from '@vueuse/schema-org'
import type { NuxtModule } from '@nuxt/schema'
import { dirname } from 'pathe'
import { SchemaOrg as SchemaOrgVitePlugin } from '@vueuse/schema-org-vite'
Expand All @@ -26,7 +26,9 @@ export interface ModuleOptions {
* @default false
*/
full?: boolean

/**
* Metadata for the schema.org generation
*/
meta?: MetaInput
}

Expand All @@ -35,7 +37,6 @@ export interface ModuleHooks {
}

const Pkg = '@vueuse/schema-org'
const RuntimeDir = '#vueuse/schema-org/runtime'

export default defineNuxtModule<ModuleOptions>({
meta: {
Expand All @@ -51,19 +52,18 @@ export default defineNuxtModule<ModuleOptions>({
const schemaOrgPath = dirname(await resolvePath(Pkg))

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

// enable client in dev mode
if (typeof moduleOptions.client === 'undefined')
moduleOptions.client = !!nuxt.options.dev

const nuxtSchemaComposablesRuntime = `${moduleRuntimeDir}/composables`

const providerPath = await resolvePath(`${schemaOrgPath}/providers/${moduleOptions.full ? 'full' : 'simple'}`)
// might need this again
// // set the alias for the types
nuxt.options.alias[AliasProvider] = providerPath
nuxt.options.alias[AliasRuntime] = '@vueuse/schema-org/runtime'
// might need this
nuxt.options.alias[Pkg] = schemaOrgPath
// set the alias for the types
nuxt.options.alias['#vueuse/schema-org/provider'] = providerPath
nuxt.options.alias['#vueuse/schema-org/runtime'] = nuxtSchemaComposablesRuntime

// fallback clears schema on route change
if (!moduleOptions.client)
Expand All @@ -79,33 +79,17 @@ export default defineNuxtModule<ModuleOptions>({
getContents: () => `export default ${JSON.stringify(moduleOptions)}`,
})

const componentPath = await resolvePath(`${schemaOrgPath}/runtime/components`)
for (const component of schemaOrgComponents) {
await addComponent({
name: component,
export: component,
chunkName: 'schema-org-components',
filePath: componentPath,
chunkName: 'nuxt-schema-org/components',
filePath: AliasRuntime,
})
}

nuxt.hooks.hook('autoImports:sources', (autoImports) => {
autoImports.unshift({
from: nuxtSchemaComposablesRuntime,
imports: [
'injectSchemaOrg',
'useSchemaOrg',
],
})

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

nuxt.hooks.hook('vite:extendConfig', (config, { isClient }) => {
Expand All @@ -120,7 +104,6 @@ export default defineNuxtModule<ModuleOptions>({
aliasPaths: {
provider: providerPath,
pkgDir: schemaOrgPath,
runtime: nuxtSchemaComposablesRuntime,
},
}))
})
Expand Down
10 changes: 0 additions & 10 deletions packages/nuxt/src/runtime/composables.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/nuxt/src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export default defineNuxtPlugin((nuxtApp) => {
},
})

nuxtApp._injectSchemaOrg = () => client
nuxtApp.vueApp.use(client)

if (ssr) {
Expand Down
1 change: 1 addition & 0 deletions packages/schema-org/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "@vueuse/schema-org",
"type": "module",
"version": "1.0.0-beta.8",
"description": "Vue Schema.org for Simple and Automated Google Rich Results for everyone",
"author": "Harlan Wilton <harlan@harlanzw.com>",
Expand Down
7 changes: 5 additions & 2 deletions packages/schema-org/src/meta.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export const AliasRuntime = '#vueuse/schema-org/runtime'
export const AliasProvider = '#vueuse/schema-org/provider'

export const RootSchemas = [
'Article',
'Breadcrumb',
Expand All @@ -19,14 +22,14 @@ export const RootSchemas = [

export const schemaOrgAutoImports = [
{
from: '#vueuse/schema-org/runtime',
from: AliasRuntime,
imports: [
'useSchemaOrg',
'injectSchemaOrg',
],
},
{
from: '#vueuse/schema-org/provider',
from: AliasProvider,
imports: RootSchemas
.map(schema => [`define${schema}`])
.flat(),
Expand Down
8 changes: 4 additions & 4 deletions packages/vite/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RootSchemas, schemaOrgComponents } from '@vueuse/schema-org'
import { AliasProvider, AliasRuntime, RootSchemas, schemaOrgComponents } from '@vueuse/schema-org'
import type { SchemaOrgResolverFn } from './types'

export interface MetaInput {
Expand Down Expand Up @@ -49,7 +49,7 @@ export function SchemaOrgResolver(options: SchemaOrgResolverOptions = {}): Schem
if (schemaOrgComponents.includes(componentName)) {
return {
name: componentName,
from: '#vueuse/schema-org/runtime',
from: AliasRuntime,
}
}
}
Expand All @@ -58,11 +58,11 @@ export function SchemaOrgResolver(options: SchemaOrgResolverOptions = {}): Schem
}

export const schemaOrgAutoImports = {
'#vueuse/schema-org/runtime': [
[AliasRuntime]: [
'useSchemaOrg',
'injectSchemaOrg',
],
'#vueuse/schema-org/provider': RootSchemas
[AliasProvider]: RootSchemas
.map(schema => [`define${schema}`])
.flat(),
}
Expand Down
31 changes: 12 additions & 19 deletions packages/vite/src/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { createUnplugin } from 'unplugin'
import { resolvePath } from 'mlly'
import { dirname, join } from 'pathe'
import { dirname } from 'pathe'
import MagicString from 'magic-string'
import { AliasProvider, AliasRuntime } from '@vueuse/schema-org'

export interface PluginOptions {
/**
Expand All @@ -21,10 +22,6 @@ export interface PluginOptions {
runtime?: string
mockPath?: string
}
/**
* Scan files from this root directory (ignoring node_modules).
*/
transformPaths?: string[]
}

const SchemaOrgPkg = '@vueuse/schema-org'
Expand Down Expand Up @@ -67,23 +64,19 @@ export const schemaOrgSwapAliases = () => createUnplugin<PluginOptions>((args) =
await fetchPaths()
},
transformInclude(id) {
if (id.startsWith(join(paths.pkgDir, 'runtime')))
return true
for (const p of args?.transformPaths || []) {
if (id.startsWith(p))
return true
}
return false
return id.startsWith(paths.pkgDir)
},
transform(code) {
// swap out aliases for real paths
const s = new MagicString(code)
s.replace('#vueuse/schema-org/provider', paths.provider)
s.replace('#vueuse/schema-org/runtime', paths.runtime)
s.replace(AliasProvider, paths.provider)
s.replace(AliasRuntime, paths.runtime)

return {
code: s.toString(),
map: s.generateMap(),
if (s.hasChanged()) {
return {
code: s.toString(),
map: s.generateMap(),
}
}
},
vite: {
Expand All @@ -99,9 +92,9 @@ export const schemaOrgSwapAliases = () => createUnplugin<PluginOptions>((args) =
// @ts-expect-error untyped
config.resolve.alias[SchemaOrgPkg] = pkgDir
// @ts-expect-error untyped
config.resolve.alias['#vueuse/schema-org/provider'] = provider
config.resolve.alias[AliasProvider] = provider
// @ts-expect-error untyped
config.resolve.alias['#vueuse/schema-org/runtime'] = runtime
config.resolve.alias[AliasRuntime] = runtime
return config
},
},
Expand Down

0 comments on commit 4c702fa

Please sign in to comment.