@unhead/addons 1.0.21 → 1.1.0

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/index.cjs CHANGED
@@ -1,12 +1,12 @@
1
1
  'use strict';
2
2
 
3
- const unhead = require('unhead');
3
+ const shared = require('@unhead/shared');
4
4
 
5
5
  const InferSeoMetaPlugin = (options) => {
6
6
  options = options || {};
7
7
  const ogTitleTemplate = options.ogTitle || "%s";
8
8
  const ogDescriptionTemplate = options.ogDescription || "%s";
9
- return unhead.defineHeadPlugin({
9
+ return shared.defineHeadPlugin({
10
10
  hooks: {
11
11
  entries: {
12
12
  resolve({ entries }) {
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { defineHeadPlugin } from 'unhead';
1
+ import { defineHeadPlugin } from '@unhead/shared';
2
2
 
3
3
  const InferSeoMetaPlugin = (options) => {
4
4
  options = options || {};
@@ -0,0 +1,194 @@
1
+ 'use strict';
2
+
3
+ const node_url = require('node:url');
4
+ const unplugin = require('unplugin');
5
+ const unpluginAst = require('unplugin-ast');
6
+ const ufo = require('ufo');
7
+ const node_vm = require('node:vm');
8
+ const unhead = require('unhead');
9
+ const MagicString = require('magic-string');
10
+ const estreeWalker = require('estree-walker');
11
+ const mlly = require('mlly');
12
+
13
+ const RemoveFunctions = (functionNames) => ({
14
+ onNode: (node) => node.type === "CallExpression" && node.callee.type === "Identifier" && functionNames.includes(node.callee.name),
15
+ transform() {
16
+ return false;
17
+ }
18
+ });
19
+ const TreeshakeServerComposables = unplugin.createUnplugin(() => {
20
+ return {
21
+ name: "unhead:remove-server-composables",
22
+ enforce: "post",
23
+ transformInclude(id) {
24
+ const { pathname, search } = ufo.parseURL(decodeURIComponent(node_url.pathToFileURL(id).href));
25
+ const { type } = ufo.parseQuery(search);
26
+ if (pathname.endsWith(".vue") && (type === "script" || !search))
27
+ return true;
28
+ if (pathname.match(/\.((c|m)?j|t)sx?$/g))
29
+ return true;
30
+ return false;
31
+ },
32
+ async transform(code, id) {
33
+ if (!code.includes("useServerHead") && !code.includes("useServerHeadSafe") && !code.includes("useServerSeoMeta") && !code.includes("useSchemaOrg"))
34
+ return null;
35
+ let transformed;
36
+ try {
37
+ transformed = await unpluginAst.transform(code, id, {
38
+ parserOptions: {},
39
+ transformer: [
40
+ RemoveFunctions([
41
+ "useServerHead",
42
+ "useServerHeadSafe",
43
+ "useServerSeoMeta",
44
+ // plugins
45
+ "useSchemaOrg"
46
+ ])
47
+ ]
48
+ });
49
+ } catch (e) {
50
+ }
51
+ return transformed;
52
+ },
53
+ webpack(ctx) {
54
+ if (ctx.name !== "server")
55
+ ;
56
+ },
57
+ vite: {
58
+ async config(config) {
59
+ root = root || config.root || process.cwd();
60
+ },
61
+ apply(config, env) {
62
+ if (!env.ssrBuild) {
63
+ return true;
64
+ }
65
+ return false;
66
+ }
67
+ }
68
+ };
69
+ });
70
+
71
+ const UseSeoMetaTransform = unplugin.createUnplugin(() => {
72
+ return {
73
+ name: "unhead:use-seo-meta-transform",
74
+ enforce: "post",
75
+ transformInclude(id) {
76
+ const { pathname, search } = ufo.parseURL(decodeURIComponent(node_url.pathToFileURL(id).href));
77
+ const { type } = ufo.parseQuery(search);
78
+ if (pathname.endsWith(".vue") && (type === "script" || !search))
79
+ return true;
80
+ if (pathname.match(/\.((c|m)?j|t)sx?$/g))
81
+ return true;
82
+ return false;
83
+ },
84
+ async transform(code, id) {
85
+ if (!code.includes("useSeoMeta") && !code.includes("useServerSeoMeta"))
86
+ return;
87
+ const packages = ["unhead", "@unhead/vue", "unhead"];
88
+ const statements = mlly.findStaticImports(code).filter((i) => packages.includes(i.specifier));
89
+ const importNames = {};
90
+ for (const i of statements.flatMap((i2) => mlly.parseStaticImport(i2))) {
91
+ if (i.namedImports) {
92
+ for (const key in i.namedImports)
93
+ importNames[i.namedImports[key]] = key;
94
+ }
95
+ }
96
+ const ast = this.parse(code);
97
+ const s = new MagicString(code);
98
+ const extraImports = /* @__PURE__ */ new Set();
99
+ estreeWalker.walk(ast, {
100
+ enter(_node) {
101
+ if (_node.type === "ImportDeclaration" && packages.includes(_node.source.value)) {
102
+ if (!_node.specifiers.some((s2) => s2.type === "ImportSpecifier" && ["useSeoMeta", "useServerSeoMeta"].includes(s2.imported?.name)))
103
+ return;
104
+ const imports = Object.values(importNames);
105
+ if (!imports.includes("useHead"))
106
+ extraImports.add(`import { useHead } from '${_node.source.value}'`);
107
+ if (!imports.includes("useServerHead") && imports.includes("useServerSeoMeta"))
108
+ extraImports.add(`import { useServerHead } from '${_node.source.value}'`);
109
+ } else if (_node.type === "CallExpression" && _node.callee.type === "Identifier" && Object.keys({
110
+ useSeoMeta: "useSeoMeta",
111
+ useServerSeoMeta: "useServerSeoMeta",
112
+ ...importNames
113
+ }).includes(_node.callee.name)) {
114
+ const node = _node;
115
+ const calleeName = importNames[node.callee.name];
116
+ const properties = node.arguments[0].properties;
117
+ if (!properties)
118
+ return;
119
+ let output = [];
120
+ const title = properties.find((property) => property.key.name === "title");
121
+ const titleTemplate = properties.find((property) => property.key.name === "titleTemplate");
122
+ const meta = properties.filter((property) => property.key.name !== "title" && property.key.name !== "titleTemplate");
123
+ if (title || titleTemplate || calleeName === "useSeoMeta") {
124
+ output.push("useHead({");
125
+ if (title)
126
+ output.push(` title: ${code.substring(title.value.start, title.value.end)},`);
127
+ if (titleTemplate)
128
+ output.push(` titleTemplate: ${code.substring(titleTemplate.value.start, titleTemplate.value.end)},`);
129
+ }
130
+ if (calleeName === "useServerSeoMeta") {
131
+ if (output.length)
132
+ output.push("})");
133
+ output.push("useServerHead({", " meta: [");
134
+ } else if (calleeName === "useSeoMeta") {
135
+ output.push(" meta: [");
136
+ }
137
+ meta.forEach((property) => {
138
+ if (output === false)
139
+ return;
140
+ const key = unhead.resolveMetaKeyType(property.key.name);
141
+ const keyValue = unhead.MetaPackingSchema[property.key.name]?.keyValue || unhead.fixKeyCase(property.key.name);
142
+ const valueKey = key === "charset" ? "charset" : "content";
143
+ let value = code.substring(property.value.start, property.value.end);
144
+ if (property.value.type === "ArrayExpression") {
145
+ output = false;
146
+ return;
147
+ } else if (property.value.type === "ObjectExpression") {
148
+ const isStatic = property.value.properties.every((property2) => property2.value.type === "Literal" && typeof property2.value.value === "string");
149
+ if (!isStatic) {
150
+ output = false;
151
+ return;
152
+ }
153
+ const context = node_vm.createContext({
154
+ resolvePackedMetaObjectValue: unhead.resolvePackedMetaObjectValue
155
+ });
156
+ const start = property.value.start;
157
+ const end = property.value.end;
158
+ try {
159
+ value = JSON.stringify(node_vm.runInContext(`resolvePackedMetaObjectValue(${code.slice(start, end)})`, context));
160
+ } catch {
161
+ output = false;
162
+ return;
163
+ }
164
+ }
165
+ if (valueKey === "charset")
166
+ output.push(` { ${key}: ${value} },`);
167
+ else
168
+ output.push(` { ${key}: '${keyValue}', ${valueKey}: ${value} },`);
169
+ });
170
+ if (output) {
171
+ if (meta.length)
172
+ output.push(" ]");
173
+ output.push("})");
174
+ s.overwrite(node.start, node.end, output.join("\n"));
175
+ }
176
+ }
177
+ }
178
+ });
179
+ if (s.hasChanged()) {
180
+ const prependImports = [...extraImports];
181
+ if (prependImports.length)
182
+ s.prepend(`${prependImports.join("\n")}
183
+ `);
184
+ return {
185
+ code: s.toString(),
186
+ map: s.generateMap({ includeContent: true, source: id })
187
+ };
188
+ }
189
+ }
190
+ };
191
+ });
192
+
193
+ exports.TreeshakeServerComposables = TreeshakeServerComposables;
194
+ exports.UseSeoMetaTransform = UseSeoMetaTransform;
@@ -0,0 +1,191 @@
1
+ import { pathToFileURL } from 'node:url';
2
+ import { createUnplugin } from 'unplugin';
3
+ import { transform } from 'unplugin-ast';
4
+ import { parseURL, parseQuery } from 'ufo';
5
+ import { createContext, runInContext } from 'node:vm';
6
+ import { resolveMetaKeyType, MetaPackingSchema, fixKeyCase, resolvePackedMetaObjectValue } from 'unhead';
7
+ import MagicString from 'magic-string';
8
+ import { walk } from 'estree-walker';
9
+ import { findStaticImports, parseStaticImport } from 'mlly';
10
+
11
+ const RemoveFunctions = (functionNames) => ({
12
+ onNode: (node) => node.type === "CallExpression" && node.callee.type === "Identifier" && functionNames.includes(node.callee.name),
13
+ transform() {
14
+ return false;
15
+ }
16
+ });
17
+ const TreeshakeServerComposables = createUnplugin(() => {
18
+ return {
19
+ name: "unhead:remove-server-composables",
20
+ enforce: "post",
21
+ transformInclude(id) {
22
+ const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href));
23
+ const { type } = parseQuery(search);
24
+ if (pathname.endsWith(".vue") && (type === "script" || !search))
25
+ return true;
26
+ if (pathname.match(/\.((c|m)?j|t)sx?$/g))
27
+ return true;
28
+ return false;
29
+ },
30
+ async transform(code, id) {
31
+ if (!code.includes("useServerHead") && !code.includes("useServerHeadSafe") && !code.includes("useServerSeoMeta") && !code.includes("useSchemaOrg"))
32
+ return null;
33
+ let transformed;
34
+ try {
35
+ transformed = await transform(code, id, {
36
+ parserOptions: {},
37
+ transformer: [
38
+ RemoveFunctions([
39
+ "useServerHead",
40
+ "useServerHeadSafe",
41
+ "useServerSeoMeta",
42
+ // plugins
43
+ "useSchemaOrg"
44
+ ])
45
+ ]
46
+ });
47
+ } catch (e) {
48
+ }
49
+ return transformed;
50
+ },
51
+ webpack(ctx) {
52
+ if (ctx.name !== "server")
53
+ ;
54
+ },
55
+ vite: {
56
+ async config(config) {
57
+ root = root || config.root || process.cwd();
58
+ },
59
+ apply(config, env) {
60
+ if (!env.ssrBuild) {
61
+ return true;
62
+ }
63
+ return false;
64
+ }
65
+ }
66
+ };
67
+ });
68
+
69
+ const UseSeoMetaTransform = createUnplugin(() => {
70
+ return {
71
+ name: "unhead:use-seo-meta-transform",
72
+ enforce: "post",
73
+ transformInclude(id) {
74
+ const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href));
75
+ const { type } = parseQuery(search);
76
+ if (pathname.endsWith(".vue") && (type === "script" || !search))
77
+ return true;
78
+ if (pathname.match(/\.((c|m)?j|t)sx?$/g))
79
+ return true;
80
+ return false;
81
+ },
82
+ async transform(code, id) {
83
+ if (!code.includes("useSeoMeta") && !code.includes("useServerSeoMeta"))
84
+ return;
85
+ const packages = ["unhead", "@unhead/vue", "unhead"];
86
+ const statements = findStaticImports(code).filter((i) => packages.includes(i.specifier));
87
+ const importNames = {};
88
+ for (const i of statements.flatMap((i2) => parseStaticImport(i2))) {
89
+ if (i.namedImports) {
90
+ for (const key in i.namedImports)
91
+ importNames[i.namedImports[key]] = key;
92
+ }
93
+ }
94
+ const ast = this.parse(code);
95
+ const s = new MagicString(code);
96
+ const extraImports = /* @__PURE__ */ new Set();
97
+ walk(ast, {
98
+ enter(_node) {
99
+ if (_node.type === "ImportDeclaration" && packages.includes(_node.source.value)) {
100
+ if (!_node.specifiers.some((s2) => s2.type === "ImportSpecifier" && ["useSeoMeta", "useServerSeoMeta"].includes(s2.imported?.name)))
101
+ return;
102
+ const imports = Object.values(importNames);
103
+ if (!imports.includes("useHead"))
104
+ extraImports.add(`import { useHead } from '${_node.source.value}'`);
105
+ if (!imports.includes("useServerHead") && imports.includes("useServerSeoMeta"))
106
+ extraImports.add(`import { useServerHead } from '${_node.source.value}'`);
107
+ } else if (_node.type === "CallExpression" && _node.callee.type === "Identifier" && Object.keys({
108
+ useSeoMeta: "useSeoMeta",
109
+ useServerSeoMeta: "useServerSeoMeta",
110
+ ...importNames
111
+ }).includes(_node.callee.name)) {
112
+ const node = _node;
113
+ const calleeName = importNames[node.callee.name];
114
+ const properties = node.arguments[0].properties;
115
+ if (!properties)
116
+ return;
117
+ let output = [];
118
+ const title = properties.find((property) => property.key.name === "title");
119
+ const titleTemplate = properties.find((property) => property.key.name === "titleTemplate");
120
+ const meta = properties.filter((property) => property.key.name !== "title" && property.key.name !== "titleTemplate");
121
+ if (title || titleTemplate || calleeName === "useSeoMeta") {
122
+ output.push("useHead({");
123
+ if (title)
124
+ output.push(` title: ${code.substring(title.value.start, title.value.end)},`);
125
+ if (titleTemplate)
126
+ output.push(` titleTemplate: ${code.substring(titleTemplate.value.start, titleTemplate.value.end)},`);
127
+ }
128
+ if (calleeName === "useServerSeoMeta") {
129
+ if (output.length)
130
+ output.push("})");
131
+ output.push("useServerHead({", " meta: [");
132
+ } else if (calleeName === "useSeoMeta") {
133
+ output.push(" meta: [");
134
+ }
135
+ meta.forEach((property) => {
136
+ if (output === false)
137
+ return;
138
+ const key = resolveMetaKeyType(property.key.name);
139
+ const keyValue = MetaPackingSchema[property.key.name]?.keyValue || fixKeyCase(property.key.name);
140
+ const valueKey = key === "charset" ? "charset" : "content";
141
+ let value = code.substring(property.value.start, property.value.end);
142
+ if (property.value.type === "ArrayExpression") {
143
+ output = false;
144
+ return;
145
+ } else if (property.value.type === "ObjectExpression") {
146
+ const isStatic = property.value.properties.every((property2) => property2.value.type === "Literal" && typeof property2.value.value === "string");
147
+ if (!isStatic) {
148
+ output = false;
149
+ return;
150
+ }
151
+ const context = createContext({
152
+ resolvePackedMetaObjectValue
153
+ });
154
+ const start = property.value.start;
155
+ const end = property.value.end;
156
+ try {
157
+ value = JSON.stringify(runInContext(`resolvePackedMetaObjectValue(${code.slice(start, end)})`, context));
158
+ } catch {
159
+ output = false;
160
+ return;
161
+ }
162
+ }
163
+ if (valueKey === "charset")
164
+ output.push(` { ${key}: ${value} },`);
165
+ else
166
+ output.push(` { ${key}: '${keyValue}', ${valueKey}: ${value} },`);
167
+ });
168
+ if (output) {
169
+ if (meta.length)
170
+ output.push(" ]");
171
+ output.push("})");
172
+ s.overwrite(node.start, node.end, output.join("\n"));
173
+ }
174
+ }
175
+ }
176
+ });
177
+ if (s.hasChanged()) {
178
+ const prependImports = [...extraImports];
179
+ if (prependImports.length)
180
+ s.prepend(`${prependImports.join("\n")}
181
+ `);
182
+ return {
183
+ code: s.toString(),
184
+ map: s.generateMap({ includeContent: true, source: id })
185
+ };
186
+ }
187
+ }
188
+ };
189
+ });
190
+
191
+ export { TreeshakeServerComposables as T, UseSeoMetaTransform as U };
package/dist/vite.cjs CHANGED
@@ -1,13 +1,20 @@
1
1
  'use strict';
2
2
 
3
- const TreeshakeServerComposables = require('./shared/addons.c0c549fb.cjs');
3
+ const UseSeoMetaTransform = require('./shared/addons.6d781a02.cjs');
4
+ require('node:url');
4
5
  require('unplugin');
5
- require('@rollup/pluginutils');
6
6
  require('unplugin-ast');
7
+ require('ufo');
8
+ require('node:vm');
9
+ require('unhead');
10
+ require('magic-string');
11
+ require('estree-walker');
12
+ require('mlly');
7
13
 
8
- const vite = (args = {}) => {
14
+ const vite = () => {
9
15
  return [
10
- TreeshakeServerComposables.TreeshakeServerComposables.vite(args)
16
+ UseSeoMetaTransform.TreeshakeServerComposables.vite(),
17
+ UseSeoMetaTransform.UseSeoMetaTransform.vite()
11
18
  ];
12
19
  };
13
20
 
package/dist/vite.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import * as vite from 'vite';
2
- import { P as PluginOptions } from './types-3ec0a9ee.js';
3
2
 
4
- declare const _default: (args?: PluginOptions) => (vite.Plugin | vite.Plugin[])[];
3
+ declare const _default: () => (vite.Plugin | vite.Plugin[])[];
5
4
 
6
5
  export { _default as default };
package/dist/vite.mjs CHANGED
@@ -1,11 +1,18 @@
1
- import { T as TreeshakeServerComposables } from './shared/addons.6d6f9a48.mjs';
1
+ import { T as TreeshakeServerComposables, U as UseSeoMetaTransform } from './shared/addons.ba318fee.mjs';
2
+ import 'node:url';
2
3
  import 'unplugin';
3
- import '@rollup/pluginutils';
4
4
  import 'unplugin-ast';
5
+ import 'ufo';
6
+ import 'node:vm';
7
+ import 'unhead';
8
+ import 'magic-string';
9
+ import 'estree-walker';
10
+ import 'mlly';
5
11
 
6
- const vite = (args = {}) => {
12
+ const vite = () => {
7
13
  return [
8
- TreeshakeServerComposables.vite(args)
14
+ TreeshakeServerComposables.vite(),
15
+ UseSeoMetaTransform.vite()
9
16
  ];
10
17
  };
11
18
 
package/dist/webpack.cjs CHANGED
@@ -1,13 +1,20 @@
1
1
  'use strict';
2
2
 
3
- const TreeshakeServerComposables = require('./shared/addons.c0c549fb.cjs');
3
+ const UseSeoMetaTransform = require('./shared/addons.6d781a02.cjs');
4
+ require('node:url');
4
5
  require('unplugin');
5
- require('@rollup/pluginutils');
6
6
  require('unplugin-ast');
7
+ require('ufo');
8
+ require('node:vm');
9
+ require('unhead');
10
+ require('magic-string');
11
+ require('estree-walker');
12
+ require('mlly');
7
13
 
8
- const webpack = (args = {}) => {
14
+ const webpack = () => {
9
15
  return [
10
- TreeshakeServerComposables.TreeshakeServerComposables.webpack(args)
16
+ UseSeoMetaTransform.TreeshakeServerComposables.webpack(),
17
+ UseSeoMetaTransform.UseSeoMetaTransform.webpack()
11
18
  ];
12
19
  };
13
20
 
package/dist/webpack.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import * as webpack from 'webpack';
2
- import { P as PluginOptions } from './types-3ec0a9ee.js';
3
2
 
4
- declare const _default: (args?: PluginOptions) => webpack.WebpackPluginInstance[];
3
+ declare const _default: () => webpack.WebpackPluginInstance[];
5
4
 
6
5
  export { _default as default };
package/dist/webpack.mjs CHANGED
@@ -1,11 +1,18 @@
1
- import { T as TreeshakeServerComposables } from './shared/addons.6d6f9a48.mjs';
1
+ import { T as TreeshakeServerComposables, U as UseSeoMetaTransform } from './shared/addons.ba318fee.mjs';
2
+ import 'node:url';
2
3
  import 'unplugin';
3
- import '@rollup/pluginutils';
4
4
  import 'unplugin-ast';
5
+ import 'ufo';
6
+ import 'node:vm';
7
+ import 'unhead';
8
+ import 'magic-string';
9
+ import 'estree-walker';
10
+ import 'mlly';
5
11
 
6
- const webpack = (args = {}) => {
12
+ const webpack = () => {
7
13
  return [
8
- TreeshakeServerComposables.webpack(args)
14
+ TreeshakeServerComposables.webpack(),
15
+ UseSeoMetaTransform.webpack()
9
16
  ];
10
17
  };
11
18
 
package/package.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "name": "@unhead/addons",
3
3
  "type": "module",
4
- "version": "1.0.21",
5
- "packageManager": "pnpm@7.25.1",
4
+ "version": "1.1.0",
5
+ "packageManager": "pnpm@7.27.1",
6
6
  "author": "Harlan Wilton <harlan@harlanzw.com>",
7
7
  "license": "MIT",
8
8
  "funding": "https://github.com/sponsors/harlan-zw",
9
- "homepage": "https://github.com/harlan-zw/unhead#readme",
9
+ "homepage": "https://unhead.harlanzw.com",
10
10
  "repository": {
11
11
  "type": "git",
12
- "url": "git+https://github.com/harlan-zw/unhead.git",
12
+ "url": "git+https://github.com/unjs/unhead.git",
13
13
  "directory": "packages/ssr"
14
14
  },
15
15
  "bugs": {
16
- "url": "https://github.com/harlan-zw/unhead/issues"
16
+ "url": "https://github.com/unjs/unhead/issues"
17
17
  },
18
18
  "sideEffects": false,
19
19
  "exports": {
@@ -51,14 +51,18 @@
51
51
  ],
52
52
  "dependencies": {
53
53
  "@rollup/pluginutils": "^5.0.2",
54
- "unplugin": "^1.0.1",
54
+ "estree-walker": "^3.0.3",
55
+ "magic-string": "^0.30.0",
56
+ "mlly": "^1.1.1",
57
+ "ufo": "^1.1.0",
58
+ "unplugin": "^1.1.0",
55
59
  "unplugin-ast": "^0.7.0",
56
- "@unhead/schema": "1.0.21",
57
- "unhead": "1.0.21"
60
+ "@unhead/schema": "1.1.0",
61
+ "@unhead/shared": "1.1.0",
62
+ "unhead": "1.1.0"
58
63
  },
59
64
  "devDependencies": {
60
- "@babel/types": "^7.20.7",
61
- "zhead": "^1.1.0"
65
+ "@babel/types": "^7.21.0"
62
66
  },
63
67
  "scripts": {
64
68
  "build": "unbuild .",
@@ -1,70 +0,0 @@
1
- import { createUnplugin } from 'unplugin';
2
- import { createFilter } from '@rollup/pluginutils';
3
- import { transform } from 'unplugin-ast';
4
-
5
- const RemoveFunctions = (functionNames) => ({
6
- onNode: (node) => node.type === "CallExpression" && node.callee.type === "Identifier" && functionNames.includes(node.callee.name),
7
- transform() {
8
- return false;
9
- }
10
- });
11
- const TreeshakeServerComposables = createUnplugin((userConfig = {}) => {
12
- const filter = createFilter([
13
- /\.[jt]sx?$/,
14
- /\.vue$/
15
- ], [
16
- "node_modules"
17
- ]);
18
- let root = userConfig.root;
19
- let enabled = false;
20
- return {
21
- name: "unhead:remove-server-composables",
22
- enforce: "post",
23
- transformInclude(id) {
24
- if (!enabled)
25
- return false;
26
- if (root && !id.startsWith(root))
27
- return false;
28
- if (!filter(id))
29
- return false;
30
- },
31
- async transform(code, id) {
32
- if (!code.includes("useServerHead") && !code.includes("useSeoMeta") && !code.includes("useSchemaOrg"))
33
- return null;
34
- let transformed;
35
- try {
36
- transformed = await transform(code, id, {
37
- parserOptions: {},
38
- transformer: [
39
- RemoveFunctions([
40
- "useServerHead",
41
- "useSeoMeta",
42
- // plugins
43
- "useSchemaOrg"
44
- ])
45
- ]
46
- });
47
- } catch (e) {
48
- }
49
- return transformed;
50
- },
51
- webpack(ctx) {
52
- if (ctx.name !== "server")
53
- enabled = true;
54
- },
55
- vite: {
56
- async config(config) {
57
- root = root || config.root || process.cwd();
58
- },
59
- apply(config, env) {
60
- if (!env.ssrBuild) {
61
- enabled = true;
62
- return true;
63
- }
64
- return false;
65
- }
66
- }
67
- };
68
- });
69
-
70
- export { TreeshakeServerComposables as T };
@@ -1,72 +0,0 @@
1
- 'use strict';
2
-
3
- const unplugin = require('unplugin');
4
- const pluginutils = require('@rollup/pluginutils');
5
- const unpluginAst = require('unplugin-ast');
6
-
7
- const RemoveFunctions = (functionNames) => ({
8
- onNode: (node) => node.type === "CallExpression" && node.callee.type === "Identifier" && functionNames.includes(node.callee.name),
9
- transform() {
10
- return false;
11
- }
12
- });
13
- const TreeshakeServerComposables = unplugin.createUnplugin((userConfig = {}) => {
14
- const filter = pluginutils.createFilter([
15
- /\.[jt]sx?$/,
16
- /\.vue$/
17
- ], [
18
- "node_modules"
19
- ]);
20
- let root = userConfig.root;
21
- let enabled = false;
22
- return {
23
- name: "unhead:remove-server-composables",
24
- enforce: "post",
25
- transformInclude(id) {
26
- if (!enabled)
27
- return false;
28
- if (root && !id.startsWith(root))
29
- return false;
30
- if (!filter(id))
31
- return false;
32
- },
33
- async transform(code, id) {
34
- if (!code.includes("useServerHead") && !code.includes("useSeoMeta") && !code.includes("useSchemaOrg"))
35
- return null;
36
- let transformed;
37
- try {
38
- transformed = await unpluginAst.transform(code, id, {
39
- parserOptions: {},
40
- transformer: [
41
- RemoveFunctions([
42
- "useServerHead",
43
- "useSeoMeta",
44
- // plugins
45
- "useSchemaOrg"
46
- ])
47
- ]
48
- });
49
- } catch (e) {
50
- }
51
- return transformed;
52
- },
53
- webpack(ctx) {
54
- if (ctx.name !== "server")
55
- enabled = true;
56
- },
57
- vite: {
58
- async config(config) {
59
- root = root || config.root || process.cwd();
60
- },
61
- apply(config, env) {
62
- if (!env.ssrBuild) {
63
- enabled = true;
64
- return true;
65
- }
66
- return false;
67
- }
68
- }
69
- };
70
- });
71
-
72
- exports.TreeshakeServerComposables = TreeshakeServerComposables;
@@ -1,8 +0,0 @@
1
- interface PluginOptions {
2
- /**
3
- * Root directory
4
- */
5
- root?: string;
6
- }
7
-
8
- export { PluginOptions as P };