Skip to content

Commit 4c702fa

Browse files
committed
fix: ensure runtime does not get bundled when mocking
1 parent 28d9d70 commit 4c702fa

File tree

7 files changed

+35
-66
lines changed

7 files changed

+35
-66
lines changed

packages/nuxt/src/module.ts

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
defineNuxtModule,
77
} from '@nuxt/kit'
88
import type { MetaInput } from 'schema-org-graph-js'
9-
import { RootSchemas, schemaOrgComponents } from '@vueuse/schema-org'
9+
import { AliasProvider, AliasRuntime, schemaOrgAutoImports, schemaOrgComponents } from '@vueuse/schema-org'
1010
import type { NuxtModule } from '@nuxt/schema'
1111
import { dirname } from 'pathe'
1212
import { SchemaOrg as SchemaOrgVitePlugin } from '@vueuse/schema-org-vite'
@@ -26,7 +26,9 @@ export interface ModuleOptions {
2626
* @default false
2727
*/
2828
full?: boolean
29-
29+
/**
30+
* Metadata for the schema.org generation
31+
*/
3032
meta?: MetaInput
3133
}
3234

@@ -35,7 +37,6 @@ export interface ModuleHooks {
3537
}
3638

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

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

5354
const moduleRuntimeDir = resolve('./runtime')
54-
nuxt.options.build.transpile.push(...[moduleRuntimeDir, RuntimeDir])
55+
nuxt.options.build.transpile.push(...[moduleRuntimeDir, AliasRuntime])
5556

57+
// enable client in dev mode
5658
if (typeof moduleOptions.client === 'undefined')
5759
moduleOptions.client = !!nuxt.options.dev
5860

59-
const nuxtSchemaComposablesRuntime = `${moduleRuntimeDir}/composables`
60-
6161
const providerPath = await resolvePath(`${schemaOrgPath}/providers/${moduleOptions.full ? 'full' : 'simple'}`)
62-
// might need this again
62+
// // set the alias for the types
63+
nuxt.options.alias[AliasProvider] = providerPath
64+
nuxt.options.alias[AliasRuntime] = '@vueuse/schema-org/runtime'
65+
// might need this
6366
nuxt.options.alias[Pkg] = schemaOrgPath
64-
// set the alias for the types
65-
nuxt.options.alias['#vueuse/schema-org/provider'] = providerPath
66-
nuxt.options.alias['#vueuse/schema-org/runtime'] = nuxtSchemaComposablesRuntime
6767

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

82-
const componentPath = await resolvePath(`${schemaOrgPath}/runtime/components`)
8382
for (const component of schemaOrgComponents) {
8483
await addComponent({
8584
name: component,
8685
export: component,
87-
chunkName: 'schema-org-components',
88-
filePath: componentPath,
86+
chunkName: 'nuxt-schema-org/components',
87+
filePath: AliasRuntime,
8988
})
9089
}
9190

9291
nuxt.hooks.hook('autoImports:sources', (autoImports) => {
93-
autoImports.unshift({
94-
from: nuxtSchemaComposablesRuntime,
95-
imports: [
96-
'injectSchemaOrg',
97-
'useSchemaOrg',
98-
],
99-
})
100-
101-
autoImports.unshift({
102-
from: '#vueuse/schema-org/provider',
103-
imports: [
104-
...RootSchemas
105-
.map(schema => [`define${schema}`])
106-
.flat(),
107-
],
108-
})
92+
autoImports.unshift(...schemaOrgAutoImports)
10993
})
11094

11195
nuxt.hooks.hook('vite:extendConfig', (config, { isClient }) => {
@@ -120,7 +104,6 @@ export default defineNuxtModule<ModuleOptions>({
120104
aliasPaths: {
121105
provider: providerPath,
122106
pkgDir: schemaOrgPath,
123-
runtime: nuxtSchemaComposablesRuntime,
124107
},
125108
}))
126109
})

packages/nuxt/src/runtime/composables.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

packages/nuxt/src/runtime/plugin.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ export default defineNuxtPlugin((nuxtApp) => {
3131
},
3232
})
3333

34-
nuxtApp._injectSchemaOrg = () => client
3534
nuxtApp.vueApp.use(client)
3635

