Skip to content

Commit 1536501

Browse files
committed
fix: try and resolve ERR_PACKAGE_IMPORT_NOT_DEFINED
1 parent 95f76ac commit 1536501

File tree

5 files changed

+98
-98
lines changed

5 files changed

+98
-98
lines changed

packages/nuxt/src/module.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ export default defineNuxtModule<ModuleOptions>({
7373
nuxt.options.alias['#vueuse/schema-org/runtime'] = await resolvePath(`${schemaOrgPath}/runtime`)
7474

7575
nuxt.hook('vite:extendConfig', (config, { isClient }) => {
76+
config.optimizeDeps = config.optimizeDeps || {}
77+
config.optimizeDeps.exclude = config.optimizeDeps.exclude || []
78+
config.optimizeDeps.exclude.push(...[`${schemaOrgPath}/runtime`, Pkg])
79+
7680
config.plugins = config.plugins || []
7781
config.plugins.push(SchemaOrgVitePlugin({
7882
mock: !moduleOptions.client && isClient,
@@ -87,23 +91,15 @@ export default defineNuxtModule<ModuleOptions>({
8791

8892
nuxt.hooks.hook('autoImports:sources', (autoImports) => {
8993
autoImports.unshift({
90-
from: `${moduleRuntime}/composables`,
94+
from: `${moduleRuntime}/schema-org-runtime`,
9195
imports: [
9296
'injectSchemaOrg',
93-
],
94-
})
95-
autoImports.unshift({
96-
from: '#vueuse/schema-org/runtime',
97-
imports: [
9897
'useSchemaOrg',
98+
...RootSchemas
99+
.map(schema => [`define${schema}`])
100+
.flat(),
99101
],
100102
})
101-
autoImports.unshift({
102-
from: '#vueuse/schema-org/provider',
103-
imports: RootSchemas
104-
.map(schema => [`define${schema}`])
105-
.flat(),
106-
})
107103
})
108104

109105
schemaOrgComponents.forEach((component) => {

packages/schema-org/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454
"import": "./dist/runtime-mock/index.mjs"
5555
}
5656
},
57+
"imports": {
58+
"#vueuse/schema-org/provider": "./dist/providers/base.mjs",
59+
"#vueuse/schema-org/runtime": "./dist/runtime/index.mjs"
60+
},
5761
"main": "dist/index.cjs",
5862
"module": "dist/index.mjs",
5963
"types": "dist/index.d.ts",

packages/schema-org/runtime/components/defineSchemaOrgComponent.ts

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

packages/schema-org/runtime/components/nodes.ts

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,91 @@
1-
import { defineSchemaOrgComponent } from './defineSchemaOrgComponent'
1+
import { computed, defineComponent, h, ref, unref } from 'vue'
22
// @ts-expect-error untyped
33
import { defineArticle, defineBreadcrumb, defineComment, defineHowTo, defineImage, defineLocalBusiness, defineOrganization, definePerson, defineProduct, defineQuestion, defineRecipe, defineReview, defineVideo, defineWebPage, defineWebSite } from '#vueuse/schema-org/provider'
4+
// @ts-expect-error untyped
5+
import { useSchemaOrg } from '#vueuse/schema-org/runtime'
6+
7+
const shallowVNodesToText = (nodes: any) => {
8+
let text = ''
9+
for (const node of nodes) {
10+
if (typeof node.children === 'string')
11+
text += node.children.trim()
12+
}
13+
return text
14+
}
15+
16+
const fixKey = (s: string) => {
17+
// kebab case to camel case
18+
let key = s.replace(/-./g, x => x[1].toUpperCase())
19+
// supports @type & @id
20+
if (key === 'type' || key === 'id')
21+
key = `@${key}`
22+
return key
23+
}
24+
25+
const ignoreKey = (s: string) => {
26+
// pretty hacky, need to setup all props
27+
if (s.startsWith('aria-') || s.startsWith('data-'))
28+
return false
29+
30+
return ['class', 'style'].includes(s)
31+
}
32+
33+
export const defineSchemaOrgComponent = (name: string, defineFn: (input: any) => any) => {
34+
return defineComponent({
35+
name,
36+
props: {
37+
as: String,
38+
renderScopedSlots: Boolean,
39+
},
40+
setup(props, { slots, attrs }) {
41+
const node = ref(null)
42+
43+
const nodePartial = computed(() => {
44+
const val: Record<string, any> = {}
45+
Object.entries(unref(attrs)).forEach(([key, value]) => {
46+
if (!ignoreKey(key)) {
47+
// keys may be passed with kebab case, and they aren't transformed
48+
val[fixKey(key)] = unref(value)
49+
}
50+
})
51+
// only render vnodes while we don't have a node
52+
if (!node.value) {
53+
// iterate through slots
54+
for (const [key, slot] of Object.entries(slots)) {
55+
if (!slot || key === 'default')
56+
continue
57+
// allow users to provide data via slots that aren't rendered
58+
val[fixKey(key)] = shallowVNodesToText(slot(props))
59+
}
60+
}
61+
return val
62+
})
63+
64+
// may not be available
65+
if (defineFn) {
66+
// register via main schema composable for route watching
67+
useSchemaOrg([defineFn(unref(nodePartial))])
68+
}
69+
70+
return () => {
71+
const data = unref(nodePartial)
72+
// renderless component
73+
if (!slots.default && !props.renderScopedSlots)
74+
return null
75+
const childSlots = []
76+
if (slots.default)
77+
childSlots.push(slots.default(data))
78+
if (props.renderScopedSlots) {
79+
for (const [key, slot] of Object.entries(slots)) {
80+
if (slot && key !== 'default')
81+
childSlots.push(slot(data))
82+
}
83+
}
84+
return h(props.as || 'div', {}, childSlots)
85+
}
86+
},
87+
})
88+
}
489

590
export const SchemaOrgArticle = defineSchemaOrgComponent('SchemaOrgArticle', defineArticle)
691
export const SchemaOrgBreadcrumb = defineSchemaOrgComponent('SchemaOrgBreadcrumb', defineBreadcrumb)

0 commit comments

Comments
 (0)