solid-refresh 0.7.2 → 0.7.4

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/babel.cjs CHANGED
@@ -1,8 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var path = require('path');
4
3
  var t = require('@babel/types');
5
- var helperModuleImports = require('@babel/helper-module-imports');
4
+ var path = require('path');
6
5
  var _generator = require('@babel/generator');
7
6
 
8
7
  function _interopNamespaceDefault(e) {
@@ -24,193 +23,6 @@ function _interopNamespaceDefault(e) {
24
23
 
25
24
  var t__namespace = /*#__PURE__*/_interopNamespaceDefault(t);
26
25
 
27
- /**
28
- * Copyright (c) 2019 Jason Dent
29
- * https://github.com/Jason3S/xxhash
30
- */
31
- const PRIME32_1 = 2654435761;
32
- const PRIME32_2 = 2246822519;
33
- const PRIME32_3 = 3266489917;
34
- const PRIME32_4 = 668265263;
35
- const PRIME32_5 = 374761393;
36
- function toUtf8(text) {
37
- const bytes = [];
38
- for (let i = 0, n = text.length; i < n; ++i) {
39
- const c = text.charCodeAt(i);
40
- if (c < 0x80) {
41
- bytes.push(c);
42
- }
43
- else if (c < 0x800) {
44
- bytes.push(0xc0 | (c >> 6), 0x80 | (c & 0x3f));
45
- }
46
- else if (c < 0xd800 || c >= 0xe000) {
47
- bytes.push(0xe0 | (c >> 12), 0x80 | ((c >> 6) & 0x3f), 0x80 | (c & 0x3f));
48
- }
49
- else {
50
- const cp = 0x10000 + (((c & 0x3ff) << 10) | (text.charCodeAt(++i) & 0x3ff));
51
- bytes.push(0xf0 | ((cp >> 18) & 0x7), 0x80 | ((cp >> 12) & 0x3f), 0x80 | ((cp >> 6) & 0x3f), 0x80 | (cp & 0x3f));
52
- }
53
- }
54
- return new Uint8Array(bytes);
55
- }
56
- /**
57
- *
58
- * @param buffer - byte array or string
59
- * @param seed - optional seed (32-bit unsigned);
60
- */
61
- function xxHash32(buffer, seed = 0) {
62
- buffer = typeof buffer === 'string' ? toUtf8(buffer) : buffer;
63
- const b = buffer;
64
- /*
65
- Step 1. Initialize internal accumulators
66
- Each accumulator gets an initial value based on optional seed input. Since the seed is optional, it can be 0.
67
- ```
68
- u32 acc1 = seed + PRIME32_1 + PRIME32_2;
69
- u32 acc2 = seed + PRIME32_2;
70
- u32 acc3 = seed + 0;
71
- u32 acc4 = seed - PRIME32_1;
72
- ```
73
- Special case : input is less than 16 bytes
74
- When input is too small (< 16 bytes), the algorithm will not process any stripe. Consequently, it will not
75
- make use of parallel accumulators.
76
- In which case, a simplified initialization is performed, using a single accumulator :
77
- u32 acc = seed + PRIME32_5;
78
- The algorithm then proceeds directly to step 4.
79
- */
80
- let acc = (seed + PRIME32_5) & 0xffffffff;
81
- let offset = 0;
82
- if (b.length >= 16) {
83
- const accN = [
84
- (seed + PRIME32_1 + PRIME32_2) & 0xffffffff,
85
- (seed + PRIME32_2) & 0xffffffff,
86
- (seed + 0) & 0xffffffff,
87
- (seed - PRIME32_1) & 0xffffffff,
88
- ];
89
- /*
90
- Step 2. Process stripes
91
- A stripe is a contiguous segment of 16 bytes. It is evenly divided into 4 lanes, of 4 bytes each.
92
- The first lane is used to update accumulator 1, the second lane is used to update accumulator 2, and so on.
93
- Each lane read its associated 32-bit value using little-endian convention.
94
- For each {lane, accumulator}, the update process is called a round, and applies the following formula :
95
- ```
96
- accN = accN + (laneN * PRIME32_2);
97
- accN = accN <<< 13;
98
- accN = accN * PRIME32_1;
99
- ```
100
- This shuffles the bits so that any bit from input lane impacts several bits in output accumulator.
101
- All operations are performed modulo 2^32.
102
- Input is consumed one full stripe at a time. Step 2 is looped as many times as necessary to consume
103
- the whole input, except the last remaining bytes which cannot form a stripe (< 16 bytes). When that
104
- happens, move to step 3.
105
- */
106
- const b = buffer;
107
- const limit = b.length - 16;
108
- let lane = 0;
109
- for (offset = 0; (offset & 0xfffffff0) <= limit; offset += 4) {
110
- const i = offset;
111
- const laneN0 = b[i + 0] + (b[i + 1] << 8);
112
- const laneN1 = b[i + 2] + (b[i + 3] << 8);
113
- const laneNP = laneN0 * PRIME32_2 + ((laneN1 * PRIME32_2) << 16);
114
- let acc = (accN[lane] + laneNP) & 0xffffffff;
115
- acc = (acc << 13) | (acc >>> 19);
116
- const acc0 = acc & 0xffff;
117
- const acc1 = acc >>> 16;
118
- accN[lane] = (acc0 * PRIME32_1 + ((acc1 * PRIME32_1) << 16)) & 0xffffffff;
119
- lane = (lane + 1) & 0x3;
120
- }
121
- /*
122
- Step 3. Accumulator convergence
123
- All 4 lane accumulators from previous steps are merged to produce a single remaining accumulator
124
- of same width (32-bit). The associated formula is as follows :
125
- ```
126
- acc = (acc1 <<< 1) + (acc2 <<< 7) + (acc3 <<< 12) + (acc4 <<< 18);
127
- ```
128
- */
129
- acc =
130
- (((accN[0] << 1) | (accN[0] >>> 31)) +
131
- ((accN[1] << 7) | (accN[1] >>> 25)) +
132
- ((accN[2] << 12) | (accN[2] >>> 20)) +
133
- ((accN[3] << 18) | (accN[3] >>> 14))) &
134
- 0xffffffff;
135
- }
136
- /*
137
- Step 4. Add input length
138
- The input total length is presumed known at this stage. This step is just about adding the length to
139
- accumulator, so that it participates to final mixing.
140
- ```
141
- acc = acc + (u32)inputLength;
142
- ```
143
- */
144
- acc = (acc + buffer.length) & 0xffffffff;
145
- /*
146
- Step 5. Consume remaining input
147
- There may be up to 15 bytes remaining to consume from the input. The final stage will digest them according
148
- to following pseudo-code :
149
- ```
150
- while (remainingLength >= 4) {
151
- lane = read_32bit_little_endian(input_ptr);
152
- acc = acc + lane * PRIME32_3;
153
- acc = (acc <<< 17) * PRIME32_4;
154
- input_ptr += 4; remainingLength -= 4;
155
- }
156
- ```
157
- This process ensures that all input bytes are present in the final mix.
158
- */
159
- const limit = buffer.length - 4;
160
- for (; offset <= limit; offset += 4) {
161
- const i = offset;
162
- const laneN0 = b[i + 0] + (b[i + 1] << 8);
163
- const laneN1 = b[i + 2] + (b[i + 3] << 8);
164
- const laneP = laneN0 * PRIME32_3 + ((laneN1 * PRIME32_3) << 16);
165
- acc = (acc + laneP) & 0xffffffff;
166
- acc = (acc << 17) | (acc >>> 15);
167
- acc =
168
- ((acc & 0xffff) * PRIME32_4 + (((acc >>> 16) * PRIME32_4) << 16)) &
169
- 0xffffffff;
170
- }
171
- /*
172
- ```
173
- while (remainingLength >= 1) {
174
- lane = read_byte(input_ptr);
175
- acc = acc + lane * PRIME32_5;
176
- acc = (acc <<< 11) * PRIME32_1;
177
- input_ptr += 1; remainingLength -= 1;
178
- }
179
- ```
180
- */
181
- for (; offset < b.length; ++offset) {
182
- const lane = b[offset];
183
- acc = acc + lane * PRIME32_5;
184
- acc = (acc << 11) | (acc >>> 21);
185
- acc =
186
- ((acc & 0xffff) * PRIME32_1 + (((acc >>> 16) * PRIME32_1) << 16)) &
187
- 0xffffffff;
188
- }
189
- /*
190
- Step 6. Final mix (avalanche)
191
- The final mix ensures that all input bits have a chance to impact any bit in the output digest,
192
- resulting in an unbiased distribution. This is also called avalanche effect.
193
- ```
194
- acc = acc xor (acc >> 15);
195
- acc = acc * PRIME32_2;
196
- acc = acc xor (acc >> 13);
197
- acc = acc * PRIME32_3;
198
- acc = acc xor (acc >> 16);
199
- ```
200
- */
201
- acc = acc ^ (acc >>> 15);
202
- acc =
203
- (((acc & 0xffff) * PRIME32_2) & 0xffffffff) +
204
- (((acc >>> 16) * PRIME32_2) << 16);
205
- acc = acc ^ (acc >>> 13);
206
- acc =
207
- (((acc & 0xffff) * PRIME32_3) & 0xffffffff) +
208
- (((acc >>> 16) * PRIME32_3) << 16);
209
- acc = acc ^ (acc >>> 16);
210
- // turn any negatives back into a positive number;
211
- return acc < 0 ? acc + 4294967296 : acc;
212
- }
213
-
214
26
  // This is just a Pascal heuristic
215
27
  // we only assume a function is a component
216
28
  // if the first character is in uppercase
@@ -224,71 +36,6 @@ function getImportSpecifierName(specifier) {
224
36
  return specifier.imported.value;
225
37
  }
226
38
 
227
- function registerImportSpecifier(state, id, specifier) {
228
- if (t__namespace.isImportDefaultSpecifier(specifier)) {
229
- if (id.definition.kind === 'default') {
230
- state.registrations.identifiers.set(specifier.local, id);
231
- }
232
- return;
233
- }
234
- if (t__namespace.isImportSpecifier(specifier)) {
235
- if ((id.definition.kind === 'named' &&
236
- getImportSpecifierName(specifier) === id.definition.name) ||
237
- (id.definition.kind === 'default' &&
238
- getImportSpecifierName(specifier) === 'default')) {
239
- state.registrations.identifiers.set(specifier.local, id);
240
- }
241
- return;
242
- }
243
- let current = state.registrations.namespaces.get(specifier.local);
244
- if (!current) {
245
- current = [];
246
- }
247
- current.push(id);
248
- state.registrations.namespaces.set(specifier.local, current);
249
- }
250
- function registerImportSpecifiers(state, path, definitions) {
251
- for (let i = 0, len = definitions.length; i < len; i++) {
252
- const id = definitions[i];
253
- if (path.node.source.value === id.definition.source) {
254
- for (let k = 0, klen = path.node.specifiers.length; k < klen; k++) {
255
- registerImportSpecifier(state, id, path.node.specifiers[k]);
256
- }
257
- }
258
- }
259
- }
260
-
261
- function getHotIdentifier(state) {
262
- switch (state.bundler) {
263
- // vite/esm uses `import.meta.hot`
264
- case 'esm':
265
- case 'vite':
266
- return t__namespace.memberExpression(t__namespace.memberExpression(t__namespace.identifier('import'), t__namespace.identifier('meta')), t__namespace.identifier('hot'));
267
- // webpack 5 uses `import.meta.webpackHot`
268
- // rspack does as well
269
- case 'webpack5':
270
- case 'rspack-esm':
271
- return t__namespace.memberExpression(t__namespace.memberExpression(t__namespace.identifier('import'), t__namespace.identifier('meta')), t__namespace.identifier('webpackHot'));
272
- default:
273
- // `module.hot` is the default.
274
- return t__namespace.memberExpression(t__namespace.identifier('module'), t__namespace.identifier('hot'));
275
- }
276
- }
277
-
278
- function getImportIdentifier(state, path, registration) {
279
- const name = registration.kind === 'named' ? registration.name : 'default';
280
- const target = `${registration.source}[${name}]`;
281
- const current = state.imports.get(target);
282
- if (current) {
283
- return current;
284
- }
285
- const newID = registration.kind === 'named'
286
- ? helperModuleImports.addNamed(path, registration.name, registration.source)
287
- : helperModuleImports.addDefault(path, registration.source);
288
- state.imports.set(target, newID);
289
- return newID;
290
- }
291
-
292
39
  // Source of solid-refresh (for import)
293
40
  const SOLID_REFRESH_MODULE = 'solid-refresh';
294
41
  // Exported names from solid-refresh that will be imported
@@ -344,6 +91,54 @@ const IMPORT_SPECIFIERS = [
344
91
  },
345
92
  ];
346
93
 
94
+ function getHotIdentifier(state) {
95
+ switch (state.bundler) {
96
+ // vite/esm uses `import.meta.hot`
97
+ case 'esm':
98
+ case 'vite':
99
+ return t__namespace.memberExpression(t__namespace.memberExpression(t__namespace.identifier('import'), t__namespace.identifier('meta')), t__namespace.identifier('hot'));
100
+ // webpack 5 uses `import.meta.webpackHot`
101
+ // rspack does as well
102
+ case 'webpack5':
103
+ case 'rspack-esm':
104
+ return t__namespace.memberExpression(t__namespace.memberExpression(t__namespace.identifier('import'), t__namespace.identifier('meta')), t__namespace.identifier('webpackHot'));
105
+ default:
106
+ // `module.hot` is the default.
107
+ return t__namespace.memberExpression(t__namespace.identifier('module'), t__namespace.identifier('hot'));
108
+ }
109
+ }
110
+
111
+ function getImportIdentifier(state, path, registration) {
112
+ const name = registration.kind === 'named' ? registration.name : 'default';
113
+ const target = `${registration.source}[${name}]`;
114
+ const current = state.imports.get(target);
115
+ if (current) {
116
+ return current;
117
+ }
118
+ const programParent = path.scope.getProgramParent();
119
+ const uid = programParent.generateUidIdentifier(registration.kind === 'named' ? registration.name : 'default');
120
+ const newPath = programParent.path.unshiftContainer('body', t__namespace.importDeclaration([
121
+ registration.kind === 'named'
122
+ ? t__namespace.importSpecifier(uid, t__namespace.identifier(registration.name))
123
+ : t__namespace.importDefaultSpecifier(uid),
124
+ ], t__namespace.stringLiteral(registration.source)))[0];
125
+ programParent.registerDeclaration(newPath);
126
+ state.imports.set(target, uid);
127
+ return uid;
128
+ }
129
+
130
+ function getRootStatementPath(path) {
131
+ let current = path.parentPath;
132
+ while (current) {
133
+ const next = current.parentPath;
134
+ if (next && t__namespace.isProgram(next.node)) {
135
+ return current;
136
+ }
137
+ current = next;
138
+ }
139
+ return path;
140
+ }
141
+
347
142
  function generateViteHMRRequirement(state, statements, pathToHot) {
348
143
  if (state.bundler === 'vite') {
349
144
  // Vite requires that the owner module has an `import.meta.hot.accept()` call
@@ -351,16 +146,44 @@ function generateViteHMRRequirement(state, statements, pathToHot) {
351
146
  }
352
147
  }
353
148
 
354
- function getHMRDeclineCall(state, path) {
149
+ const REGISTRY = 'REGISTRY';
150
+ function createRegistry(state, path) {
151
+ const current = state.imports.get(REGISTRY);
152
+ if (current) {
153
+ return current;
154
+ }
155
+ const root = getRootStatementPath(path);
156
+ const identifier = path.scope.generateUidIdentifier(REGISTRY);
157
+ const [tmp] = root.insertBefore(t__namespace.variableDeclaration('const', [
158
+ t__namespace.variableDeclarator(identifier, t__namespace.callExpression(getImportIdentifier(state, path, IMPORT_REGISTRY), [])),
159
+ ]));
160
+ root.scope.registerDeclaration(tmp);
355
161
  const pathToHot = getHotIdentifier(state);
356
162
  const statements = [
357
- t__namespace.expressionStatement(t__namespace.callExpression(getImportIdentifier(state, path, IMPORT_DECLINE), [
163
+ t__namespace.expressionStatement(t__namespace.callExpression(getImportIdentifier(state, path, IMPORT_REFRESH), [
358
164
  t__namespace.stringLiteral(state.bundler),
359
165
  pathToHot,
166
+ identifier,
360
167
  ])),
361
168
  ];
362
169
  generateViteHMRRequirement(state, statements, pathToHot);
363
- return t__namespace.ifStatement(pathToHot, t__namespace.blockStatement(statements));
170
+ path.scope.getProgramParent().path.pushContainer('body', [
171
+ t__namespace.ifStatement(pathToHot, t__namespace.blockStatement(statements)),
172
+ ]);
173
+ state.imports.set(REGISTRY, identifier);
174
+ return identifier;
175
+ }
176
+
177
+ // https://github.com/babel/babel/issues/15269
178
+ let generator;
179
+ if (typeof _generator !== 'function') {
180
+ generator = _generator.default;
181
+ }
182
+ else {
183
+ generator = _generator;
184
+ }
185
+ function generateCode(node) {
186
+ return generator(node).code;
364
187
  }
365
188
 
366
189
  function isPathValid(path, key) {
@@ -390,6 +213,80 @@ function unwrapNode(node, key) {
390
213
  return undefined;
391
214
  }
392
215
 
216
+ function isForeignBinding(source, current, name) {
217
+ if (source === current) {
218
+ return true;
219
+ }
220
+ if (current.scope.hasOwnBinding(name)) {
221
+ return false;
222
+ }
223
+ if (current.parentPath) {
224
+ return isForeignBinding(source, current.parentPath, name);
225
+ }
226
+ return true;
227
+ }
228
+ function isInTypescript(path) {
229
+ let parent = path.parentPath;
230
+ while (parent) {
231
+ if (t__namespace.isTypeScript(parent.node) && !t__namespace.isExpression(parent.node)) {
232
+ return true;
233
+ }
234
+ parent = parent.parentPath;
235
+ }
236
+ return false;
237
+ }
238
+ function getForeignBindings(path) {
239
+ const identifiers = new Set();
240
+ path.traverse({
241
+ ReferencedIdentifier(p) {
242
+ // Check identifiers that aren't in a TS expression
243
+ if (!isInTypescript(p) && isForeignBinding(path, p, p.node.name)) {
244
+ if (isPathValid(p, t__namespace.isIdentifier) ||
245
+ isPathValid(p.parentPath, t__namespace.isJSXMemberExpression)) {
246
+ identifiers.add(p.node.name);
247
+ }
248
+ }
249
+ },
250
+ });
251
+ const collected = [];
252
+ for (const identifier of identifiers) {
253
+ collected.push(t__namespace.identifier(identifier));
254
+ }
255
+ return collected;
256
+ }
257
+
258
+ function getHMRDeclineCall(state, path) {
259
+ const pathToHot = getHotIdentifier(state);
260
+ const statements = [
261
+ t__namespace.expressionStatement(t__namespace.callExpression(getImportIdentifier(state, path, IMPORT_DECLINE), [
262
+ t__namespace.stringLiteral(state.bundler),
263
+ pathToHot,
264
+ ])),
265
+ ];
266
+ generateViteHMRRequirement(state, statements, pathToHot);
267
+ return t__namespace.ifStatement(pathToHot, t__namespace.blockStatement(statements));
268
+ }
269
+
270
+ function getStatementPath(path) {
271
+ if (t__namespace.isStatement(path.node)) {
272
+ return path;
273
+ }
274
+ if (path.parentPath) {
275
+ return getStatementPath(path.parentPath);
276
+ }
277
+ return null;
278
+ }
279
+
280
+ function isStatementTopLevel(path) {
281
+ let blockParent = path.scope.getBlockParent();
282
+ const programParent = path.scope.getProgramParent();
283
+ // a FunctionDeclaration binding refers to itself as the block parent
284
+ if (blockParent.path === path) {
285
+ blockParent = blockParent.parent;
286
+ }
287
+ return programParent === blockParent;
288
+ }
289
+
393
290
  function isIdentifierValidCallee(state, path, callee, target) {
394
291
  const binding = path.scope.getBindingIdentifier(callee.name);
395
292
  if (binding) {
@@ -449,94 +346,56 @@ function isValidCallee(state, path, { callee }, target) {
449
346
  return false;
450
347
  }
451
348
 
452
- function getStatementPath(path) {
453
- if (t__namespace.isStatement(path.node)) {
454
- return path;
455
- }
456
- if (path.parentPath) {
457
- return getStatementPath(path.parentPath);
458
- }
459
- return null;
460
- }
461
-
462
- function getRootStatementPath(path) {
463
- let current = path.parentPath;
464
- while (current) {
465
- const next = current.parentPath;
466
- if (next && t__namespace.isProgram(next.node)) {
467
- return current;
349
+ function registerImportSpecifier(state, id, specifier) {
350
+ if (t__namespace.isImportDefaultSpecifier(specifier)) {
351
+ if (id.definition.kind === 'default') {
352
+ state.registrations.identifiers.set(specifier.local, id);
468
353
  }
469
- current = next;
470
- }
471
- return path;
472
- }
473
-
474
- const REGISTRY = 'REGISTRY';
475
- function createRegistry(state, path) {
476
- const current = state.imports.get(REGISTRY);
477
- if (current) {
478
- return current;
479
- }
480
- const root = getRootStatementPath(path);
481
- const identifier = path.scope.generateUidIdentifier(REGISTRY);
482
- root.insertBefore(t__namespace.variableDeclaration('const', [
483
- t__namespace.variableDeclarator(identifier, t__namespace.callExpression(getImportIdentifier(state, path, IMPORT_REGISTRY), [])),
484
- ]));
485
- const pathToHot = getHotIdentifier(state);
486
- const statements = [
487
- t__namespace.expressionStatement(t__namespace.callExpression(getImportIdentifier(state, path, IMPORT_REFRESH), [
488
- t__namespace.stringLiteral(state.bundler),
489
- pathToHot,
490
- identifier,
491
- ])),
492
- ];
493
- generateViteHMRRequirement(state, statements, pathToHot);
494
- path.scope.getProgramParent().path.pushContainer('body', [
495
- t__namespace.ifStatement(pathToHot, t__namespace.blockStatement(statements)),
496
- ]);
497
- state.imports.set(REGISTRY, identifier);
498
- return identifier;
499
- }
500
-
501
- function isForeignBinding(source, current, name) {
502
- if (source === current) {
503
- return true;
354
+ return;
504
355
  }
505
- if (current.scope.hasOwnBinding(name)) {
506
- return false;
356
+ if (t__namespace.isImportSpecifier(specifier)) {
357
+ if (specifier.importKind === 'type' || specifier.importKind === 'typeof') {
358
+ return;
359
+ }
360
+ const name = getImportSpecifierName(specifier);
361
+ if ((id.definition.kind === 'named' && name === id.definition.name) ||
362
+ (id.definition.kind === 'default' && name === 'default')) {
363
+ state.registrations.identifiers.set(specifier.local, id);
364
+ }
365
+ return;
507
366
  }
508
- if (current.parentPath) {
509
- return isForeignBinding(source, current.parentPath, name);
367
+ let current = state.registrations.namespaces.get(specifier.local);
368
+ if (!current) {
369
+ current = [];
510
370
  }
511
- return true;
371
+ current.push(id);
372
+ state.registrations.namespaces.set(specifier.local, current);
512
373
  }
513
- function isInTypescript(path) {
514
- let parent = path.parentPath;
515
- while (parent) {
516
- if (t__namespace.isTypeScript(parent.node) && !t__namespace.isExpression(parent.node)) {
517
- return true;
374
+ function registerImportSpecifiers(state, path, definitions) {
375
+ for (let i = 0, len = definitions.length; i < len; i++) {
376
+ const id = definitions[i];
377
+ if (path.node.source.value === id.definition.source) {
378
+ for (let k = 0, klen = path.node.specifiers.length; k < klen; k++) {
379
+ registerImportSpecifier(state, id, path.node.specifiers[k]);
380
+ }
518
381
  }
519
- parent = parent.parentPath;
520
382
  }
521
- return false;
522
383
  }
523
- function getForeignBindings(path) {
524
- const identifiers = new Set();
525
- path.traverse({
526
- ReferencedIdentifier(p) {
527
- // Check identifiers that aren't in a TS expression
528
- if (!isInTypescript(p) && isForeignBinding(path, p, p.node.name)) {
529
- if (p.isIdentifier() || p.parentPath.isJSXMemberExpression()) {
530
- identifiers.add(p.node.name);
531
- }
532
- }
533
- },
534
- });
535
- const collected = [];
536
- for (const identifier of identifiers) {
537
- collected.push(t__namespace.identifier(identifier));
538
- }
539
- return collected;
384
+
385
+ function generateUniqueName(path, name) {
386
+ let uid;
387
+ let i = 1;
388
+ do {
389
+ uid = name + '_' + i;
390
+ i++;
391
+ } while (path.scope.hasLabel(uid) ||
392
+ path.scope.hasBinding(uid) ||
393
+ path.scope.hasGlobal(uid) ||
394
+ path.scope.hasReference(uid));
395
+ const program = path.scope.getProgramParent();
396
+ program.references[uid] = true;
397
+ program.uids[uid] = true;
398
+ return t__namespace.identifier(uid);
540
399
  }
541
400
 
542
401
  function getDescriptiveName(path, defaultName) {
@@ -573,22 +432,6 @@ function getDescriptiveName(path, defaultName) {
573
432
  return defaultName;
574
433
  }
575
434
 
576
- function generateUniqueName(path, name) {
577
- let uid;
578
- let i = 1;
579
- do {
580
- uid = name + '_' + i;
581
- i++;
582
- } while (path.scope.hasLabel(uid) ||
583
- path.scope.hasBinding(uid) ||
584
- path.scope.hasGlobal(uid) ||
585
- path.scope.hasReference(uid));
586
- const program = path.scope.getProgramParent();
587
- program.references[uid] = true;
588
- program.uids[uid] = true;
589
- return t__namespace.identifier(uid);
590
- }
591
-
592
435
  const REFRESH_JSX_SKIP = /^\s*@refresh jsx-skip\s*$/;
593
436
  function shouldSkipJSX(node) {
594
437
  // Node without leading comments shouldn't be skipped
@@ -700,6 +543,15 @@ function extractJSXExpressionsFromJSXElement(state, path) {
700
543
  if ((isPathValid(openingName, t__namespace.isJSXIdentifier) &&
701
544
  /^[A-Z_]/.test(openingName.node.name)) ||
702
545
  isPathValid(openingName, t__namespace.isJSXMemberExpression)) {
546
+ if (isPathValid(openingName, t__namespace.isJSXIdentifier)) {
547
+ const binding = path.scope.getBinding(openingName.node.name);
548
+ if (binding) {
549
+ const statementPath = binding.path.getStatementParent();
550
+ if (statementPath && isStatementTopLevel(statementPath)) {
551
+ return;
552
+ }
553
+ }
554
+ }
703
555
  const key = pushAttribute(state, convertJSXOpeningToExpression(openingName.node));
704
556
  const replacement = t__namespace.jsxMemberExpression(t__namespace.jsxIdentifier(state.props.name), t__namespace.jsxIdentifier(key));
705
557
  openingName.replaceWith(replacement);
@@ -765,20 +617,196 @@ function transformJSX(path) {
765
617
  if (path.node.loc) {
766
618
  templateComp.loc = path.node.loc;
767
619
  }
768
- rootPath.insertBefore(t__namespace.variableDeclaration('const', [t__namespace.variableDeclarator(id, templateComp)]));
620
+ const [tmp] = rootPath.insertBefore(t__namespace.variableDeclaration('const', [t__namespace.variableDeclarator(id, templateComp)]));
621
+ rootPath.scope.registerDeclaration(tmp);
769
622
  path.replaceWith(skippableJSX(t__namespace.jsxElement(t__namespace.jsxOpeningElement(t__namespace.jsxIdentifier(id.name), [...state.attributes], true), t__namespace.jsxClosingElement(t__namespace.jsxIdentifier(id.name)), [], true)));
770
623
  }
771
624
 
772
- // https://github.com/babel/babel/issues/15269
773
- let generator;
774
- if (typeof _generator !== 'function') {
775
- generator = _generator.default;
776
- }
777
- else {
778
- generator = _generator;
625
+ /**
626
+ * Copyright (c) 2019 Jason Dent
627
+ * https://github.com/Jason3S/xxhash
628
+ */
629
+ const PRIME32_1 = 2654435761;
630
+ const PRIME32_2 = 2246822519;
631
+ const PRIME32_3 = 3266489917;
632
+ const PRIME32_4 = 668265263;
633
+ const PRIME32_5 = 374761393;
634
+ function toUtf8(text) {
635
+ const bytes = [];
636
+ for (let i = 0, n = text.length; i < n; ++i) {
637
+ const c = text.charCodeAt(i);
638
+ if (c < 0x80) {
639
+ bytes.push(c);
640
+ }
641
+ else if (c < 0x800) {
642
+ bytes.push(0xc0 | (c >> 6), 0x80 | (c & 0x3f));
643
+ }
644
+ else if (c < 0xd800 || c >= 0xe000) {
645
+ bytes.push(0xe0 | (c >> 12), 0x80 | ((c >> 6) & 0x3f), 0x80 | (c & 0x3f));
646
+ }
647
+ else {
648
+ const cp = 0x10000 + (((c & 0x3ff) << 10) | (text.charCodeAt(++i) & 0x3ff));
649
+ bytes.push(0xf0 | ((cp >> 18) & 0x7), 0x80 | ((cp >> 12) & 0x3f), 0x80 | ((cp >> 6) & 0x3f), 0x80 | (cp & 0x3f));
650
+ }
651
+ }
652
+ return new Uint8Array(bytes);
779
653
  }
780
- function generateCode(node) {
781
- return generator(node).code;
654
+ /**
655
+ *
656
+ * @param buffer - byte array or string
657
+ * @param seed - optional seed (32-bit unsigned);
658
+ */
659
+ function xxHash32(buffer, seed = 0) {
660
+ buffer = typeof buffer === 'string' ? toUtf8(buffer) : buffer;
661
+ const b = buffer;
662
+ /*
663
+ Step 1. Initialize internal accumulators
664
+ Each accumulator gets an initial value based on optional seed input. Since the seed is optional, it can be 0.
665
+ ```
666
+ u32 acc1 = seed + PRIME32_1 + PRIME32_2;
667
+ u32 acc2 = seed + PRIME32_2;
668
+ u32 acc3 = seed + 0;
669
+ u32 acc4 = seed - PRIME32_1;
670
+ ```
671
+ Special case : input is less than 16 bytes
672
+ When input is too small (< 16 bytes), the algorithm will not process any stripe. Consequently, it will not
673
+ make use of parallel accumulators.
674
+ In which case, a simplified initialization is performed, using a single accumulator :
675
+ u32 acc = seed + PRIME32_5;
676
+ The algorithm then proceeds directly to step 4.
677
+ */
678
+ let acc = (seed + PRIME32_5) & 0xffffffff;
679
+ let offset = 0;
680
+ if (b.length >= 16) {
681
+ const accN = [
682
+ (seed + PRIME32_1 + PRIME32_2) & 0xffffffff,
683
+ (seed + PRIME32_2) & 0xffffffff,
684
+ (seed + 0) & 0xffffffff,
685
+ (seed - PRIME32_1) & 0xffffffff,
686
+ ];
687
+ /*
688
+ Step 2. Process stripes
689
+ A stripe is a contiguous segment of 16 bytes. It is evenly divided into 4 lanes, of 4 bytes each.
690
+ The first lane is used to update accumulator 1, the second lane is used to update accumulator 2, and so on.
691
+ Each lane read its associated 32-bit value using little-endian convention.
692
+ For each {lane, accumulator}, the update process is called a round, and applies the following formula :
693
+ ```
694
+ accN = accN + (laneN * PRIME32_2);
695
+ accN = accN <<< 13;
696
+ accN = accN * PRIME32_1;
697
+ ```
698
+ This shuffles the bits so that any bit from input lane impacts several bits in output accumulator.
699
+ All operations are performed modulo 2^32.
700
+ Input is consumed one full stripe at a time. Step 2 is looped as many times as necessary to consume
701
+ the whole input, except the last remaining bytes which cannot form a stripe (< 16 bytes). When that
702
+ happens, move to step 3.
703
+ */
704
+ const b = buffer;
705
+ const limit = b.length - 16;
706
+ let lane = 0;
707
+ for (offset = 0; (offset & 0xfffffff0) <= limit; offset += 4) {
708
+ const i = offset;
709
+ const laneN0 = b[i + 0] + (b[i + 1] << 8);
710
+ const laneN1 = b[i + 2] + (b[i + 3] << 8);
711
+ const laneNP = laneN0 * PRIME32_2 + ((laneN1 * PRIME32_2) << 16);
712
+ let acc = (accN[lane] + laneNP) & 0xffffffff;
713
+ acc = (acc << 13) | (acc >>> 19);
714
+ const acc0 = acc & 0xffff;
715
+ const acc1 = acc >>> 16;
716
+ accN[lane] = (acc0 * PRIME32_1 + ((acc1 * PRIME32_1) << 16)) & 0xffffffff;
717
+ lane = (lane + 1) & 0x3;
718
+ }
719
+ /*
720
+ Step 3. Accumulator convergence
721
+ All 4 lane accumulators from previous steps are merged to produce a single remaining accumulator
722
+ of same width (32-bit). The associated formula is as follows :
723
+ ```
724
+ acc = (acc1 <<< 1) + (acc2 <<< 7) + (acc3 <<< 12) + (acc4 <<< 18);
725
+ ```
726
+ */
727
+ acc =
728
+ (((accN[0] << 1) | (accN[0] >>> 31)) +
729
+ ((accN[1] << 7) | (accN[1] >>> 25)) +
730
+ ((accN[2] << 12) | (accN[2] >>> 20)) +
731
+ ((accN[3] << 18) | (accN[3] >>> 14))) &
732
+ 0xffffffff;
733
+ }
734
+ /*
735
+ Step 4. Add input length
736
+ The input total length is presumed known at this stage. This step is just about adding the length to
737
+ accumulator, so that it participates to final mixing.
738
+ ```
739
+ acc = acc + (u32)inputLength;
740
+ ```
741
+ */
742
+ acc = (acc + buffer.length) & 0xffffffff;
743
+ /*
744
+ Step 5. Consume remaining input
745
+ There may be up to 15 bytes remaining to consume from the input. The final stage will digest them according
746
+ to following pseudo-code :
747
+ ```
748
+ while (remainingLength >= 4) {
749
+ lane = read_32bit_little_endian(input_ptr);
750
+ acc = acc + lane * PRIME32_3;
751
+ acc = (acc <<< 17) * PRIME32_4;
752
+ input_ptr += 4; remainingLength -= 4;
753
+ }
754
+ ```
755
+ This process ensures that all input bytes are present in the final mix.
756
+ */
757
+ const limit = buffer.length - 4;
758
+ for (; offset <= limit; offset += 4) {
759
+ const i = offset;
760
+ const laneN0 = b[i + 0] + (b[i + 1] << 8);
761
+ const laneN1 = b[i + 2] + (b[i + 3] << 8);
762
+ const laneP = laneN0 * PRIME32_3 + ((laneN1 * PRIME32_3) << 16);
763
+ acc = (acc + laneP) & 0xffffffff;
764
+ acc = (acc << 17) | (acc >>> 15);
765
+ acc =
766
+ ((acc & 0xffff) * PRIME32_4 + (((acc >>> 16) * PRIME32_4) << 16)) &
767
+ 0xffffffff;
768
+ }
769
+ /*
770
+ ```
771
+ while (remainingLength >= 1) {
772
+ lane = read_byte(input_ptr);
773
+ acc = acc + lane * PRIME32_5;
774
+ acc = (acc <<< 11) * PRIME32_1;
775
+ input_ptr += 1; remainingLength -= 1;
776
+ }
777
+ ```
778
+ */
779
+ for (; offset < b.length; ++offset) {
780
+ const lane = b[offset];
781
+ acc = acc + lane * PRIME32_5;
782
+ acc = (acc << 11) | (acc >>> 21);
783
+ acc =
784
+ ((acc & 0xffff) * PRIME32_1 + (((acc >>> 16) * PRIME32_1) << 16)) &
785
+ 0xffffffff;
786
+ }
787
+ /*
788
+ Step 6. Final mix (avalanche)
789
+ The final mix ensures that all input bits have a chance to impact any bit in the output digest,
790
+ resulting in an unbiased distribution. This is also called avalanche effect.
791
+ ```
792
+ acc = acc xor (acc >> 15);
793
+ acc = acc * PRIME32_2;
794
+ acc = acc xor (acc >> 13);
795
+ acc = acc * PRIME32_3;
796
+ acc = acc xor (acc >> 16);
797
+ ```
798
+ */
799
+ acc = acc ^ (acc >>> 15);
800
+ acc =
801
+ (((acc & 0xffff) * PRIME32_2) & 0xffffffff) +
802
+ (((acc >>> 16) * PRIME32_2) << 16);
803
+ acc = acc ^ (acc >>> 13);
804
+ acc =
805
+ (((acc & 0xffff) * PRIME32_3) & 0xffffffff) +
806
+ (((acc >>> 16) * PRIME32_3) << 16);
807
+ acc = acc ^ (acc >>> 16);
808
+ // turn any negatives back into a positive number;
809
+ return acc < 0 ? acc + 4294967296 : acc;
782
810
  }
783
811
 
784
812
  const CWD = process.cwd();
@@ -793,7 +821,7 @@ function createSignatureValue(node) {
793
821
  function captureIdentifiers(state, path) {
794
822
  path.traverse({
795
823
  ImportDeclaration(p) {
796
- if (p.node.importKind === 'value') {
824
+ if (!(p.node.importKind === 'type' || p.node.importKind === 'typeof')) {
797
825
  registerImportSpecifiers(state, p, state.specifiers);
798
826
  }
799
827
  },
@@ -897,14 +925,8 @@ function setupProgram(state, path, comments) {
897
925
  }
898
926
  return isDone;
899
927
  }
900
- function isStatementTopLevel(path) {
901
- let blockParent = path.scope.getBlockParent();
902
- const programParent = path.scope.getProgramParent();
903
- // a FunctionDeclaration binding refers to itself as the block parent
904
- if (blockParent.path === path) {
905
- blockParent = blockParent.parent;
906
- }
907
- return programParent === blockParent;
928
+ function isValidFunction(node) {
929
+ return t__namespace.isArrowFunctionExpression(node) || t__namespace.isFunctionExpression(node);
908
930
  }
909
931
  function transformVariableDeclarator(state, path) {
910
932
  if (path.parentPath.isVariableDeclaration() &&
@@ -917,8 +939,7 @@ function transformVariableDeclarator(state, path) {
917
939
  return;
918
940
  }
919
941
  if (isComponentishName(identifier.name)) {
920
- const trueFuncExpr = unwrapNode(init, t__namespace.isFunctionExpression) ||
921
- unwrapNode(init, t__namespace.isArrowFunctionExpression);
942
+ const trueFuncExpr = unwrapNode(init, isValidFunction);
922
943
  // Check for valid FunctionExpression or ArrowFunctionExpression
923
944
  if (trueFuncExpr &&
924
945
  // Must not be async or generator
@@ -950,9 +971,10 @@ function transformFunctionDeclaration(state, path) {
950
971
  // Might be component-like, but the only valid components
951
972
  // have zero or one parameter
952
973
  decl.params.length < 2) {
953
- path.replaceWith(t__namespace.variableDeclaration('const', [
974
+ const [tmp] = path.replaceWith(t__namespace.variableDeclaration('const', [
954
975
  t__namespace.variableDeclarator(decl.id, wrapComponent(state, path, decl.id, t__namespace.functionExpression(decl.id, decl.params, decl.body), decl)),
955
976
  ]));
977
+ path.scope.registerDeclaration(tmp);
956
978
  path.skip();
957
979
  }
958
980
  }
@@ -972,6 +994,7 @@ function bubbleFunctionDeclaration(program, path) {
972
994
  decl.params.length < 2) {
973
995
  const first = program.get('body')[0];
974
996
  const [tmp] = first.insertBefore(decl);
997
+ program.scope.registerDeclaration(tmp);
975
998
  tmp.skip();
976
999
  if (path.parentPath.isExportNamedDeclaration()) {
977
1000
  path.parentPath.replaceWith(t__namespace.exportNamedDeclaration(undefined, [
@@ -1014,7 +1037,6 @@ function solidRefreshPlugin() {
1014
1037
  bubbleFunctionDeclaration(programPath, path);
1015
1038
  },
1016
1039
  });
1017
- programPath.scope.crawl();
1018
1040
  programPath.traverse({
1019
1041
  JSXElement(path) {
1020
1042
  transformJSX(path);
@@ -1023,7 +1045,6 @@ function solidRefreshPlugin() {
1023
1045
  transformJSX(path);
1024
1046
  },
1025
1047
  });
1026
- programPath.scope.crawl();
1027
1048
  programPath.traverse({
1028
1049
  VariableDeclarator(path) {
1029
1050
  transformVariableDeclarator(state, path);
@@ -1032,7 +1053,6 @@ function solidRefreshPlugin() {
1032
1053
  transformFunctionDeclaration(state, path);
1033
1054
  },
1034
1055
  });
1035
- programPath.scope.crawl();
1036
1056
  },
1037
1057
  },
1038
1058
  };