nuxt-schema-org 0.7.0-beta.3 → 1.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/module.d.ts CHANGED
@@ -1,23 +1,21 @@
1
- import { SchemaOrgOptions } from '@vueuse/schema-org';
1
+ import { MetaInput } from 'schema-org-graph-js';
2
2
  import { NuxtModule } from '@nuxt/schema';
3
3
 
4
- interface ModuleOptions extends SchemaOrgOptions {
4
+ interface ModuleOptions {
5
5
  /**
6
6
  * Should schema.org only be rendered by the server.
7
7
  *
8
8
  * Useful for optimising performance as it may not be needed by search engines. Changes runtime package size to 0kb.
9
+ *
10
+ * @default false
9
11
  */
10
- disableRuntimeScriptsWhenSSR: boolean;
11
- /**
12
- * Whether composables will be automatically imported for you.
13
- * @default true
14
- */
15
- autoImportComposables: boolean;
12
+ client?: boolean;
16
13
  /**
17
- * Whether components will be automatically imported for you.
18
- * @default true
14
+ * Should full schema types from `schema-dts` be used over a simplified version.
15
+ * @default false
19
16
  */
20
- autoImportComponents: boolean;
17
+ full?: boolean;
18
+ meta?: MetaInput;
21
19
  }
22
20
  interface ModuleHooks {
23
21
  }
package/dist/module.json CHANGED
@@ -4,5 +4,5 @@
4
4
  "bridge": false
5
5
  },
6
6
  "name": "nuxt-schema-org",
7
- "version": "0.7.0-beta.3"
7
+ "version": "1.0.0-beta.3"
8
8
  }
package/dist/module.mjs CHANGED
@@ -1,23 +1,10 @@
1
1
  import { defineNuxtModule, createResolver, addPlugin, addTemplate, addComponent } from '@nuxt/kit';
2
- import { schemaOrgAutoImports, schemaOrgComponents } from '@vueuse/schema-org';
3
- import { createUnplugin } from 'unplugin';
4
- import MagicString from 'magic-string';
2
+ import { schemaOrgComponents, RootSchemas } from '@vueuse/schema-org';
3
+ import { dirname } from 'pathe';
4
+ import { SchemaOrg } from '@vueuse/schema-org-vite';
5
5
 
