Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

import-injector.js 8.2KB

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