@wyw-in-js/transform 0.1.1 → 0.2.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 (142) hide show
  1. package/esm/index.js +4 -1
  2. package/esm/index.js.map +1 -1
  3. package/esm/options/loadBabelOptions.js +1 -1
  4. package/esm/options/loadBabelOptions.js.map +1 -1
  5. package/esm/plugins/babel-transform.js +2 -2
  6. package/esm/plugins/babel-transform.js.map +1 -1
  7. package/esm/plugins/collector.js +4 -4
  8. package/esm/plugins/collector.js.map +1 -1
  9. package/esm/plugins/preeval.js +11 -11
  10. package/esm/plugins/preeval.js.map +1 -1
  11. package/esm/plugins/shaker.js +5 -5
  12. package/esm/plugins/shaker.js.map +1 -1
  13. package/esm/shaker.js +2 -2
  14. package/esm/shaker.js.map +1 -1
  15. package/esm/transform/Entrypoint.helpers.js +1 -2
  16. package/esm/transform/Entrypoint.helpers.js.map +1 -1
  17. package/esm/transform/Entrypoint.js +4 -4
  18. package/esm/transform/Entrypoint.js.map +1 -1
  19. package/esm/transform/generators/collect.js +3 -2
  20. package/esm/transform/generators/collect.js.map +1 -1
  21. package/esm/transform/generators/evalFile.js +6 -6
  22. package/esm/transform/generators/evalFile.js.map +1 -1
  23. package/esm/transform/generators/extract.js +18 -14
  24. package/esm/transform/generators/extract.js.map +1 -1
  25. package/esm/transform/generators/transform.js +2 -2
  26. package/esm/transform/generators/transform.js.map +1 -1
  27. package/esm/transform/generators/workflow.js +3 -3
  28. package/esm/transform/generators/workflow.js.map +1 -1
  29. package/esm/transform/helpers/{loadLinariaOptions.js → loadWywOptions.js} +5 -5
  30. package/esm/transform/helpers/loadWywOptions.js.map +1 -0
  31. package/esm/transform.js +11 -10
  32. package/esm/transform.js.map +1 -1
  33. package/esm/types.js.map +1 -1
  34. package/esm/utils/ShakerMetadata.js +1 -1
  35. package/esm/utils/ShakerMetadata.js.map +1 -1
  36. package/esm/utils/{addIdentifierToLinariaPreval.js → addIdentifierToWywPreval.js} +10 -10
  37. package/esm/utils/addIdentifierToWywPreval.js.map +1 -0
  38. package/esm/utils/collectExportsAndImports.js +141 -53
  39. package/esm/utils/collectExportsAndImports.js.map +1 -1
  40. package/esm/utils/collectTemplateDependencies.js +2 -2
  41. package/esm/utils/collectTemplateDependencies.js.map +1 -1
  42. package/esm/utils/getTagProcessor.js +1 -1
  43. package/esm/utils/getTagProcessor.js.map +1 -1
  44. package/esm/utils/hasWywPreval.js +7 -0
  45. package/esm/utils/hasWywPreval.js.map +1 -0
  46. package/lib/index.js +28 -4
  47. package/lib/index.js.map +1 -1
  48. package/lib/options/loadBabelOptions.js +1 -1
  49. package/lib/options/loadBabelOptions.js.map +1 -1
  50. package/lib/plugins/babel-transform.js +2 -2
  51. package/lib/plugins/babel-transform.js.map +1 -1
  52. package/lib/plugins/collector.js +4 -4
  53. package/lib/plugins/collector.js.map +1 -1
  54. package/lib/plugins/preeval.js +11 -11
  55. package/lib/plugins/preeval.js.map +1 -1
  56. package/lib/plugins/shaker.js +5 -5
  57. package/lib/plugins/shaker.js.map +1 -1
  58. package/lib/shaker.js +2 -2
  59. package/lib/shaker.js.map +1 -1
  60. package/lib/transform/Entrypoint.helpers.js +1 -2
  61. package/lib/transform/Entrypoint.helpers.js.map +1 -1
  62. package/lib/transform/Entrypoint.js +4 -4
  63. package/lib/transform/Entrypoint.js.map +1 -1
  64. package/lib/transform/generators/collect.js +1 -1
  65. package/lib/transform/generators/collect.js.map +1 -1
  66. package/lib/transform/generators/evalFile.js +6 -6
  67. package/lib/transform/generators/evalFile.js.map +1 -1
  68. package/lib/transform/generators/extract.js +18 -14
  69. package/lib/transform/generators/extract.js.map +1 -1
  70. package/lib/transform/generators/transform.js +2 -2
  71. package/lib/transform/generators/transform.js.map +1 -1
  72. package/lib/transform/generators/workflow.js +3 -3
  73. package/lib/transform/generators/workflow.js.map +1 -1
  74. package/lib/transform/helpers/{loadLinariaOptions.js → loadWywOptions.js} +6 -6
  75. package/lib/transform/helpers/loadWywOptions.js.map +1 -0
  76. package/lib/transform/helpers/withDefaultServices.js +2 -2
  77. package/lib/transform/helpers/withDefaultServices.js.map +1 -1
  78. package/lib/transform.js +11 -10
  79. package/lib/transform.js.map +1 -1
  80. package/lib/types.js.map +1 -1
  81. package/lib/utils/ShakerMetadata.js +1 -1
  82. package/lib/utils/ShakerMetadata.js.map +1 -1
  83. package/lib/utils/{addIdentifierToLinariaPreval.js → addIdentifierToWywPreval.js} +12 -12
  84. package/lib/utils/addIdentifierToWywPreval.js.map +1 -0
  85. package/lib/utils/collectExportsAndImports.js +141 -53
  86. package/lib/utils/collectExportsAndImports.js.map +1 -1
  87. package/lib/utils/collectTemplateDependencies.js +2 -2
  88. package/lib/utils/collectTemplateDependencies.js.map +1 -1
  89. package/lib/utils/getTagProcessor.js +2 -2
  90. package/lib/utils/getTagProcessor.js.map +1 -1
  91. package/lib/utils/hasWywPreval.js +13 -0
  92. package/lib/utils/hasWywPreval.js.map +1 -0
  93. package/lib/vm/createVmContext.js +2 -2
  94. package/lib/vm/createVmContext.js.map +1 -1
  95. package/package.json +24 -21
  96. package/types/index.d.ts +5 -2
  97. package/types/index.js +9 -3
  98. package/types/options/loadBabelOptions.js +1 -1
  99. package/types/plugins/babel-transform.js +2 -2
  100. package/types/plugins/collector.js +4 -4
  101. package/types/plugins/preeval.js +11 -11
  102. package/types/plugins/shaker.js +5 -5
  103. package/types/shaker.js +2 -2
  104. package/types/transform/Entrypoint.d.ts +1 -1
  105. package/types/transform/Entrypoint.helpers.js +3 -3
  106. package/types/transform/Entrypoint.js +4 -4
  107. package/types/transform/generators/collect.js +1 -1
  108. package/types/transform/generators/evalFile.js +7 -7
  109. package/types/transform/generators/extract.js +21 -12
  110. package/types/transform/generators/transform.js +2 -4
  111. package/types/transform/generators/workflow.js +3 -3
  112. package/types/transform/helpers/{loadLinariaOptions.d.ts → loadWywOptions.d.ts} +1 -1
  113. package/types/transform/helpers/{loadLinariaOptions.js → loadWywOptions.js} +19 -19
  114. package/types/transform.d.ts +7 -6
  115. package/types/transform.js +11 -10
  116. package/types/types.d.ts +1 -1
  117. package/types/utils/ShakerMetadata.d.ts +1 -1
  118. package/types/utils/ShakerMetadata.js +1 -1
  119. package/types/utils/addIdentifierToWywPreval.d.ts +4 -0
  120. package/types/utils/{addIdentifierToLinariaPreval.js → addIdentifierToWywPreval.js} +12 -12
  121. package/types/utils/collectExportsAndImports.js +157 -58
  122. package/types/utils/collectTemplateDependencies.d.ts +1 -1
  123. package/types/utils/collectTemplateDependencies.js +2 -2
  124. package/types/utils/getTagProcessor.js +2 -2
  125. package/types/utils/hasWywPreval.d.ts +4 -0
  126. package/types/utils/{hasLinariaPreval.js → hasWywPreval.js} +3 -3
  127. package/esm/transform/helpers/loadLinariaOptions.js.map +0 -1
  128. package/esm/utils/addIdentifierToLinariaPreval.js.map +0 -1
  129. package/esm/utils/findPackageJSON.js +0 -26
  130. package/esm/utils/findPackageJSON.js.map +0 -1
  131. package/esm/utils/hasLinariaPreval.js +0 -7
  132. package/esm/utils/hasLinariaPreval.js.map +0 -1
  133. package/lib/transform/helpers/loadLinariaOptions.js.map +0 -1
  134. package/lib/utils/addIdentifierToLinariaPreval.js.map +0 -1
  135. package/lib/utils/findPackageJSON.js +0 -35
  136. package/lib/utils/findPackageJSON.js.map +0 -1
  137. package/lib/utils/hasLinariaPreval.js +0 -13
  138. package/lib/utils/hasLinariaPreval.js.map +0 -1
  139. package/types/utils/addIdentifierToLinariaPreval.d.ts +0 -4
  140. package/types/utils/findPackageJSON.d.ts +0 -1
  141. package/types/utils/findPackageJSON.js +0 -56
  142. package/types/utils/hasLinariaPreval.d.ts +0 -4