6
- function normalizeWindowsPath(input = "") {
7
- if (!input.includes("\\")) {
8
- return input;
9
- }
10
- return input.replace(/\\/g, "/");
11
- }
12
- const _IS_ABSOLUTE_RE = /^\/|^\\|^[a-zA-Z]:[/\\]/;
13
- const isAbsolute = function(p) {
14
- return _IS_ABSOLUTE_RE.test(p);
15
- };
16
- const dirname = function(p) {
17
- return normalizeWindowsPath(p).replace(/\/$/, "").split("/").slice(0, -1).join("/") || (isAbsolute(p) ? "/" : ".");
18
- };
19
-
20
- const SchemaOrgPkg = "@vueuse/schema-org";
6
+ const Pkg = "@vueuse/schema-org";
7
+ const RuntimeDir = "#vueuse/schema-org/runtime";
21
8
  const module = defineNuxtModule({
22
9
  meta: {
23
10
  configKey: "schemaOrg",
@@ -25,88 +12,59 @@ const module = defineNuxtModule({
25
12
  bridge: false
26
13
  }
27
14
  },
28
- defaults: {
29
- disableRuntimeScriptsWhenSSR: false,
30
- autoImportComposables: true,
31
- autoImportComponents: true
32
- },
33
- async setup(config, nuxt) {
15
+ async setup(moduleOptions, nuxt) {
34
16
  const { resolve, resolvePath } = createResolver(import.meta.url);
35
- const runtimeDir = resolve("./runtime");
36
- nuxt.options.build.transpile.push(runtimeDir);
37
- const registerClientScripts = !config.disableRuntimeScriptsWhenSSR || nuxt.options.dev;
38
- if (registerClientScripts)
39
- addPlugin(resolve(runtimeDir, "plugin.client"));
40
- addPlugin(resolve(runtimeDir, "plugin.server"));
41
- const schemaOrgPath = dirname(await resolvePath(SchemaOrgPkg));
42
- nuxt.options.alias[SchemaOrgPkg] = schemaOrgPath;
43
- nuxt.options.build.transpile.push(...[schemaOrgPath, SchemaOrgPkg]);
44
- nuxt.hook("vite:extendConfig", (config2) => {
45
- config2.optimizeDeps = config2.optimizeDeps || {};
46
- config2.optimizeDeps.exclude = config2.optimizeDeps.exclude || [];
47
- config2.optimizeDeps.exclude.push(...[schemaOrgPath, SchemaOrgPkg]);
17
+ const schemaOrgPath = dirname(await resolvePath(Pkg));
18
+ const moduleRuntimeDir = resolve("./runtime");
19
+ nuxt.options.build.transpile.push(...[moduleRuntimeDir, RuntimeDir]);
20
+ if (typeof moduleOptions.client === "undefined")
21
+ moduleOptions.client = !!nuxt.options.dev;
22
+ if (!moduleOptions.client)
23
+ addPlugin(resolve(moduleRuntimeDir, "plugin-fallback.client"));
24
+ addPlugin({
25
+ src: resolve(moduleRuntimeDir, "plugin"),
26
+ mode: moduleOptions.client ? "all" : "server"
48
27
  });
28
+ const nuxtSchemaComposablesRuntime = `${moduleRuntimeDir}/composables`;
49
29
  addTemplate({
50
- filename: "schemaOrg.config.mjs",
51
- getContents: () => `export default ${JSON.stringify({ config })}`
30
+ filename: "nuxt-schema-org-config.mjs",
31
+ getContents: () => `export default ${JSON.stringify(moduleOptions)}`
52
32
  });
53
- if (config.autoImportComposables) {
54
- nuxt.hooks.hookOnce("autoImports:sources", (autoImports) => {
55
- autoImports.unshift({
56
- from: resolve("./runtime/composables"),
57
- imports: schemaOrgAutoImports[SchemaOrgPkg]
58
- });
59
- });
60
- }
61
- if (config.autoImportComponents) {
62
- schemaOrgComponents.forEach((component) => {
63
- addComponent({
64
- name: component,
65
- export: component,
66
- filePath: schemaOrgPath
67
- });
33
+ const componentPath = await resolvePath("@vueuse/schema-org/components");
34
+ for (const component of schemaOrgComponents) {
35
+ await addComponent({
36
+ name: component,
37
+ export: component,
38
+ chunkName: "schema-org-components",
39
+ filePath: componentPath
68
40
  });
69
41
  }
70
- if (!registerClientScripts) {
71
- const mockTemplate = addTemplate({
72
- filename: "schema-org-mock.mjs",
73
- getContents() {
74
- return schemaOrgAutoImports[SchemaOrgPkg].map((s) => `export function ${s}() {}`).join("\n");
75
- }
42
+ nuxt.hooks.hook("autoImports:sources", (autoImports) => {
43
+ autoImports.unshift({
44
+ from: nuxtSchemaComposablesRuntime,
45
+ imports: [
46
+ "injectSchemaOrg",
47
+ "useSchemaOrg"
48
+ ]
76
49
  });
77
- addComponent({
78
- name: "SchemaOrgMock",
79
- export: "SchemaOrgMock",
80
- filePath: schemaOrgPath
50
+ autoImports.unshift({
51
+ from: "#vueuse/schema-org/provider",
52
+ imports: [
53
+ ...RootSchemas.map((schema) => [`define${schema}`]).flat()
54
+ ]
81
55
  });
82
- nuxt.options.alias["#schema-org/mock"] = mockTemplate.dst;
83
- const mockerPlugin = createUnplugin(() => {
84
- return {
85
- name: "nuxt-schema-org:mocker",
86
- enforce: "post",
87
- transformInclude(id) {
88
- return id.endsWith(".vue") && id.includes(nuxt.options.srcDir);
89
- },
90
- transform(code, id) {
91
- const s = new MagicString(code);
92
- s.replace(resolve(runtimeDir, "composables"), "#schema-org/mock");
93
- s.replace(/_resolveComponent\("SchemaOrg(.*?)"\)/gm, '_resolveComponent("SchemaOrgMock")');
94
- if (s.hasChanged()) {
95
- return {
96
- code: s.toString(),
97
- map: s.generateMap({ includeContent: true, source: id })
98
- };
99
- }
100
- }
101
- };
102
- });
103
- nuxt.hook("vite:extendConfig", (config2, { isClient }) => {
104
- if (isClient) {
105
- config2.plugins = config2.plugins || [];
106
- config2.plugins.push(mockerPlugin.vite());
107
- }
108
- });
109
- }
56
+ });
57
+ nuxt.hooks.hook("vite:extendConfig", (config, { isClient }) => {
58
+ config.optimizeDeps = config.optimizeDeps || {};
59
+ config.optimizeDeps.exclude = config.optimizeDeps.exclude || [];
60
+ config.optimizeDeps.exclude.push(...[`${schemaOrgPath}/runtime`, Pkg]);
61
+ config.plugins = config.plugins || [];
62
+ config.plugins.push(SchemaOrg({
63
+ mock: !moduleOptions.client && isClient,
64
+ full: moduleOptions.full,
65
+ runtimePath: nuxtSchemaComposablesRuntime
66
+ }));
67
+ });
110
68
  }
111
69
  });
112
70
 
@@ -1,2 +1,2 @@
1
- export * from '@vueuse/schema-org';
2
- export declare function useSchemaOrg(input: any): void;
1
+ export { useSchemaOrg } from '@vueuse/schema-org/runtime';
2
+ export declare function injectSchemaOrg(): any;
@@ -1,8 +1,7 @@
1
- import { isFunction } from "@vue/shared";
2
- import { computed } from "#imports";
3
1
  import { useNuxtApp } from "#app";
4
- export * from "@vueuse/schema-org";
5
- export function useSchemaOrg(input) {
6
- const resolveInput = isFunction(input) ? computed(input) : input;
7
- useNuxtApp()._useSchemaOrg(resolveInput);
2
+ export { useSchemaOrg } from "@vueuse/schema-org/runtime";
3
+ export function injectSchemaOrg() {
4
+ const nuxtApp = useNuxtApp();
5
+ if (nuxtApp._injectSchemaOrg)
6
+ return nuxtApp._injectSchemaOrg();
8
7
  }
@@ -0,0 +1,7 @@
1
+ import { defineNuxtPlugin } from "#app";
2
+ import { watch } from "#imports";
3
+ export default defineNuxtPlugin((nuxtApp) => {
4
+ watch(() => nuxtApp._route.path, () => {
5
+ document.querySelector('script[data-id="schema-org-graph"]')?.remove();
6
+ });
7
+ });
File without changes
@@ -0,0 +1,36 @@
1
+ import { createSchemaOrg } from "@vueuse/schema-org";
2
+ import { defineNuxtPlugin } from "#app";
3
+ import { unref, watch } from "#imports";
4
+ import config from "#build/nuxt-schema-org-config.mjs";
5
+ export default defineNuxtPlugin((nuxtApp) => {
6
+ const ssr = !!nuxtApp.ssrContext?.url;
7
+ const client = createSchemaOrg({
8
+ updateHead(fn) {
9
+ nuxtApp._useHead(unref(fn));
10
+ },
11
+ meta() {
12
+ const head = nuxtApp.vueApp._context.provides.usehead;
13
+ const inferredMeta = {};
14
+ const headTag = head.headTags.reverse().filter((t) => t.tag === "title" && !!t.props.children);
15
+ if (headTag.length)
16
+ inferredMeta.title = headTag[0].props.children;
17
+ return {
18
+ path: nuxtApp._route.path,
19
+ ...inferredMeta,
20
+ ...nuxtApp._route.meta,
21
+ ...config.meta || {}
22
+ };
23
+ }
24
+ });
25
+ nuxtApp._injectSchemaOrg = () => client;
26
+ nuxtApp.vueApp.use(client);
27
+ if (ssr) {
28
+ client.generateSchema();
29
+ client.setupDOM();
30
+ return;
31
+ }
32
+ watch(() => nuxtApp._route.path, () => {
33
+ client.generateSchema();
34
+ client.setupDOM();
35
+ });
36
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-schema-org",
3
- "version": "0.7.0-beta.3",
3
+ "version": "1.0.0-beta.3",
4
4
  "description": "Nuxt module for @vueuse/schema-org",
5
5
  "author": "Harlan Wilton <harlan@harlanzw.com>",
6
6
  "license": "MIT",
@@ -35,14 +35,16 @@
35
35
  "dist"
36
36
  ],
37
37
  "dependencies": {
38
- "@nuxt/kit": "3.0.0-rc.6",
39
- "@vueuse/schema-org": "0.7.0-beta.3",
40
- "magic-string": "^0.26.2",
41
- "unplugin": "^0.7.2"
38
+ "@nuxt/kit": "3.0.0-rc.8",
39
+ "@vueuse/schema-org": "1.0.0-beta.3",
40
+ "@vueuse/schema-org-vite": "1.0.0-beta.3",
41
+ "pathe": "^0.3.4",
42
+ "schema-org-graph-js": "0.3.3"
42
43
  },
43
44
  "devDependencies": {
44
45
  "@nuxt/module-builder": "latest",
45
- "@nuxt/schema": "3.0.0-rc.6"
46
+ "@nuxt/schema": "3.0.0-rc.8",
47
+ "nuxt": "3.0.0-rc.8"
46
48
  },
47
49
  "scripts": {
48
50
  "build": "npm run -C ../../playgrounds/nuxt3 prepare && nuxt-module-build",
@@ -1,19 +0,0 @@
1
- import { createSchemaOrg, handleNodesCSR, useVueUseHead } from "@vueuse/schema-org";
2
- import { defineNuxtPlugin } from "#app";
3
- import { useRoute } from "#imports";
4
- import meta from "#build/schemaOrg.config.mjs";
5
- export default defineNuxtPlugin((nuxtApp) => {
6
- const head = nuxtApp.vueApp._context.provides.usehead;
7
- const client = createSchemaOrg({
8
- provider: {
9
- useRoute,
10
- setupDOM: useVueUseHead(head),
11
- name: "nuxt"
12
- },
13
- ...meta.config
14
- });
15
- nuxtApp._useSchemaOrg = (input) => {
16
- return handleNodesCSR(client, input);
17
- };
18
- nuxtApp.vueApp.use(client);
19
- });
@@ -1,37 +0,0 @@
1
- import { createSchemaOrg, handleNodesSSR } from "@vueuse/schema-org";
2
- import { defineNuxtPlugin } from "#app";
3
- import { computed, useRoute } from "#imports";
4
- import meta from "#build/schemaOrg.config.mjs";
5
- export default defineNuxtPlugin((nuxtApp) => {
6
- const head = nuxtApp.vueApp._context.provides.usehead;
7
- let _domSetup = false;
8
- const client = createSchemaOrg({
9
- provider: {
10
- useRoute,
11
- setupDOM({ schemaRef }) {
12
- if (_domSetup)
13
- return;
14
- head.addHeadObjs(computed(() => {
15
- return {
16
- script: [
17
- {
18
- "type": "application/ld+json",
19
- "data-id": "schema-org-graph",
20
- "key": "schema-org-graph",
21
- "children": schemaRef.value
22
- }
23
- ]
24
- };
25
- }));
26
- _domSetup = true;
27
- },
28
- name: "nuxt"
29
- },
30
- ...meta.config
31
- });
32
- nuxtApp._useSchemaOrg = (input) => {
33
- return handleNodesSSR(client, input);
34
- };
35
- client.setupDOM();
36
- nuxtApp.vueApp.use(client);
37
- });