import-injector.js 9.2 KB

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