@@ -1 +1 @@
1
- {"version":3,"file":"transform.js","names":["isFeatureEnabled","TransformCacheCollection","Entrypoint","asyncActionRunner","syncActionRunner","baseHandlers","asyncResolveImports","syncResolveImports","loadLinariaOptions","withDefaultServices","transformSync","partialServices","originalCode","syncResolve","customHandlers","options","pluginOptions","services","features","filename","cache","entrypoint","createRoot","ignored","code","sourceMap","inputSourceMap","workflowAction","createAction","undefined","result","resolveImports","call","log","name","err","console","error","transform","asyncResolve"],"sources":["../src/transform.ts"],"sourcesContent":["/**\n * This file exposes transform function that:\n * - parse the passed code to AST\n * - transforms the AST using Linaria babel preset ('./babel/index.js) and additional config defined in Linaria config file or passed to bundler configuration.\n * - runs generated CSS files through default of user-defined preprocessor\n * - generates source maps for CSS files\n * - return transformed code (without Linaria template literals), generated CSS, source maps and babel metadata from transform step.\n */\n\nimport { isFeatureEnabled } from '@wyw-in-js/shared';\n\nimport { TransformCacheCollection } from './cache';\nimport { Entrypoint } from './transform/Entrypoint';\nimport {\n asyncActionRunner,\n syncActionRunner,\n} from './transform/actions/actionRunner';\nimport { baseHandlers } from './transform/generators';\nimport {\n asyncResolveImports,\n syncResolveImports,\n} from './transform/generators/resolveImports';\nimport type { PartialOptions } from './transform/helpers/loadLinariaOptions';\nimport { loadLinariaOptions } from './transform/helpers/loadLinariaOptions';\nimport { withDefaultServices } from './transform/helpers/withDefaultServices';\nimport type {\n Handlers,\n IResolveImportsAction,\n Services,\n} from './transform/types';\nimport type { Result } from './types';\n\ntype PartialServices = Partial<Omit<Services, 'options'>> & {\n options: Omit<Services['options'], 'pluginOptions'> & {\n pluginOptions?: PartialOptions;\n };\n};\n\ntype AllHandlers<TMode extends 'async' | 'sync'> = Handlers<TMode>;\n\nexport function transformSync(\n partialServices: PartialServices,\n originalCode: string,\n syncResolve: (what: string, importer: string, stack: string[]) => string,\n customHandlers: Partial<AllHandlers<'sync'>> = {}\n): Result {\n const { options } = partialServices;\n const pluginOptions = loadLinariaOptions(options.pluginOptions);\n const services = withDefaultServices({\n ...partialServices,\n options: {\n ...options,\n pluginOptions,\n },\n });\n\n if (\n !isFeatureEnabled(pluginOptions.features, 'globalCache', options.filename)\n ) {\n // If global cache is disabled, we need to create a new cache for each file\n services.cache = new TransformCacheCollection();\n }\n\n const entrypoint = Entrypoint.createRoot(\n services,\n options.filename,\n ['__linariaPreval'],\n originalCode\n );\n\n if (entrypoint.ignored) {\n return {\n code: originalCode,\n sourceMap: options.inputSourceMap,\n };\n }\n\n const workflowAction = entrypoint.createAction('workflow', undefined);\n\n try {\n const result = syncActionRunner(workflowAction, {\n ...baseHandlers,\n ...customHandlers,\n resolveImports() {\n return syncResolveImports.call(this, syncResolve);\n },\n });\n\n entrypoint.log('%s is ready', entrypoint.name);\n\n return result;\n } catch (err) {\n entrypoint.log('Unhandled error %O', err);\n\n if (\n isFeatureEnabled(pluginOptions.features, 'softErrors', options.filename)\n ) {\n // eslint-disable-next-line no-console\n console.error(`Error during transform of ${entrypoint.name}:`, err);\n\n return {\n code: originalCode,\n sourceMap: options.inputSourceMap,\n };\n }\n\n throw err;\n }\n}\n\nexport async function transform(\n partialServices: PartialServices,\n originalCode: string,\n asyncResolve: (\n what: string,\n importer: string,\n stack: string[]\n ) => Promise<string | null>,\n customHandlers: Partial<AllHandlers<'sync'>> = {}\n): Promise<Result> {\n const { options } = partialServices;\n const pluginOptions = loadLinariaOptions(options.pluginOptions);\n const services = withDefaultServices({\n ...partialServices,\n options: {\n ...options,\n pluginOptions,\n },\n });\n\n if (\n !isFeatureEnabled(pluginOptions.features, 'globalCache', options.filename)\n ) {\n // If global cache is disabled, we need to create a new cache for each file\n services.cache = new TransformCacheCollection();\n }\n\n /*\n * This method can be run simultaneously for multiple files.\n * A shared cache is accessible for all runs, but each run has its own queue\n * to maintain the correct processing order. The cache stores the outcome\n * of tree-shaking, and if the result is already stored in the cache\n * but the \"only\" option has changed, the file will be re-processed using\n * the combined \"only\" option.\n */\n const entrypoint = Entrypoint.createRoot(\n services,\n options.filename,\n ['__linariaPreval'],\n originalCode\n );\n\n if (entrypoint.ignored) {\n return {\n code: originalCode,\n sourceMap: options.inputSourceMap,\n };\n }\n\n const workflowAction = entrypoint.createAction('workflow', undefined);\n\n try {\n const result = await asyncActionRunner(workflowAction, {\n ...baseHandlers,\n ...customHandlers,\n resolveImports(this: IResolveImportsAction) {\n return asyncResolveImports.call(this, asyncResolve);\n },\n });\n\n entrypoint.log('%s is ready', entrypoint.name);\n\n return result;\n } catch (err) {\n entrypoint.log('Unhandled error %O', err);\n\n if (\n isFeatureEnabled(pluginOptions.features, 'softErrors', options.filename)\n ) {\n // eslint-disable-next-line no-console\n console.error(`Error during transform of ${entrypoint.name}:`, err);\n\n return {\n code: originalCode,\n sourceMap: options.inputSourceMap,\n };\n }\n\n throw err;\n }\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA,SAASA,gBAAgB,QAAQ,mBAAmB;AAEpD,SAASC,wBAAwB,QAAQ,SAAS;AAClD,SAASC,UAAU,QAAQ,wBAAwB;AACnD,SACEC,iBAAiB,EACjBC,gBAAgB,QACX,kCAAkC;AACzC,SAASC,YAAY,QAAQ,wBAAwB;AACrD,SACEC,mBAAmB,EACnBC,kBAAkB,QACb,uCAAuC;AAE9C,SAASC,kBAAkB,QAAQ,wCAAwC;AAC3E,SAASC,mBAAmB,QAAQ,yCAAyC;AAgB7E,OAAO,SAASC,aAAaA,CAC3BC,eAAgC,EAChCC,YAAoB,EACpBC,WAAwE,EACxEC,cAA4C,GAAG,CAAC,CAAC,EACzC;EACR,MAAM;IAAEC;EAAQ,CAAC,GAAGJ,eAAe;EACnC,MAAMK,aAAa,GAAGR,kBAAkB,CAACO,OAAO,CAACC,aAAa,CAAC;EAC/D,MAAMC,QAAQ,GAAGR,mBAAmB,CAAC;IACnC,GAAGE,eAAe;IAClBI,OAAO,EAAE;MACP,GAAGA,OAAO;MACVC;IACF;EACF,CAAC,CAAC;EAEF,IACE,CAAChB,gBAAgB,CAACgB,aAAa,CAACE,QAAQ,EAAE,aAAa,EAAEH,OAAO,CAACI,QAAQ,CAAC,EAC1E;IACA;IACAF,QAAQ,CAACG,KAAK,GAAG,IAAInB,wBAAwB,CAAC,CAAC;EACjD;EAEA,MAAMoB,UAAU,GAAGnB,UAAU,CAACoB,UAAU,CACtCL,QAAQ,EACRF,OAAO,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EAAEC,GAAG,CAAC;MAEnE,OAAO;QACLX,IAAI,EAAEZ,YAAY;QAClBa,SAAS,EAAEV,OAAO,CAACW;MACrB,CAAC;IACH;IAEA,MAAMS,GAAG;EACX;AACF"}
1
+ {"version":3,"file":"transform.js","names":["isFeatureEnabled","loadWywOptions","TransformCacheCollection","Entrypoint","asyncActionRunner","syncActionRunner","baseHandlers","asyncResolveImports","syncResolveImports","withDefaultServices","transformSync","partialServices","originalCode","syncResolve","customHandlers","options","pluginOptions","services","features","filename","cache","entrypoint","createRoot","ignored","code","sourceMap","inputSourceMap","workflowAction","createAction","undefined","result","resolveImports","call","log","name","err","console","error","transform","asyncResolve"],"sources":["../src/transform.ts"],"sourcesContent":["/**\n * This file exposes sync and async transform functions that:\n * - parse the passed code to AST\n * - builds a dependency graph for the file\n * - shakes each dependency and removes unused code\n * - runs generated code in a sandbox\n * - collects artifacts\n * - returns transformed code (without WYW template literals), generated CSS, source maps and babel metadata from transform step.\n */\n\nimport { isFeatureEnabled } from '@wyw-in-js/shared';\n\nimport type { PartialOptions } from './transform/helpers/loadWywOptions';\nimport { loadWywOptions } from './transform/helpers/loadWywOptions';\nimport { TransformCacheCollection } from './cache';\nimport { Entrypoint } from './transform/Entrypoint';\nimport {\n asyncActionRunner,\n syncActionRunner,\n} from './transform/actions/actionRunner';\nimport { baseHandlers } from './transform/generators';\nimport {\n asyncResolveImports,\n syncResolveImports,\n} from './transform/generators/resolveImports';\nimport { withDefaultServices } from './transform/helpers/withDefaultServices';\nimport type {\n Handlers,\n IResolveImportsAction,\n Services,\n} from './transform/types';\nimport type { Result } from './types';\n\ntype PartialServices = Partial<Omit<Services, 'options'>> & {\n options: Omit<Services['options'], 'pluginOptions'> & {\n pluginOptions?: PartialOptions;\n };\n};\n\ntype AllHandlers<TMode extends 'async' | 'sync'> = Handlers<TMode>;\n\nexport function transformSync(\n partialServices: PartialServices,\n originalCode: string,\n syncResolve: (what: string, importer: string, stack: string[]) => string,\n customHandlers: Partial<AllHandlers<'sync'>> = {}\n): Result {\n const { options } = partialServices;\n const pluginOptions = loadWywOptions(options.pluginOptions);\n const services = withDefaultServices({\n ...partialServices,\n options: {\n ...options,\n pluginOptions,\n },\n });\n\n if (\n !isFeatureEnabled(pluginOptions.features, 'globalCache', options.filename)\n ) {\n // If global cache is disabled, we need to create a new cache for each file\n services.cache = new TransformCacheCollection();\n }\n\n const entrypoint = Entrypoint.createRoot(\n services,\n options.filename,\n ['__wywPreval'],\n originalCode\n );\n\n if (entrypoint.ignored) {\n return {\n code: originalCode,\n sourceMap: options.inputSourceMap,\n };\n }\n\n const workflowAction = entrypoint.createAction('workflow', undefined);\n\n try {\n const result = syncActionRunner(workflowAction, {\n ...baseHandlers,\n ...customHandlers,\n resolveImports() {\n return syncResolveImports.call(this, syncResolve);\n },\n });\n\n entrypoint.log('%s is ready', entrypoint.name);\n\n return result;\n } catch (err) {\n entrypoint.log('Unhandled error %O', err);\n\n if (\n isFeatureEnabled(pluginOptions.features, 'softErrors', options.filename)\n ) {\n // eslint-disable-next-line no-console\n console.error(`Error during transform of ${entrypoint.name}:`, err);\n\n return {\n code: originalCode,\n sourceMap: options.inputSourceMap,\n };\n }\n\n throw err;\n }\n}\n\nexport async function transform(\n partialServices: PartialServices,\n originalCode: string,\n asyncResolve: (\n what: string,\n importer: string,\n stack: string[]\n ) => Promise<string | null>,\n customHandlers: Partial<AllHandlers<'sync'>> = {}\n): Promise<Result> {\n const { options } = partialServices;\n const pluginOptions = loadWywOptions(options.pluginOptions);\n const services = withDefaultServices({\n ...partialServices,\n options: {\n ...options,\n pluginOptions,\n },\n });\n\n if (\n !isFeatureEnabled(pluginOptions.features, 'globalCache', options.filename)\n ) {\n // If global cache is disabled, we need to create a new cache for each file\n services.cache = new TransformCacheCollection();\n }\n\n /*\n * This method can be run simultaneously for multiple files.\n * A shared cache is accessible for all runs, but each run has its own queue\n * to maintain the correct processing order. The cache stores the outcome\n * of tree-shaking, and if the result is already stored in the cache\n * but the \"only\" option has changed, the file will be re-processed using\n * the combined \"only\" option.\n */\n const entrypoint = Entrypoint.createRoot(\n services,\n options.filename,\n ['__wywPreval'],\n originalCode\n );\n\n if (entrypoint.ignored) {\n return {\n code: originalCode,\n sourceMap: options.inputSourceMap,\n };\n }\n\n const workflowAction = entrypoint.createAction('workflow', undefined);\n\n try {\n const result = await asyncActionRunner(workflowAction, {\n ...baseHandlers,\n ...customHandlers,\n resolveImports(this: IResolveImportsAction) {\n return asyncResolveImports.call(this, asyncResolve);\n },\n });\n\n entrypoint.log('%s is ready', entrypoint.name);\n\n return result;\n } catch (err) {\n entrypoint.log('Unhandled error %O', err);\n\n if (\n isFeatureEnabled(pluginOptions.features, 'softErrors', options.filename)\n ) {\n // eslint-disable-next-line no-console\n console.error(`Error during transform of ${entrypoint.name}:`, err);\n\n return {\n code: originalCode,\n sourceMap: options.inputSourceMap,\n };\n }\n\n throw err;\n }\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA,SAASA,gBAAgB,QAAQ,mBAAmB;AAGpD,SAASC,cAAc,QAAQ,oCAAoC;AACnE,SAASC,wBAAwB,QAAQ,SAAS;AAClD,SAASC,UAAU,QAAQ,wBAAwB;AACnD,SACEC,iBAAiB,EACjBC,gBAAgB,QACX,kCAAkC;AACzC,SAASC,YAAY,QAAQ,wBAAwB;AACrD,SACEC,mBAAmB,EACnBC,kBAAkB,QACb,uCAAuC;AAC9C,SAASC,mBAAmB,QAAQ,yCAAyC;AAgB7E,OAAO,SAASC,aAAaA,CAC3BC,eAAgC,EAChCC,YAAoB,EACpBC,WAAwE,EACxEC,cAA4C,GAAG,CAAC,CAAC,EACzC;EACR,MAAM;IAAEC;EAAQ,CAAC,GAAGJ,eAAe;EACnC,MAAMK,aAAa,GAAGf,cAAc,CAACc,OAAO,CAACC,aAAa,CAAC;EAC3D,MAAMC,QAAQ,GAAGR,mBAAmB,CAAC;IACnC,GAAGE,eAAe;IAClBI,OAAO,EAAE;MACP,GAA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,OAAOJ,MAAM;EACf,CAAC,CAAC,OAAOK,GAAG,EAAE;IACZd,UAAU,CAACY,GAAG,CAAC,oBAAoB,EAAEE,GAAG,CAAC;IAEzC,IACEnC,gBAAgB,CAACgB,aAAa,CAACE,QAAQ,EAAE,YAAY,EAAEH,OAAO,CAACI,QAAQ,CAAC,EACxE;MACA;MACAiB,OAAO,CAACC,KAAK,CAAE,6BAA4BhB,UAAU,CAACa,IAAK,GAAE,EAAEC,GAAG,CAAC;MAEnE,OAAO;QACLX,IAAI,EAAEZ,YAAY;QAClBa,SAAS,EAAEV,OAAO,CAACW;MACrB,CAAC;IACH;IAEA,MAAMS,GAAG;EACX;AACF"}
package/esm/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","names":[],"sources":["../src/types.ts"],"sourcesContent":["import type { BabelFile, PluginPass } from '@babel/core';\nimport type { NodePath } from '@babel/traverse';\nimport type { File, Program } from '@babel/types';\nimport type { RawSourceMap } from 'source-map';\n\nimport type { BaseProcessor } from '@wyw-in-js/processor-utils';\nimport type {\n Debugger,\n Replacement,\n Rules,\n StrictOptions,\n} from '@wyw-in-js/shared';\n\nimport type { WYWTransformMetadata } from './utils/TransformMetadata';\n\nexport type PluginOptions = StrictOptions & {\n configFile?: string | false;\n stage?: Stage;\n};\n\nexport type ParentEntrypoint = {\n evaluated: boolean;\n log: Debugger;\n name: string;\n parents: ParentEntrypoint[];\n seqId: number;\n};\n\nexport type Dependencies = string[];\n\nexport interface IPluginState extends PluginPass {\n dependencies: Dependencies;\n file: BabelFile & {\n metadata: {\n linaria?: WYWTransformMetadata;\n };\n };\n processors: BaseProcessor[];\n}\n\nexport interface ITransformFileResult {\n code: string;\n metadata: WYWTransformMetadata | null;\n}\n\nexport type Stage = 'preeval' | 'collect';\n\nexport type Result = {\n code: string;\n cssSourceMapText?: string;\n cssText?: string;\n dependencies?: string[];\n replacements?: Replacement[];\n rules?: Rules;\n sourceMap?: RawSourceMap | null;\n};\n\nexport type Options = {\n filename: string;\n inputSourceMap?: RawSourceMap;\n outputFilename?: string;\n pluginOptions?: Partial<PluginOptions>;\n preprocessor?: Preprocessor;\n root?: string;\n};\n\nexport type PreprocessorFn = (selector: string, cssText: string) => string;\nexport type Preprocessor = 'none' | 'stylis' | PreprocessorFn | void;\n\nexport type MissedBabelCoreTypes = {\n File: new (\n options: { filename: string },\n file: { ast: File; code: string }\n ) => { path: NodePath<Program> };\n};\n\nexport type JSONValue =\n | null\n | string\n | number\n | boolean\n | JSONObject\n | JSONArray;\n\nexport interface JSONObject {\n [x: string]: JSONValue;\n}\n\nexport type JSONArray = Array<JSONValue>;\n\nexport type Serializable = JSONValue;\n\n/**\n * CSS-related types\n */\n"],"mappings":""}
1
+ {"version":3,"file":"types.js","names":[],"sources":["../src/types.ts"],"sourcesContent":["import type { BabelFile, PluginPass } from '@babel/core';\nimport type { NodePath } from '@babel/traverse';\nimport type { File, Program } from '@babel/types';\nimport type { RawSourceMap } from 'source-map';\n\nimport type { BaseProcessor } from '@wyw-in-js/processor-utils';\nimport type {\n Debugger,\n Replacement,\n Rules,\n StrictOptions,\n} from '@wyw-in-js/shared';\n\nimport type { WYWTransformMetadata } from './utils/TransformMetadata';\n\nexport type PluginOptions = StrictOptions & {\n configFile?: string | false;\n stage?: Stage;\n};\n\nexport type ParentEntrypoint = {\n evaluated: boolean;\n log: Debugger;\n name: string;\n parents: ParentEntrypoint[];\n seqId: number;\n};\n\nexport type Dependencies = string[];\n\nexport interface IPluginState extends PluginPass {\n dependencies: Dependencies;\n file: BabelFile & {\n metadata: {\n wywInJS?: WYWTransformMetadata;\n };\n };\n processors: BaseProcessor[];\n}\n\nexport interface ITransformFileResult {\n code: string;\n metadata: WYWTransformMetadata | null;\n}\n\nexport type Stage = 'preeval' | 'collect';\n\nexport type Result = {\n code: string;\n cssSourceMapText?: string;\n cssText?: string;\n dependencies?: string[];\n replacements?: Replacement[];\n rules?: Rules;\n sourceMap?: RawSourceMap | null;\n};\n\nexport type Options = {\n filename: string;\n inputSourceMap?: RawSourceMap;\n outputFilename?: string;\n pluginOptions?: Partial<PluginOptions>;\n preprocessor?: Preprocessor;\n root?: string;\n};\n\nexport type PreprocessorFn = (selector: string, cssText: string) => string;\nexport type Preprocessor = 'none' | 'stylis' | PreprocessorFn | void;\n\nexport type MissedBabelCoreTypes = {\n File: new (\n options: { filename: string },\n file: { ast: File; code: string }\n ) => { path: NodePath<Program> };\n};\n\nexport type JSONValue =\n | null\n | string\n | number\n | boolean\n | JSONObject\n | JSONArray;\n\nexport interface JSONObject {\n [x: string]: JSONValue;\n}\n\nexport type JSONArray = Array<JSONValue>;\n\nexport type Serializable = JSONValue;\n\n/**\n * CSS-related types\n */\n"],"mappings":""}
@@ -1,2 +1,2 @@
1
- export const hasShakerMetadata = metadata => metadata !== undefined && 'linariaEvaluator' in metadata;
1
+ export const hasShakerMetadata = metadata => metadata !== undefined && 'wywEvaluator' in metadata;
2
2
  //# sourceMappingURL=ShakerMetadata.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"ShakerMetadata.js","names":["hasShakerMetadata","metadata","undefined"],"sources":["../../src/utils/ShakerMetadata.ts"],"sourcesContent":["export interface IShakerMetadata {\n imports: Map<string, string[]>;\n}\n\nexport interface IMetadata {\n linariaEvaluator: IShakerMetadata;\n}\n\nexport const hasShakerMetadata = (\n metadata: object | undefined\n): metadata is IMetadata =>\n metadata !== undefined && 'linariaEvaluator' in metadata;\n"],"mappings":"AAQA,OAAO,MAAMA,iBAAiB,GAC5BC,QAA4B,IAE5BA,QAAQ,KAAKC,SAAS,IAAI,kBAAkB,IAAID,QAAQ"}
1
+ {"version":3,"file":"ShakerMetadata.js","names":["hasShakerMetadata","metadata","undefined"],"sources":["../../src/utils/ShakerMetadata.ts"],"sourcesContent":["export interface IShakerMetadata {\n imports: Map<string, string[]>;\n}\n\nexport interface IMetadata {\n wywEvaluator: IShakerMetadata;\n}\n\nexport const hasShakerMetadata = (\n metadata: object | undefined\n): metadata is IMetadata =>\n metadata !== undefined && 'wywEvaluator' in metadata;\n"],"mappings":"AAQA,OAAO,MAAMA,iBAAiB,GAC5BC,QAA4B,IAE5BA,QAAQ,KAAKC,SAAS,IAAI,cAAc,IAAID,QAAQ"}
@@ -1,14 +1,14 @@
1
1
  import { createId } from './createId';
2
2
  import { reference } from './scopeHelpers';
3
- export function getOrAddLinariaPreval(scope) {
3
+ export function getOrAddWywPreval(scope) {
4
4
  const rootScope = scope.getProgramParent();
5
5
  const programPath = rootScope.path;
6
- let object = programPath.getData('__linariaPreval');
6
+ let object = programPath.getData('__wywPreval');
7
7
  if (object) {
8
8
  return object;
9
9
  }
10
10
  if (programPath.node.sourceType === 'script') {
11
- // CJS exports.__linariaPreval = {};
11
+ // CJS exports.__wywPreval = {};
12
12
  const prevalExport = {
13
13
  expression: {
14
14
  type: 'AssignmentExpression',
@@ -16,7 +16,7 @@ export function getOrAddLinariaPreval(scope) {
16
16
  left: {
17
17
  computed: false,
18
18
  object: createId('exports'),
19
- property: createId('__linariaPreval'),
19
+ property: createId('__wywPreval'),
20
20
  type: 'MemberExpression'
21
21
  },
22
22
  right: {
@@ -29,11 +29,11 @@ export function getOrAddLinariaPreval(scope) {
29
29
  const [inserted] = programPath.pushContainer('body', [prevalExport]);
30
30
  object = inserted.get('expression.right');
31
31
  } else {
32
- // ESM export const __linariaPreval = {};
32
+ // ESM export const __wywPreval = {};
33
33
  const prevalExport = {
34
34
  declaration: {
35
35
  declarations: [{
36
- id: createId('__linariaPreval'),
36
+ id: createId('__wywPreval'),
37
37
  init: {
38
38
  properties: [],
39
39
  type: 'ObjectExpression'
@@ -49,12 +49,12 @@ export function getOrAddLinariaPreval(scope) {
49
49
  const [inserted] = programPath.pushContainer('body', [prevalExport]);
50
50
  object = inserted.get('declaration.declarations.0.init');
51
51
  }
52
- programPath.setData('__linariaPreval', object);
52
+ programPath.setData('__wywPreval', object);
53
53
  return object;
54
54
  }
55
- export function addIdentifierToLinariaPreval(scope, name) {
55
+ export function addIdentifierToWywPreval(scope, name) {
56
56
  const rootScope = scope.getProgramParent();
57
- const object = getOrAddLinariaPreval(rootScope);
57
+ const object = getOrAddWywPreval(rootScope);
58
58
  const newProperty = {
59
59
  type: 'ObjectProperty',
60
60
  key: createId(name),
@@ -65,4 +65,4 @@ export function addIdentifierToLinariaPreval(scope, name) {
65
65
  const [inserted] = object.pushContainer('properties', [newProperty]);
66
66
  reference(inserted.get('value'));
67
67
  }
68
- //# sourceMappingURL=addIdentifierToLinariaPreval.js.map
68
+ //# sourceMappingURL=addIdentifierToWywPreval.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"addIdentifierToWywPreval.js","names":["createId","reference","getOrAddWywPreval","scope","rootScope","getProgramParent","programPath","path","object","getData","node","sourceType","prevalExport","expression","type","operator","left","computed","property","right","properties","inserted","pushContainer","get","declaration","declarations","id","init","kind","specifiers","setData","addIdentifierToWywPreval","name","newProperty","key","value","shorthand"],"sources":["../../src/utils/addIdentifierToWywPreval.ts"],"sourcesContent":["import type { NodePath, Scope } from '@babel/traverse';\nimport type {\n ExportNamedDeclaration,\n ExpressionStatement,\n Identifier,\n ObjectExpression,\n ObjectProperty,\n Program,\n} from '@babel/types';\n\nimport { createId } from './createId';\nimport { reference } from './scopeHelpers';\n\nexport function getOrAddWywPreval(scope: Scope): NodePath<ObjectExpression> {\n const rootScope = scope.getProgramParent();\n const programPath = rootScope.path as NodePath<Program>;\n\n let object = programPath.getData('__wywPreval');\n if (object) {\n return object;\n }\n\n if (programPath.node.sourceType === 'script') {\n // CJS exports.__wywPreval = {};\n const prevalExport: ExpressionStatement = {\n expression: {\n type: 'AssignmentExpression',\n operator: '=',\n left: {\n computed: false,\n object: createId('exports'),\n property: createId('__wywPreval'),\n type: 'MemberExpression',\n },\n right: {\n properties: [],\n type: 'ObjectExpression',\n },\n },\n type: 'ExpressionStatement',\n };\n\n const [inserted] = programPath.pushContainer('body', [prevalExport]);\n object = inserted.get('expression.right') as NodePath<ObjectExpression>;\n } else {\n // ESM export const __wywPreval = {};\n const prevalExport: ExportNamedDeclaration = {\n declaration: {\n declarations: [\n {\n id: createId('__wywPreval'),\n init: {\n properties: [],\n type: 'ObjectExpression',\n },\n type: 'VariableDeclarator',\n },\n ],\n kind: 'const',\n type: 'VariableDeclaration',\n },\n specifiers: [],\n type: 'ExportNamedDeclaration',\n };\n\n const [inserted] = programPath.pushContainer('body', [prevalExport]);\n object = inserted.get(\n 'declaration.declarations.0.init'\n ) as NodePath<ObjectExpression>;\n }\n\n programPath.setData('__wywPreval', object);\n return object;\n}\n\nexport function addIdentifierToWywPreval(scope: Scope, name: string) {\n const rootScope = scope.getProgramParent();\n const object = getOrAddWywPreval(rootScope);\n const newProperty: ObjectProperty = {\n type: 'ObjectProperty',\n key: createId(name),\n value: createId(name),\n computed: false,\n shorthand: false,\n };\n\n const [inserted] = object.pushContainer('properties', [newProperty]);\n reference(inserted.get('value') as NodePath<Identifier>);\n}\n"],"mappings":"AAUA,SAASA,QAAQ,QAAQ,YAAY;AACrC,SAASC,SAAS,QAAQ,gBAAgB;AAE1C,OAAO,SAASC,iBAAiBA,CAACC,KAAY,EAA8B;EAC1E,MAAMC,SAAS,GAAGD,KAAK,CAACE,gBAAgB,CAAC,CAAC;EAC1C,MAAMC,WAAW,GAAGF,SAAS,CAACG,IAAyB;EAEvD,IAAIC,MAAM,GAAGF,WAAW,CAACG,OAAO,CAAC,aAAa,CAAC;EAC/C,IAAID,MAAM,EAAE;IACV,OAAOA,MAAM;EACf;EAEA,IAAIF,WAAW,CAACI,IAAI,CAACC,UAAU,KAAK,QAAQ,EAAE;IAC5C;IACA,MAAMC,YAAiC,GAAG;MACxCC,UAAU,EAAE;QACVC,IAAI,EAAE,sBAAsB;QAC5BC,QAAQ,EAAE,GAAG;QACbC,IAAI,EAAE;UACJC,QAAQ,EAAE,KAAK;UACfT,MAAM,EAAER,QAAQ,CAAC,SAAS,CAAC;UAC3BkB,QAAQ,EAAElB,QAAQ,CAAC,aAAa,CAAC;UACjCc,IAAI,EAAE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,KAAK;IACfmB,SAAS,EAAE;EACb,CAAC;EAED,MAAM,CAACf,QAAQ,CAAC,GAAGb,MAAM,CAACc,aAAa,CAAC,YAAY,EAAE,CAACW,WAAW,CAAC,CAAC;EACpEhC,SAAS,CAACoB,QAAQ,CAACE,GAAG,CAAC,OAAO,CAAyB,CAAC;AAC1D"}
@@ -242,21 +242,60 @@ function getCalleeName(path) {
242
242
  }
243
243
  return undefined;
244
244
  }
245
- function getImportExportTypeByInteropFunction(path) {
246
- const name = getCalleeName(path);
245
+ const matchCall = (p, rules) => {
246
+ const name = getCalleeName(p);
247
247
  if (name === undefined) {
248
- return undefined;
248
+ return false;
249
249
  }
250
- if (name.startsWith('__exportStar')) {
250
+ return rules.some(([n, ...args]) => {
251
+ if (name !== n) return false;
252
+ const fnArgs = p.get('arguments');
253
+ if (fnArgs.length !== args.length) return false;
254
+ return args.every((arg, i) => {
255
+ if (arg === '*') return true;
256
+ if (typeof arg === 'function') return arg(fnArgs[i]);
257
+ return arg === fnArgs[i];
258
+ });
259
+ });
260
+ };
261
+ function getImportExportTypeByInteropFunction(path, argPath) {
262
+ if (matchCall(path, [['__exportStar', argPath, p => isExports(p)]])) {
251
263
  return 're-export:*';
252
264
  }
253
- if (name.startsWith('_interopRequireDefault') || name.startsWith('__importDefault')) {
265
+ if (matchCall(path, [['_interopRequireDefault', argPath],
266
+ // babel and swc <1.3.50
267
+ ['_interop_require_default', argPath],
268
+ // swc >=1.3.50
269
+ ['__importDefault', argPath] // ?
270
+ ])) {
254
271
  return 'default';
255
272
  }
256
- if (name.startsWith('_interopRequireWildcard') || name.startsWith('__importStar') || name.startsWith('__toESM')) {
273
+ if (matchCall(path, [['_interopRequireWildcard', argPath],
274
+ // babel and swc <1.3.50
275
+ ['_interop_require_wildcard', argPath],
276
+ // swc >=1.3.50
277
+ ['__importStar', argPath],
278
+ // ?
279
+ ['__toESM', argPath],
280
+ // esbuild >=0.14.7
281
+ ['__toModule', argPath] // esbuild <0.14.7
282
+ ])) {
257
283
  return 'import:*';
258
284
  }
259
- if (name.startsWith('__rest') || name.startsWith('__objRest') || name.startsWith('_objectDestructuringEmpty')) {
285
+ if (matchCall(path, [['_extends', isEmptyObject, argPath],
286
+ // babel and swc
287
+ ['__rest', argPath, isArrayExpression],
288
+ // tsc and esbuild <=0.11.3
289
+ ['__objRest', argPath, isArrayExpression],
290
+ // esbuild >0.11.3
291
+ ['_objectWithoutProperties', argPath, isArrayExpression],
292
+ // babel and swc <1.3.50
293
+ ['_object_without_properties', argPath, isArrayExpression],
294
+ // swc >=1.3.50
295
+ ['_objectDestructuringEmpty', argPath],
296
+ // swc <1.3.50
297
+ ['_object_destructuring_empty', argPath] // swc >=1.3.50
298
+ ])) {
260
299
  return 'import:*';
261
300
  }
262
301
  return undefined;
@@ -270,6 +309,31 @@ function isAlreadyProcessed(path) {
270
309
  }
271
310
  return false;
272
311
  }
312
+ function isRequireCall(path) {
313
+ return path.isCallExpression() && isRequire(path.get('callee'));
314
+ }
315
+ function isEmptyObject(path) {
316
+ return path.isObjectExpression() && path.node.properties.length === 0;
317
+ }
318
+ function isArrayExpression(path) {
319
+ return path.isArrayExpression();
320
+ }
321
+ function isCallExpression(pathOrName) {
322
+ if (typeof pathOrName === 'string') {
323
+ return p => p.isCallExpression() && p.get('callee').isIdentifier({
324
+ name: pathOrName
325
+ });
326
+ }
327
+ return pathOrName.isCallExpression();
328
+ }
329
+ function isObjectExpression(path) {
330
+ return path.isObjectExpression();
331
+ }
332
+ function isIdentifier(name) {
333
+ return path => path.isIdentifier({
334
+ name
335
+ });
336
+ }
273
337
  function collectFromRequire(path, state) {
274
338
  if (!isRequire(path)) return;
275
339
 
@@ -294,10 +358,10 @@ function collectFromRequire(path, state) {
294
358
  parentPath: container,
295
359
  key
296
360
  } = callExpression;
297
- if (container.isCallExpression() && key === 0) {
361
+ if (container.isCallExpression()) {
298
362
  // It may be transpiled import such as
299
363
  // `var _atomic = _interopRequireDefault(require("@linaria/atomic"));`
300
- const imported = getImportExportTypeByInteropFunction(container);
364
+ const imported = getImportExportTypeByInteropFunction(container, callExpression);
301
365
  if (!imported) {
302
366
  // It's not a transpiled import.
303
367
  // TODO: Can we guess that it's a namespace import?
@@ -553,10 +617,7 @@ function collectFromExports(path, state) {
553
617
  // It is `exports.foo = void 0`
554
618
  return;
555
619
  }
556
- const {
557
- name
558
- } = property.node;
559
- if (name === '__esModule') {
620
+ if (exportName === '__esModule') {
560
621
  // eslint-disable-next-line no-param-reassign
561
622
  state.isEsModule = true;
562
623
  return;
@@ -623,7 +684,10 @@ function unfoldNamespaceImport(importItem) {
623
684
  if (!binding?.referenced) {
624
685
  // Imported namespace is not referenced and probably not used,
625
686
  // but it can have side effects, so we should keep it as is
626
- return [importItem];
687
+ return [{
688
+ ...importItem,
689
+ imported: 'side-effect'
690
+ }];
627
691
  }
628
692
  for (const referencePath of binding?.referencePaths ?? []) {
629
693
  if (referencePath.find(ancestor => ancestor.isTSType() || ancestor.isFlowType())) {
@@ -665,7 +729,7 @@ function unfoldNamespaceImport(importItem) {
665
729
  }
666
730
  if (parentPath?.isCallExpression() && referencePath.listKey === 'arguments') {
667
731
  // The defined variable is used as a function argument. Let's try to figure out what is imported.
668
- const importType = getImportExportTypeByInteropFunction(parentPath);
732
+ const importType = getImportExportTypeByInteropFunction(parentPath, referencePath);
669
733
  if (!importType) {
670
734
  // Imported value is used as an unknown function argument,
671
735
  // so we can't predict usage and import it as is.
@@ -808,8 +872,10 @@ function collectFromAssignmentExpression(path, state) {
808
872
  let exported;
809
873
  if (left.isMemberExpression() && isExports(left.get('object'))) {
810
874
  const property = left.get('property');
811
- if (property.isIdentifier()) {
875
+ if (!left.node.computed && property.isIdentifier()) {
812
876
  exported = property.node.name;
877
+ } else if (left.node.computed && property.isStringLiteral()) {
878
+ exported = property.node.value;
813
879
  }
814
880
  } else if (isExports(left)) {
815
881
  // module.exports = ...
@@ -818,9 +884,23 @@ function collectFromAssignmentExpression(path, state) {
818
884
  }
819
885
  }
820
886
  if (!exported) return;
821
- if (!right.isCallExpression() || !isRequire(right.get('callee'))) {
887
+ if (exported === '__esModule') {
822
888
  // eslint-disable-next-line no-param-reassign
823
- state.exports[exported] = right;
889
+ state.isEsModule = true;
890
+ return;
891
+ }
892
+ if (!isRequireCall(right)) {
893
+ const relatedImport = state.imports.find(imp => imp.local === right);
894
+ if (relatedImport) {
895
+ state.reexports.push({
896
+ exported,
897
+ ...relatedImport
898
+ });
899
+ } else {
900
+ // eslint-disable-next-line no-param-reassign
901
+ state.exports[exported] = right;
902
+ }
903
+ path.skip();
824
904
  return;
825
905
  }
826
906
  const sourcePath = right.get('arguments')?.[0];
@@ -841,9 +921,8 @@ function collectFromAssignmentExpression(path, state) {
841
921
  });
842
922
  path.skip();
843
923
  }
844
- function collectFromExportStarCall(path, state) {
845
- const [requireCall, exports] = path.get('arguments');
846
- if (!isExports(exports)) return;
924
+ function collectAllFromCall(path, require, state) {
925
+ const requireCall = typeof require === 'number' ? path.get('arguments')[require] : require;
847
926
  if (!requireCall.isCallExpression()) return;
848
927
  const callee = requireCall.get('callee');
849
928
  const sourcePath = requireCall.get('arguments')?.[0];
@@ -873,24 +952,31 @@ function collectFromMap(map, state) {
873
952
  addExport(returnValue, exported, state);
874
953
  });
875
954
  }
876
- function collectFromEsbuildExportCall(path, state) {
877
- const [sourceExports, map] = path.get('arguments');
878
- if (!sourceExports.isIdentifier({
879
- name: 'source_exports'
880
- })) return;
955
+ function collectMapFromCall(path, mapPosition, state) {
956
+ const map = path.get('arguments')[mapPosition];
881
957
  if (!map.isObjectExpression()) return;
882
958
  collectFromMap(map, state);
883
959
  path.skip();
884
960
  }
885
961
  function collectFromEsbuildReExportCall(path, state) {
886
- const [sourceExports, requireCall, exports] = path.get('arguments');
962
+ const [sourceExports, someCall, exports] = path.get('arguments');
887
963
  if (!sourceExports.isIdentifier({
888
964
  name: 'source_exports'
889
- })) return;
890
- if (!requireCall.isCallExpression()) return;
891
- if (!isExports(exports)) return;
892
- const callee = requireCall.get('callee');
893
- if (!isRequire(callee)) return;
965
+ }) && !isExports(sourceExports)) return;
966
+ if (!someCall.isCallExpression()) return;
967
+ let requireCall = someCall;
968
+ while (!isRequire(requireCall.get('callee'))) {
969
+ const args = requireCall.get('arguments');
970
+ if (args.length !== 1) {
971
+ return;
972
+ }
973
+ const firstArg = args[0];
974
+ if (!firstArg.isCallExpression()) {
975
+ return;
976
+ }
977
+ requireCall = firstArg;
978
+ }
979
+ if (exports !== undefined && !isExports(exports)) return;
894
980
  const sourcePath = requireCall.get('arguments')?.[0];
895
981
  if (!sourcePath.isStringLiteral()) return;
896
982
  state.reexports.push({
@@ -901,41 +987,43 @@ function collectFromEsbuildReExportCall(path, state) {
901
987
  });
902
988
  path.skip();
903
989
  }
904
- function collectFromSwcExportCall(path, state) {
905
- const [exports, map] = path.get('arguments');
906
- if (!isExports(exports)) return;
907
- if (!map.isObjectExpression()) return;
908
- collectFromMap(map, state);
909
- path.skip();
910
- }
911
990
  function collectFromCallExpression(path, state) {
912
991
  const maybeExportStart = path.get('callee');
913
992
  if (!maybeExportStart.isIdentifier()) {
914
993
  return;
915
994
  }
916
- const {
917
- name
918
- } = maybeExportStart.node;
995
+ if (matchCall(path, [['__exportStar', isExports, isCallExpression('__toModule')]])) {
996
+ // __exportStar(exports, __toModule(require('…')));
919
997
 
920
- // TypeScript
921
- if (name.startsWith('__exportStar')) {
922
- collectFromExportStarCall(path, state);
998
+ const secondArg = path.get('arguments')[1];
999
+ collectAllFromCall(path, secondArg.get('arguments')[0], state);
923
1000
  return;
924
1001
  }
925
1002
 
926
- // swc
927
- if (name === '_exportStar') {
928
- collectFromExportStarCall(path, state);
1003
+ // TypeScript & swc
1004
+ if (matchCall(path, [['__exportStart', isRequireCall, isExports], ['_exportStar', isRequireCall, isExports], ['_export_star', isRequireCall, isExports], ['__export', isRequireCall] // TypeScript <=3.8.3
1005
+ ])) {
1006
+ collectAllFromCall(path, 0, state);
1007
+ return;
1008
+ }
1009
+ if (matchCall(path, [['_export', isExports, isObjectExpression]])) {
1010
+ collectMapFromCall(path, 1, state);
1011
+ return;
929
1012
  }
930
- if (name === '_export') {
931
- collectFromSwcExportCall(path, state);
1013
+ if (matchCall(path, [['_extends', isEmptyObject, isRequireCall] // swc <=1.3.16
1014
+ ])) {
1015
+ collectAllFromCall(path, 1, state);
1016
+ return;
932
1017
  }
933
1018
 
934
1019
  // esbuild
935
- if (name === '__export') {
936
- collectFromEsbuildExportCall(path, state);
1020
+ if (matchCall(path, [['__export', isExports, isObjectExpression], ['__export', isIdentifier('source_exports'), isObjectExpression]])) {
1021
+ collectMapFromCall(path, 1, state);
1022
+ return;
937
1023
  }
938
- if (name === '__reExport') {
1024
+ if (matchCall(path, [
1025
+ // Different variants of re-exports in esbuild
1026
+ ['__reExport', isIdentifier('source_exports'), isCallExpression, isExports], ['__reExport', isIdentifier('source_exports'), isCallExpression], ['__reExport', isExports, isCallExpression]])) {
939
1027
  collectFromEsbuildReExportCall(path, state);
940
1028
  }
941
1029
  }