You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

270 lines
8.8 KiB

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