1- /* eslint-disable @typescript-eslint/no-non-null-assertion */
2- import { readdirSync } from "fs" ;
3- import { dirname , join , normalize , parse , resolve , sep } from "path" ;
4- import { type NodePath } from "@babel/traverse" ;
1+ import tBabelTypes , { type CallExpression } from "@babel/types" ;
52
6- import t from "@babel/types" ;
7-
8- export type BabelTypes = typeof t ;
3+ export type BabelTypes = typeof tBabelTypes ;
94
105export interface PluginOpts {
116 target ?: string ;
@@ -18,165 +13,28 @@ export interface PluginState {
1813 filename : string ;
1914}
2015
21- export const allowedModules = new Set (
22- getFilesWithoutExtension ( join ( __dirname , "../components" ) ) ,
23- ) ;
24-
25- function getFilesWithoutExtension ( dirPath : string ) {
26- // Read all files and directories inside dirPath synchronously
27- const entries = readdirSync ( dirPath , { withFileTypes : true } ) ;
28-
29- // Filter only files (ignore directories)
30- const files = entries . filter (
31- ( entry ) =>
32- entry . isFile ( ) &&
33- / \. [ j t ] s x ? $ / . exec ( entry . name ) &&
34- ! / i n d e x \. [ j t ] s x ? $ / . exec ( entry . name ) ,
35- ) ;
36-
37- // For each file, get the filename without extension
38- const filesWithoutExt = files . map ( ( file ) => parse ( file . name ) . name ) ;
39-
40- return filesWithoutExt ;
41- }
42-
43- export function isInsideModule ( filename : string , module : string ) : boolean {
44- const normalized = normalize ( filename ) ;
45-
46- // Match exact module name
47- if ( normalized === module ) {
48- return true ;
49- }
50-
51- // Absolute posix paths
52- if ( normalized . startsWith ( `${ module } /` ) ) {
53- return true ;
16+ export function getInteropRequireDefaultSource (
17+ init : CallExpression ,
18+ t : BabelTypes ,
19+ ) {
20+ if ( ! t . isIdentifier ( init . callee , { name : "_interopRequireDefault" } ) ) {
21+ return ;
5422 }
5523
56- // Check for our local development structure
57- if ( normalized . includes ( `${ sep } ${ module } ${ sep } src${ sep } ` ) ) {
58- // Ignore the test files
59- return ! normalized . includes ( "__tests__" ) ;
60- }
24+ const interopArg = init . arguments . at ( 0 ) ;
6125
62- // Match classic node_modules
63- if ( normalized . includes ( `${ sep } node_modules${ sep } ${ module } ${ sep } ` ) ) {
64- return true ;
65- }
66-
67- // Match Yarn PnP .zip-style paths (e.g., .zip/node_modules/${module}/)
6826 if (
69- normalized . includes ( `${ sep } .zip${ sep } node_modules${ sep } ${ module } ${ sep } ` )
27+ ! t . isCallExpression ( interopArg ) ||
28+ ! t . isIdentifier ( interopArg . callee , { name : "require" } )
7029 ) {
71- return true ;
72- }
73-
74- // Match Yarn .yarn/cache/${module}/*
75- if ( normalized . includes ( `${ sep } .yarn${ sep } cache${ sep } ${ module } ${ sep } ` ) ) {
76- return true ;
30+ return ;
7731 }
7832
79- return false ;
80- }
81-
82- export function isPackageImport (
83- path : NodePath < t . ImportDeclaration | t . ExportNamedDeclaration > ,
84- ) {
85- const source = path . node . source ?. value ;
86- if ( ! source ) {
87- return false ;
88- }
89-
90- return source === "react-native" || source === "react-native-web" ;
91- }
92-
93- export function shouldTransformImport (
94- path : NodePath < t . ImportDeclaration | t . ExportNamedDeclaration > ,
95- filename : string ,
96- ) {
97- let source = path . node . source ?. value ;
98-
99- if ( ! source ) {
100- return false ;
101- }
102-
103- if ( source . startsWith ( "." ) ) {
104- source = resolve ( dirname ( filename ) , source ) ;
105- }
106-
107- // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
108- return isReactNativeSource ( source ) || isReactNativeWebSource ( source ) ;
109- }
110-
111- function isReactNativeSource ( source : string ) {
112- if ( source === "react-native" ) return true ;
113-
114- const components = source . split (
115- `react-native${ sep } Libraries${ sep } Components${ sep } ` ,
116- ) ;
117-
118- if ( components . length > 1 ) {
119- const component = components [ 1 ] ?. split ( sep ) [ 0 ] ;
120- return component && allowedModules . has ( component ) ;
121- }
122-
123- return false ;
124- }
125-
126- function isReactNativeWebSource ( source : string ) {
127- if ( source === "react-native-web" ) return true ;
128-
129- let components = source . split (
130- `react-native-web${ sep } dist${ sep } commonjs${ sep } exports${ sep } ` ,
131- ) ;
132- if ( components . length > 1 ) {
133- const component = components [ 1 ] ?. split ( sep ) [ 0 ] ;
134- return component && allowedModules . has ( component ) ;
135- }
136-
137- components = source . split (
138- `react-native-web${ sep } dist${ sep } module${ sep } exports${ sep } ` ,
139- ) ;
140- if ( components . length > 1 ) {
141- const component = components [ 1 ] ?. split ( sep ) [ 0 ] ;
142- return component && allowedModules . has ( component ) ;
143- }
144-
145- return false ;
146- }
147-
148- export function shouldTransformRequire (
149- t : BabelTypes ,
150- node : t . VariableDeclaration ,
151- basePath : string ,
152- ) {
153- const { declarations } = node ;
154-
155- const declaration = declarations [ 0 ] ;
156-
157- if ( declarations . length > 1 || ! declaration ) {
158- return false ;
159- }
160- const { id, init } = declaration ;
161-
162- let source =
163- ( t . isObjectPattern ( id ) || t . isIdentifier ( id ) ) &&
164- t . isCallExpression ( init ) &&
165- t . isIdentifier ( init . callee ) &&
166- init . callee . name === "require" &&
167- init . arguments . length === 1 &&
168- "value" in init . arguments [ 0 ] ! &&
169- typeof init . arguments [ 0 ] . value === "string" &&
170- init . arguments [ 0 ] . value ;
171-
172- if ( ! source ) {
173- return false ;
174- }
33+ const requireArg = interopArg . arguments . at ( 0 ) ;
17534
176- if ( source . startsWith ( "." ) ) {
177- source = resolve ( basePath , source ) ;
35+ if ( ! t . isStringLiteral ( requireArg ) ) {
36+ return ;
17837 }
17938
180- // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
181- return isReactNativeSource ( source ) || isReactNativeWebSource ( source ) ;
39+ return requireArg . value ;
18240}
0 commit comments