3736
if (ssr) {

packages/schema-org/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "@vueuse/schema-org",
3+
"type": "module",
34
"version": "1.0.0-beta.8",
45
"description": "Vue Schema.org for Simple and Automated Google Rich Results for everyone",
56
"author": "Harlan Wilton <[email protected]>",

packages/schema-org/src/meta.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
export const AliasRuntime = '#vueuse/schema-org/runtime'
2+
export const AliasProvider = '#vueuse/schema-org/provider'
3+
14
export const RootSchemas = [
25
'Article',
36
'Breadcrumb',
@@ -19,14 +22,14 @@ export const RootSchemas = [
1922

2023
export const schemaOrgAutoImports = [
2124
{
22-
from: '#vueuse/schema-org/runtime',
25+
from: AliasRuntime,
2326
imports: [
2427
'useSchemaOrg',
2528
'injectSchemaOrg',
2629
],
2730
},
2831
{
29-
from: '#vueuse/schema-org/provider',
32+
from: AliasProvider,
3033
imports: RootSchemas
3134
.map(schema => [`define${schema}`])
3235
.flat(),

packages/vite/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RootSchemas, schemaOrgComponents } from '@vueuse/schema-org'
1+
import { AliasProvider, AliasRuntime, RootSchemas, schemaOrgComponents } from '@vueuse/schema-org'
22
import type { SchemaOrgResolverFn } from './types'
33

44
export interface MetaInput {
@@ -49,7 +49,7 @@ export function SchemaOrgResolver(options: SchemaOrgResolverOptions = {}): Schem
4949
if (schemaOrgComponents.includes(componentName)) {
5050
return {
5151
name: componentName,
52-
from: '#vueuse/schema-org/runtime',
52+
from: AliasRuntime,
5353
}
5454
}
5555
}
@@ -58,11 +58,11 @@ export function SchemaOrgResolver(options: SchemaOrgResolverOptions = {}): Schem
5858
}
5959

6060
export const schemaOrgAutoImports = {
61-
'#vueuse/schema-org/runtime': [
61+
[AliasRuntime]: [
6262
'useSchemaOrg',
6363
'injectSchemaOrg',
6464
],
65-
'#vueuse/schema-org/provider': RootSchemas
65+
[AliasProvider]: RootSchemas
6666
.map(schema => [`define${schema}`])
6767
.flat(),
6868
}

packages/vite/src/plugins.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { createUnplugin } from 'unplugin'
22
import { resolvePath } from 'mlly'
3-
import { dirname, join } from 'pathe'
3+
import { dirname } from 'pathe'
44
import MagicString from 'magic-string'
5+
import { AliasProvider, AliasRuntime } from '@vueuse/schema-org'
56

67
export interface PluginOptions {
78
/**
@@ -21,10 +22,6 @@ export interface PluginOptions {
2122
runtime?: string
2223
mockPath?: string
2324
}
24-
/**
25-
* Scan files from this root directory (ignoring node_modules).
26-
*/
27-
transformPaths?: string[]
2825
}
2926

3027
const SchemaOrgPkg = '@vueuse/schema-org'
@@ -67,23 +64,19 @@ export const schemaOrgSwapAliases = () => createUnplugin<PluginOptions>((args) =
6764
await fetchPaths()
6865
},
6966
transformInclude(id) {
70-
if (id.startsWith(join(paths.pkgDir, 'runtime')))
71-
return true
72-
for (const p of args?.transformPaths || []) {
73-
if (id.startsWith(p))
74-
return true
75-
}
76-
return false
67+
return id.startsWith(paths.pkgDir)
7768
},
7869
transform(code) {
7970
// swap out aliases for real paths
8071
const s = new MagicString(code)
81-
s.replace('#vueuse/schema-org/provider', paths.provider)
82-
s.replace('#vueuse/schema-org/runtime', paths.runtime)
72+
s.replace(AliasProvider, paths.provider)
73+
s.replace(AliasRuntime, paths.runtime)
8374

84-
return {
85-
code: s.toString(),
86-
map: s.generateMap(),
75+
if (s.hasChanged()) {
76+
return {
77+
code: s.toString(),
78+
map: s.generateMap(),
79+
}
8780
}
8881
},
8982
vite: {
@@ -99,9 +92,9 @@ export const schemaOrgSwapAliases = () => createUnplugin<PluginOptions>((args) =
9992
// @ts-expect-error untyped
10093
config.resolve.alias[SchemaOrgPkg] = pkgDir
10194
// @ts-expect-error untyped
102-
config.resolve.alias['#vueuse/schema-org/provider'] = provider
95+
config.resolve.alias[AliasProvider] = provider
10396
// @ts-expect-error untyped
104-
config.resolve.alias['#vueuse/schema-org/runtime'] = runtime
97+
config.resolve.alias[AliasRuntime] = runtime
10598
return config
10699
},
107100
},

0 commit comments

Comments
 (0)