@wyw-in-js/transform 0.1.0

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 (480) hide show
  1. package/LICENSE +21 -0
  2. package/esm/babel.js +2 -0
  3. package/esm/babel.js.map +1 -0
  4. package/esm/cache.js +80 -0
  5. package/esm/cache.js.map +1 -0
  6. package/esm/debug/fileReporter.js +144 -0
  7. package/esm/debug/fileReporter.js.map +1 -0
  8. package/esm/evaluators/index.js +14 -0
  9. package/esm/evaluators/index.js.map +1 -0
  10. package/esm/index.js +24 -0
  11. package/esm/index.js.map +1 -0
  12. package/esm/module.js +316 -0
  13. package/esm/module.js.map +1 -0
  14. package/esm/options/babel-merge.d.js +2 -0
  15. package/esm/options/babel-merge.d.js.map +1 -0
  16. package/esm/options/buildOptions.js +28 -0
  17. package/esm/options/buildOptions.js.map +1 -0
  18. package/esm/options/loadBabelOptions.js +24 -0
  19. package/esm/options/loadBabelOptions.js.map +1 -0
  20. package/esm/plugins/babel-transform.js +53 -0
  21. package/esm/plugins/babel-transform.js.map +1 -0
  22. package/esm/plugins/collector.js +66 -0
  23. package/esm/plugins/collector.js.map +1 -0
  24. package/esm/plugins/dynamic-import.js +28 -0
  25. package/esm/plugins/dynamic-import.js.map +1 -0
  26. package/esm/plugins/preeval.js +75 -0
  27. package/esm/plugins/preeval.js.map +1 -0
  28. package/esm/plugins/shaker.js +296 -0
  29. package/esm/plugins/shaker.js.map +1 -0
  30. package/esm/shaker.js +50 -0
  31. package/esm/shaker.js.map +1 -0
  32. package/esm/transform/BaseEntrypoint.js +164 -0
  33. package/esm/transform/BaseEntrypoint.js.map +1 -0
  34. package/esm/transform/Entrypoint.helpers.js +168 -0
  35. package/esm/transform/Entrypoint.helpers.js.map +1 -0
  36. package/esm/transform/Entrypoint.js +212 -0
  37. package/esm/transform/Entrypoint.js.map +1 -0
  38. package/esm/transform/Entrypoint.types.js +2 -0
  39. package/esm/transform/Entrypoint.types.js.map +1 -0
  40. package/esm/transform/EvaluatedEntrypoint.js +6 -0
  41. package/esm/transform/EvaluatedEntrypoint.js.map +1 -0
  42. package/esm/transform/actions/AbortError.js +8 -0
  43. package/esm/transform/actions/AbortError.js.map +1 -0
  44. package/esm/transform/actions/BaseAction.js +134 -0
  45. package/esm/transform/actions/BaseAction.js.map +1 -0
  46. package/esm/transform/actions/UnprocessedEntrypointError.js +8 -0
  47. package/esm/transform/actions/UnprocessedEntrypointError.js.map +1 -0
  48. package/esm/transform/actions/actionRunner.js +73 -0
  49. package/esm/transform/actions/actionRunner.js.map +1 -0
  50. package/esm/transform/actions/types.js +2 -0
  51. package/esm/transform/actions/types.js.map +1 -0
  52. package/esm/transform/generators/baseProcessingHandlers.js +22 -0
  53. package/esm/transform/generators/baseProcessingHandlers.js.map +1 -0
  54. package/esm/transform/generators/collect.js +58 -0
  55. package/esm/transform/generators/collect.js.map +1 -0
  56. package/esm/transform/generators/evalFile.js +50 -0
  57. package/esm/transform/generators/evalFile.js.map +1 -0
  58. package/esm/transform/generators/explodeReexports.js +63 -0
  59. package/esm/transform/generators/explodeReexports.js.map +1 -0
  60. package/esm/transform/generators/extract.js +120 -0
  61. package/esm/transform/generators/extract.js.map +1 -0
  62. package/esm/transform/generators/getExports.js +73 -0
  63. package/esm/transform/generators/getExports.js.map +1 -0
  64. package/esm/transform/generators/index.js +13 -0
  65. package/esm/transform/generators/index.js.map +1 -0
  66. package/esm/transform/generators/processEntrypoint.js +43 -0
  67. package/esm/transform/generators/processEntrypoint.js.map +1 -0
  68. package/esm/transform/generators/processImports.js +23 -0
  69. package/esm/transform/generators/processImports.js.map +1 -0
  70. package/esm/transform/generators/resolveImports.js +148 -0
  71. package/esm/transform/generators/resolveImports.js.map +1 -0
  72. package/esm/transform/generators/transform.js +117 -0
  73. package/esm/transform/generators/transform.js.map +1 -0
  74. package/esm/transform/generators/workflow.js +82 -0
  75. package/esm/transform/generators/workflow.js.map +1 -0
  76. package/esm/transform/helpers/loadLinariaOptions.js +66 -0
  77. package/esm/transform/helpers/loadLinariaOptions.js.map +1 -0
  78. package/esm/transform/helpers/withDefaultServices.js +21 -0
  79. package/esm/transform/helpers/withDefaultServices.js.map +1 -0
  80. package/esm/transform/rootLog.js +3 -0
  81. package/esm/transform/rootLog.js.map +1 -0
  82. package/esm/transform/types.js +2 -0
  83. package/esm/transform/types.js.map +1 -0
  84. package/esm/transform.js +121 -0
  85. package/esm/transform.js.map +1 -0
  86. package/esm/types.js +2 -0
  87. package/esm/types.js.map +1 -0
  88. package/esm/utils/EventEmitter.js +52 -0
  89. package/esm/utils/EventEmitter.js.map +1 -0
  90. package/esm/utils/ShakerMetadata.js +2 -0
  91. package/esm/utils/ShakerMetadata.js.map +1 -0
  92. package/esm/utils/TransformMetadata.js +11 -0
  93. package/esm/utils/TransformMetadata.js.map +1 -0
  94. package/esm/utils/addIdentifierToLinariaPreval.js +68 -0
  95. package/esm/utils/addIdentifierToLinariaPreval.js.map +1 -0
  96. package/esm/utils/collectExportsAndImports.js +974 -0
  97. package/esm/utils/collectExportsAndImports.js.map +1 -0
  98. package/esm/utils/collectTemplateDependencies.js +219 -0
  99. package/esm/utils/collectTemplateDependencies.js.map +1 -0
  100. package/esm/utils/createId.js +6 -0
  101. package/esm/utils/createId.js.map +1 -0
  102. package/esm/utils/dispose-polyfill.js +6 -0
  103. package/esm/utils/dispose-polyfill.js.map +1 -0
  104. package/esm/utils/findIdentifiers.js +62 -0
  105. package/esm/utils/findIdentifiers.js.map +1 -0
  106. package/esm/utils/findPackageJSON.js +22 -0
  107. package/esm/utils/findPackageJSON.js.map +1 -0
  108. package/esm/utils/getFileIdx.js +10 -0
  109. package/esm/utils/getFileIdx.js.map +1 -0
  110. package/esm/utils/getPluginKey.js +13 -0
  111. package/esm/utils/getPluginKey.js.map +1 -0
  112. package/esm/utils/getScope.js +6 -0
  113. package/esm/utils/getScope.js.map +1 -0
  114. package/esm/utils/getSource.js +15 -0
  115. package/esm/utils/getSource.js.map +1 -0
  116. package/esm/utils/getTagProcessor.js +304 -0
  117. package/esm/utils/getTagProcessor.js.map +1 -0
  118. package/esm/utils/getVisitorKeys.js +5 -0
  119. package/esm/utils/getVisitorKeys.js.map +1 -0
  120. package/esm/utils/hasLinariaPreval.js +7 -0
  121. package/esm/utils/hasLinariaPreval.js.map +1 -0
  122. package/esm/utils/isExports.js +22 -0
  123. package/esm/utils/isExports.js.map +1 -0
  124. package/esm/utils/isGlobal.js +6 -0
  125. package/esm/utils/isGlobal.js.map +1 -0
  126. package/esm/utils/isNode.js +2 -0
  127. package/esm/utils/isNode.js.map +1 -0
  128. package/esm/utils/isNodePath.js +4 -0
  129. package/esm/utils/isNodePath.js.map +1 -0
  130. package/esm/utils/isNotNull.js +4 -0
  131. package/esm/utils/isNotNull.js.map +1 -0
  132. package/esm/utils/isRemoved.js +46 -0
  133. package/esm/utils/isRemoved.js.map +1 -0
  134. package/esm/utils/isRequire.js +13 -0
  135. package/esm/utils/isRequire.js.map +1 -0
  136. package/esm/utils/isSerializable.js +13 -0
  137. package/esm/utils/isSerializable.js.map +1 -0
  138. package/esm/utils/isTypedNode.js +6 -0
  139. package/esm/utils/isTypedNode.js.map +1 -0
  140. package/esm/utils/isUnnecessaryReactCall.js +72 -0
  141. package/esm/utils/isUnnecessaryReactCall.js.map +1 -0
  142. package/esm/utils/peek.js +2 -0
  143. package/esm/utils/peek.js.map +1 -0
  144. package/esm/utils/processTemplateExpression.js +11 -0
  145. package/esm/utils/processTemplateExpression.js.map +1 -0
  146. package/esm/utils/removeDangerousCode.js +127 -0
  147. package/esm/utils/removeDangerousCode.js.map +1 -0
  148. package/esm/utils/scopeHelpers.js +514 -0
  149. package/esm/utils/scopeHelpers.js.map +1 -0
  150. package/esm/utils/traversalCache.js +28 -0
  151. package/esm/utils/traversalCache.js.map +1 -0
  152. package/esm/utils/unwrapSequence.js +14 -0
  153. package/esm/utils/unwrapSequence.js.map +1 -0
  154. package/esm/utils/valueToLiteral.js +59 -0
  155. package/esm/utils/valueToLiteral.js.map +1 -0
  156. package/esm/utils/visitors/JSXElementsRemover.js +51 -0
  157. package/esm/utils/visitors/JSXElementsRemover.js.map +1 -0
  158. package/esm/vm/createVmContext.js +72 -0
  159. package/esm/vm/createVmContext.js.map +1 -0
  160. package/esm/vm/process.js +29 -0
  161. package/esm/vm/process.js.map +1 -0
  162. package/lib/babel.js +2 -0
  163. package/lib/babel.js.map +1 -0
  164. package/lib/cache.js +87 -0
  165. package/lib/cache.js.map +1 -0
  166. package/lib/debug/fileReporter.js +153 -0
  167. package/lib/debug/fileReporter.js.map +1 -0
  168. package/lib/evaluators/index.js +20 -0
  169. package/lib/evaluators/index.js.map +1 -0
  170. package/lib/index.js +225 -0
  171. package/lib/index.js.map +1 -0
  172. package/lib/module.js +327 -0
  173. package/lib/module.js.map +1 -0
  174. package/lib/options/babel-merge.d.js +2 -0
  175. package/lib/options/babel-merge.d.js.map +1 -0
  176. package/lib/options/buildOptions.js +35 -0
  177. package/lib/options/buildOptions.js.map +1 -0
  178. package/lib/options/loadBabelOptions.js +31 -0
  179. package/lib/options/loadBabelOptions.js.map +1 -0
  180. package/lib/plugins/babel-transform.js +60 -0
  181. package/lib/plugins/babel-transform.js.map +1 -0
  182. package/lib/plugins/collector.js +76 -0
  183. package/lib/plugins/collector.js.map +1 -0
  184. package/lib/plugins/dynamic-import.js +34 -0
  185. package/lib/plugins/dynamic-import.js.map +1 -0
  186. package/lib/plugins/preeval.js +82 -0
  187. package/lib/plugins/preeval.js.map +1 -0
  188. package/lib/plugins/shaker.js +303 -0
  189. package/lib/plugins/shaker.js.map +1 -0
  190. package/lib/shaker.js +58 -0
  191. package/lib/shaker.js.map +1 -0
  192. package/lib/transform/BaseEntrypoint.js +175 -0
  193. package/lib/transform/BaseEntrypoint.js.map +1 -0
  194. package/lib/transform/Entrypoint.helpers.js +182 -0
  195. package/lib/transform/Entrypoint.helpers.js.map +1 -0
  196. package/lib/transform/Entrypoint.js +226 -0
  197. package/lib/transform/Entrypoint.js.map +1 -0
  198. package/lib/transform/Entrypoint.types.js +2 -0
  199. package/lib/transform/Entrypoint.types.js.map +1 -0
  200. package/lib/transform/EvaluatedEntrypoint.js +13 -0
  201. package/lib/transform/EvaluatedEntrypoint.js.map +1 -0
  202. package/lib/transform/actions/AbortError.js +16 -0
  203. package/lib/transform/actions/AbortError.js.map +1 -0
  204. package/lib/transform/actions/BaseAction.js +144 -0
  205. package/lib/transform/actions/BaseAction.js.map +1 -0
  206. package/lib/transform/actions/UnprocessedEntrypointError.js +16 -0
  207. package/lib/transform/actions/UnprocessedEntrypointError.js.map +1 -0
  208. package/lib/transform/actions/actionRunner.js +82 -0
  209. package/lib/transform/actions/actionRunner.js.map +1 -0
  210. package/lib/transform/actions/types.js +2 -0
  211. package/lib/transform/actions/types.js.map +1 -0
  212. package/lib/transform/generators/baseProcessingHandlers.js +27 -0
  213. package/lib/transform/generators/baseProcessingHandlers.js.map +1 -0
  214. package/lib/transform/generators/collect.js +66 -0
  215. package/lib/transform/generators/collect.js.map +1 -0
  216. package/lib/transform/generators/evalFile.js +57 -0
  217. package/lib/transform/generators/evalFile.js.map +1 -0
  218. package/lib/transform/generators/explodeReexports.js +70 -0
  219. package/lib/transform/generators/explodeReexports.js.map +1 -0
  220. package/lib/transform/generators/extract.js +128 -0
  221. package/lib/transform/generators/extract.js.map +1 -0
  222. package/lib/transform/generators/getExports.js +80 -0
  223. package/lib/transform/generators/getExports.js.map +1 -0
  224. package/lib/transform/generators/index.js +19 -0
  225. package/lib/transform/generators/index.js.map +1 -0
  226. package/lib/transform/generators/processEntrypoint.js +49 -0
  227. package/lib/transform/generators/processEntrypoint.js.map +1 -0
  228. package/lib/transform/generators/processImports.js +29 -0
  229. package/lib/transform/generators/processImports.js.map +1 -0
  230. package/lib/transform/generators/resolveImports.js +158 -0
  231. package/lib/transform/generators/resolveImports.js.map +1 -0
  232. package/lib/transform/generators/transform.js +130 -0
  233. package/lib/transform/generators/transform.js.map +1 -0
  234. package/lib/transform/generators/workflow.js +90 -0
  235. package/lib/transform/generators/workflow.js.map +1 -0
  236. package/lib/transform/helpers/loadLinariaOptions.js +73 -0
  237. package/lib/transform/helpers/loadLinariaOptions.js.map +1 -0
  238. package/lib/transform/helpers/withDefaultServices.js +30 -0
  239. package/lib/transform/helpers/withDefaultServices.js.map +1 -0
  240. package/lib/transform/rootLog.js +9 -0
  241. package/lib/transform/rootLog.js.map +1 -0
  242. package/lib/transform/types.js +8 -0
  243. package/lib/transform/types.js.map +1 -0
  244. package/lib/transform.js +128 -0
  245. package/lib/transform.js.map +1 -0
  246. package/lib/types.js +2 -0
  247. package/lib/types.js.map +1 -0
  248. package/lib/utils/EventEmitter.js +61 -0
  249. package/lib/utils/EventEmitter.js.map +1 -0
  250. package/lib/utils/ShakerMetadata.js +9 -0
  251. package/lib/utils/ShakerMetadata.js.map +1 -0
  252. package/lib/utils/TransformMetadata.js +19 -0
  253. package/lib/utils/TransformMetadata.js.map +1 -0
  254. package/lib/utils/addIdentifierToLinariaPreval.js +75 -0
  255. package/lib/utils/addIdentifierToLinariaPreval.js.map +1 -0
  256. package/lib/utils/collectExportsAndImports.js +990 -0
  257. package/lib/utils/collectExportsAndImports.js.map +1 -0
  258. package/lib/utils/collectTemplateDependencies.js +233 -0
  259. package/lib/utils/collectTemplateDependencies.js.map +1 -0
  260. package/lib/utils/createId.js +13 -0
  261. package/lib/utils/createId.js.map +1 -0
  262. package/lib/utils/dispose-polyfill.js +9 -0
  263. package/lib/utils/dispose-polyfill.js.map +1 -0
  264. package/lib/utils/findIdentifiers.js +73 -0
  265. package/lib/utils/findIdentifiers.js.map +1 -0
  266. package/lib/utils/findPackageJSON.js +29 -0
  267. package/lib/utils/findPackageJSON.js.map +1 -0
  268. package/lib/utils/getFileIdx.js +16 -0
  269. package/lib/utils/getFileIdx.js.map +1 -0
  270. package/lib/utils/getPluginKey.js +21 -0
  271. package/lib/utils/getPluginKey.js.map +1 -0
  272. package/lib/utils/getScope.js +12 -0
  273. package/lib/utils/getScope.js.map +1 -0
  274. package/lib/utils/getSource.js +24 -0
  275. package/lib/utils/getSource.js.map +1 -0
  276. package/lib/utils/getTagProcessor.js +318 -0
  277. package/lib/utils/getTagProcessor.js.map +1 -0
  278. package/lib/utils/getVisitorKeys.js +11 -0
  279. package/lib/utils/getVisitorKeys.js.map +1 -0
  280. package/lib/utils/hasLinariaPreval.js +13 -0
  281. package/lib/utils/hasLinariaPreval.js.map +1 -0
  282. package/lib/utils/isExports.js +27 -0
  283. package/lib/utils/isExports.js.map +1 -0
  284. package/lib/utils/isGlobal.js +13 -0
  285. package/lib/utils/isGlobal.js.map +1 -0
  286. package/lib/utils/isNode.js +9 -0
  287. package/lib/utils/isNode.js.map +1 -0
  288. package/lib/utils/isNodePath.js +10 -0
  289. package/lib/utils/isNodePath.js.map +1 -0
  290. package/lib/utils/isNotNull.js +10 -0
  291. package/lib/utils/isNotNull.js.map +1 -0
  292. package/lib/utils/isRemoved.js +52 -0
  293. package/lib/utils/isRemoved.js.map +1 -0
  294. package/lib/utils/isRequire.js +18 -0
  295. package/lib/utils/isRequire.js.map +1 -0
  296. package/lib/utils/isSerializable.js +19 -0
  297. package/lib/utils/isSerializable.js.map +1 -0
  298. package/lib/utils/isTypedNode.js +13 -0
  299. package/lib/utils/isTypedNode.js.map +1 -0
  300. package/lib/utils/isUnnecessaryReactCall.js +81 -0
  301. package/lib/utils/isUnnecessaryReactCall.js.map +1 -0
  302. package/lib/utils/peek.js +9 -0
  303. package/lib/utils/peek.js.map +1 -0
  304. package/lib/utils/processTemplateExpression.js +18 -0
  305. package/lib/utils/processTemplateExpression.js.map +1 -0
  306. package/lib/utils/removeDangerousCode.js +134 -0
  307. package/lib/utils/removeDangerousCode.js.map +1 -0
  308. package/lib/utils/scopeHelpers.js +544 -0
  309. package/lib/utils/scopeHelpers.js.map +1 -0
  310. package/lib/utils/traversalCache.js +38 -0
  311. package/lib/utils/traversalCache.js.map +1 -0
  312. package/lib/utils/unwrapSequence.js +20 -0
  313. package/lib/utils/unwrapSequence.js.map +1 -0
  314. package/lib/utils/valueToLiteral.js +65 -0
  315. package/lib/utils/valueToLiteral.js.map +1 -0
  316. package/lib/utils/visitors/JSXElementsRemover.js +57 -0
  317. package/lib/utils/visitors/JSXElementsRemover.js.map +1 -0
  318. package/lib/vm/createVmContext.js +80 -0
  319. package/lib/vm/createVmContext.js.map +1 -0
  320. package/lib/vm/process.js +38 -0
  321. package/lib/vm/process.js.map +1 -0
  322. package/package.json +63 -0
  323. package/types/babel.d.ts +2 -0
  324. package/types/babel.js +2 -0
  325. package/types/cache.d.ts +24 -0
  326. package/types/cache.js +85 -0
  327. package/types/debug/fileReporter.d.ts +27 -0
  328. package/types/debug/fileReporter.js +151 -0
  329. package/types/evaluators/index.d.ts +10 -0
  330. package/types/evaluators/index.js +15 -0
  331. package/types/index.d.ts +30 -0
  332. package/types/index.js +69 -0
  333. package/types/module.d.ts +62 -0
  334. package/types/module.js +318 -0
  335. package/types/options/buildOptions.d.ts +7 -0
  336. package/types/options/buildOptions.js +35 -0
  337. package/types/options/loadBabelOptions.d.ts +3 -0
  338. package/types/options/loadBabelOptions.js +27 -0
  339. package/types/plugins/babel-transform.d.ts +4 -0
  340. package/types/plugins/babel-transform.js +49 -0
  341. package/types/plugins/collector.d.ts +16 -0
  342. package/types/plugins/collector.js +67 -0
  343. package/types/plugins/dynamic-import.d.ts +6 -0
  344. package/types/plugins/dynamic-import.js +32 -0
  345. package/types/plugins/preeval.d.ts +16 -0
  346. package/types/plugins/preeval.js +69 -0
  347. package/types/plugins/shaker.d.ts +11 -0
  348. package/types/plugins/shaker.js +299 -0
  349. package/types/shaker.d.ts +3 -0
  350. package/types/shaker.js +64 -0
  351. package/types/transform/BaseEntrypoint.d.ts +23 -0
  352. package/types/transform/BaseEntrypoint.js +182 -0
  353. package/types/transform/Entrypoint.d.ts +50 -0
  354. package/types/transform/Entrypoint.helpers.d.ts +14 -0
  355. package/types/transform/Entrypoint.helpers.js +169 -0
  356. package/types/transform/Entrypoint.js +223 -0
  357. package/types/transform/Entrypoint.types.d.ts +23 -0
  358. package/types/transform/Entrypoint.types.js +2 -0
  359. package/types/transform/EvaluatedEntrypoint.d.ts +16 -0
  360. package/types/transform/EvaluatedEntrypoint.js +9 -0
  361. package/types/transform/actions/AbortError.d.ts +4 -0
  362. package/types/transform/actions/AbortError.js +12 -0
  363. package/types/transform/actions/BaseAction.d.ts +40 -0
  364. package/types/transform/actions/BaseAction.js +149 -0
  365. package/types/transform/actions/UnprocessedEntrypointError.d.ts +6 -0
  366. package/types/transform/actions/UnprocessedEntrypointError.js +13 -0
  367. package/types/transform/actions/actionRunner.d.ts +4 -0
  368. package/types/transform/actions/actionRunner.js +86 -0
  369. package/types/transform/actions/types.d.ts +15 -0
  370. package/types/transform/actions/types.js +2 -0
  371. package/types/transform/generators/baseProcessingHandlers.d.ts +17 -0
  372. package/types/transform/generators/baseProcessingHandlers.js +23 -0
  373. package/types/transform/generators/collect.d.ts +6 -0
  374. package/types/transform/generators/collect.js +57 -0
  375. package/types/transform/generators/evalFile.d.ts +6 -0
  376. package/types/transform/generators/evalFile.js +56 -0
  377. package/types/transform/generators/explodeReexports.d.ts +7 -0
  378. package/types/transform/generators/explodeReexports.js +67 -0
  379. package/types/transform/generators/extract.d.ts +8 -0
  380. package/types/transform/generators/extract.js +116 -0
  381. package/types/transform/generators/getExports.d.ts +8 -0
  382. package/types/transform/generators/getExports.js +65 -0
  383. package/types/transform/generators/index.d.ts +15 -0
  384. package/types/transform/generators/index.js +15 -0
  385. package/types/transform/generators/processEntrypoint.d.ts +9 -0
  386. package/types/transform/generators/processEntrypoint.js +88 -0
  387. package/types/transform/generators/processImports.d.ts +5 -0
  388. package/types/transform/generators/processImports.js +21 -0
  389. package/types/transform/generators/resolveImports.d.ts +9 -0
  390. package/types/transform/generators/resolveImports.js +124 -0
  391. package/types/transform/generators/transform.d.ts +17 -0
  392. package/types/transform/generators/transform.js +115 -0
  393. package/types/transform/generators/workflow.d.ts +7 -0
  394. package/types/transform/generators/workflow.js +77 -0
  395. package/types/transform/helpers/loadLinariaOptions.d.ts +6 -0
  396. package/types/transform/helpers/loadLinariaOptions.js +86 -0
  397. package/types/transform/helpers/withDefaultServices.d.ts +5 -0
  398. package/types/transform/helpers/withDefaultServices.js +40 -0
  399. package/types/transform/rootLog.d.ts +3 -0
  400. package/types/transform/rootLog.js +5 -0
  401. package/types/transform/types.d.ts +137 -0
  402. package/types/transform/types.js +4 -0
  403. package/types/transform.d.ts +20 -0
  404. package/types/transform.js +121 -0
  405. package/types/types.d.ts +72 -0
  406. package/types/types.js +5 -0
  407. package/types/utils/EventEmitter.d.ts +58 -0
  408. package/types/utils/EventEmitter.js +61 -0
  409. package/types/utils/ShakerMetadata.d.ts +7 -0
  410. package/types/utils/ShakerMetadata.js +5 -0
  411. package/types/utils/TransformMetadata.d.ts +13 -0
  412. package/types/utils/TransformMetadata.js +17 -0
  413. package/types/utils/addIdentifierToLinariaPreval.d.ts +4 -0
  414. package/types/utils/addIdentifierToLinariaPreval.js +75 -0
  415. package/types/utils/collectExportsAndImports.d.ts +31 -0
  416. package/types/utils/collectExportsAndImports.js +962 -0
  417. package/types/utils/collectTemplateDependencies.d.ts +17 -0
  418. package/types/utils/collectTemplateDependencies.js +212 -0
  419. package/types/utils/createId.d.ts +2 -0
  420. package/types/utils/createId.js +9 -0
  421. package/types/utils/dispose-polyfill.d.ts +0 -0
  422. package/types/utils/dispose-polyfill.js +5 -0
  423. package/types/utils/findIdentifiers.d.ts +6 -0
  424. package/types/utils/findIdentifiers.js +68 -0
  425. package/types/utils/findPackageJSON.d.ts +1 -0
  426. package/types/utils/findPackageJSON.js +29 -0
  427. package/types/utils/getFileIdx.d.ts +1 -0
  428. package/types/utils/getFileIdx.js +13 -0
  429. package/types/utils/getPluginKey.d.ts +2 -0
  430. package/types/utils/getPluginKey.js +16 -0
  431. package/types/utils/getScope.d.ts +2 -0
  432. package/types/utils/getScope.js +11 -0
  433. package/types/utils/getSource.d.ts +2 -0
  434. package/types/utils/getSource.js +22 -0
  435. package/types/utils/getTagProcessor.d.ts +6 -0
  436. package/types/utils/getTagProcessor.js +315 -0
  437. package/types/utils/getVisitorKeys.d.ts +5 -0
  438. package/types/utils/getVisitorKeys.js +8 -0
  439. package/types/utils/hasLinariaPreval.d.ts +4 -0
  440. package/types/utils/hasLinariaPreval.js +9 -0
  441. package/types/utils/isExports.d.ts +6 -0
  442. package/types/utils/isExports.js +20 -0
  443. package/types/utils/isGlobal.d.ts +2 -0
  444. package/types/utils/isGlobal.js +9 -0
  445. package/types/utils/isNode.d.ts +2 -0
  446. package/types/utils/isNode.js +7 -0
  447. package/types/utils/isNodePath.d.ts +3 -0
  448. package/types/utils/isNodePath.js +7 -0
  449. package/types/utils/isNotNull.d.ts +1 -0
  450. package/types/utils/isNotNull.js +7 -0
  451. package/types/utils/isRemoved.d.ts +5 -0
  452. package/types/utils/isRemoved.js +42 -0
  453. package/types/utils/isRequire.d.ts +6 -0
  454. package/types/utils/isRequire.js +15 -0
  455. package/types/utils/isSerializable.d.ts +2 -0
  456. package/types/utils/isSerializable.js +18 -0
  457. package/types/utils/isTypedNode.d.ts +5 -0
  458. package/types/utils/isTypedNode.js +9 -0
  459. package/types/utils/isUnnecessaryReactCall.d.ts +3 -0
  460. package/types/utils/isUnnecessaryReactCall.js +76 -0
  461. package/types/utils/peek.d.ts +1 -0
  462. package/types/utils/peek.js +5 -0
  463. package/types/utils/processTemplateExpression.d.ts +5 -0
  464. package/types/utils/processTemplateExpression.js +17 -0
  465. package/types/utils/removeDangerousCode.d.ts +3 -0
  466. package/types/utils/removeDangerousCode.js +144 -0
  467. package/types/utils/scopeHelpers.d.ts +12 -0
  468. package/types/utils/scopeHelpers.js +568 -0
  469. package/types/utils/traversalCache.d.ts +5 -0
  470. package/types/utils/traversalCache.js +36 -0
  471. package/types/utils/unwrapSequence.d.ts +8 -0
  472. package/types/utils/unwrapSequence.js +17 -0
  473. package/types/utils/valueToLiteral.d.ts +3 -0
  474. package/types/utils/valueToLiteral.js +64 -0
  475. package/types/utils/visitors/JSXElementsRemover.d.ts +3 -0
  476. package/types/utils/visitors/JSXElementsRemover.js +52 -0
  477. package/types/vm/createVmContext.d.ts +7 -0
  478. package/types/vm/createVmContext.js +92 -0
  479. package/types/vm/process.d.ts +25 -0
  480. package/types/vm/process.js +32 -0
