import-injector.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _assert = require("assert");
  7. var _t = require("@babel/types");
  8. var _importBuilder = require("./import-builder");
  9. var _isModule = require("./is-module");
  10. const {
  11. numericLiteral,
  12. sequenceExpression
  13. } = _t;
  14. class ImportInjector {
  15. constructor(path, importedSource, opts) {
  16. this._defaultOpts = {
  17. importedSource: null,
  18. importedType: "commonjs",
  19. importedInterop: "babel",
  20. importingInterop: "babel",
  21. ensureLiveReference: false,
  22. ensureNoContext: false,
  23. importPosition: "before"
  24. };
  25. const programPath = path.find(p => p.isProgram());
  26. this._programPath = programPath;
  27. this._programScope = programPath.scope;
  28. this._hub = programPath.hub;
  29. this._defaultOpts = this._applyDefaults(importedSource, opts, true);
  30. }
  31. addDefault(importedSourceIn, opts) {
  32. return this.addNamed("default", importedSourceIn, opts);
  33. }
  34. addNamed(importName, importedSourceIn, opts) {
  35. _assert(typeof importName === "string");
  36. return this._generateImport(this._applyDefaults(importedSourceIn, opts), importName);
  37. }
  38. addNamespace(importedSourceIn, opts) {
  39. return this._generateImport(this._applyDefaults(importedSourceIn, opts), null);
  40. }
  41. addSideEffect(importedSourceIn, opts) {
  42. return this._generateImport(this._applyDefaults(importedSourceIn, opts), void 0);
  43. }
  44. _applyDefaults(importedSource, opts, isInit = false) {
  45. let newOpts;
  46. if (typeof importedSource === "string") {
  47. newOpts = Object.assign({}, this._defaultOpts, {
  48. importedSource
  49. }, opts);
  50. } else {
  51. _assert(!opts, "Unexpected secondary arguments.");
  52. newOpts = Object.assign({}, this._defaultOpts, importedSource);
  53. }
  54. if (!isInit && opts) {
  55. if (opts.nameHint !== undefined) newOpts.nameHint = opts.nameHint;
  56. if (opts.blockHoist !== undefined) newOpts.blockHoist = opts.blockHoist;
  57. }
  58. return newOpts;
  59. }
  60. _generateImport(opts, importName) {
  61. const isDefault = importName === "default";
  62. const isNamed = !!importName && !isDefault;
  63. const isNamespace = importName === null;
  64. const {
  65. importedSource,
  66. importedType,
  67. importedInterop,
  68. importingInterop,
  69. ensureLiveReference,
  70. ensureNoContext,
  71. nameHint,
  72. importPosition,
  73. blockHoist
  74. } = opts;
  75. let name = nameHint || importName;
  76. const isMod = (0, _isModule.default)(this._programPath);
  77. const isModuleForNode = isMod && importingInterop === "node";
  78. const isModuleForBabel = isMod && importingInterop === "babel";
  79. if (importPosition === "after" && !isMod) {
  80. throw new Error(`"importPosition": "after" is only supported in modules`);
  81. }
  82. const builder = new _importBuilder.default(importedSource, this._programScope, this._hub);
  83. if (importedType === "es6") {
  84. if (!isModuleForNode && !isModuleForBabel) {
  85. throw new Error("Cannot import an ES6 module from CommonJS");
  86. }
  87. builder.import();
  88. if (isNamespace) {
  89. builder.namespace(nameHint || importedSource);
  90. } else if (isDefault || isNamed) {
  91. builder.named(name, importName);
  92. }
  93. } else if (importedType !== "commonjs") {
  94. throw new Error(`Unexpected interopType "${importedType}"`);
  95. } else if (importedInterop === "babel") {
  96. if (isModuleForNode) {
  97. name = name !== "default" ? name : importedSource;
  98. const es6Default = `${importedSource}$es6Default`;
  99. builder.import();
  100. if (isNamespace) {
  101. builder.default(es6Default).var(name || importedSource).wildcardInterop();
  102. } else if (isDefault) {
  103. if (ensureLiveReference) {
  104. builder.default(es6Default).var(name || importedSource).defaultInterop().read("default");
  105. } else {
  106. builder.default(es6Default).var(name).defaultInterop().prop(importName);
  107. }
  108. } else if (isNamed) {
  109. builder.default(es6Default).read(importName);
  110. }
  111. } else if (isModuleForBabel) {
  112. builder.import();
  113. if (isNamespace) {
  114. builder.namespace(name || importedSource);
  115. } else if (isDefault || isNamed) {
  116. builder.named(name, importName);
  117. }
  118. } else {
  119. builder.require();
  120. if (isNamespace) {
  121. builder.var(name || importedSource).wildcardInterop();
  122. } else if ((isDefault || isNamed) && ensureLiveReference) {
  123. if (isDefault) {
  124. name = name !== "default" ? name : importedSource;
  125. builder.var(name).read(importName);
  126. builder.defaultInterop();
  127. } else {
  128. builder.var(importedSource).read(importName);
  129. }
  130. } else if (isDefault) {
  131. builder.var(name).defaultInterop().prop(importName);
  132. } else if (isNamed) {
  133. builder.var(name).prop(importName);
  134. }
  135. }
  136. } else if (importedInterop === "compiled") {
  137. if (isModuleForNode) {
  138. builder.import();
  139. if (isNamespace) {
  140. builder.default(name || importedSource);
  141. } else if (isDefault || isNamed) {
  142. builder.default(importedSource).read(name);
  143. }
  144. } else if (isModuleForBabel) {
  145. builder.import();
  146. if (isNamespace) {
  147. builder.namespace(name || importedSource);
  148. } else if (isDefault || isNamed) {
  149. builder.named(name, importName);
  150. }
  151. } else {
  152. builder.require();
  153. if (isNamespace) {
  154. builder.var(name || importedSource);
  155. } else if (isDefault || isNamed) {
  156. if (ensureLiveReference) {
  157. builder.var(importedSource).read(name);
  158. } else {
  159. builder.prop(importName).var(name);
  160. }
  161. }
  162. }
  163. } else if (importedInterop === "uncompiled") {
  164. if (isDefault && ensureLiveReference) {
  165. throw new Error("No live reference for commonjs default");
  166. }
  167. if (isModuleForNode) {
  168. builder.import();
  169. if (isNamespace) {
  170. builder.default(name || importedSource);
  171. } else if (isDefault) {
  172. builder.default(name);
  173. } else if (isNamed) {
  174. builder.default(importedSource).read(name);
  175. }
  176. } else if (isModuleForBabel) {
  177. builder.import();
  178. if (isNamespace) {
  179. builder.default(name || importedSource);
  180. } else if (isDefault) {
  181. builder.default(name);
  182. } else if (isNamed) {
  183. builder.named(name, importName);
  184. }
  185. } else {
  186. builder.require();
  187. if (isNamespace) {
  188. builder.var(name || importedSource);
  189. } else if (isDefault) {
  190. builder.var(name);
  191. } else if (isNamed) {
  192. if (ensureLiveReference) {
  193. builder.var(importedSource).read(name);
  194. } else {
  195. builder.var(name).prop(importName);
  196. }
  197. }
  198. }
  199. } else {
  200. throw new Error(`Unknown importedInterop "${importedInterop}".`);
  201. }
  202. const {
  203. statements,
  204. resultName
  205. } = builder.done();
  206. this._insertStatements(statements, importPosition, blockHoist);
  207. if ((isDefault || isNamed) && ensureNoContext && resultName.type !== "Identifier") {
  208. return sequenceExpression([numericLiteral(0), resultName]);
  209. }
  210. return resultName;
  211. }
  212. _insertStatements(statements, importPosition = "before", blockHoist = 3) {
  213. const body = this._programPath.get("body");
  214. if (importPosition === "after") {
  215. for (let i = body.length - 1; i >= 0; i--) {
  216. if (body[i].isImportDeclaration()) {
  217. body[i].insertAfter(statements);
  218. return;
  219. }
  220. }
  221. } else {
  222. statements.forEach(node => {
  223. node._blockHoist = blockHoist;
  224. });
  225. const targetPath = body.find(p => {
  226. const val = p.node._blockHoist;
  227. return Number.isFinite(val) && val < 4;
  228. });
  229. if (targetPath) {
  230. targetPath.insertBefore(statements);
  231. return;
  232. }
  233. }
  234. this._programPath.unshiftContainer("body", statements);
  235. }
  236. }
  237. exports.default = ImportInjector;