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