react-reinspect 0.1.7 → 0.1.9

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.
Files changed (66) hide show
  1. package/README.md +35 -41
  2. package/dist/lib/autoWrap.d.ts +1 -8
  3. package/dist/lib/autoWrap.d.ts.map +1 -1
  4. package/dist/lib/constants.d.ts +3 -2
  5. package/dist/lib/constants.d.ts.map +1 -1
  6. package/dist/lib/context.d.ts +1 -3
  7. package/dist/lib/context.d.ts.map +1 -1
  8. package/dist/lib/core/renderCounter.d.ts +11 -0
  9. package/dist/lib/core/renderCounter.d.ts.map +1 -0
  10. package/dist/lib/core/state.d.ts +67 -0
  11. package/dist/lib/core/state.d.ts.map +1 -0
  12. package/dist/lib/dom/styleInjection.d.ts +2 -0
  13. package/dist/lib/dom/styleInjection.d.ts.map +1 -0
  14. package/dist/lib/index.cjs +1 -1
  15. package/dist/lib/index.d.ts +2 -5
  16. package/dist/lib/index.d.ts.map +1 -1
  17. package/dist/lib/index.js +781 -1199
  18. package/dist/lib/injectStyles.d.ts +1 -1
  19. package/dist/lib/injectStyles.d.ts.map +1 -1
  20. package/dist/lib/internal/auto-wrap.cjs +1 -0
  21. package/dist/lib/internal/auto-wrap.d.ts +9 -0
  22. package/dist/lib/internal/auto-wrap.d.ts.map +1 -0
  23. package/dist/lib/internal/auto-wrap.js +12 -0
  24. package/dist/lib/propsInspector.d.ts +8 -2
  25. package/dist/lib/propsInspector.d.ts.map +1 -1
  26. package/dist/lib/react/context.d.ts +3 -0
  27. package/dist/lib/react/context.d.ts.map +1 -0
  28. package/dist/lib/react/focusTrap.d.ts +3 -0
  29. package/dist/lib/react/focusTrap.d.ts.map +1 -0
  30. package/dist/lib/react/overlay.d.ts +14 -0
  31. package/dist/lib/react/overlay.d.ts.map +1 -0
  32. package/dist/lib/react/provider.d.ts +4 -0
  33. package/dist/lib/react/provider.d.ts.map +1 -0
  34. package/dist/lib/react/wrap.d.ts +11 -0
  35. package/dist/lib/react/wrap.d.ts.map +1 -0
  36. package/dist/lib/store.d.ts +1 -2
  37. package/dist/lib/store.d.ts.map +1 -1
  38. package/dist/lib/style.css +1 -1
  39. package/dist/lib/syntaxHighlight.d.ts +6 -0
  40. package/dist/lib/syntaxHighlight.d.ts.map +1 -0
  41. package/dist/lib/types.d.ts +70 -18
  42. package/dist/lib/types.d.ts.map +1 -1
  43. package/dist/lib/utils.d.ts +33 -4
  44. package/dist/lib/utils.d.ts.map +1 -1
  45. package/dist/lib/withReinspect.d.ts +1 -6
  46. package/dist/lib/withReinspect.d.ts.map +1 -1
  47. package/dist/lib/wrap-Cc3mMcCA.cjs +2 -0
  48. package/dist/lib/wrap-DkOzTshV.js +2456 -0
  49. package/dist/lib/wrapInspectableMap.d.ts +1 -1
  50. package/dist/lib/wrapInspectableMap.d.ts.map +1 -1
  51. package/dist/lib/wrapMarker.d.ts +6 -3
  52. package/dist/lib/wrapMarker.d.ts.map +1 -1
  53. package/dist/plugin/internal/autoDiscoverTransform.d.ts +26 -0
  54. package/dist/plugin/internal/autoDiscoverTransform.d.ts.map +1 -0
  55. package/dist/plugin/internal/autoDiscoverTransform.js +397 -0
  56. package/dist/plugin/reinspectAutoDiscoverPlugin.d.ts +5 -8
  57. package/dist/plugin/reinspectAutoDiscoverPlugin.d.ts.map +1 -1
  58. package/dist/plugin/reinspectAutoDiscoverPlugin.js +11 -358
  59. package/dist/plugin/reinspectNextAutoDiscoverLoader.d.ts +1 -0
  60. package/dist/plugin/reinspectNextAutoDiscoverLoader.d.ts.map +1 -1
  61. package/dist/plugin/reinspectNextAutoDiscoverLoader.js +5 -27
  62. package/dist/plugin/reinspectNextPlugin.d.ts +2 -0
  63. package/dist/plugin/reinspectNextPlugin.d.ts.map +1 -1
  64. package/dist/plugin/reinspectNextPlugin.js +19 -15
  65. package/docs/assets/screenshot-example.png +0 -0
  66. package/package.json +32 -3
