@unhead/addons 1.1.5 → 1.1.6

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.
@@ -7,7 +7,6 @@ const ufo = require('ufo');
7
7
  const node_vm = require('node:vm');
8
8
  const unhead = require('unhead');
9
9
  const MagicString = require('magic-string');
10
- const estreeWalker = require('estree-walker');
11
10
  const mlly = require('mlly');
12
11
 
13
12
  const RemoveFunctions = (functionNames) => ({
@@ -71,6 +70,238 @@ const TreeshakeServerComposables = unplugin.createUnplugin(() => {
71
70
  };
72
71
  });
73
72
 
73
+ /**
74
+ * @typedef { import('estree').Node} Node
75
+ * @typedef {{
76
+ * skip: () => void;
77
+ * remove: () => void;
78
+ * replace: (node: Node) => void;
79
+ * }} WalkerContext
80
+ */
81
+
82
+ class WalkerBase {
83
+ constructor() {
84
+ /** @type {boolean} */
85
+ this.should_skip = false;
86
+
87
+ /** @type {boolean} */
88
+ this.should_remove = false;
89
+
90
+ /** @type {Node | null} */
91
+ this.replacement = null;
92
+
93
+ /** @type {WalkerContext} */
94
+ this.context = {
95
+ skip: () => (this.should_skip = true),
96
+ remove: () => (this.should_remove = true),
97
+ replace: (node) => (this.replacement = node)
98
+ };
99
+ }
100
+
101
+ /**
102
+ * @template {Node} Parent
103
+ * @param {Parent | null | undefined} parent
104
+ * @param {keyof Parent | null | undefined} prop
105
+ * @param {number | null | undefined} index
106
+ * @param {Node} node
107
+ */
108
+ replace(parent, prop, index, node) {
109
+ if (parent && prop) {
110
+ if (index != null) {
111
+ /** @type {Array<Node>} */ (parent[prop])[index] = node;
112
+ } else {
113
+ /** @type {Node} */ (parent[prop]) = node;
114
+ }
115
+ }
116
+ }
117
+
118
+ /**
119
+ * @template {Node} Parent
120
+ * @param {Parent | null | undefined} parent
121
+ * @param {keyof Parent | null | undefined} prop
122
+ * @param {number | null | undefined} index
123
+ */
124
+ remove(parent, prop, index) {
125
+ if (parent && prop) {
126
+ if (index !== null && index !== undefined) {
127
+ /** @type {Array<Node>} */ (parent[prop]).splice(index, 1);
128
+ } else {
129
+ delete parent[prop];
130
+ }
131
+ }
132
+ }
133
+ }
134
+
135
+ /**
136
+ * @typedef { import('estree').Node} Node
137
+ * @typedef { import('./walker.js').WalkerContext} WalkerContext
138
+ * @typedef {(
139
+ * this: WalkerContext,
140
+ * node: Node,
141
+ * parent: Node | null,
142
+ * key: string | number | symbol | null | undefined,
143
+ * index: number | null | undefined
144
+ * ) => void} SyncHandler
145
+ */
146
+
147
+ class SyncWalker extends WalkerBase {
148
+ /**
149
+ *
150
+ * @param {SyncHandler} [enter]
151
+ * @param {SyncHandler} [leave]
152
+ */
153
+ constructor(enter, leave) {
154
+ super();
155
+
156
+ /** @type {boolean} */
157
+ this.should_skip = false;
158
+
159
+ /** @type {boolean} */
160
+ this.should_remove = false;
161
+
162
+ /** @type {Node | null} */
163
+ this.replacement = null;
164
+
165
+ /** @type {WalkerContext} */
166
+ this.context = {
167
+ skip: () => (this.should_skip = true),
168
+ remove: () => (this.should_remove = true),
169
+ replace: (node) => (this.replacement = node)
170
+ };
171
+
172
+ /** @type {SyncHandler | undefined} */
173
+ this.enter = enter;
174
+
175
+ /** @type {SyncHandler | undefined} */
176
+ this.leave = leave;
177
+ }
178
+
179
+ /**
180
+ * @template {Node} Parent
181
+ * @param {Node} node
182
+ * @param {Parent | null} parent
183
+ * @param {keyof Parent} [prop]
184
+ * @param {number | null} [index]
185
+ * @returns {Node | null}
186
+ */
187
+ visit(node, parent, prop, index) {
188
+ if (node) {
189
+ if (this.enter) {
190
+ const _should_skip = this.should_skip;
191
+ const _should_remove = this.should_remove;
192
+ const _replacement = this.replacement;
193
+ this.should_skip = false;
194
+ this.should_remove = false;
195
+ this.replacement = null;
196
+
197
+ this.enter.call(this.context, node, parent, prop, index);
198
+
199
+ if (this.replacement) {
200
+ node = this.replacement;
201
+ this.replace(parent, prop, index, node);
202
+ }
203
+
204
+ if (this.should_remove) {
205
+ this.remove(parent, prop, index);
206
+ }
207
+
208
+ const skipped = this.should_skip;
209
+ const removed = this.should_remove;
210
+
211
+ this.should_skip = _should_skip;
212
+ this.should_remove = _should_remove;
213
+ this.replacement = _replacement;
214
+
215
+ if (skipped) return node;
216
+ if (removed) return null;
217
+ }
218
+
219
+ /** @type {keyof Node} */
220
+ let key;
221
+
222
+ for (key in node) {
223
+ /** @type {unknown} */
224
+ const value = node[key];
225
+
226
+ if (value && typeof value === 'object') {
227
+ if (Array.isArray(value)) {
228
+ const nodes = /** @type {Array<unknown>} */ (value);
229
+ for (let i = 0; i < nodes.length; i += 1) {
230
+ const item = nodes[i];
231
+ if (isNode(item)) {
232
+ if (!this.visit(item, node, key, i)) {
233
+ // removed
234
+ i--;
235
+ }
236
+ }
237
+ }
238
+ } else if (isNode(value)) {
239
+ this.visit(value, node, key, null);
240
+ }
241
+ }
242
+ }
243
+
244
+ if (this.leave) {
245
+ const _replacement = this.replacement;
246
+ const _should_remove = this.should_remove;
247
+ this.replacement = null;
248
+ this.should_remove = false;
249
+
250
+ this.leave.call(this.context, node, parent, prop, index);
251
+
252
+ if (this.replacement) {
253
+ node = this.replacement;
254
+ this.replace(parent, prop, index, node);
255
+ }
256
+
257
+ if (this.should_remove) {
258
+ this.remove(parent, prop, index);
259
+ }
260
+
261
+ const removed = this.should_remove;
262
+
263
+ this.replacement = _replacement;
264
+ this.should_remove = _should_remove;
265
+
266
+ if (removed) return null;
267
+ }
268
+ }
269
+
270
+ return node;
271
+ }
272
+ }
273
+
274
+ /**
275
+ * Ducktype a node.
276
+ *
277
+ * @param {unknown} value
278
+ * @returns {value is Node}
279
+ */
280
+ function isNode(value) {
281
+ return (
282
+ value !== null && typeof value === 'object' && 'type' in value && typeof value.type === 'string'
283
+ );
284
+ }
285
+
286
+ /**
287
+ * @typedef {import('estree').Node} Node
288
+ * @typedef {import('./sync.js').SyncHandler} SyncHandler
289
+ * @typedef {import('./async.js').AsyncHandler} AsyncHandler
290
+ */
291
+
292
+ /**
293
+ * @param {Node} ast
294
+ * @param {{
295
+ * enter?: SyncHandler
296
+ * leave?: SyncHandler
297
+ * }} walker
298
+ * @returns {Node | null}
299
+ */
300
+ function walk(ast, { enter, leave }) {
301
+ const instance = new SyncWalker(enter, leave);
302
+ return instance.visit(ast, null);
303
+ }
304
+
74
305
  const UseSeoMetaTransform = unplugin.createUnplugin(() => {
75
306
  return {
76
307
  name: "unhead:use-seo-meta-transform",
@@ -101,7 +332,7 @@ const UseSeoMetaTransform = unplugin.createUnplugin(() => {
101
332
  const ast = this.parse(code);
102
333
  const s = new MagicString(code);
103
334
  const extraImports = /* @__PURE__ */ new Set();
104
- estreeWalker.walk(ast, {
335
+ walk(ast, {
105
336
  enter(_node) {
106
337
  if (_node.type === "ImportDeclaration" && packages.includes(_node.source.value)) {
107
338
  if (!_node.specifiers.some((s2) => s2.type === "ImportSpecifier" && ["useSeoMeta", "useServerSeoMeta"].includes(s2.imported?.name)))
@@ -5,7 +5,6 @@ import { parseURL, parseQuery } from 'ufo';
5
5
  import { createContext, runInContext } from 'node:vm';
6
6
  import { resolveMetaKeyType, MetaPackingSchema, fixKeyCase, resolvePackedMetaObjectValue } from 'unhead';
7
7
  import MagicString from 'magic-string';
8
- import { walk } from 'estree-walker';
9
8
  import { findStaticImports, parseStaticImport } from 'mlly';
10
9
 
11
10
  const RemoveFunctions = (functionNames) => ({
@@ -69,6 +68,238 @@ const TreeshakeServerComposables = createUnplugin(() => {
69
68
  };
70
69
  });
71
70
 
71
+ /**
72
+ * @typedef { import('estree').Node} Node
73
+ * @typedef {{
74
+ * skip: () => void;
75
+ * remove: () => void;
76
+ * replace: (node: Node) => void;
77
+ * }} WalkerContext
78
+ */
79
+
80
+ class WalkerBase {
81
+ constructor() {
82
+ /** @type {boolean} */
83
+ this.should_skip = false;
84
+
85
+ /** @type {boolean} */
86
+ this.should_remove = false;
87
+
88
+ /** @type {Node | null} */
89
+ this.replacement = null;
90
+
91
+ /** @type {WalkerContext} */
92
+ this.context = {
93
+ skip: () => (this.should_skip = true),
94
+ remove: () => (this.should_remove = true),
95
+ replace: (node) => (this.replacement = node)
96
+ };
97
+ }
98
+
99
+ /**
100
+ * @template {Node} Parent
101
+ * @param {Parent | null | undefined} parent
102
+ * @param {keyof Parent | null | undefined} prop
103
+ * @param {number | null | undefined} index
104
+ * @param {Node} node
105
+ */
106
+ replace(parent, prop, index, node) {
107
+ if (parent && prop) {
108
+ if (index != null) {
109
+ /** @type {Array<Node>} */ (parent[prop])[index] = node;
110
+ } else {
111
+ /** @type {Node} */ (parent[prop]) = node;
112
+ }
113
+ }
114
+ }
115
+
116
+ /**
117
+ * @template {Node} Parent
118
+ * @param {Parent | null | undefined} parent
119
+ * @param {keyof Parent | null | undefined} prop
120
+ * @param {number | null | undefined} index
121
+ */
122
+ remove(parent, prop, index) {
123
+ if (parent && prop) {
124
+ if (index !== null && index !== undefined) {
125
+ /** @type {Array<Node>} */ (parent[prop]).splice(index, 1);
126
+ } else {
127
+ delete parent[prop];
128
+ }
129
+ }
130
+ }
131
+ }
132
+
133
+ /**
134
+ * @typedef { import('estree').Node} Node
135
+ * @typedef { import('./walker.js').WalkerContext} WalkerContext
136
+ * @typedef {(
137
+ * this: WalkerContext,
138
+ * node: Node,
139
+ * parent: Node | null,
140
+ * key: string | number | symbol | null | undefined,
141
+ * index: number | null | undefined
142
+ * ) => void} SyncHandler
143
+ */
144
+
145
+ class SyncWalker extends WalkerBase {
146
+ /**
147
+ *
148
+ * @param {SyncHandler} [enter]
149
+ * @param {SyncHandler} [leave]
150
+ */
151
+ constructor(enter, leave) {
152
+ super();
153
+
154
+ /** @type {boolean} */
155
+ this.should_skip = false;
156
+
157
+ /** @type {boolean} */
158
+ this.should_remove = false;
159
+
160
+ /** @type {Node | null} */
161
+ this.replacement = null;
162
+
163
+ /** @type {WalkerContext} */
164
+ this.context = {
165
+ skip: () => (this.should_skip = true),
166
+ remove: () => (this.should_remove = true),
167
+ replace: (node) => (this.replacement = node)
168
+ };
169
+
170
+ /** @type {SyncHandler | undefined} */
171
+ this.enter = enter;
172
+
173
+ /** @type {SyncHandler | undefined} */
174
+ this.leave = leave;
175
+ }
176
+
177
+ /**
178
+ * @template {Node} Parent
179
+ * @param {Node} node
180
+ * @param {Parent | null} parent
181
+ * @param {keyof Parent} [prop]
182
+ * @param {number | null} [index]
183
+ * @returns {Node | null}
184
+ */
185
+ visit(node, parent, prop, index) {
186
+ if (node) {
187
+ if (this.enter) {
188
+ const _should_skip = this.should_skip;
189
+ const _should_remove = this.should_remove;
190
+ const _replacement = this.replacement;
191
+ this.should_skip = false;
192
+ this.should_remove = false;
193
+ this.replacement = null;
194
+
195
+ this.enter.call(this.context, node, parent, prop, index);
196
+
197
+ if (this.replacement) {
198
+ node = this.replacement;
199
+ this.replace(parent, prop, index, node);
200
+ }
201
+
202
+ if (this.should_remove) {
203
+ this.remove(parent, prop, index);
204
+ }
205
+
206
+ const skipped = this.should_skip;
207
+ const removed = this.should_remove;
208
+
209
+ this.should_skip = _should_skip;
210
+ this.should_remove = _should_remove;
211
+ this.replacement = _replacement;
212
+
213
+ if (skipped) return node;
214
+ if (removed) return null;
215
+ }
216
+
217
+ /** @type {keyof Node} */
218
+ let key;
219
+
220
+ for (key in node) {
221
+ /** @type {unknown} */
222
+ const value = node[key];
223
+
224
+ if (value && typeof value === 'object') {
225
+ if (Array.isArray(value)) {
226
+ const nodes = /** @type {Array<unknown>} */ (value);
227
+ for (let i = 0; i < nodes.length; i += 1) {
228
+ const item = nodes[i];
229
+ if (isNode(item)) {
230
+ if (!this.visit(item, node, key, i)) {
231
+ // removed
232
+ i--;
233
+ }
234
+ }
235
+ }
236
+ } else if (isNode(value)) {
237
+ this.visit(value, node, key, null);
238
+ }
239
+ }
240
+ }
241
+
242
+ if (this.leave) {
243
+ const _replacement = this.replacement;
244
+ const _should_remove = this.should_remove;
245
+ this.replacement = null;
246
+ this.should_remove = false;
247
+
248
+ this.leave.call(this.context, node, parent, prop, index);
249
+
250
+ if (this.replacement) {
251
+ node = this.replacement;
252
+ this.replace(parent, prop, index, node);
253
+ }
254
+
255
+ if (this.should_remove) {
256
+ this.remove(parent, prop, index);
257
+ }
258
+
259
+ const removed = this.should_remove;
260
+
261
+ this.replacement = _replacement;
262
+ this.should_remove = _should_remove;
263
+
264
+ if (removed) return null;
265
+ }
266
+ }
267
+
268
+ return node;
269
+ }
270
+ }
271
+
272
+ /**
273
+ * Ducktype a node.
274
+ *
275
+ * @param {unknown} value
276
+ * @returns {value is Node}
277
+ */
278
+ function isNode(value) {
279
+ return (
280
+ value !== null && typeof value === 'object' && 'type' in value && typeof value.type === 'string'
281
+ );
282
+ }
283
+
284
+ /**
285
+ * @typedef {import('estree').Node} Node
286
+ * @typedef {import('./sync.js').SyncHandler} SyncHandler
287
+ * @typedef {import('./async.js').AsyncHandler} AsyncHandler
288
+ */
289
+
290
+ /**
291
+ * @param {Node} ast
292
+ * @param {{
293
+ * enter?: SyncHandler
294
+ * leave?: SyncHandler
295
+ * }} walker
296
+ * @returns {Node | null}
297
+ */
298
+ function walk(ast, { enter, leave }) {
299
+ const instance = new SyncWalker(enter, leave);
300
+ return instance.visit(ast, null);
301
+ }
302
+
72
303
  const UseSeoMetaTransform = createUnplugin(() => {
73
304
  return {
74
305
  name: "unhead:use-seo-meta-transform",
package/dist/vite.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const UseSeoMetaTransform = require('./shared/addons.1b8e1760.cjs');
3
+ const UseSeoMetaTransform = require('./shared/addons.cdb14016.cjs');
4
4
  require('node:url');
5
5
  require('unplugin');
6
6
  require('unplugin-ast');
@@ -8,7 +8,6 @@ require('ufo');
8
8
  require('node:vm');
9
9
  require('unhead');
10
10
  require('magic-string');
11
- require('estree-walker');
12
11
  require('mlly');
13
12
 
14
13
  const vite = () => {
package/dist/vite.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { T as TreeshakeServerComposables, U as UseSeoMetaTransform } from './shared/addons.6ad24ce7.mjs';
1
+ import { T as TreeshakeServerComposables, U as UseSeoMetaTransform } from './shared/addons.f060f6c8.mjs';
2
2
  import 'node:url';
3
3
  import 'unplugin';
4
4
  import 'unplugin-ast';
@@ -6,7 +6,6 @@ import 'ufo';
6
6
  import 'node:vm';
7
7
  import 'unhead';
8
8
  import 'magic-string';
9
- import 'estree-walker';
10
9
  import 'mlly';
11
10
 
12
11
  const vite = () => {
package/dist/webpack.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const UseSeoMetaTransform = require('./shared/addons.1b8e1760.cjs');
3
+ const UseSeoMetaTransform = require('./shared/addons.cdb14016.cjs');
4
4
  require('node:url');
5
5
  require('unplugin');
6
6
  require('unplugin-ast');
@@ -8,7 +8,6 @@ require('ufo');
8
8
  require('node:vm');
9
9
  require('unhead');
10
10
  require('magic-string');
11
- require('estree-walker');
12
11
  require('mlly');
13
12
 
14
13
  const webpack = () => {
package/dist/webpack.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { T as TreeshakeServerComposables, U as UseSeoMetaTransform } from './shared/addons.6ad24ce7.mjs';
1
+ import { T as TreeshakeServerComposables, U as UseSeoMetaTransform } from './shared/addons.f060f6c8.mjs';
2
2
  import 'node:url';
3
3
  import 'unplugin';
4
4
  import 'unplugin-ast';
@@ -6,7 +6,6 @@ import 'ufo';
6
6
  import 'node:vm';
7
7
  import 'unhead';
8
8
  import 'magic-string';
9
- import 'estree-walker';
10
9
  import 'mlly';
11
10
 
12
11
  const webpack = () => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unhead/addons",
3
3
  "type": "module",
4
- "version": "1.1.5",
4
+ "version": "1.1.6",
5
5
  "packageManager": "pnpm@7.27.1",
6
6
  "author": "Harlan Wilton <harlan@harlanzw.com>",
7
7
  "license": "MIT",
@@ -51,18 +51,18 @@
51
51
  ],
52
52
  "dependencies": {
53
53
  "@rollup/pluginutils": "^5.0.2",
54
- "estree-walker": "^3.0.3",
55
54
  "magic-string": "^0.30.0",
56
55
  "mlly": "^1.1.1",
57
56
  "ufo": "^1.1.0",
58
57
  "unplugin": "^1.1.0",
59
58
  "unplugin-ast": "^0.7.0",
60
- "@unhead/schema": "1.1.5",
61
- "@unhead/shared": "1.1.5",
62
- "unhead": "1.1.5"
59
+ "@unhead/schema": "1.1.6",
60
+ "@unhead/shared": "1.1.6",
61
+ "unhead": "1.1.6"
63
62
  },
64
63
  "devDependencies": {
65
- "@babel/types": "^7.21.0"
64
+ "@babel/types": "^7.21.2",
65
+ "estree-walker": "^3.0.3"
66
66
  },
67
67
  "scripts": {
68
68
  "build": "unbuild .",