This commit is contained in:
2025-06-16 13:37:14 +02:00
parent ac273655e6
commit a8b82208f7
5100 changed files with 737524 additions and 2 deletions

65
node_modules/@prefresh/babel-plugin/CHANGELOG.md generated vendored Normal file
View File

@@ -0,0 +1,65 @@
# @prefresh/babel-plugin
## 0.5.1
### Patch Changes
- [`7e47061`](https://github.com/preactjs/prefresh/commit/7e470614e70915e994937e97245df9914806be86) [#524](https://github.com/preactjs/prefresh/pull/524) Thanks [@JoviDeCroock](https://github.com/JoviDeCroock)! - Ensure we accoutn for the filename when hashing
## 0.5.0
### Minor Changes
- [`e641c69`](https://github.com/preactjs/prefresh/commit/e641c69c610c3adeeb5dcb9e912d030a6fbb5229) [#499](https://github.com/preactjs/prefresh/pull/499) Thanks [@JoviDeCroock](https://github.com/JoviDeCroock)! - Publish automatically with npm provenance enabled
* [`35e18f7`](https://github.com/preactjs/prefresh/commit/35e18f719cf17415e33cd2ac0ed83031b1f62b44) [#488](https://github.com/preactjs/prefresh/pull/488) Thanks [@JoviDeCroock](https://github.com/JoviDeCroock)! - Transform createContext calls that we find within a closure to make them unique
## 0.4.4
### Patch Changes
- [`01bf615`](https://github.com/preactjs/prefresh/commit/01bf615f99f8615d892883c6e47d4f0c94822e89) [#477](https://github.com/preactjs/prefresh/pull/477) Thanks [@JoviDeCroock](https://github.com/JoviDeCroock)! - Support contexts nested in an object
## 0.4.2
### Patch Changes
- [`eb9aa93`](https://github.com/preactjs/prefresh/commit/eb9aa932fc2a01fed3ecb662e195422986529419) [#425](https://github.com/preactjs/prefresh/pull/425) Thanks [@JoviDeCroock](https://github.com/JoviDeCroock)! - Fix import paths for newer node versions
* [`f39ab40`](https://github.com/preactjs/prefresh/commit/f39ab409a46a7a06f8e892920e407be728fcefa1) [#432](https://github.com/preactjs/prefresh/pull/432) Thanks [@jvdsande](https://github.com/jvdsande)! - Allow curried HOC and any parameter order for HOC
## 0.4.1
### Patch Changes
- [`010f21b`](https://github.com/preactjs/prefresh/commit/010f21b947d0cdee59fac6af6a17d10cb6a696b5) [#287](https://github.com/preactjs/prefresh/pull/287) Thanks [@JoviDeCroock](https://github.com/JoviDeCroock)! - Fix createContext detection in React-Preact/compat
## 0.4.0
### Minor Changes
- [`21f8c43`](https://github.com/preactjs/prefresh/commit/21f8c4330a29edcb5d4493cda5465e6556a5f92c) [#243](https://github.com/preactjs/prefresh/pull/243) Thanks [@JoviDeCroock](https://github.com/JoviDeCroock)! - Invalidate useEffect/useLayoutEffect/useMemo/useCallback without resetting hook-state aggressively, now hook-state will only be reset for stateful hooks.
## 0.3.0
### Minor Changes
- [`c0835d5`](https://github.com/preactjs/prefresh/commit/c0835d5c5820809563ec768296a610b45d7dc0c0) [#233](https://github.com/preactjs/prefresh/pull/233) Thanks [@JoviDeCroock](https://github.com/JoviDeCroock)! - Calculate hashes for useEffect and useLayoutEffect
## 0.2.2
### Patch Changes
- [`39c60c5`](https://github.com/preactjs/prefresh/commit/39c60c5862adef106fed1ca59a968f40cdacdd10) [#195](https://github.com/preactjs/prefresh/pull/195) Thanks [@JoviDeCroock](https://github.com/JoviDeCroock)! - Fix syntax issue preventing babel from correctly assigning defaultValues
## 0.2.1
### Patch Changes
- [`53e79a8`](https://github.com/preactjs/prefresh/commit/53e79a8bcdf5ef3a9387e46307cfd0ce1a2a3186) [#193](https://github.com/preactjs/prefresh/pull/193) Thanks [@JoviDeCroock](https://github.com/JoviDeCroock)! - Fix bug where a TSAsExpression would make our transform behave funky
## 0.2.0
### Minor Changes
- [`430fe2c`](https://github.com/preactjs/prefresh/commit/430fe2c2b281b1973a74c542a38c1bb5be2a6559) [#185](https://github.com/preactjs/prefresh/pull/185) Thanks [@JoviDeCroock](https://github.com/JoviDeCroock)! - Add support for registering class components

22
node_modules/@prefresh/babel-plugin/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,22 @@
The MIT License (MIT)
Copyright (c) 2020-2021 Jovi De Croock
Copyright (c) 2021-Present Preact Team
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

6
node_modules/@prefresh/babel-plugin/README.md generated vendored Normal file
View File

@@ -0,0 +1,6 @@
# Prefresh-babel
[![npm version](https://badgen.net/npm/v/@prefresh/babel-plugin)](https://www.npmjs.com/package/@prefresh/babel-plugin)
This is a forked equivalent of [the react-refresh babel plugin](https://github.com/facebook/react/blob/master/packages/react-refresh/src/ReactFreshBabelPlugin.js)
difference being that we need a way to memoize createContext between HMR.

924
node_modules/@prefresh/babel-plugin/dist/src/index.js generated vendored Normal file
View File

@@ -0,0 +1,924 @@
function hash(str) {
let hash = 5381,
i = str.length;
while (i) {
hash = (hash * 33) ^ str.charCodeAt(--i);
}
return (hash >>> 0).toString(36);
}
/**
*
* Majority copied from https://github.com/facebook/react/blob/master/packages/react-refresh/src/ReactFreshBabelPlugin.js
*/
module.exports=function (babel, opts = {}) {
if (typeof babel.env === 'function') {
const env = babel.env();
if (env !== 'development' && !opts.skipEnvCheck) {
throw new Error(
'Prefresh Babel transform should only be enabled in development environment. ' +
'Instead, the environment is: "' +
env +
'". If you want to override this check, pass {skipEnvCheck: true} as plugin options.'
);
}
}
const { types: t, template } = babel;
const refreshReg = t.identifier(opts.refreshReg || '$RefreshReg$');
const refreshSig = t.identifier(opts.refreshSig || '$RefreshSig$');
const registrationsByProgramPath = new Map();
function createRegistration(programPath, persistentID) {
const handle = programPath.scope.generateUidIdentifier('c');
if (!registrationsByProgramPath.has(programPath)) {
registrationsByProgramPath.set(programPath, []);
}
const registrations = registrationsByProgramPath.get(programPath);
registrations.push({
handle,
persistentID,
});
return handle;
}
function isComponentishName(name) {
return typeof name === 'string' && name[0] >= 'A' && name[0] <= 'Z';
}
function findInnerComponents(inferredName, path, callback) {
const node = path.node;
switch (node.type) {
case 'Identifier': {
if (!isComponentishName(node.name)) {
return false;
}
// export default hoc(Foo)
// const X = hoc(Foo)
callback(inferredName, node, null);
return true;
}
case 'FunctionDeclaration': {
// function Foo() {}
// function Foo() {}
// module.exports=function Foo() {}
callback(inferredName, node.id, null);
return true;
}
case 'ArrowFunctionExpression': {
if (node.body.type === 'ArrowFunctionExpression') {
return false;
}
// let Foo = () => {}
// export default hoc1(hoc2(() => {}))
callback(inferredName, node, path);
return true;
}
case 'FunctionExpression': {
// let Foo = function() {}
// const Foo = hoc1(forwardRef(function renderFoo() {}))
// export default memo(function() {})
callback(inferredName, node, path);
return true;
}
case 'CallExpression': {
const argsPath = path.get('arguments');
if (argsPath === undefined || argsPath.length === 0) {
return false;
}
const calleePath = path.get('callee');
switch (calleePath.node.type) {
case 'CallExpression':
case 'MemberExpression':
case 'Identifier': {
const calleeSource = calleePath.getSource().split('(')[0];
const innerName = inferredName + '$' + calleeSource;
const foundInside = argsPath.some(argPath =>
findInnerComponents(innerName, argPath, callback)
);
if (!foundInside) {
return false;
}
// const Foo = hoc1(hoc2(() => {}))
// export default memo(React.forwardRef(function() {}))
callback(inferredName, node, path);
return true;
}
default: {
return false;
}
}
}
case 'VariableDeclarator': {
const init = node.init;
if (init === null) {
return false;
}
const name = node.id.name;
if (!isComponentishName(name)) {
return false;
}
switch (init.type) {
case 'ArrowFunctionExpression':
case 'FunctionExpression':
// Likely component definitions.
break;
case 'CallExpression': {
// Maybe a HOC.
// Try to determine if this is some form of import.
const callee = init.callee;
const calleeType = callee.type;
if (calleeType === 'Import') {
return false;
} else if (calleeType === 'Identifier') {
if (callee.name.indexOf('require') === 0) {
return false;
} else if (callee.name.indexOf('import') === 0) {
return false;
}
// Neither require nor import. Might be a HOC.
// Pass through.
} else if (calleeType === 'MemberExpression') {
// Could be something like React.forwardRef(...)
// Pass through.
}
break;
}
case 'TaggedTemplateExpression':
// Maybe something like styled.div`...`
break;
default:
return false;
}
const initPath = path.get('init');
const foundInside = findInnerComponents(
inferredName,
initPath,
callback
);
if (foundInside) {
return true;
}
// See if this identifier is used in JSX. Then it's a component.
const binding = path.scope.getBinding(name);
if (binding === undefined) {
return;
}
let isLikelyUsedAsType = false;
const referencePaths = binding.referencePaths;
for (let i = 0; i < referencePaths.length; i++) {
const ref = referencePaths[i];
if (
ref.node &&
ref.node.type !== 'JSXIdentifier' &&
ref.node.type !== 'Identifier'
) {
continue;
}
const refParent = ref.parent;
if (refParent.type === 'JSXOpeningElement') {
isLikelyUsedAsType = true;
} else if (refParent.type === 'CallExpression') {
const callee = refParent.callee;
let fnName;
switch (callee.type) {
case 'Identifier':
fnName = callee.name;
break;
case 'MemberExpression':
fnName = callee.property.name;
break;
}
switch (fnName) {
case 'createElement':
case 'jsx':
case 'jsxDEV':
case 'jsxs':
isLikelyUsedAsType = true;
break;
}
}
if (isLikelyUsedAsType) {
// const X = ... + later <X />
callback(inferredName, init, initPath);
return true;
}
}
}
}
return false;
}
function isBuiltinHook(hookName) {
switch (hookName) {
case 'useErrorBoundary':
case 'React.useErrorBoundary':
case 'useState':
case 'React.useState':
case 'useReducer':
case 'React.useReducer':
case 'useEffect':
case 'React.useEffect':
case 'useLayoutEffect':
case 'React.useLayoutEffect':
case 'useMemo':
case 'React.useMemo':
case 'useCallback':
case 'React.useCallback':
case 'useRef':
case 'React.useRef':
case 'useContext':
case 'React.useContext':
case 'useImperativeMethods':
case 'React.useImperativeMethods':
case 'useDebugValue':
case 'React.useDebugValue':
return true;
default:
return false;
}
}
function getHookCallsSignature(functionNode) {
const fnHookCalls = hookCalls.get(functionNode);
if (fnHookCalls === undefined) {
return null;
}
return {
key: fnHookCalls.map(call => call.name + '{' + call.key + '}').join('\n'),
customHooks: fnHookCalls
.filter(call => !isBuiltinHook(call.name))
.map(call => t.cloneDeep(call.callee)),
};
}
const hasForceResetCommentByFile = new WeakMap();
// We let user do /* @refresh reset */ to reset state in the whole file.
function hasForceResetComment(path) {
const file = path.hub.file;
let hasForceReset = hasForceResetCommentByFile.get(file);
if (hasForceReset !== undefined) {
return hasForceReset;
}
hasForceReset = false;
const comments = file.ast.comments;
for (let i = 0; i < comments.length; i++) {
const cmt = comments[i];
if (cmt.value.indexOf('@refresh reset') !== -1) {
hasForceReset = true;
break;
}
}
hasForceResetCommentByFile.set(file, hasForceReset);
return hasForceReset;
}
function createArgumentsForSignature(node, signature, scope) {
const { key, customHooks } = signature;
let forceReset = hasForceResetComment(scope.path);
const customHooksInScope = [];
customHooks.forEach(callee => {
// Check if a corresponding binding exists where we emit the signature.
let bindingName;
switch (callee.type) {
case 'MemberExpression':
if (callee.object.type === 'Identifier') {
bindingName = callee.object.name;
}
break;
case 'Identifier':
bindingName = callee.name;
break;
}
if (scope.hasBinding(bindingName)) {
customHooksInScope.push(callee);
} else {
// We don't have anything to put in the array because Hook is out of scope.
// Since it could potentially have been edited, remount the component.
forceReset = true;
}
});
let finalKey = key;
if (typeof require === 'function' && !opts.emitFullSignatures) {
// Prefer to hash when we can (e.g. outside of ASTExplorer).
// This makes it deterministically compact, even if there's
// e.g. a useState initializer with some code inside.
// We also need it for www that has transforms like cx()
// that don't understand if something is part of a string.
finalKey = require('crypto')
.createHash('sha1')
.update(key)
.digest('base64');
}
const args = [node, t.stringLiteral(finalKey)];
if (forceReset || customHooksInScope.length > 0) {
args.push(t.booleanLiteral(forceReset));
}
if (customHooksInScope.length > 0) {
args.push(
// TODO: We could use an arrow here to be more compact.
// However, don't do it until AMA can run them natively.
t.functionExpression(
null,
[],
t.blockStatement([
t.returnStatement(t.arrayExpression(customHooksInScope)),
])
)
);
}
return args;
}
const seenForRegistration = new WeakSet();
const seenForSignature = new WeakSet();
const seenForOutro = new WeakSet();
const hookCalls = new WeakMap();
const HookCallsVisitor = {
CallExpression(path) {
const node = path.node;
const callee = node.callee;
// Note: this visitor MUST NOT mutate the tree in any way.
// It runs early in a separate traversal and should be very fast.
let name = null;
switch (callee.type) {
case 'Identifier':
name = callee.name;
break;
case 'MemberExpression':
name = callee.property.name;
break;
}
if (name === null || !/^use[A-Z]/.test(name)) {
return;
}
const fnScope = path.scope.getFunctionParent();
if (fnScope === null) {
return;
}
// This is a Hook call. Record it.
const fnNode = fnScope.block;
if (!hookCalls.has(fnNode)) {
hookCalls.set(fnNode, []);
}
const hookCallsForFn = hookCalls.get(fnNode);
let key = '';
if (path.parent.type === 'VariableDeclarator') {
// TODO: if there is no LHS, consider some other heuristic.
key = path.parentPath.get('id').getSource();
}
// Some built-in Hooks reset on edits to arguments.
const args = path.get('arguments');
if (name === 'useState' && args.length > 0) {
// useState first argument is initial state.
key += '(' + args[0].getSource() + ')';
} else if (name === 'useReducer' && args.length > 1) {
// useReducer second argument is initial state.
key += '(' + args[1].getSource() + ')';
}
hookCallsForFn.push({
callee: path.node.callee,
name,
key,
});
},
};
const createContextTemplate = template(
`
Object.assign((CREATECONTEXT[IDENT] || (CREATECONTEXT[IDENT]=CREATECONTEXT(VALUE))), {__:VALUE});
`,
{ placeholderPattern: /^[A-Z]+$/ }
);
const emptyTemplate = template(`
(CREATECONTEXT[IDENT] || (CREATECONTEXT[IDENT]=CREATECONTEXT()));
`);
const getFirstNonTsExpression = expression =>
expression.type === 'TSAsExpression'
? getFirstNonTsExpression(expression.expression)
: expression;
return {
visitor: {
ClassDeclaration: {
enter(path) {
const node = path.node;
let programPath;
let insertAfterPath;
switch (path.parent.type) {
case 'Program':
insertAfterPath = path;
programPath = path.parentPath;
break;
case 'ExportNamedDeclaration':
insertAfterPath = path.parentPath;
programPath = insertAfterPath.parentPath;
break;
case 'ExportDefaultDeclaration':
insertAfterPath = path.parentPath;
programPath = insertAfterPath.parentPath;
break;
default:
return;
}
const id = node.id;
if (id === null) {
// We don't currently handle anonymous default exports.
return;
}
const inferredName = id.name;
if (!isComponentishName(inferredName)) {
return;
}
// Make sure we're not mutating the same tree twice.
// This can happen if another Babel plugin replaces parents.
if (seenForRegistration.has(node)) {
return;
}
seenForRegistration.add(node);
// Don't mutate the tree above this point.
const handle = createRegistration(programPath, inferredName);
insertAfterPath.insertAfter(
t.expressionStatement(
t.assignmentExpression('=', handle, path.node.id)
)
);
},
},
CallExpression(path, state) {
if (
!path.get('callee').referencesImport('preact', 'createContext') &&
!path.get('callee').referencesImport('react', 'createContext') &&
!path.get('callee').referencesImport('preact/compat', 'createContext')
)
return;
let id = '';
if (t.isObjectProperty(path.parentPath)) {
id += '__' + path.parent.key.name;
} else if (t.isVariableDeclarator(path.parentPath)) {
id += '$' + path.parent.id.name;
} else if (t.isAssignmentExpression(path.parentPath)) {
if (t.isIdentifier(path.parent.left)) {
id += '_' + path.parent.left.name;
} else {
id += '_' + hash(path.parentPath.get('left').getSource());
}
}
const getFirstParent = parentPath => {
if (
t.isProgram(parentPath) ||
t.isFunctionDeclaration(parentPath) ||
t.isArrowFunctionExpression(parentPath)
)
return parentPath;
return getFirstParent(parentPath.parentPath);
};
const closestClosurePath = getFirstParent(path.parentPath);
const contexts = state.get('contexts');
let counter = (contexts.get(id) || -1) + 1;
contexts.set(id, counter);
if (counter) id += counter;
id = '_' + state.get('filehash') + id;
path.skip();
if (!t.isProgram(closestClosurePath)) {
const params = closestClosurePath.node.params;
params.forEach(param => {
if (t.isIdentifier(param)) {
id += '_PARAM' + param.name;
}
});
}
// TODO: maybe wrap with JSON.stringify
if (path.node.arguments[0]) {
const [quasi, ...expressions] = id.split('_PARAM');
const first = t.templateElement({ raw: quasi, cooked: '' });
const expr = expressions.map(x => t.identifier(x.replace('}', '')));
path.replaceWith(
createContextTemplate({
CREATECONTEXT: path.get('callee').node,
IDENT: t.templateLiteral(
[
first,
...expressions.map(() =>
t.templateElement({ raw: '', cooked: '' }, true)
),
],
expr
),
VALUE: t.clone(getFirstNonTsExpression(path.node.arguments[0])),
})
);
} else {
const [quasi, ...expressions] = id.split('_PARAM');
const first = t.templateElement({ raw: quasi, cooked: '' });
const expr = expressions.map(x => t.identifier(x.replace('}', '')));
path.replaceWith(
emptyTemplate({
CREATECONTEXT: path.get('callee').node,
IDENT: t.templateLiteral(
[
first,
...expressions.map(() =>
t.templateElement({ raw: '', cooked: '' }, true)
),
],
expr
),
})
);
}
},
ExportDefaultDeclaration(path) {
const node = path.node;
const decl = node.declaration;
const declPath = path.get('declaration');
if (decl.type !== 'CallExpression') {
// For now, we only support possible HOC calls here.
// Named function declarations are handled in FunctionDeclaration.
// Anonymous direct exports like module.exports=function() {}
// are currently ignored.
return;
}
// Make sure we're not mutating the same tree twice.
// This can happen if another Babel plugin replaces parents.
if (seenForRegistration.has(node)) {
return;
}
seenForRegistration.add(node);
// Don't mutate the tree above this point.
// This code path handles nested cases like:
// export default memo(() => {})
// In those cases it is more plausible people will omit names
// so they're worth handling despite possible false positives.
// More importantly, it handles the named case:
// export default memo(function Named() {})
const inferredName = '%default%';
const programPath = path.parentPath;
findInnerComponents(
inferredName,
declPath,
(persistentID, targetExpr, targetPath) => {
if (targetPath === null) {
// For case like:
// export default hoc(Foo)
// we don't want to wrap Foo inside the call.
// Instead we assume it's registered at definition.
return;
}
const handle = createRegistration(programPath, persistentID);
targetPath.replaceWith(
t.assignmentExpression('=', handle, targetExpr)
);
}
);
},
FunctionDeclaration: {
enter(path) {
const node = path.node;
let programPath;
let insertAfterPath;
switch (path.parent.type) {
case 'Program':
insertAfterPath = path;
programPath = path.parentPath;
break;
case 'ExportNamedDeclaration':
insertAfterPath = path.parentPath;
programPath = insertAfterPath.parentPath;
break;
case 'ExportDefaultDeclaration':
insertAfterPath = path.parentPath;
programPath = insertAfterPath.parentPath;
break;
default:
return;
}
const id = node.id;
if (id === null) {
// We don't currently handle anonymous default exports.
return;
}
const inferredName = id.name;
if (!isComponentishName(inferredName)) {
return;
}
// Make sure we're not mutating the same tree twice.
// This can happen if another Babel plugin replaces parents.
if (seenForRegistration.has(node)) {
return;
}
seenForRegistration.add(node);
// Don't mutate the tree above this point.
// function Named() {}
// function Named() {}
findInnerComponents(
inferredName,
path,
(persistentID, targetExpr) => {
const handle = createRegistration(programPath, persistentID);
insertAfterPath.insertAfter(
t.expressionStatement(
t.assignmentExpression('=', handle, targetExpr)
)
);
}
);
},
exit(path) {
const node = path.node;
const id = node.id;
if (id === null) {
return;
}
const signature = getHookCallsSignature(node);
if (signature === null) {
return;
}
// Make sure we're not mutating the same tree twice.
// This can happen if another Babel plugin replaces parents.
if (seenForSignature.has(node)) {
return;
}
seenForSignature.add(node);
// Don't mutate the tree above this point.
const sigCallID = path.scope.generateUidIdentifier('_s');
path.scope.parent.push({
id: sigCallID,
init: t.callExpression(refreshSig, []),
});
// The signature call is split in two parts. One part is called inside the function.
// This is used to signal when first render happens.
path
.get('body')
.unshiftContainer(
'body',
t.expressionStatement(t.callExpression(sigCallID, []))
);
// The second call is around the function itself.
// This is used to associate a type with a signature.
// Unlike with $RefreshReg$, this needs to work for nested
// declarations too. So we need to search for a path where
// we can insert a statement rather than hard coding it.
let insertAfterPath = null;
path.find(p => {
if (p.parentPath.isBlock()) {
insertAfterPath = p;
return true;
}
});
if (insertAfterPath === null) {
return;
}
insertAfterPath.insertAfter(
t.expressionStatement(
t.callExpression(
sigCallID,
createArgumentsForSignature(
id,
signature,
insertAfterPath.scope
)
)
)
);
},
},
'ArrowFunctionExpression|FunctionExpression': {
exit(path) {
const node = path.node;
const signature = getHookCallsSignature(node);
if (signature === null) {
return;
}
// Make sure we're not mutating the same tree twice.
// This can happen if another Babel plugin replaces parents.
if (seenForSignature.has(node)) {
return;
}
seenForSignature.add(node);
// Don't mutate the tree above this point.
const sigCallID = path.scope.generateUidIdentifier('_s');
path.scope.parent.push({
id: sigCallID,
init: t.callExpression(refreshSig, []),
});
// The signature call is split in two parts. One part is called inside the function.
// This is used to signal when first render happens.
if (path.node.body.type !== 'BlockStatement') {
path.node.body = t.blockStatement([
t.returnStatement(path.node.body),
]);
}
path
.get('body')
.unshiftContainer(
'body',
t.expressionStatement(t.callExpression(sigCallID, []))
);
// The second call is around the function itself.
// This is used to associate a type with a signature.
if (path.parent.type === 'VariableDeclarator') {
let insertAfterPath = null;
path.find(p => {
if (p.parentPath.isBlock()) {
insertAfterPath = p;
return true;
}
});
if (insertAfterPath === null) {
return;
}
// Special case when a function would get an inferred name:
// let Foo = () => {}
// let Foo = function() {}
// We'll add signature it on next line so that
// we don't mess up the inferred 'Foo' function name.
insertAfterPath.insertAfter(
t.expressionStatement(
t.callExpression(
sigCallID,
createArgumentsForSignature(
path.parent.id,
signature,
insertAfterPath.scope
)
)
)
);
// Result: let Foo = () => {}; __signature(Foo, ...);
} else {
// let Foo = hoc(() => {})
path.replaceWith(
t.callExpression(
sigCallID,
createArgumentsForSignature(node, signature, path.scope)
)
);
// Result: let Foo = hoc(__signature(() => {}, ...))
}
},
},
VariableDeclaration(path) {
const node = path.node;
let programPath;
let insertAfterPath;
switch (path.parent.type) {
case 'Program':
insertAfterPath = path;
programPath = path.parentPath;
break;
case 'ExportNamedDeclaration':
insertAfterPath = path.parentPath;
programPath = insertAfterPath.parentPath;
break;
case 'ExportDefaultDeclaration':
insertAfterPath = path.parentPath;
programPath = insertAfterPath.parentPath;
break;
default:
return;
}
// Make sure we're not mutating the same tree twice.
// This can happen if another Babel plugin replaces parents.
if (seenForRegistration.has(node)) {
return;
}
seenForRegistration.add(node);
// Don't mutate the tree above this point.
const declPaths = path.get('declarations');
if (declPaths.length !== 1) {
return;
}
const declPath = declPaths[0];
const inferredName = declPath.node.id.name;
findInnerComponents(
inferredName,
declPath,
(persistentID, targetExpr, targetPath) => {
if (targetPath === null) {
// For case like:
// const Something = hoc(Foo)
// we don't want to wrap Foo inside the call.
// Instead we assume it's registered at definition.
return;
}
const handle = createRegistration(programPath, persistentID);
if (targetPath.parent.type === 'VariableDeclarator') {
// Special case when a variable would get an inferred name:
// let Foo = () => {}
// let Foo = function() {}
// let Foo = styled.div``;
// We'll register it on next line so that
// we don't mess up the inferred 'Foo' function name.
// (eg: with @babel/plugin-transform-react-display-name or
// babel-plugin-styled-components)
insertAfterPath.insertAfter(
t.expressionStatement(
t.assignmentExpression('=', handle, declPath.node.id)
)
);
// Result: let Foo = () => {}; _c1 = Foo;
} else {
// let Foo = hoc(() => {})
targetPath.replaceWith(
t.assignmentExpression('=', handle, targetExpr)
);
// Result: let Foo = hoc(_c1 = () => {})
}
}
);
},
Program: {
enter(path, state) {
state.set(
'filehash',
hash(
path.hub.file.opts.filename ||
path.hub.file.opts.sourceFileName ||
path.hub.file.opts.generatorOpts?.sourceFileName ||
'unnamed'
)
);
state.set('contexts', new Map());
// This is a separate early visitor because we need to collect Hook calls
// and "const [foo, setFoo] = ..." signatures before the destructuring
// transform mangles them. This extra traversal is not ideal for perf,
// but it's the best we can do until we stop transpiling destructuring.
path.traverse(HookCallsVisitor);
},
exit(path) {
const registrations = registrationsByProgramPath.get(path);
if (registrations === undefined) {
return;
}
// Make sure we're not mutating the same tree twice.
// This can happen if another Babel plugin replaces parents.
const node = path.node;
if (seenForOutro.has(node)) {
return;
}
seenForOutro.add(node);
// Don't mutate the tree above this point.
registrationsByProgramPath.delete(path);
const declarators = [];
path.pushContainer('body', t.variableDeclaration('var', declarators));
registrations.forEach(({ handle, persistentID }) => {
path.pushContainer(
'body',
t.expressionStatement(
t.callExpression(refreshReg, [
handle,
t.stringLiteral(persistentID),
])
)
);
declarators.push(t.variableDeclarator(handle));
});
},
},
},
};
}

33
node_modules/@prefresh/babel-plugin/package.json generated vendored Normal file
View File

@@ -0,0 +1,33 @@
{
"name": "@prefresh/babel-plugin",
"version": "0.5.1",
"main": "dist/src/index.js",
"module": "src/index.mjs",
"exports": {
".": {
"import": "./src/index.mjs",
"require": "./dist/src/index.js"
},
"./package.json": "./package.json"
},
"repository": {
"type": "git",
"url": "git+https://github.com/preactjs/prefresh.git",
"directory": "packages/babel"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/preactjs/prefresh/issues"
},
"homepage": "https://github.com/preactjs/prefresh#readme",
"devDependencies": {
"cjyes": "0.3.1"
},
"publishConfig": {
"provenance": true
},
"scripts": {
"build": "cjyes src/index.mjs",
"lint": "eslint src"
}
}

924
node_modules/@prefresh/babel-plugin/src/index.mjs generated vendored Normal file
View File

@@ -0,0 +1,924 @@
function hash(str) {
let hash = 5381,
i = str.length;
while (i) {
hash = (hash * 33) ^ str.charCodeAt(--i);
}
return (hash >>> 0).toString(36);
}
/**
*
* Majority copied from https://github.com/facebook/react/blob/master/packages/react-refresh/src/ReactFreshBabelPlugin.js
*/
export default function (babel, opts = {}) {
if (typeof babel.env === 'function') {
const env = babel.env();
if (env !== 'development' && !opts.skipEnvCheck) {
throw new Error(
'Prefresh Babel transform should only be enabled in development environment. ' +
'Instead, the environment is: "' +
env +
'". If you want to override this check, pass {skipEnvCheck: true} as plugin options.'
);
}
}
const { types: t, template } = babel;
const refreshReg = t.identifier(opts.refreshReg || '$RefreshReg$');
const refreshSig = t.identifier(opts.refreshSig || '$RefreshSig$');
const registrationsByProgramPath = new Map();
function createRegistration(programPath, persistentID) {
const handle = programPath.scope.generateUidIdentifier('c');
if (!registrationsByProgramPath.has(programPath)) {
registrationsByProgramPath.set(programPath, []);
}
const registrations = registrationsByProgramPath.get(programPath);
registrations.push({
handle,
persistentID,
});
return handle;
}
function isComponentishName(name) {
return typeof name === 'string' && name[0] >= 'A' && name[0] <= 'Z';
}
function findInnerComponents(inferredName, path, callback) {
const node = path.node;
switch (node.type) {
case 'Identifier': {
if (!isComponentishName(node.name)) {
return false;
}
// export default hoc(Foo)
// const X = hoc(Foo)
callback(inferredName, node, null);
return true;
}
case 'FunctionDeclaration': {
// function Foo() {}
// export function Foo() {}
// export default function Foo() {}
callback(inferredName, node.id, null);
return true;
}
case 'ArrowFunctionExpression': {
if (node.body.type === 'ArrowFunctionExpression') {
return false;
}
// let Foo = () => {}
// export default hoc1(hoc2(() => {}))
callback(inferredName, node, path);
return true;
}
case 'FunctionExpression': {
// let Foo = function() {}
// const Foo = hoc1(forwardRef(function renderFoo() {}))
// export default memo(function() {})
callback(inferredName, node, path);
return true;
}
case 'CallExpression': {
const argsPath = path.get('arguments');
if (argsPath === undefined || argsPath.length === 0) {
return false;
}
const calleePath = path.get('callee');
switch (calleePath.node.type) {
case 'CallExpression':
case 'MemberExpression':
case 'Identifier': {
const calleeSource = calleePath.getSource().split('(')[0];
const innerName = inferredName + '$' + calleeSource;
const foundInside = argsPath.some(argPath =>
findInnerComponents(innerName, argPath, callback)
);
if (!foundInside) {
return false;
}
// const Foo = hoc1(hoc2(() => {}))
// export default memo(React.forwardRef(function() {}))
callback(inferredName, node, path);
return true;
}
default: {
return false;
}
}
}
case 'VariableDeclarator': {
const init = node.init;
if (init === null) {
return false;
}
const name = node.id.name;
if (!isComponentishName(name)) {
return false;
}
switch (init.type) {
case 'ArrowFunctionExpression':
case 'FunctionExpression':
// Likely component definitions.
break;
case 'CallExpression': {
// Maybe a HOC.
// Try to determine if this is some form of import.
const callee = init.callee;
const calleeType = callee.type;
if (calleeType === 'Import') {
return false;
} else if (calleeType === 'Identifier') {
if (callee.name.indexOf('require') === 0) {
return false;
} else if (callee.name.indexOf('import') === 0) {
return false;
}
// Neither require nor import. Might be a HOC.
// Pass through.
} else if (calleeType === 'MemberExpression') {
// Could be something like React.forwardRef(...)
// Pass through.
}
break;
}
case 'TaggedTemplateExpression':
// Maybe something like styled.div`...`
break;
default:
return false;
}
const initPath = path.get('init');
const foundInside = findInnerComponents(
inferredName,
initPath,
callback
);
if (foundInside) {
return true;
}
// See if this identifier is used in JSX. Then it's a component.
const binding = path.scope.getBinding(name);
if (binding === undefined) {
return;
}
let isLikelyUsedAsType = false;
const referencePaths = binding.referencePaths;
for (let i = 0; i < referencePaths.length; i++) {
const ref = referencePaths[i];
if (
ref.node &&
ref.node.type !== 'JSXIdentifier' &&
ref.node.type !== 'Identifier'
) {
continue;
}
const refParent = ref.parent;
if (refParent.type === 'JSXOpeningElement') {
isLikelyUsedAsType = true;
} else if (refParent.type === 'CallExpression') {
const callee = refParent.callee;
let fnName;
switch (callee.type) {
case 'Identifier':
fnName = callee.name;
break;
case 'MemberExpression':
fnName = callee.property.name;
break;
}
switch (fnName) {
case 'createElement':
case 'jsx':
case 'jsxDEV':
case 'jsxs':
isLikelyUsedAsType = true;
break;
}
}
if (isLikelyUsedAsType) {
// const X = ... + later <X />
callback(inferredName, init, initPath);
return true;
}
}
}
}
return false;
}
function isBuiltinHook(hookName) {
switch (hookName) {
case 'useErrorBoundary':
case 'React.useErrorBoundary':
case 'useState':
case 'React.useState':
case 'useReducer':
case 'React.useReducer':
case 'useEffect':
case 'React.useEffect':
case 'useLayoutEffect':
case 'React.useLayoutEffect':
case 'useMemo':
case 'React.useMemo':
case 'useCallback':
case 'React.useCallback':
case 'useRef':
case 'React.useRef':
case 'useContext':
case 'React.useContext':
case 'useImperativeMethods':
case 'React.useImperativeMethods':
case 'useDebugValue':
case 'React.useDebugValue':
return true;
default:
return false;
}
}
function getHookCallsSignature(functionNode) {
const fnHookCalls = hookCalls.get(functionNode);
if (fnHookCalls === undefined) {
return null;
}
return {
key: fnHookCalls.map(call => call.name + '{' + call.key + '}').join('\n'),
customHooks: fnHookCalls
.filter(call => !isBuiltinHook(call.name))
.map(call => t.cloneDeep(call.callee)),
};
}
const hasForceResetCommentByFile = new WeakMap();
// We let user do /* @refresh reset */ to reset state in the whole file.
function hasForceResetComment(path) {
const file = path.hub.file;
let hasForceReset = hasForceResetCommentByFile.get(file);
if (hasForceReset !== undefined) {
return hasForceReset;
}
hasForceReset = false;
const comments = file.ast.comments;
for (let i = 0; i < comments.length; i++) {
const cmt = comments[i];
if (cmt.value.indexOf('@refresh reset') !== -1) {
hasForceReset = true;
break;
}
}
hasForceResetCommentByFile.set(file, hasForceReset);
return hasForceReset;
}
function createArgumentsForSignature(node, signature, scope) {
const { key, customHooks } = signature;
let forceReset = hasForceResetComment(scope.path);
const customHooksInScope = [];
customHooks.forEach(callee => {
// Check if a corresponding binding exists where we emit the signature.
let bindingName;
switch (callee.type) {
case 'MemberExpression':
if (callee.object.type === 'Identifier') {
bindingName = callee.object.name;
}
break;
case 'Identifier':
bindingName = callee.name;
break;
}
if (scope.hasBinding(bindingName)) {
customHooksInScope.push(callee);
} else {
// We don't have anything to put in the array because Hook is out of scope.
// Since it could potentially have been edited, remount the component.
forceReset = true;
}
});
let finalKey = key;
if (typeof require === 'function' && !opts.emitFullSignatures) {
// Prefer to hash when we can (e.g. outside of ASTExplorer).
// This makes it deterministically compact, even if there's
// e.g. a useState initializer with some code inside.
// We also need it for www that has transforms like cx()
// that don't understand if something is part of a string.
finalKey = require('crypto')
.createHash('sha1')
.update(key)
.digest('base64');
}
const args = [node, t.stringLiteral(finalKey)];
if (forceReset || customHooksInScope.length > 0) {
args.push(t.booleanLiteral(forceReset));
}
if (customHooksInScope.length > 0) {
args.push(
// TODO: We could use an arrow here to be more compact.
// However, don't do it until AMA can run them natively.
t.functionExpression(
null,
[],
t.blockStatement([
t.returnStatement(t.arrayExpression(customHooksInScope)),
])
)
);
}
return args;
}
const seenForRegistration = new WeakSet();
const seenForSignature = new WeakSet();
const seenForOutro = new WeakSet();
const hookCalls = new WeakMap();
const HookCallsVisitor = {
CallExpression(path) {
const node = path.node;
const callee = node.callee;
// Note: this visitor MUST NOT mutate the tree in any way.
// It runs early in a separate traversal and should be very fast.
let name = null;
switch (callee.type) {
case 'Identifier':
name = callee.name;
break;
case 'MemberExpression':
name = callee.property.name;
break;
}
if (name === null || !/^use[A-Z]/.test(name)) {
return;
}
const fnScope = path.scope.getFunctionParent();
if (fnScope === null) {
return;
}
// This is a Hook call. Record it.
const fnNode = fnScope.block;
if (!hookCalls.has(fnNode)) {
hookCalls.set(fnNode, []);
}
const hookCallsForFn = hookCalls.get(fnNode);
let key = '';
if (path.parent.type === 'VariableDeclarator') {
// TODO: if there is no LHS, consider some other heuristic.
key = path.parentPath.get('id').getSource();
}
// Some built-in Hooks reset on edits to arguments.
const args = path.get('arguments');
if (name === 'useState' && args.length > 0) {
// useState first argument is initial state.
key += '(' + args[0].getSource() + ')';
} else if (name === 'useReducer' && args.length > 1) {
// useReducer second argument is initial state.
key += '(' + args[1].getSource() + ')';
}
hookCallsForFn.push({
callee: path.node.callee,
name,
key,
});
},
};
const createContextTemplate = template(
`
Object.assign((CREATECONTEXT[IDENT] || (CREATECONTEXT[IDENT]=CREATECONTEXT(VALUE))), {__:VALUE});
`,
{ placeholderPattern: /^[A-Z]+$/ }
);
const emptyTemplate = template(`
(CREATECONTEXT[IDENT] || (CREATECONTEXT[IDENT]=CREATECONTEXT()));
`);
const getFirstNonTsExpression = expression =>
expression.type === 'TSAsExpression'
? getFirstNonTsExpression(expression.expression)
: expression;
return {
visitor: {
ClassDeclaration: {
enter(path) {
const node = path.node;
let programPath;
let insertAfterPath;
switch (path.parent.type) {
case 'Program':
insertAfterPath = path;
programPath = path.parentPath;
break;
case 'ExportNamedDeclaration':
insertAfterPath = path.parentPath;
programPath = insertAfterPath.parentPath;
break;
case 'ExportDefaultDeclaration':
insertAfterPath = path.parentPath;
programPath = insertAfterPath.parentPath;
break;
default:
return;
}
const id = node.id;
if (id === null) {
// We don't currently handle anonymous default exports.
return;
}
const inferredName = id.name;
if (!isComponentishName(inferredName)) {
return;
}
// Make sure we're not mutating the same tree twice.
// This can happen if another Babel plugin replaces parents.
if (seenForRegistration.has(node)) {
return;
}
seenForRegistration.add(node);
// Don't mutate the tree above this point.
const handle = createRegistration(programPath, inferredName);
insertAfterPath.insertAfter(
t.expressionStatement(
t.assignmentExpression('=', handle, path.node.id)
)
);
},
},
CallExpression(path, state) {
if (
!path.get('callee').referencesImport('preact', 'createContext') &&
!path.get('callee').referencesImport('react', 'createContext') &&
!path.get('callee').referencesImport('preact/compat', 'createContext')
)
return;
let id = '';
if (t.isObjectProperty(path.parentPath)) {
id += '__' + path.parent.key.name;
} else if (t.isVariableDeclarator(path.parentPath)) {
id += '$' + path.parent.id.name;
} else if (t.isAssignmentExpression(path.parentPath)) {
if (t.isIdentifier(path.parent.left)) {
id += '_' + path.parent.left.name;
} else {
id += '_' + hash(path.parentPath.get('left').getSource());
}
}
const getFirstParent = parentPath => {
if (
t.isProgram(parentPath) ||
t.isFunctionDeclaration(parentPath) ||
t.isArrowFunctionExpression(parentPath)
)
return parentPath;
return getFirstParent(parentPath.parentPath);
};
const closestClosurePath = getFirstParent(path.parentPath);
const contexts = state.get('contexts');
let counter = (contexts.get(id) || -1) + 1;
contexts.set(id, counter);
if (counter) id += counter;
id = '_' + state.get('filehash') + id;
path.skip();
if (!t.isProgram(closestClosurePath)) {
const params = closestClosurePath.node.params;
params.forEach(param => {
if (t.isIdentifier(param)) {
id += '_PARAM' + param.name;
}
});
}
// TODO: maybe wrap with JSON.stringify
if (path.node.arguments[0]) {
const [quasi, ...expressions] = id.split('_PARAM');
const first = t.templateElement({ raw: quasi, cooked: '' });
const expr = expressions.map(x => t.identifier(x.replace('}', '')));
path.replaceWith(
createContextTemplate({
CREATECONTEXT: path.get('callee').node,
IDENT: t.templateLiteral(
[
first,
...expressions.map(() =>
t.templateElement({ raw: '', cooked: '' }, true)
),
],
expr
),
VALUE: t.clone(getFirstNonTsExpression(path.node.arguments[0])),
})
);
} else {
const [quasi, ...expressions] = id.split('_PARAM');
const first = t.templateElement({ raw: quasi, cooked: '' });
const expr = expressions.map(x => t.identifier(x.replace('}', '')));
path.replaceWith(
emptyTemplate({
CREATECONTEXT: path.get('callee').node,
IDENT: t.templateLiteral(
[
first,
...expressions.map(() =>
t.templateElement({ raw: '', cooked: '' }, true)
),
],
expr
),
})
);
}
},
ExportDefaultDeclaration(path) {
const node = path.node;
const decl = node.declaration;
const declPath = path.get('declaration');
if (decl.type !== 'CallExpression') {
// For now, we only support possible HOC calls here.
// Named function declarations are handled in FunctionDeclaration.
// Anonymous direct exports like export default function() {}
// are currently ignored.
return;
}
// Make sure we're not mutating the same tree twice.
// This can happen if another Babel plugin replaces parents.
if (seenForRegistration.has(node)) {
return;
}
seenForRegistration.add(node);
// Don't mutate the tree above this point.
// This code path handles nested cases like:
// export default memo(() => {})
// In those cases it is more plausible people will omit names
// so they're worth handling despite possible false positives.
// More importantly, it handles the named case:
// export default memo(function Named() {})
const inferredName = '%default%';
const programPath = path.parentPath;
findInnerComponents(
inferredName,
declPath,
(persistentID, targetExpr, targetPath) => {
if (targetPath === null) {
// For case like:
// export default hoc(Foo)
// we don't want to wrap Foo inside the call.
// Instead we assume it's registered at definition.
return;
}
const handle = createRegistration(programPath, persistentID);
targetPath.replaceWith(
t.assignmentExpression('=', handle, targetExpr)
);
}
);
},
FunctionDeclaration: {
enter(path) {
const node = path.node;
let programPath;
let insertAfterPath;
switch (path.parent.type) {
case 'Program':
insertAfterPath = path;
programPath = path.parentPath;
break;
case 'ExportNamedDeclaration':
insertAfterPath = path.parentPath;
programPath = insertAfterPath.parentPath;
break;
case 'ExportDefaultDeclaration':
insertAfterPath = path.parentPath;
programPath = insertAfterPath.parentPath;
break;
default:
return;
}
const id = node.id;
if (id === null) {
// We don't currently handle anonymous default exports.
return;
}
const inferredName = id.name;
if (!isComponentishName(inferredName)) {
return;
}
// Make sure we're not mutating the same tree twice.
// This can happen if another Babel plugin replaces parents.
if (seenForRegistration.has(node)) {
return;
}
seenForRegistration.add(node);
// Don't mutate the tree above this point.
// export function Named() {}
// function Named() {}
findInnerComponents(
inferredName,
path,
(persistentID, targetExpr) => {
const handle = createRegistration(programPath, persistentID);
insertAfterPath.insertAfter(
t.expressionStatement(
t.assignmentExpression('=', handle, targetExpr)
)
);
}
);
},
exit(path) {
const node = path.node;
const id = node.id;
if (id === null) {
return;
}
const signature = getHookCallsSignature(node);
if (signature === null) {
return;
}
// Make sure we're not mutating the same tree twice.
// This can happen if another Babel plugin replaces parents.
if (seenForSignature.has(node)) {
return;
}
seenForSignature.add(node);
// Don't mutate the tree above this point.
const sigCallID = path.scope.generateUidIdentifier('_s');
path.scope.parent.push({
id: sigCallID,
init: t.callExpression(refreshSig, []),
});
// The signature call is split in two parts. One part is called inside the function.
// This is used to signal when first render happens.
path
.get('body')
.unshiftContainer(
'body',
t.expressionStatement(t.callExpression(sigCallID, []))
);
// The second call is around the function itself.
// This is used to associate a type with a signature.
// Unlike with $RefreshReg$, this needs to work for nested
// declarations too. So we need to search for a path where
// we can insert a statement rather than hard coding it.
let insertAfterPath = null;
path.find(p => {
if (p.parentPath.isBlock()) {
insertAfterPath = p;
return true;
}
});
if (insertAfterPath === null) {
return;
}
insertAfterPath.insertAfter(
t.expressionStatement(
t.callExpression(
sigCallID,
createArgumentsForSignature(
id,
signature,
insertAfterPath.scope
)
)
)
);
},
},
'ArrowFunctionExpression|FunctionExpression': {
exit(path) {
const node = path.node;
const signature = getHookCallsSignature(node);
if (signature === null) {
return;
}
// Make sure we're not mutating the same tree twice.
// This can happen if another Babel plugin replaces parents.
if (seenForSignature.has(node)) {
return;
}
seenForSignature.add(node);
// Don't mutate the tree above this point.
const sigCallID = path.scope.generateUidIdentifier('_s');
path.scope.parent.push({
id: sigCallID,
init: t.callExpression(refreshSig, []),
});
// The signature call is split in two parts. One part is called inside the function.
// This is used to signal when first render happens.
if (path.node.body.type !== 'BlockStatement') {
path.node.body = t.blockStatement([
t.returnStatement(path.node.body),
]);
}
path
.get('body')
.unshiftContainer(
'body',
t.expressionStatement(t.callExpression(sigCallID, []))
);
// The second call is around the function itself.
// This is used to associate a type with a signature.
if (path.parent.type === 'VariableDeclarator') {
let insertAfterPath = null;
path.find(p => {
if (p.parentPath.isBlock()) {
insertAfterPath = p;
return true;
}
});
if (insertAfterPath === null) {
return;
}
// Special case when a function would get an inferred name:
// let Foo = () => {}
// let Foo = function() {}
// We'll add signature it on next line so that
// we don't mess up the inferred 'Foo' function name.
insertAfterPath.insertAfter(
t.expressionStatement(
t.callExpression(
sigCallID,
createArgumentsForSignature(
path.parent.id,
signature,
insertAfterPath.scope
)
)
)
);
// Result: let Foo = () => {}; __signature(Foo, ...);
} else {
// let Foo = hoc(() => {})
path.replaceWith(
t.callExpression(
sigCallID,
createArgumentsForSignature(node, signature, path.scope)
)
);
// Result: let Foo = hoc(__signature(() => {}, ...))
}
},
},
VariableDeclaration(path) {
const node = path.node;
let programPath;
let insertAfterPath;
switch (path.parent.type) {
case 'Program':
insertAfterPath = path;
programPath = path.parentPath;
break;
case 'ExportNamedDeclaration':
insertAfterPath = path.parentPath;
programPath = insertAfterPath.parentPath;
break;
case 'ExportDefaultDeclaration':
insertAfterPath = path.parentPath;
programPath = insertAfterPath.parentPath;
break;
default:
return;
}
// Make sure we're not mutating the same tree twice.
// This can happen if another Babel plugin replaces parents.
if (seenForRegistration.has(node)) {
return;
}
seenForRegistration.add(node);
// Don't mutate the tree above this point.
const declPaths = path.get('declarations');
if (declPaths.length !== 1) {
return;
}
const declPath = declPaths[0];
const inferredName = declPath.node.id.name;
findInnerComponents(
inferredName,
declPath,
(persistentID, targetExpr, targetPath) => {
if (targetPath === null) {
// For case like:
// export const Something = hoc(Foo)
// we don't want to wrap Foo inside the call.
// Instead we assume it's registered at definition.
return;
}
const handle = createRegistration(programPath, persistentID);
if (targetPath.parent.type === 'VariableDeclarator') {
// Special case when a variable would get an inferred name:
// let Foo = () => {}
// let Foo = function() {}
// let Foo = styled.div``;
// We'll register it on next line so that
// we don't mess up the inferred 'Foo' function name.
// (eg: with @babel/plugin-transform-react-display-name or
// babel-plugin-styled-components)
insertAfterPath.insertAfter(
t.expressionStatement(
t.assignmentExpression('=', handle, declPath.node.id)
)
);
// Result: let Foo = () => {}; _c1 = Foo;
} else {
// let Foo = hoc(() => {})
targetPath.replaceWith(
t.assignmentExpression('=', handle, targetExpr)
);
// Result: let Foo = hoc(_c1 = () => {})
}
}
);
},
Program: {
enter(path, state) {
state.set(
'filehash',
hash(
path.hub.file.opts.filename ||
path.hub.file.opts.sourceFileName ||
path.hub.file.opts.generatorOpts?.sourceFileName ||
'unnamed'
)
);
state.set('contexts', new Map());
// This is a separate early visitor because we need to collect Hook calls
// and "const [foo, setFoo] = ..." signatures before the destructuring
// transform mangles them. This extra traversal is not ideal for perf,
// but it's the best we can do until we stop transpiling destructuring.
path.traverse(HookCallsVisitor);
},
exit(path) {
const registrations = registrationsByProgramPath.get(path);
if (registrations === undefined) {
return;
}
// Make sure we're not mutating the same tree twice.
// This can happen if another Babel plugin replaces parents.
const node = path.node;
if (seenForOutro.has(node)) {
return;
}
seenForOutro.add(node);
// Don't mutate the tree above this point.
registrationsByProgramPath.delete(path);
const declarators = [];
path.pushContainer('body', t.variableDeclaration('var', declarators));
registrations.forEach(({ handle, persistentID }) => {
path.pushContainer(
'body',
t.expressionStatement(
t.callExpression(refreshReg, [
handle,
t.stringLiteral(persistentID),
])
)
);
declarators.push(t.variableDeclarator(handle));
});
},
},
},
};
}