@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
@@ -250,21 +250,60 @@ function getCalleeName(path) {
250
250
  }
251
251
  return undefined;
252
252
  }
253
- function getImportExportTypeByInteropFunction(path) {
254
- const name = getCalleeName(path);
253
+ const matchCall = (p, rules) => {
254
+ const name = getCalleeName(p);
255
255
  if (name === undefined) {
256
- return undefined;
256
+ return false;
257
257
  }
258
- if (name.startsWith('__exportStar')) {
258
+ return rules.some(([n, ...args]) => {
259
+ if (name !== n) return false;
260
+ const fnArgs = p.get('arguments');
261
+ if (fnArgs.length !== args.length) return false;
262
+ return args.every((arg, i) => {
263
+ if (arg === '*') return true;
264
+ if (typeof arg === 'function') return arg(fnArgs[i]);
265
+ return arg === fnArgs[i];
266
+ });
267
+ });
268
+ };
269
+ function getImportExportTypeByInteropFunction(path, argPath) {
270
+ if (matchCall(path, [['__exportStar', argPath, p => (0, _isExports.isExports)(p)]])) {
259
271
  return 're-export:*';
260
272
  }
261
- if (name.startsWith('_interopRequireDefault') || name.startsWith('__importDefault')) {
273
+ if (matchCall(path, [['_interopRequireDefault', argPath],
274
+ // babel and swc <1.3.50
275
+ ['_interop_require_default', argPath],
276
+ // swc >=1.3.50
277
+ ['__importDefault', argPath] // ?
278
+ ])) {
262
279
  return 'default';
263
280
  }
264
- if (name.startsWith('_interopRequireWildcard') || name.startsWith('__importStar') || name.startsWith('__toESM')) {
281
+ if (matchCall(path, [['_interopRequireWildcard', argPath],
282
+ // babel and swc <1.3.50
283
+ ['_interop_require_wildcard', argPath],
284
+ // swc >=1.3.50
285
+ ['__importStar', argPath],
286
+ // ?
287
+ ['__toESM', argPath],
288
+ // esbuild >=0.14.7
289
+ ['__toModule', argPath] // esbuild <0.14.7
290
+ ])) {
265
291
  return 'import:*';
266
292
  }
267
- if (name.startsWith('__rest') || name.startsWith('__objRest') || name.startsWith('_objectDestructuringEmpty')) {
293
+ if (matchCall(path, [['_extends', isEmptyObject, argPath],
294
+ // babel and swc
295
+ ['__rest', argPath, isArrayExpression],
296
+ // tsc and esbuild <=0.11.3
297
+ ['__objRest', argPath, isArrayExpression],
298
+ // esbuild >0.11.3
299
+ ['_objectWithoutProperties', argPath, isArrayExpression],
300
+ // babel and swc <1.3.50
301
+ ['_object_without_properties', argPath, isArrayExpression],
302
+ // swc >=1.3.50
303
+ ['_objectDestructuringEmpty', argPath],
304
+ // swc <1.3.50
305
+ ['_object_destructuring_empty', argPath] // swc >=1.3.50
306
+ ])) {
268
307
  return 'import:*';
269
308
  }
270
309
  return undefined;
@@ -278,6 +317,31 @@ function isAlreadyProcessed(path) {
278
317
  }
279
318
  return false;
280
319
  }
320
+ function isRequireCall(path) {
321
+ return path.isCallExpression() && (0, _isRequire.isRequire)(path.get('callee'));
322
+ }
323
+ function isEmptyObject(path) {
324
+ return path.isObjectExpression() && path.node.properties.length === 0;
325
+ }
326
+ function isArrayExpression(path) {
327
+ return path.isArrayExpression();
328
+ }
329
+ function isCallExpression(pathOrName) {
330
+ if (typeof pathOrName === 'string') {
331
+ return p => p.isCallExpression() && p.get('callee').isIdentifier({
332
+ name: pathOrName
333
+ });
334
+ }
335
+ return pathOrName.isCallExpression();
336
+ }
337
+ function isObjectExpression(path) {
338
+ return path.isObjectExpression();
339
+ }
340
+ function isIdentifier(name) {
341
+ return path => path.isIdentifier({
342
+ name
343
+ });
344
+ }
281
345
  function collectFromRequire(path, state) {
282
346
  if (!(0, _isRequire.isRequire)(path)) return;
283
347
 
@@ -302,10 +366,10 @@ function collectFromRequire(path, state) {
302
366
  parentPath: container,
303
367
  key
304
368
  } = callExpression;
305
- if (container.isCallExpression() && key === 0) {
369
+ if (container.isCallExpression()) {
306
370
  // It may be transpiled import such as
307
371
  // `var _atomic = _interopRequireDefault(require("@linaria/atomic"));`
308
- const imported = getImportExportTypeByInteropFunction(container);
372
+ const imported = getImportExportTypeByInteropFunction(container, callExpression);
309
373
  if (!imported) {
310
374
  // It's not a transpiled import.
311
375
  // TODO: Can we guess that it's a namespace import?
@@ -562,10 +626,7 @@ function collectFromExports(path, state) {
562
626
  // It is `exports.foo = void 0`
563
627
  return;
564
628
  }
565
- const {
566
- name
567
- } = property.node;
568
- if (name === '__esModule') {
629
+ if (exportName === '__esModule') {
569
630
  // eslint-disable-next-line no-param-reassign
570
631
  state.isEsModule = true;
571
632
  return;
@@ -632,7 +693,10 @@ function unfoldNamespaceImport(importItem) {
632
693
  if (!(binding !== null && binding !== void 0 && binding.referenced)) {
633
694
  // Imported namespace is not referenced and probably not used,
634
695
  // but it can have side effects, so we should keep it as is
635
- return [importItem];
696
+ return [{
697
+ ...importItem,
698
+ imported: 'side-effect'
699
+ }];
636
700
  }
637
701
  for (const referencePath of (_binding$referencePat = binding === null || binding === void 0 ? void 0 : binding.referencePaths) !== null && _binding$referencePat !== void 0 ? _binding$referencePat : []) {
638
702
  var _binding$referencePat;
@@ -675,7 +739,7 @@ function unfoldNamespaceImport(importItem) {
675
739
  }
676
740
  if (parentPath !== null && parentPath !== void 0 && parentPath.isCallExpression() && referencePath.listKey === 'arguments') {
677
741
  // The defined variable is used as a function argument. Let's try to figure out what is imported.
678
- const importType = getImportExportTypeByInteropFunction(parentPath);
742
+ const importType = getImportExportTypeByInteropFunction(parentPath, referencePath);
679
743
  if (!importType) {
680
744
  // Imported value is used as an unknown function argument,
681
745
  // so we can't predict usage and import it as is.
@@ -821,8 +885,10 @@ function collectFromAssignmentExpression(path, state) {
821
885
  let exported;
822
886
  if (left.isMemberExpression() && (0, _isExports.isExports)(left.get('object'))) {
823
887
  const property = left.get('property');
824
- if (property.isIdentifier()) {
888
+ if (!left.node.computed && property.isIdentifier()) {
825
889
  exported = property.node.name;
890
+ } else if (left.node.computed && property.isStringLiteral()) {
891
+ exported = property.node.value;
826
892
  }
827
893
  } else if ((0, _isExports.isExports)(left)) {
828
894
  // module.exports = ...
@@ -831,9 +897,23 @@ function collectFromAssignmentExpression(path, state) {
831
897
  }
832
898
  }
833
899
  if (!exported) return;
834
- if (!right.isCallExpression() || !(0, _isRequire.isRequire)(right.get('callee'))) {
900
+ if (exported === '__esModule') {
835
901
  // eslint-disable-next-line no-param-reassign
836
- state.exports[exported] = right;
902
+ state.isEsModule = true;
903
+ return;
904
+ }
905
+ if (!isRequireCall(right)) {
906
+ const relatedImport = state.imports.find(imp => imp.local === right);
907
+ if (relatedImport) {
908
+ state.reexports.push({
909
+ exported,
910
+ ...relatedImport
911
+ });
912
+ } else {
913
+ // eslint-disable-next-line no-param-reassign
914
+ state.exports[exported] = right;
915
+ }
916
+ path.skip();
837
917
  return;
838
918
  }
839
919
  const sourcePath = (_right$get = right.get('arguments')) === null || _right$get === void 0 ? void 0 : _right$get[0];
@@ -854,10 +934,9 @@ function collectFromAssignmentExpression(path, state) {
854
934
  });
855
935
  path.skip();
856
936
  }
857
- function collectFromExportStarCall(path, state) {
937
+ function collectAllFromCall(path, require, state) {
858
938
  var _requireCall$get;
859
- const [requireCall, exports] = path.get('arguments');
860
- if (!(0, _isExports.isExports)(exports)) return;
939
+ const requireCall = typeof require === 'number' ? path.get('arguments')[require] : require;
861
940
  if (!requireCall.isCallExpression()) return;
862
941
  const callee = requireCall.get('callee');
863
942
  const sourcePath = (_requireCall$get = requireCall.get('arguments')) === null || _requireCall$get === void 0 ? void 0 : _requireCall$get[0];
@@ -887,25 +966,32 @@ function collectFromMap(map, state) {
887
966
  addExport(returnValue, exported, state);
888
967
  });
889
968
  }
890
- function collectFromEsbuildExportCall(path, state) {
891
- const [sourceExports, map] = path.get('arguments');
892
- if (!sourceExports.isIdentifier({
893
- name: 'source_exports'
894
- })) return;
969
+ function collectMapFromCall(path, mapPosition, state) {
970
+ const map = path.get('arguments')[mapPosition];
895
971
  if (!map.isObjectExpression()) return;
896
972
  collectFromMap(map, state);
897
973
  path.skip();
898
974
  }
899
975
  function collectFromEsbuildReExportCall(path, state) {
900
976
  var _requireCall$get2;
901
- const [sourceExports, requireCall, exports] = path.get('arguments');
977
+ const [sourceExports, someCall, exports] = path.get('arguments');
902
978
  if (!sourceExports.isIdentifier({
903
979
  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;
980
+ }) && !(0, _isExports.isExports)(sourceExports)) return;
981
+ if (!someCall.isCallExpression()) return;
982
+ let requireCall = someCall;
983
+ while (!(0, _isRequire.isRequire)(requireCall.get('callee'))) {
984
+ const args = requireCall.get('arguments');
985
+ if (args.length !== 1) {
986
+ return;
987
+ }
988
+ const firstArg = args[0];
989
+ if (!firstArg.isCallExpression()) {
990
+ return;
991
+ }
992
+ requireCall = firstArg;
993
+ }
994
+ if (exports !== undefined && !(0, _isExports.isExports)(exports)) return;
909
995
  const sourcePath = (_requireCall$get2 = requireCall.get('arguments')) === null || _requireCall$get2 === void 0 ? void 0 : _requireCall$get2[0];
910
996
  if (!sourcePath.isStringLiteral()) return;
911
997
  state.reexports.push({
@@ -916,41 +1002,43 @@ function collectFromEsbuildReExportCall(path, state) {
916
1002
  });
917
1003
  path.skip();
918
1004
  }
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
1005
  function collectFromCallExpression(path, state) {
927
1006
  const maybeExportStart = path.get('callee');
928
1007
  if (!maybeExportStart.isIdentifier()) {
929
1008
  return;
930
1009
  }
931
- const {
932
- name
933
- } = maybeExportStart.node;
1010
+ if (matchCall(path, [['__exportStar', _isExports.isExports, isCallExpression('__toModule')]])) {
1011
+ // __exportStar(exports, __toModule(require('…')));
934
1012
 
935
- // TypeScript
936
- if (name.startsWith('__exportStar')) {
937
- collectFromExportStarCall(path, state);
1013
+ const secondArg = path.get('arguments')[1];
1014
+ collectAllFromCall(path, secondArg.get('arguments')[0], state);
938
1015
  return;
939
1016
  }
940
1017
 
941
- // swc
942
- if (name === '_exportStar') {
943
- collectFromExportStarCall(path, state);
1018
+ // TypeScript & swc
1019
+ if (matchCall(path, [['__exportStart', isRequireCall, _isExports.isExports], ['_exportStar', isRequireCall, _isExports.isExports], ['_export_star', isRequireCall, _isExports.isExports], ['__export', isRequireCall] // TypeScript <=3.8.3
1020
+ ])) {
1021
+ collectAllFromCall(path, 0, state);
1022
+ return;
1023
+ }
1024
+ if (matchCall(path, [['_export', _isExports.isExports, isObjectExpression]])) {
1025
+ collectMapFromCall(path, 1, state);
1026
+ return;
944
1027
  }
945
- if (name === '_export') {
946
- collectFromSwcExportCall(path, state);
1028
+ if (matchCall(path, [['_extends', isEmptyObject, isRequireCall] // swc <=1.3.16
1029
+ ])) {
1030
+ collectAllFromCall(path, 1, state);
1031
+ return;
947
1032
  }
948
1033
 
949
1034
  // esbuild
950
- if (name === '__export') {
951
- collectFromEsbuildExportCall(path, state);
1035
+ if (matchCall(path, [['__export', _isExports.isExports, isObjectExpression], ['__export', isIdentifier('source_exports'), isObjectExpression]])) {
1036
+ collectMapFromCall(path, 1, state);
1037
+ return;
952
1038
  }
953
- if (name === '__reExport') {
1039
+ if (matchCall(path, [
1040
+ // Different variants of re-exports in esbuild
1041
+ ['__reExport', isIdentifier('source_exports'), isCallExpression, _isExports.isExports], ['__reExport', isIdentifier('source_exports'), isCallExpression], ['__reExport', _isExports.isExports, isCallExpression]])) {
954
1042
  collectFromEsbuildReExportCall(path, state);
955
1043
  }
956
1044
  }