@@ -0,0 +1,962 @@
1
+ "use strict";
2
+ /* eslint @typescript-eslint/no-use-before-define: ["error", { "functions": false }] */
3
+ /* eslint-disable no-restricted-syntax,no-continue */
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.collectExportsAndImports = exports.explicitImport = exports.sideEffectImport = void 0;
6
+ const shared_1 = require("@wyw-in-js/shared");
7
+ const getScope_1 = require("./getScope");
8
+ const isExports_1 = require("./isExports");
9
+ const isNotNull_1 = require("./isNotNull");
10
+ const isRequire_1 = require("./isRequire");
11
+ const isTypedNode_1 = require("./isTypedNode");
12
+ const traversalCache_1 = require("./traversalCache");
13
+ const sideEffectImport = (item) => item.imported === 'side-effect';
14
+ exports.sideEffectImport = sideEffectImport;
15
+ const explicitImport = (item) => item.imported !== 'side-effect';
16
+ exports.explicitImport = explicitImport;
17
+ function getValue({ node }) {
18
+ return node.type === 'Identifier' ? node.name : node.value;
19
+ }
20
+ // We ignore imports and exports of types
21
+ const isType = (p) => ('importKind' in p.node && p.node.importKind === 'type') ||
22
+ ('exportKind' in p.node && p.node.exportKind === 'type');
23
+ const collectors = {
24
+ ImportSpecifier(path, source) {
25
+ if (isType(path))
26
+ return [];
27
+ const imported = getValue(path.get('imported'));
28
+ const local = path.get('local');
29
+ return [{ imported, local, source, type: 'esm' }];
30
+ },
31
+ ImportDefaultSpecifier(path, source) {
32
+ const local = path.get('local');
33
+ return [{ imported: 'default', local, source, type: 'esm' }];
34
+ },
35
+ ImportNamespaceSpecifier(path, source) {
36
+ const local = path.get('local');
37
+ return unfoldNamespaceImport({ imported: '*', local, source, type: 'esm' });
38
+ },
39
+ };
40
+ function collectFromImportDeclaration(path, state) {
41
+ // If importKind is specified, and it's not a value, ignore that import
42
+ if (isType(path))
43
+ return;
44
+ const source = getValue(path.get('source'));
45
+ const specifiers = path.get('specifiers');
46
+ if (specifiers.length === 0) {
47
+ state.imports.push({ imported: 'side-effect', local: path, source });
48
+ }
49
+ specifiers.forEach((specifier) => {
50
+ if (specifier.isImportSpecifier() && isType(specifier))
51
+ return;
52
+ const collector = collectors[specifier.node.type];
53
+ state.imports.push(...collector(specifier, source));
54
+ });
55
+ }
56
+ function getAncestorsWhile(path, cond) {
57
+ const result = [];
58
+ let current = path;
59
+ while (current && cond(current)) {
60
+ result.push(current);
61
+ current = current.parentPath;
62
+ }
63
+ return result;
64
+ }
65
+ function whatIsDestructed(objectPattern) {
66
+ const destructedProps = [];
67
+ objectPattern.traverse({
68
+ Identifier(identifier) {
69
+ if (identifier.isBindingIdentifier()) {
70
+ const parent = identifier.parentPath;
71
+ if (parent.isObjectProperty({ value: identifier.node })) {
72
+ const chain = getAncestorsWhile(parent, (p) => p !== objectPattern)
73
+ .filter((0, isTypedNode_1.isTypedNode)('ObjectProperty'))
74
+ .map((p) => {
75
+ const key = p.get('key');
76
+ if (!key.isIdentifier()) {
77
+ // TODO: try to process other type of keys or at least warn about this
78
+ return null;
79
+ }
80
+ return key;
81
+ })
82
+ .filter(isNotNull_1.isNotNull);
83
+ chain.reverse();
84
+ if (chain.length > 0) {
85
+ destructedProps.push({
86
+ what: chain[0].node.name,
87
+ as: identifier,
88
+ });
89
+ }
90
+ return;
91
+ }
92
+ if (parent.isRestElement({ argument: identifier.node })) {
93
+ destructedProps.push({
94
+ what: '*',
95
+ as: identifier,
96
+ });
97
+ }
98
+ }
99
+ },
100
+ });
101
+ return destructedProps;
102
+ }
103
+ const debug = shared_1.logger.extend('evaluator:collectExportsAndImports');
104
+ function importFromVariableDeclarator(path, isSync) {
105
+ const id = path.get('id');
106
+ if (id.isIdentifier()) {
107
+ // It's the simplest case when the full namespace is imported
108
+ return [
109
+ {
110
+ as: id,
111
+ what: '*',
112
+ },
113
+ ];
114
+ }
115
+ if (!isSync) {
116
+ // Something went wrong
117
+ // Is it something like `const { … } = import(…)`?
118
+ debug('`import` should be awaited');
119
+ return [];
120
+ }
121
+ if (id.isObjectPattern()) {
122
+ return whatIsDestructed(id);
123
+ }
124
+ // What else it can be?
125
+ debug('importFromVariableDeclarator: unknown type of id %o', id.node.type);
126
+ return [];
127
+ }
128
+ const findIIFE = (path) => {
129
+ if (path.isCallExpression() && path.get('callee').isFunctionExpression()) {
130
+ return path;
131
+ }
132
+ if (!path.parentPath) {
133
+ return null;
134
+ }
135
+ return findIIFE(path.parentPath);
136
+ };
137
+ function exportFromVariableDeclarator(path) {
138
+ const id = path.get('id');
139
+ const init = path.get('init');
140
+ // If there is no init and id is an identifier, we should find IIFE
141
+ if (!init.node && id.isIdentifier()) {
142
+ const binding = (0, getScope_1.getScope)(path).getBinding(id.node.name);
143
+ if (!binding) {
144
+ return {};
145
+ }
146
+ const iife = [
147
+ ...binding.referencePaths,
148
+ ...binding.constantViolations,
149
+ binding.path,
150
+ ]
151
+ .map(findIIFE)
152
+ .find(isNotNull_1.isNotNull);
153
+ if (!iife) {
154
+ return {};
155
+ }
156
+ return {
157
+ [id.node.name]: iife,
158
+ };
159
+ }
160
+ if (!init || !init.isExpression()) {
161
+ return {};
162
+ }
163
+ if (id.isIdentifier()) {
164
+ // It is `export const a = 1;`
165
+ return {
166
+ [id.node.name]: init,
167
+ };
168
+ }
169
+ if (id.isObjectPattern()) {
170
+ // It is `export const { a, ...rest } = obj;`
171
+ return whatIsDestructed(id).reduce((acc, destructed) => ({
172
+ ...acc,
173
+ [destructed.as.node.name]: init,
174
+ }), {});
175
+ }
176
+ // What else it can be?
177
+ debug('exportFromVariableDeclarator: unknown type of id %o', id.node.type);
178
+ return {};
179
+ }
180
+ function collectFromDynamicImport(path, state) {
181
+ const { parentPath: callExpression } = path;
182
+ if (!callExpression.isCallExpression()) {
183
+ // It's wrong `import`
184
+ return;
185
+ }
186
+ const [sourcePath] = callExpression.get('arguments');
187
+ if (!sourcePath || !sourcePath.isStringLiteral()) {
188
+ // Import should have at least one argument, and it should be StringLiteral
189
+ return;
190
+ }
191
+ const source = sourcePath.node.value;
192
+ let { parentPath: container, key } = callExpression;
193
+ let isAwaited = false;
194
+ if (container.isAwaitExpression()) {
195
+ // If it's not awaited import, it imports the full namespace
196
+ isAwaited = true;
197
+ key = container.key;
198
+ container = container.parentPath;
199
+ }
200
+ // Is it `const something = await import("something")`?
201
+ if (key === 'init' && container.isVariableDeclarator()) {
202
+ importFromVariableDeclarator(container, isAwaited).map((prop) => state.imports.push({
203
+ imported: prop.what,
204
+ local: prop.as,
205
+ source,
206
+ type: 'dynamic',
207
+ }));
208
+ }
209
+ }
210
+ function getCalleeName(path) {
211
+ const callee = path.get('callee');
212
+ if (callee.isIdentifier()) {
213
+ return callee.node.name;
214
+ }
215
+ if (callee.isMemberExpression()) {
216
+ const property = callee.get('property');
217
+ if (property.isIdentifier()) {
218
+ return property.node.name;
219
+ }
220
+ }
221
+ return undefined;
222
+ }
223
+ function getImportExportTypeByInteropFunction(path) {
224
+ const name = getCalleeName(path);
225
+ if (name === undefined) {
226
+ return undefined;
227
+ }
228
+ if (name.startsWith('__exportStar')) {
229
+ return 're-export:*';
230
+ }
231
+ if (name.startsWith('_interopRequireDefault') ||
232
+ name.startsWith('__importDefault')) {
233
+ return 'default';
234
+ }
235
+ if (name.startsWith('_interopRequireWildcard') ||
236
+ name.startsWith('__importStar') ||
237
+ name.startsWith('__toESM')) {
238
+ return 'import:*';
239
+ }
240
+ if (name.startsWith('__rest') ||
241
+ name.startsWith('__objRest') ||
242
+ name.startsWith('_objectDestructuringEmpty')) {
243
+ return 'import:*';
244
+ }
245
+ return undefined;
246
+ }
247
+ function isAlreadyProcessed(path) {
248
+ if (path.isCallExpression() &&
249
+ path.get('callee').isIdentifier({ name: '__toCommonJS' })) {
250
+ // because its esbuild and we already processed all exports
251
+ return true;
252
+ }
253
+ return false;
254
+ }
255
+ function collectFromRequire(path, state) {
256
+ if (!(0, isRequire_1.isRequire)(path))
257
+ return;
258
+ // This method can be reached many times from multiple visitors for the same path
259
+ // So we need to check if we already processed it
260
+ if (state.processedRequires.has(path))
261
+ return;
262
+ state.processedRequires.add(path);
263
+ const { parentPath: callExpression } = path;
264
+ if (!callExpression.isCallExpression()) {
265
+ // It's wrong `require`
266
+ return;
267
+ }
268
+ const [sourcePath] = callExpression.get('arguments');
269
+ if (!sourcePath || !sourcePath.isStringLiteral()) {
270
+ // Import should have at least one argument, and it should be StringLiteral
271
+ return;
272
+ }
273
+ const source = sourcePath.node.value;
274
+ const { parentPath: container, key } = callExpression;
275
+ if (container.isCallExpression() && key === 0) {
276
+ // It may be transpiled import such as
277
+ // `var _atomic = _interopRequireDefault(require("@linaria/atomic"));`
278
+ const imported = getImportExportTypeByInteropFunction(container);
279
+ if (!imported) {
280
+ // It's not a transpiled import.
281
+ // TODO: Can we guess that it's a namespace import?
282
+ debug('Unknown wrapper of require: %o', container.node.callee);
283
+ return;
284
+ }
285
+ if (imported === 're-export:*') {
286
+ state.reexports.push({
287
+ exported: '*',
288
+ imported: '*',
289
+ local: path,
290
+ source,
291
+ });
292
+ return;
293
+ }
294
+ let { parentPath: variableDeclarator } = container;
295
+ if (variableDeclarator.isCallExpression()) {
296
+ if (variableDeclarator.get('callee').isIdentifier({ name: '_extends' })) {
297
+ variableDeclarator = variableDeclarator.parentPath;
298
+ }
299
+ }
300
+ if (!variableDeclarator.isVariableDeclarator()) {
301
+ // TODO: Where else it can be?
302
+ debug('Unexpected require inside %o', variableDeclarator.node.type);
303
+ return;
304
+ }
305
+ const id = variableDeclarator.get('id');
306
+ if (!id.isIdentifier()) {
307
+ debug('Id should be Identifier %o', variableDeclarator.node.type);
308
+ return;
309
+ }
310
+ if (imported === 'import:*') {
311
+ const unfolded = unfoldNamespaceImport({
312
+ imported: '*',
313
+ local: id,
314
+ source,
315
+ type: 'cjs',
316
+ });
317
+ state.imports.push(...unfolded);
318
+ }
319
+ else {
320
+ state.imports.push({
321
+ imported,
322
+ local: id,
323
+ source,
324
+ type: 'cjs',
325
+ });
326
+ }
327
+ }
328
+ if (container.isMemberExpression()) {
329
+ // It is `require('@linaria/shaker').dep`
330
+ const property = container.get('property');
331
+ if (!property.isIdentifier() && !property.isStringLiteral()) {
332
+ debug('Property should be Identifier or StringLiteral %s', property.node.type);
333
+ return;
334
+ }
335
+ const { parentPath: variableDeclarator } = container;
336
+ if (variableDeclarator.isVariableDeclarator()) {
337
+ // It is `const … = require('@linaria/shaker').dep`;
338
+ const id = variableDeclarator.get('id');
339
+ if (id.isIdentifier()) {
340
+ state.imports.push({
341
+ imported: getValue(property),
342
+ local: id,
343
+ source,
344
+ type: 'cjs',
345
+ });
346
+ }
347
+ else {
348
+ debug('Id should be Identifier %o', variableDeclarator.node.type);
349
+ }
350
+ }
351
+ else {
352
+ // Maybe require is passed as an argument to some function?
353
+ // Just use the whole MemberExpression as a local
354
+ state.imports.push({
355
+ imported: getValue(property),
356
+ local: container,
357
+ source,
358
+ type: 'cjs',
359
+ });
360
+ }
361
+ return;
362
+ }
363
+ // Is it `const something = require("something")`?
364
+ if (key === 'init' && container.isVariableDeclarator()) {
365
+ importFromVariableDeclarator(container, true).forEach((prop) => {
366
+ if (prop.what === '*') {
367
+ const unfolded = unfoldNamespaceImport({
368
+ imported: '*',
369
+ local: prop.as,
370
+ source,
371
+ type: 'cjs',
372
+ });
373
+ state.imports.push(...unfolded);
374
+ }
375
+ else {
376
+ state.imports.push({
377
+ imported: prop.what,
378
+ local: prop.as,
379
+ source,
380
+ type: 'cjs',
381
+ });
382
+ }
383
+ });
384
+ }
385
+ if (container.isExpressionStatement()) {
386
+ // Looks like standalone require
387
+ state.imports.push({
388
+ imported: 'side-effect',
389
+ local: container,
390
+ source,
391
+ });
392
+ }
393
+ }
394
+ function collectFromVariableDeclarator(path, state) {
395
+ let found = false;
396
+ path.traverse({
397
+ Identifier(identifierPath) {
398
+ if ((0, isRequire_1.isRequire)(identifierPath)) {
399
+ collectFromRequire(identifierPath, state);
400
+ found = true;
401
+ }
402
+ },
403
+ });
404
+ if (found) {
405
+ path.skip();
406
+ }
407
+ }
408
+ function isChainOfVoidAssignment(path) {
409
+ const right = path.get('right');
410
+ if (right.isUnaryExpression({ operator: 'void' })) {
411
+ return true;
412
+ }
413
+ if (right.isAssignmentExpression()) {
414
+ return isChainOfVoidAssignment(right);
415
+ }
416
+ return false;
417
+ }
418
+ function getReturnValue(path) {
419
+ if (path.node.params.length !== 0)
420
+ return undefined;
421
+ const body = path.get('body');
422
+ if (body.isExpression()) {
423
+ return body;
424
+ }
425
+ if (body.node.body.length === 1) {
426
+ const returnStatement = body.get('body')?.[0];
427
+ if (!returnStatement.isReturnStatement())
428
+ return undefined;
429
+ const argument = returnStatement.get('argument');
430
+ if (!argument.isExpression())
431
+ return undefined;
432
+ return argument;
433
+ }
434
+ return undefined;
435
+ }
436
+ function getGetterValueFromDescriptor(descriptor) {
437
+ const props = descriptor
438
+ .get('properties')
439
+ .filter((0, isTypedNode_1.isTypedNode)('ObjectProperty'));
440
+ const getter = props.find((p) => p.get('key').isIdentifier({ name: 'get' }));
441
+ const value = getter?.get('value');
442
+ if (value?.isFunctionExpression() || value?.isArrowFunctionExpression()) {
443
+ return getReturnValue(value);
444
+ }
445
+ const valueProp = props.find((p) => p.get('key').isIdentifier({ name: 'value' }));
446
+ const valueValue = valueProp?.get('value');
447
+ return valueValue?.isExpression() ? valueValue : undefined;
448
+ }
449
+ function addExport(path, exported, state) {
450
+ function getRelatedImport() {
451
+ if (path.isMemberExpression()) {
452
+ const object = path.get('object');
453
+ if (!object.isIdentifier()) {
454
+ return undefined;
455
+ }
456
+ const objectBinding = object.scope.getBinding(object.node.name);
457
+ if (!objectBinding) {
458
+ return undefined;
459
+ }
460
+ if (objectBinding.path.isVariableDeclarator()) {
461
+ collectFromVariableDeclarator(objectBinding.path, state);
462
+ }
463
+ const found = state.imports.find((i) => objectBinding.identifier === i.local.node ||
464
+ objectBinding.referencePaths.some((p) => i.local.isAncestor(p)));
465
+ if (!found) {
466
+ return undefined;
467
+ }
468
+ const property = path.get('property');
469
+ let what = '*';
470
+ if (path.node.computed && property.isStringLiteral()) {
471
+ what = property.node.value;
472
+ }
473
+ else if (!path.node.computed && property.isIdentifier()) {
474
+ what = property.node.name;
475
+ }
476
+ return {
477
+ import: { ...found, local: path },
478
+ what,
479
+ };
480
+ }
481
+ return undefined;
482
+ }
483
+ const relatedImport = getRelatedImport();
484
+ if (relatedImport) {
485
+ // eslint-disable-next-line no-param-reassign
486
+ state.reexports.push({
487
+ local: relatedImport.import.local,
488
+ imported: relatedImport.import.imported,
489
+ source: relatedImport.import.source,
490
+ exported,
491
+ });
492
+ }
493
+ else {
494
+ // eslint-disable-next-line no-param-reassign
495
+ state.exports[exported] = path;
496
+ }
497
+ }
498
+ function collectFromExports(path, state) {
499
+ if (!(0, isExports_1.isExports)(path))
500
+ return;
501
+ if (path.parentPath.isMemberExpression({ object: path.node })) {
502
+ // It is `exports.prop = …`
503
+ const memberExpression = path.parentPath;
504
+ const property = memberExpression.get('property');
505
+ if (!property.isIdentifier() || memberExpression.node.computed) {
506
+ return;
507
+ }
508
+ const exportName = property.node.name;
509
+ const saveRef = () => {
510
+ // Save all export.____ usages for later
511
+ if (!state.exportRefs.has(exportName)) {
512
+ state.exportRefs.set(exportName, []);
513
+ }
514
+ state.exportRefs.get(exportName).push(memberExpression);
515
+ };
516
+ const assignmentExpression = memberExpression.parentPath;
517
+ if (!assignmentExpression.isAssignmentExpression({
518
+ left: memberExpression.node,
519
+ })) {
520
+ // If it's not `exports.prop = …`. Just save it.
521
+ saveRef();
522
+ return;
523
+ }
524
+ const right = assignmentExpression.get('right');
525
+ if (isChainOfVoidAssignment(assignmentExpression)) {
526
+ // It is `exports.foo = void 0`
527
+ return;
528
+ }
529
+ const { name } = property.node;
530
+ if (name === '__esModule') {
531
+ // eslint-disable-next-line no-param-reassign
532
+ state.isEsModule = true;
533
+ return;
534
+ }
535
+ saveRef();
536
+ // eslint-disable-next-line no-param-reassign
537
+ state.exports[property.node.name] = right;
538
+ return;
539
+ }
540
+ if (path.parentPath.isCallExpression() &&
541
+ path.parentPath.get('callee').matchesPattern('Object.defineProperty')) {
542
+ const [obj, prop, descriptor] = path.parentPath.get('arguments');
543
+ if (obj?.isIdentifier(path.node) &&
544
+ prop?.isStringLiteral() &&
545
+ descriptor?.isObjectExpression()) {
546
+ if (prop.node.value === '__esModule') {
547
+ // eslint-disable-next-line no-param-reassign
548
+ state.isEsModule = true;
549
+ }
550
+ else {
551
+ /**
552
+ * Object.defineProperty(exports, "token", {
553
+ * enumerable: true,
554
+ * get: function get() {
555
+ * return _unknownPackage.token;
556
+ * }
557
+ * });
558
+ */
559
+ const exported = prop.node.value;
560
+ const local = getGetterValueFromDescriptor(descriptor);
561
+ if (local) {
562
+ addExport(local, exported, state);
563
+ }
564
+ }
565
+ }
566
+ else if (obj?.isIdentifier(path.node) &&
567
+ prop?.isIdentifier() &&
568
+ descriptor?.isObjectExpression()) {
569
+ /**
570
+ * Object.defineProperty(exports, key, {
571
+ * enumerable: true,
572
+ * get: function get() {
573
+ * return _unknownPackage[key];
574
+ * }
575
+ * });
576
+ */
577
+ const local = getGetterValueFromDescriptor(descriptor);
578
+ if (local) {
579
+ addExport(local, '*', state);
580
+ }
581
+ }
582
+ }
583
+ }
584
+ function collectFromRequireOrExports(path, state) {
585
+ if ((0, isRequire_1.isRequire)(path)) {
586
+ collectFromRequire(path, state);
587
+ }
588
+ else if ((0, isExports_1.isExports)(path)) {
589
+ collectFromExports(path, state);
590
+ }
591
+ }
592
+ function unfoldNamespaceImport(importItem) {
593
+ const result = [];
594
+ const { local } = importItem;
595
+ if (!local.isIdentifier()) {
596
+ // TODO: handle it
597
+ return [importItem];
598
+ }
599
+ const binding = (0, getScope_1.getScope)(local).getBinding(local.node.name);
600
+ if (!binding?.referenced) {
601
+ // Imported namespace is not referenced and probably not used,
602
+ // but it can have side effects, so we should keep it as is
603
+ return [importItem];
604
+ }
605
+ for (const referencePath of binding?.referencePaths ?? []) {
606
+ if (referencePath.find((ancestor) => ancestor.isTSType() || ancestor.isFlowType())) {
607
+ continue;
608
+ }
609
+ const { parentPath } = referencePath;
610
+ if (parentPath?.isMemberExpression() && referencePath.key === 'object') {
611
+ const property = parentPath.get('property');
612
+ const object = parentPath.get('object');
613
+ let imported;
614
+ if (parentPath.node.computed && property.isStringLiteral()) {
615
+ imported = property.node.value;
616
+ }
617
+ else if (!parentPath.node.computed && property.isIdentifier()) {
618
+ imported = property.node.name;
619
+ }
620
+ else {
621
+ imported = null;
622
+ }
623
+ if (object.isIdentifier() && imported) {
624
+ result.push({
625
+ ...importItem,
626
+ imported,
627
+ local: parentPath,
628
+ });
629
+ }
630
+ else {
631
+ result.push(importItem);
632
+ break;
633
+ }
634
+ continue;
635
+ }
636
+ if (parentPath?.isVariableDeclarator() && referencePath.key === 'init') {
637
+ importFromVariableDeclarator(parentPath, true).map((prop) => result.push({ ...importItem, imported: prop.what, local: prop.as }));
638
+ continue;
639
+ }
640
+ if (parentPath?.isCallExpression() &&
641
+ referencePath.listKey === 'arguments') {
642
+ // The defined variable is used as a function argument. Let's try to figure out what is imported.
643
+ const importType = getImportExportTypeByInteropFunction(parentPath);
644
+ if (!importType) {
645
+ // Imported value is used as an unknown function argument,
646
+ // so we can't predict usage and import it as is.
647
+ result.push(importItem);
648
+ break;
649
+ }
650
+ if (importType === 'default') {
651
+ result.push({
652
+ ...importItem,
653
+ imported: 'default',
654
+ local: parentPath.get('id'),
655
+ });
656
+ continue;
657
+ }
658
+ if (importType === 'import:*') {
659
+ result.push(importItem);
660
+ break;
661
+ }
662
+ debug('unfoldNamespaceImports: unknown import type %o', importType);
663
+ result.push(importItem);
664
+ continue;
665
+ }
666
+ if (parentPath?.isExportSpecifier() ||
667
+ parentPath?.isExportDefaultDeclaration()) {
668
+ // The whole namespace is re-exported
669
+ result.push(importItem);
670
+ break;
671
+ }
672
+ // Otherwise, we can't predict usage and import it as is
673
+ // TODO: handle more cases
674
+ debug('unfoldNamespaceImports: unknown reference %o', referencePath.node.type);
675
+ result.push(importItem);
676
+ break;
677
+ }
678
+ return result;
679
+ }
680
+ function collectFromExportAllDeclaration(path, state) {
681
+ if (isType(path))
682
+ return;
683
+ const source = path.get('source')?.node?.value;
684
+ if (!source)
685
+ return;
686
+ // It is `export * from './css';`
687
+ state.reexports.push({
688
+ exported: '*',
689
+ imported: '*',
690
+ local: path,
691
+ source,
692
+ });
693
+ }
694
+ function collectFromExportSpecifier(path, source, state) {
695
+ if (path.isExportSpecifier()) {
696
+ const exported = getValue(path.get('exported'));
697
+ if (source) {
698
+ // It is `export { foo } from './css';`
699
+ const imported = path.get('local').node.name;
700
+ state.reexports.push({
701
+ exported,
702
+ imported,
703
+ local: path,
704
+ source,
705
+ });
706
+ }
707
+ else {
708
+ const local = path.get('local');
709
+ // eslint-disable-next-line no-param-reassign
710
+ state.exports[exported] = local;
711
+ }
712
+ return;
713
+ }
714
+ if (path.isExportDefaultSpecifier() && source) {
715
+ // It is `export default from './css';`
716
+ state.reexports.push({
717
+ exported: 'default',
718
+ imported: 'default',
719
+ local: path,
720
+ source,
721
+ });
722
+ }
723
+ if (path.isExportNamespaceSpecifier() && source) {
724
+ const exported = path.get('exported').node.name;
725
+ // It is `export * as foo from './css';`
726
+ state.reexports.push({
727
+ exported,
728
+ imported: '*',
729
+ local: path,
730
+ source,
731
+ });
732
+ }
733
+ // TODO: handle other cases
734
+ debug('collectFromExportSpecifier: unprocessed ExportSpecifier %o', path.node.type);
735
+ }
736
+ function collectFromExportNamedDeclaration(path, state) {
737
+ if (isType(path))
738
+ return;
739
+ const source = path.get('source')?.node?.value;
740
+ const specifiers = path.get('specifiers');
741
+ if (specifiers) {
742
+ specifiers.forEach((specifier) => collectFromExportSpecifier(specifier, source, state));
743
+ }
744
+ const declaration = path.get('declaration');
745
+ if (declaration.isVariableDeclaration()) {
746
+ declaration.get('declarations').forEach((declarator) => {
747
+ // eslint-disable-next-line no-param-reassign
748
+ state.exports = {
749
+ ...state.exports,
750
+ ...exportFromVariableDeclarator(declarator),
751
+ };
752
+ });
753
+ }
754
+ if (declaration.isTSEnumDeclaration()) {
755
+ // eslint-disable-next-line no-param-reassign
756
+ state.exports[declaration.get('id').node.name] = declaration;
757
+ }
758
+ if (declaration.isFunctionDeclaration()) {
759
+ const id = declaration.get('id');
760
+ if (id.isIdentifier()) {
761
+ // eslint-disable-next-line no-param-reassign
762
+ state.exports[id.node.name] = id;
763
+ }
764
+ }
765
+ if (declaration.isClassDeclaration()) {
766
+ const id = declaration.get('id');
767
+ if (id.isIdentifier()) {
768
+ // eslint-disable-next-line no-param-reassign
769
+ state.exports[id.node.name] = id;
770
+ }
771
+ }
772
+ }
773
+ function collectFromExportDefaultDeclaration(path, state) {
774
+ if (isType(path))
775
+ return;
776
+ // eslint-disable-next-line no-param-reassign
777
+ state.exports.default = path.get('declaration');
778
+ }
779
+ function collectFromAssignmentExpression(path, state) {
780
+ if (isChainOfVoidAssignment(path)) {
781
+ return;
782
+ }
783
+ const left = path.get('left');
784
+ const right = path.get('right');
785
+ let exported;
786
+ if (left.isMemberExpression() && (0, isExports_1.isExports)(left.get('object'))) {
787
+ const property = left.get('property');
788
+ if (property.isIdentifier()) {
789
+ exported = property.node.name;
790
+ }
791
+ }
792
+ else if ((0, isExports_1.isExports)(left)) {
793
+ // module.exports = ...
794
+ if (!isAlreadyProcessed(right)) {
795
+ exported = 'default';
796
+ }
797
+ }
798
+ if (!exported)
799
+ return;
800
+ if (!right.isCallExpression() || !(0, isRequire_1.isRequire)(right.get('callee'))) {
801
+ // eslint-disable-next-line no-param-reassign
802
+ state.exports[exported] = right;
803
+ return;
804
+ }
805
+ const sourcePath = right.get('arguments')?.[0];
806
+ const source = sourcePath.isStringLiteral()
807
+ ? sourcePath.node.value
808
+ : undefined;
809
+ if (!source)
810
+ return;
811
+ // It is `exports.foo = require('./css');`
812
+ if (state.exports[exported]) {
813
+ // eslint-disable-next-line no-param-reassign
814
+ delete state.exports[exported];
815
+ }
816
+ state.reexports.push({
817
+ exported,
818
+ imported: '*',
819
+ local: path,
820
+ source,
821
+ });
822
+ path.skip();
823
+ }
824
+ function collectFromExportStarCall(path, state) {
825
+ const [requireCall, exports] = path.get('arguments');
826
+ if (!(0, isExports_1.isExports)(exports))
827
+ return;
828
+ if (!requireCall.isCallExpression())
829
+ return;
830
+ const callee = requireCall.get('callee');
831
+ const sourcePath = requireCall.get('arguments')?.[0];
832
+ if (!(0, isRequire_1.isRequire)(callee) || !sourcePath.isStringLiteral())
833
+ return;
834
+ const source = sourcePath.node.value;
835
+ if (!source)
836
+ return;
837
+ state.reexports.push({
838
+ exported: '*',
839
+ imported: '*',
840
+ local: path,
841
+ source,
842
+ });
843
+ path.skip();
844
+ }
845
+ function collectFromMap(map, state) {
846
+ const properties = map.get('properties');
847
+ properties.forEach((property) => {
848
+ if (!property.isObjectProperty())
849
+ return;
850
+ const key = property.get('key');
851
+ const value = property.get('value');
852
+ if (!key.isIdentifier())
853
+ return;
854
+ const exported = key.node.name;
855
+ if (!value.isFunction())
856
+ return;
857
+ if (value.node.params.length !== 0)
858
+ return;
859
+ const returnValue = getReturnValue(value);
860
+ if (!returnValue)
861
+ return;
862
+ addExport(returnValue, exported, state);
863
+ });
864
+ }
865
+ function collectFromEsbuildExportCall(path, state) {
866
+ const [sourceExports, map] = path.get('arguments');
867
+ if (!sourceExports.isIdentifier({ name: 'source_exports' }))
868
+ return;
869
+ if (!map.isObjectExpression())
870
+ return;
871
+ collectFromMap(map, state);
872
+ path.skip();
873
+ }
874
+ function collectFromEsbuildReExportCall(path, state) {
875
+ const [sourceExports, requireCall, exports] = path.get('arguments');
876
+ if (!sourceExports.isIdentifier({ name: 'source_exports' }))
877
+ return;
878
+ if (!requireCall.isCallExpression())
879
+ return;
880
+ if (!(0, isExports_1.isExports)(exports))
881
+ return;
882
+ const callee = requireCall.get('callee');
883
+ if (!(0, isRequire_1.isRequire)(callee))
884
+ return;
885
+ const sourcePath = requireCall.get('arguments')?.[0];
886
+ if (!sourcePath.isStringLiteral())
887
+ return;
888
+ state.reexports.push({
889
+ exported: '*',
890
+ imported: '*',
891
+ local: path,
892
+ source: sourcePath.node.value,
893
+ });
894
+ path.skip();
895
+ }
896
+ function collectFromSwcExportCall(path, state) {
897
+ const [exports, map] = path.get('arguments');
898
+ if (!(0, isExports_1.isExports)(exports))
899
+ return;
900
+ if (!map.isObjectExpression())
901
+ return;
902
+ collectFromMap(map, state);
903
+ path.skip();
904
+ }
905
+ function collectFromCallExpression(path, state) {
906
+ const maybeExportStart = path.get('callee');
907
+ if (!maybeExportStart.isIdentifier()) {
908
+ return;
909
+ }
910
+ const { name } = maybeExportStart.node;
911
+ // TypeScript
912
+ if (name.startsWith('__exportStar')) {
913
+ collectFromExportStarCall(path, state);
914
+ return;
915
+ }
916
+ // swc
917
+ if (name === '_exportStar') {
918
+ collectFromExportStarCall(path, state);
919
+ }
920
+ if (name === '_export') {
921
+ collectFromSwcExportCall(path, state);
922
+ }
923
+ // esbuild
924
+ if (name === '__export') {
925
+ collectFromEsbuildExportCall(path, state);
926
+ }
927
+ if (name === '__reExport') {
928
+ collectFromEsbuildReExportCall(path, state);
929
+ }
930
+ }
931
+ function collectExportsAndImports(path, cacheMode = 'enabled') {
932
+ const localState = {
933
+ deadExports: [],
934
+ exportRefs: new Map(),
935
+ exports: {},
936
+ imports: [],
937
+ reexports: [],
938
+ isEsModule: path.node.sourceType === 'module',
939
+ processedRequires: new WeakSet(),
940
+ };
941
+ const cache = cacheMode !== 'disabled'
942
+ ? (0, traversalCache_1.getTraversalCache)(path, 'collectExportsAndImports')
943
+ : undefined;
944
+ if (cacheMode === 'enabled' && cache?.has(path)) {
945
+ return cache.get(path) ?? localState;
946
+ }
947
+ path.traverse({
948
+ AssignmentExpression: collectFromAssignmentExpression,
949
+ CallExpression: collectFromCallExpression,
950
+ ExportAllDeclaration: collectFromExportAllDeclaration,
951
+ ExportDefaultDeclaration: collectFromExportDefaultDeclaration,
952
+ ExportNamedDeclaration: collectFromExportNamedDeclaration,
953
+ ImportDeclaration: collectFromImportDeclaration,
954
+ Import: collectFromDynamicImport,
955
+ Identifier: collectFromRequireOrExports,
956
+ VariableDeclarator: collectFromVariableDeclarator,
957
+ }, localState);
958
+ const { processedRequires, ...state } = localState;
959
+ cache?.set(path, state);
960
+ return state;
961
+ }
962
+ exports.collectExportsAndImports = collectExportsAndImports;