import-injector.js 8.2 KB

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