@@ -1,6 +1,6 @@
1
1
  import type { ComponentType } from 'react';
2
2
  import { type WithReinspectOptions } from './withReinspect';
3
- type ComponentMap = Record<string, ComponentType<any>>;
3
+ type ComponentMap = Record<string, ComponentType<unknown>>;
4
4
  type WrapInspectableMapOptions<T extends ComponentMap> = Partial<Record<keyof T, WithReinspectOptions>> | ((key: keyof T, component: T[keyof T]) => WithReinspectOptions | undefined);
5
5
  export declare function wrapInspectableMap<T extends ComponentMap>(componentMap: T, options?: WrapInspectableMapOptions<T>): {
6
6
  [K in keyof T]: T[K];
@@ -1 +1 @@
1
- {"version":3,"file":"wrapInspectableMap.d.ts","sourceRoot":"","sources":["../../src/reinspect/wrapInspectableMap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,OAAO,CAAA;AAC1C,OAAO,EAAiB,KAAK,oBAAoB,EAAE,MAAM,iBAAiB,CAAA;AAI1E,KAAK,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,GAAG,CAAC,CAAC,CAAA;AAEtD,KAAK,yBAAyB,CAAC,CAAC,SAAS,YAAY,IACjD,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,oBAAoB,CAAC,CAAC,GAC9C,CAAC,CAAC,GAAG,EAAE,MAAM,CAAC,EAAE,SAAS,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,oBAAoB,GAAG,SAAS,CAAC,CAAA;AAE/E,wBAAgB,kBAAkB,CAAC,CAAC,SAAS,YAAY,EACvD,YAAY,EAAE,CAAC,EACf,OAAO,CAAC,EAAE,yBAAyB,CAAC,CAAC,CAAC,GACrC;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAAE,CAY1B"}
1
+ {"version":3,"file":"wrapInspectableMap.d.ts","sourceRoot":"","sources":["../../src/reinspect/wrapInspectableMap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,OAAO,CAAA;AAC1C,OAAO,EAAiB,KAAK,oBAAoB,EAAE,MAAM,iBAAiB,CAAA;AAE1E,KAAK,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC,CAAA;AAE1D,KAAK,yBAAyB,CAAC,CAAC,SAAS,YAAY,IACjD,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,oBAAoB,CAAC,CAAC,GAC9C,CAAC,CAAC,GAAG,EAAE,MAAM,CAAC,EAAE,SAAS,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,oBAAoB,GAAG,SAAS,CAAC,CAAA;AAE/E,wBAAgB,kBAAkB,CAAC,CAAC,SAAS,YAAY,EACvD,YAAY,EAAE,CAAC,EACf,OAAO,CAAC,EAAE,yBAAyB,CAAC,CAAC,CAAC,GACrC;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAAE,CAgB1B"}
@@ -1,11 +1,14 @@
1
1
  import type { ComponentType } from 'react';
2
2
  import type { AutoDiscoverScope } from './types';
3
+ type ComponentProps = Record<string, unknown>;
3
4
  export type ReinspectWrapSource = 'manual' | 'auto';
4
- export interface ReinspectWrappedMetadata {
5
+ export interface ReinspectWrappedMetadata<P extends object = ComponentProps> {
5
6
  source: ReinspectWrapSource;
6
7
  scope: AutoDiscoverScope;
7
- original: ComponentType<any>;
8
+ original: ComponentType<P>;
8
9
  }
9
10
  export declare const REINSPECT_WRAPPED_SYMBOL: unique symbol;
10
- export declare function getReinspectWrappedMetadata(component: ComponentType<any>): ReinspectWrappedMetadata | undefined;
11
+ export declare function getReinspectWrappedMetadata<P extends object>(component: ComponentType<P>): ReinspectWrappedMetadata<P> | undefined;
12
+ export declare function setReinspectWrappedMetadata<P extends object>(component: ComponentType<P>, metadata: ReinspectWrappedMetadata<P>): void;
13
+ export {};
11
14
  //# sourceMappingURL=wrapMarker.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"wrapMarker.d.ts","sourceRoot":"","sources":["../../src/reinspect/wrapMarker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,OAAO,CAAA;AAC1C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAA;AAEhD,MAAM,MAAM,mBAAmB,GAAG,QAAQ,GAAG,MAAM,CAAA;AAEnD,MAAM,WAAW,wBAAwB;IACvC,MAAM,EAAE,mBAAmB,CAAA;IAC3B,KAAK,EAAE,iBAAiB,CAAA;IAExB,QAAQ,EAAE,aAAa,CAAC,GAAG,CAAC,CAAA;CAC7B;AAED,eAAO,MAAM,wBAAwB,eAAkC,CAAA;AAEvE,wBAAgB,2BAA2B,CAEzC,SAAS,EAAE,aAAa,CAAC,GAAG,CAAC,GAC5B,wBAAwB,GAAG,SAAS,CAQtC"}
1
+ {"version":3,"file":"wrapMarker.d.ts","sourceRoot":"","sources":["../../src/reinspect/wrapMarker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,OAAO,CAAA;AAC1C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAA;AAEhD,KAAK,cAAc,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;AAE7C,MAAM,MAAM,mBAAmB,GAAG,QAAQ,GAAG,MAAM,CAAA;AAEnD,MAAM,WAAW,wBAAwB,CAAC,CAAC,SAAS,MAAM,GAAG,cAAc;IACzE,MAAM,EAAE,mBAAmB,CAAA;IAC3B,KAAK,EAAE,iBAAiB,CAAA;IACxB,QAAQ,EAAE,aAAa,CAAC,CAAC,CAAC,CAAA;CAC3B;AAED,eAAO,MAAM,wBAAwB,eAAkC,CAAA;AAMvE,wBAAgB,2BAA2B,CAAC,CAAC,SAAS,MAAM,EAC1D,SAAS,EAAE,aAAa,CAAC,CAAC,CAAC,GAC1B,wBAAwB,CAAC,CAAC,CAAC,GAAG,SAAS,CAEzC;AAED,wBAAgB,2BAA2B,CAAC,CAAC,SAAS,MAAM,EAC1D,SAAS,EAAE,aAAa,CAAC,CAAC,CAAC,EAC3B,QAAQ,EAAE,wBAAwB,CAAC,CAAC,CAAC,GACpC,IAAI,CAEN"}
@@ -0,0 +1,26 @@
1
+ export type AutoDiscoverScope = 'first-party' | 'third-party';
2
+ export type AutoDiscoverIncludeMode = 'first-party' | 'all';
3
+ export interface AutoDiscoverResolutionOptions {
4
+ include: AutoDiscoverIncludeMode;
5
+ thirdPartyAllowlist?: readonly string[];
6
+ }
7
+ export interface AutoDiscoverTransformResult {
8
+ code: string;
9
+ modified: boolean;
10
+ map: {
11
+ version: number;
12
+ sources: string[];
13
+ names: string[];
14
+ mappings: string;
15
+ file?: string | undefined;
16
+ sourceRoot?: string | undefined;
17
+ sourcesContent?: string[] | undefined;
18
+ } | null;
19
+ }
20
+ export declare function isSupportedSourceFile(id: string): boolean;
21
+ export declare function isFirstPartyModule(id: string): boolean;
22
+ export declare function shouldSkipThirdPartyModule(id: string): boolean;
23
+ export declare function normalizeModuleId(id: string): string;
24
+ export declare function resolveAutoDiscoverScope(normalizedId: string, options: AutoDiscoverResolutionOptions): AutoDiscoverScope | null;
25
+ export declare function transformModuleForAutoDiscover(code: string, fileId: string, scope: AutoDiscoverScope): AutoDiscoverTransformResult;
26
+ //# sourceMappingURL=autoDiscoverTransform.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"autoDiscoverTransform.d.ts","sourceRoot":"","sources":["../../../src/plugin/internal/autoDiscoverTransform.ts"],"names":[],"mappings":"AAKA,MAAM,MAAM,iBAAiB,GAAG,aAAa,GAAG,aAAa,CAAA;AAC7D,MAAM,MAAM,uBAAuB,GAAG,aAAa,GAAG,KAAK,CAAA;AAE3D,MAAM,WAAW,6BAA6B;IAC5C,OAAO,EAAE,uBAAuB,CAAA;IAChC,mBAAmB,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;CACxC;AAiBD,MAAM,WAAW,2BAA2B;IAC1C,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,OAAO,CAAA;IACjB,GAAG,EAAE;QACH,OAAO,EAAE,MAAM,CAAA;QACf,OAAO,EAAE,MAAM,EAAE,CAAA;QACjB,KAAK,EAAE,MAAM,EAAE,CAAA;QACf,QAAQ,EAAE,MAAM,CAAA;QAChB,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;QACzB,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;QAC/B,cAAc,CAAC,EAAE,MAAM,EAAE,GAAG,SAAS,CAAA;KACtC,GAAG,IAAI,CAAA;CACT;AAuQD,wBAAgB,qBAAqB,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAMzD;AAED,wBAAgB,kBAAkB,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAEtD;AAED,wBAAgB,0BAA0B,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAE9D;AAaD,wBAAgB,iBAAiB,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAGpD;AAED,wBAAgB,wBAAwB,CACtC,YAAY,EAAE,MAAM,EACpB,OAAO,EAAE,6BAA6B,GACrC,iBAAiB,GAAG,IAAI,CAyB1B;AAED,wBAAgB,8BAA8B,CAC5C,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,iBAAiB,GACvB,2BAA2B,CA+P7B"}
@@ -0,0 +1,397 @@
1
+ import path from 'node:path';
2
+ import { parse } from '@babel/parser';
3
+ import { generate } from '@babel/generator';
4
+ import * as t from '@babel/types';
5
+ const AUTO_WRAP_IMPORT_SOURCE = 'react-reinspect/internal/auto-wrap';
6
+ const AUTO_WRAP_IMPORT_NAME = 'autoWrapInspectable';
7
+ const DEFAULT_EXPORT_LOCAL_IDENTIFIER = '__reinspect_default_component';
8
+ const SUPPORTED_FILE_PATTERN = /\.[cm]?[jt]sx?$/;
9
+ const MAX_NODE_VISITS = 12_000;
10
+ const THIRD_PARTY_SKIP_PATTERNS = [
11
+ '/node_modules/react/',
12
+ '/node_modules/react-dom/',
13
+ '/node_modules/scheduler/',
14
+ '/node_modules/@vite/',
15
+ '/node_modules/vite/',
16
+ '/node_modules/@react-refresh/',
17
+ ];
18
+ function isPascalCaseIdentifier(name) {
19
+ return /^[A-Z][A-Za-z0-9]*$/.test(name);
20
+ }
21
+ function inferFallbackName(filePath) {
22
+ const baseName = path.basename(filePath).replace(/\.[^.]+$/, '');
23
+ const tokens = baseName
24
+ .replace(/[^A-Za-z0-9]+/g, ' ')
25
+ .trim()
26
+ .split(/\s+/)
27
+ .filter(Boolean);
28
+ if (tokens.length === 0) {
29
+ return 'Component';
30
+ }
31
+ return tokens
32
+ .map((token) => token.slice(0, 1).toUpperCase() + token.slice(1))
33
+ .join('');
34
+ }
35
+ function unwrapExpression(node) {
36
+ let currentNode = node;
37
+ while (t.isParenthesizedExpression(currentNode) ||
38
+ t.isTSAsExpression(currentNode) ||
39
+ t.isTSTypeAssertion(currentNode) ||
40
+ t.isTSNonNullExpression(currentNode) ||
41
+ t.isTSInstantiationExpression(currentNode) ||
42
+ t.isTypeCastExpression(currentNode)) {
43
+ currentNode = currentNode.expression;
44
+ }
45
+ return currentNode;
46
+ }
47
+ function isReactCreateElementCall(node) {
48
+ const callee = unwrapExpression(node.callee);
49
+ if (t.isIdentifier(callee)) {
50
+ return callee.name === 'createElement';
51
+ }
52
+ if (t.isMemberExpression(callee) &&
53
+ t.isIdentifier(callee.object) &&
54
+ callee.object.name === 'React' &&
55
+ t.isIdentifier(callee.property)) {
56
+ return callee.property.name === 'createElement';
57
+ }
58
+ return false;
59
+ }
60
+ function isAstNode(value) {
61
+ return (typeof value === 'object' &&
62
+ value !== null &&
63
+ 'type' in value &&
64
+ typeof value.type === 'string');
65
+ }
66
+ function containsRenderableReactNode(node) {
67
+ if (!node) {
68
+ return false;
69
+ }
70
+ const stack = [node];
71
+ let visitedCount = 0;
72
+ while (stack.length > 0) {
73
+ const nextNode = stack.pop();
74
+ if (!nextNode) {
75
+ continue;
76
+ }
77
+ visitedCount += 1;
78
+ if (visitedCount > MAX_NODE_VISITS) {
79
+ return false;
80
+ }
81
+ if (t.isJSXElement(nextNode) || t.isJSXFragment(nextNode)) {
82
+ return true;
83
+ }
84
+ if (t.isCallExpression(nextNode) && isReactCreateElementCall(nextNode)) {
85
+ return true;
86
+ }
87
+ const visitorKeys = t.VISITOR_KEYS[nextNode.type] ?? [];
88
+ for (const visitorKey of visitorKeys) {
89
+ const value = nextNode[visitorKey];
90
+ if (Array.isArray(value)) {
91
+ for (const item of value) {
92
+ if (isAstNode(item)) {
93
+ stack.push(item);
94
+ }
95
+ }
96
+ continue;
97
+ }
98
+ if (isAstNode(value)) {
99
+ stack.push(value);
100
+ }
101
+ }
102
+ }
103
+ return false;
104
+ }
105
+ function isComponentFunction(node) {
106
+ if (t.isBlockStatement(node.body)) {
107
+ return containsRenderableReactNode(node.body);
108
+ }
109
+ return containsRenderableReactNode(node.body);
110
+ }
111
+ function isMemoForwardRefCallee(callee) {
112
+ const expression = unwrapExpression(callee);
113
+ if (t.isIdentifier(expression)) {
114
+ return expression.name === 'memo' || expression.name === 'forwardRef';
115
+ }
116
+ if (t.isMemberExpression(expression) &&
117
+ t.isIdentifier(expression.object) &&
118
+ expression.object.name === 'React' &&
119
+ t.isIdentifier(expression.property)) {
120
+ return (expression.property.name === 'memo' ||
121
+ expression.property.name === 'forwardRef');
122
+ }
123
+ return false;
124
+ }
125
+ function isMemoForwardRefCall(node) {
126
+ const callee = unwrapExpression(node.callee);
127
+ if (!isMemoForwardRefCallee(callee)) {
128
+ return false;
129
+ }
130
+ if (node.arguments.length === 0) {
131
+ return false;
132
+ }
133
+ const firstArgument = node.arguments[0];
134
+ if (!t.isExpression(firstArgument)) {
135
+ return false;
136
+ }
137
+ const componentCandidate = unwrapExpression(firstArgument);
138
+ if (t.isArrowFunctionExpression(componentCandidate) ||
139
+ t.isFunctionExpression(componentCandidate)) {
140
+ return isComponentFunction(componentCandidate);
141
+ }
142
+ if (t.isIdentifier(componentCandidate)) {
143
+ return isPascalCaseIdentifier(componentCandidate.name);
144
+ }
145
+ return true;
146
+ }
147
+ function isWrappedInitializer(node) {
148
+ const expression = unwrapExpression(node);
149
+ if (!t.isCallExpression(expression)) {
150
+ return false;
151
+ }
152
+ const callee = unwrapExpression(expression.callee);
153
+ return (t.isIdentifier(callee) &&
154
+ (callee.name === AUTO_WRAP_IMPORT_NAME || callee.name === 'withReinspect'));
155
+ }
156
+ function isComponentInitializer(node) {
157
+ const expression = unwrapExpression(node);
158
+ if (t.isArrowFunctionExpression(expression) || t.isFunctionExpression(expression)) {
159
+ return isComponentFunction(expression);
160
+ }
161
+ if (t.isCallExpression(expression)) {
162
+ return isMemoForwardRefCall(expression);
163
+ }
164
+ return false;
165
+ }
166
+ function createAutoWrapCall(expression, componentName, scope, fallbackName) {
167
+ const metadataProperties = [
168
+ t.objectProperty(t.identifier('scope'), t.stringLiteral(scope)),
169
+ t.objectProperty(t.identifier('fallbackName'), t.stringLiteral(fallbackName)),
170
+ ];
171
+ if (componentName) {
172
+ metadataProperties.unshift(t.objectProperty(t.identifier('componentName'), t.stringLiteral(componentName)));
173
+ }
174
+ return t.callExpression(t.identifier(AUTO_WRAP_IMPORT_NAME), [
175
+ expression,
176
+ t.objectExpression(metadataProperties),
177
+ ]);
178
+ }
179
+ function createFunctionWrapAssignment(name, scope, fallbackName) {
180
+ return t.expressionStatement(t.assignmentExpression('=', t.identifier(name), createAutoWrapCall(t.identifier(name), name, scope, fallbackName)));
181
+ }
182
+ function functionDeclarationToExpression(node) {
183
+ const expression = t.functionExpression(node.id, node.params, node.body, node.generator, node.async);
184
+ if (node.typeParameters) {
185
+ expression.typeParameters = node.typeParameters;
186
+ }
187
+ if (node.returnType) {
188
+ expression.returnType = node.returnType;
189
+ }
190
+ return expression;
191
+ }
192
+ export function isSupportedSourceFile(id) {
193
+ if (id.startsWith('\0') || id.startsWith('/@')) {
194
+ return false;
195
+ }
196
+ return SUPPORTED_FILE_PATTERN.test(id);
197
+ }
198
+ export function isFirstPartyModule(id) {
199
+ return id.includes('/src/') && !id.includes('/node_modules/');
200
+ }
201
+ export function shouldSkipThirdPartyModule(id) {
202
+ return THIRD_PARTY_SKIP_PATTERNS.some((pattern) => id.includes(pattern));
203
+ }
204
+ function isInThirdPartyAllowlist(moduleId, allowlist) {
205
+ if (!allowlist || allowlist.length === 0) {
206
+ return false;
207
+ }
208
+ return allowlist.some((pattern) => moduleId.includes(pattern));
209
+ }
210
+ export function normalizeModuleId(id) {
211
+ const withoutQuery = id.split('?')[0] ?? '';
212
+ return withoutQuery.split(path.sep).join('/');
213
+ }
214
+ export function resolveAutoDiscoverScope(normalizedId, options) {
215
+ if (normalizedId.includes('/node_modules/react-reinspect/')) {
216
+ return null;
217
+ }
218
+ if (normalizedId.includes('/src/reinspect/')) {
219
+ return null;
220
+ }
221
+ if (isFirstPartyModule(normalizedId)) {
222
+ return 'first-party';
223
+ }
224
+ if (options.include !== 'all') {
225
+ return null;
226
+ }
227
+ if (shouldSkipThirdPartyModule(normalizedId) &&
228
+ !isInThirdPartyAllowlist(normalizedId, options.thirdPartyAllowlist)) {
229
+ return null;
230
+ }
231
+ return 'third-party';
232
+ }
233
+ export function transformModuleForAutoDiscover(code, fileId, scope) {
234
+ let ast;
235
+ try {
236
+ ast = parse(code, {
237
+ sourceType: 'module',
238
+ plugins: ['typescript', 'jsx'],
239
+ });
240
+ }
241
+ catch {
242
+ return { code, modified: false, map: null };
243
+ }
244
+ const program = ast.program;
245
+ const fallbackName = inferFallbackName(fileId);
246
+ let modified = false;
247
+ let autoWrapImportDeclaration;
248
+ let hasAutoWrapImport = false;
249
+ for (const statement of program.body) {
250
+ if (!t.isImportDeclaration(statement)) {
251
+ continue;
252
+ }
253
+ if (statement.source.value !== AUTO_WRAP_IMPORT_SOURCE) {
254
+ continue;
255
+ }
256
+ autoWrapImportDeclaration = statement;
257
+ hasAutoWrapImport = statement.specifiers.some((specifier) => t.isImportSpecifier(specifier) &&
258
+ t.isIdentifier(specifier.imported) &&
259
+ specifier.imported.name === AUTO_WRAP_IMPORT_NAME);
260
+ }
261
+ for (let index = 0; index < program.body.length; index += 1) {
262
+ const statement = program.body[index];
263
+ if (t.isFunctionDeclaration(statement) && statement.id) {
264
+ if (isPascalCaseIdentifier(statement.id.name) &&
265
+ isComponentFunction(statement)) {
266
+ program.body.splice(index + 1, 0, createFunctionWrapAssignment(statement.id.name, scope, fallbackName));
267
+ index += 1;
268
+ modified = true;
269
+ }
270
+ continue;
271
+ }
272
+ if (t.isExportNamedDeclaration(statement) &&
273
+ statement.declaration &&
274
+ t.isFunctionDeclaration(statement.declaration) &&
275
+ statement.declaration.id &&
276
+ isPascalCaseIdentifier(statement.declaration.id.name) &&
277
+ isComponentFunction(statement.declaration)) {
278
+ program.body.splice(index + 1, 0, createFunctionWrapAssignment(statement.declaration.id.name, scope, fallbackName));
279
+ index += 1;
280
+ modified = true;
281
+ continue;
282
+ }
283
+ const processVariableDeclaration = (declaration) => {
284
+ let declarationChanged = false;
285
+ for (const declarator of declaration.declarations) {
286
+ if (!t.isIdentifier(declarator.id) || !declarator.init) {
287
+ continue;
288
+ }
289
+ if (!isPascalCaseIdentifier(declarator.id.name)) {
290
+ continue;
291
+ }
292
+ if (!isComponentInitializer(declarator.init)) {
293
+ continue;
294
+ }
295
+ if (isWrappedInitializer(declarator.init)) {
296
+ continue;
297
+ }
298
+ declarator.init = createAutoWrapCall(declarator.init, declarator.id.name, scope, fallbackName);
299
+ declarationChanged = true;
300
+ }
301
+ return declarationChanged;
302
+ };
303
+ if (t.isVariableDeclaration(statement)) {
304
+ if (processVariableDeclaration(statement)) {
305
+ modified = true;
306
+ }
307
+ continue;
308
+ }
309
+ if (t.isExportNamedDeclaration(statement) &&
310
+ statement.declaration &&
311
+ t.isVariableDeclaration(statement.declaration)) {
312
+ if (processVariableDeclaration(statement.declaration)) {
313
+ modified = true;
314
+ }
315
+ continue;
316
+ }
317
+ if (!t.isExportDefaultDeclaration(statement)) {
318
+ continue;
319
+ }
320
+ const declaration = statement.declaration;
321
+ if (t.isFunctionDeclaration(declaration)) {
322
+ if (declaration.id &&
323
+ isPascalCaseIdentifier(declaration.id.name) &&
324
+ isComponentFunction(declaration)) {
325
+ const localName = declaration.id.name;
326
+ const replacementStatements = [
327
+ declaration,
328
+ createFunctionWrapAssignment(localName, scope, fallbackName),
329
+ t.exportDefaultDeclaration(t.identifier(localName)),
330
+ ];
331
+ program.body.splice(index, 1, ...replacementStatements);
332
+ index += replacementStatements.length - 1;
333
+ modified = true;
334
+ continue;
335
+ }
336
+ if (!declaration.id && isComponentFunction(declaration)) {
337
+ const localIdentifier = t.identifier(DEFAULT_EXPORT_LOCAL_IDENTIFIER);
338
+ const wrappedValue = createAutoWrapCall(functionDeclarationToExpression(declaration), undefined, scope, fallbackName);
339
+ const replacementStatements = [
340
+ t.variableDeclaration('const', [
341
+ t.variableDeclarator(localIdentifier, wrappedValue),
342
+ ]),
343
+ t.exportDefaultDeclaration(localIdentifier),
344
+ ];
345
+ program.body.splice(index, 1, ...replacementStatements);
346
+ index += replacementStatements.length - 1;
347
+ modified = true;
348
+ }
349
+ continue;
350
+ }
351
+ if (!t.isExpression(declaration)) {
352
+ continue;
353
+ }
354
+ if (!isComponentInitializer(declaration) || isWrappedInitializer(declaration)) {
355
+ continue;
356
+ }
357
+ const localIdentifier = t.identifier(DEFAULT_EXPORT_LOCAL_IDENTIFIER);
358
+ const wrappedValue = createAutoWrapCall(declaration, undefined, scope, fallbackName);
359
+ const replacementStatements = [
360
+ t.variableDeclaration('const', [
361
+ t.variableDeclarator(localIdentifier, wrappedValue),
362
+ ]),
363
+ t.exportDefaultDeclaration(localIdentifier),
364
+ ];
365
+ program.body.splice(index, 1, ...replacementStatements);
366
+ index += replacementStatements.length - 1;
367
+ modified = true;
368
+ }
369
+ if (!modified) {
370
+ return { code, modified: false, map: null };
371
+ }
372
+ if (autoWrapImportDeclaration) {
373
+ if (!hasAutoWrapImport) {
374
+ autoWrapImportDeclaration.specifiers.push(t.importSpecifier(t.identifier(AUTO_WRAP_IMPORT_NAME), t.identifier(AUTO_WRAP_IMPORT_NAME)));
375
+ }
376
+ }
377
+ else {
378
+ let insertIndex = 0;
379
+ while (insertIndex < program.body.length &&
380
+ t.isImportDeclaration(program.body[insertIndex])) {
381
+ insertIndex += 1;
382
+ }
383
+ program.body.splice(insertIndex, 0, t.importDeclaration([
384
+ t.importSpecifier(t.identifier(AUTO_WRAP_IMPORT_NAME), t.identifier(AUTO_WRAP_IMPORT_NAME)),
385
+ ], t.stringLiteral(AUTO_WRAP_IMPORT_SOURCE)));
386
+ }
387
+ const output = generate(ast, {
388
+ jsescOption: { minimal: true },
389
+ sourceMaps: true,
390
+ sourceFileName: fileId,
391
+ }, code);
392
+ return {
393
+ code: output.code,
394
+ modified: true,
395
+ map: output.map ?? null,
396
+ };
397
+ }
@@ -1,11 +1,8 @@
1
1
  import type { Plugin } from 'vite';
2
- type AutoDiscoverScope = 'first-party' | 'third-party';
3
- export interface AutoDiscoverTransformResult {
4
- code: string;
5
- modified: boolean;
2
+ import { type AutoDiscoverIncludeMode } from './internal/autoDiscoverTransform';
3
+ export interface ReinspectAutoDiscoverPluginOptions {
4
+ include?: AutoDiscoverIncludeMode;
5
+ thirdPartyAllowlist?: readonly string[];
6
6
  }
7
- export declare function shouldSkipThirdPartyModule(id: string): boolean;
8
- export declare function transformModuleForAutoDiscover(code: string, fileId: string, scope: AutoDiscoverScope): AutoDiscoverTransformResult;
9
- export declare function reinspectAutoDiscoverPlugin(): Plugin;
10
- export {};
7
+ export declare function reinspectAutoDiscoverPlugin(options?: ReinspectAutoDiscoverPluginOptions): Plugin;
11
8
  //# sourceMappingURL=reinspectAutoDiscoverPlugin.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"reinspectAutoDiscoverPlugin.d.ts","sourceRoot":"","sources":["../../src/plugin/reinspectAutoDiscoverPlugin.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAElC,KAAK,iBAAiB,GAAG,aAAa,GAAG,aAAa,CAAA;AAgBtD,MAAM,WAAW,2BAA2B;IAC1C,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,OAAO,CAAA;CAClB;AAmQD,wBAAgB,0BAA0B,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAE9D;AAED,wBAAgB,8BAA8B,CAC5C,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,iBAAiB,GACvB,2BAA2B,CAwP7B;AAED,wBAAgB,2BAA2B,IAAI,MAAM,CA2CpD"}
1
+ {"version":3,"file":"reinspectAutoDiscoverPlugin.d.ts","sourceRoot":"","sources":["../../src/plugin/reinspectAutoDiscoverPlugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAClC,OAAO,EAKL,KAAK,uBAAuB,EAC7B,MAAM,kCAAkC,CAAA;AAEzC,MAAM,WAAW,kCAAkC;IACjD,OAAO,CAAC,EAAE,uBAAuB,CAAA;IACjC,mBAAmB,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;CACxC;AAWD,wBAAgB,2BAA2B,CACzC,OAAO,CAAC,EAAE,kCAAkC,GAC3C,MAAM,CAqCR"}