web 3d图形渲染器
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.

465 lines
18 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getExportSpecifierName = getExportSpecifierName;
  6. exports.default = void 0;
  7. var _helperPluginUtils = require("@babel/helper-plugin-utils");
  8. var _helperHoistVariables = _interopRequireDefault(require("@babel/helper-hoist-variables"));
  9. var _core = require("@babel/core");
  10. var _utils = require("babel-plugin-dynamic-import-node/utils");
  11. var _helperModuleTransforms = require("@babel/helper-module-transforms");
  12. var _helperValidatorIdentifier = require("@babel/helper-validator-identifier");
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. const buildTemplate = (0, _core.template)(`
  15. SYSTEM_REGISTER(MODULE_NAME, SOURCES, function (EXPORT_IDENTIFIER, CONTEXT_IDENTIFIER) {
  16. "use strict";
  17. BEFORE_BODY;
  18. return {
  19. setters: SETTERS,
  20. execute: EXECUTE,
  21. };
  22. });
  23. `);
  24. const buildExportAll = (0, _core.template)(`
  25. for (var KEY in TARGET) {
  26. if (KEY !== "default" && KEY !== "__esModule") EXPORT_OBJ[KEY] = TARGET[KEY];
  27. }
  28. `);
  29. const MISSING_PLUGIN_WARNING = `\
  30. WARNING: Dynamic import() transformation must be enabled using the
  31. @babel/plugin-proposal-dynamic-import plugin. Babel 8 will
  32. no longer transform import() without using that plugin.
  33. `;
  34. const MISSING_PLUGIN_ERROR = `\
  35. ERROR: Dynamic import() transformation must be enabled using the
  36. @babel/plugin-proposal-dynamic-import plugin. Babel 8
  37. no longer transforms import() without using that plugin.
  38. `;
  39. function getExportSpecifierName(node, stringSpecifiers) {
  40. if (node.type === "Identifier") {
  41. return node.name;
  42. } else if (node.type === "StringLiteral") {
  43. const stringValue = node.value;
  44. if (!(0, _helperValidatorIdentifier.isIdentifierName)(stringValue)) {
  45. stringSpecifiers.add(stringValue);
  46. }
  47. return stringValue;
  48. } else {
  49. throw new Error(`Expected export specifier to be either Identifier or StringLiteral, got ${node.type}`);
  50. }
  51. }
  52. function constructExportCall(path, exportIdent, exportNames, exportValues, exportStarTarget, stringSpecifiers) {
  53. const statements = [];
  54. if (!exportStarTarget) {
  55. if (exportNames.length === 1) {
  56. statements.push(_core.types.expressionStatement(_core.types.callExpression(exportIdent, [_core.types.stringLiteral(exportNames[0]), exportValues[0]])));
  57. } else {
  58. const objectProperties = [];
  59. for (let i = 0; i < exportNames.length; i++) {
  60. const exportName = exportNames[i];
  61. const exportValue = exportValues[i];
  62. objectProperties.push(_core.types.objectProperty(stringSpecifiers.has(exportName) ? _core.types.stringLiteral(exportName) : _core.types.identifier(exportName), exportValue));
  63. }
  64. statements.push(_core.types.expressionStatement(_core.types.callExpression(exportIdent, [_core.types.objectExpression(objectProperties)])));
  65. }
  66. } else {
  67. const exportObj = path.scope.generateUid("exportObj");
  68. statements.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(_core.types.identifier(exportObj), _core.types.objectExpression([]))]));
  69. statements.push(buildExportAll({
  70. KEY: path.scope.generateUidIdentifier("key"),
  71. EXPORT_OBJ: _core.types.identifier(exportObj),
  72. TARGET: exportStarTarget
  73. }));
  74. for (let i = 0; i < exportNames.length; i++) {
  75. const exportName = exportNames[i];
  76. const exportValue = exportValues[i];
  77. statements.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.memberExpression(_core.types.identifier(exportObj), _core.types.identifier(exportName)), exportValue)));
  78. }
  79. statements.push(_core.types.expressionStatement(_core.types.callExpression(exportIdent, [_core.types.identifier(exportObj)])));
  80. }
  81. return statements;
  82. }
  83. var _default = (0, _helperPluginUtils.declare)((api, options) => {
  84. api.assertVersion(7);
  85. const {
  86. systemGlobal = "System",
  87. allowTopLevelThis = false
  88. } = options;
  89. const IGNORE_REASSIGNMENT_SYMBOL = Symbol();
  90. const reassignmentVisitor = {
  91. "AssignmentExpression|UpdateExpression"(path) {
  92. if (path.node[IGNORE_REASSIGNMENT_SYMBOL]) return;
  93. path.node[IGNORE_REASSIGNMENT_SYMBOL] = true;
  94. const arg = path.get(path.isAssignmentExpression() ? "left" : "argument");
  95. if (arg.isObjectPattern() || arg.isArrayPattern()) {
  96. const exprs = [path.node];
  97. for (const name of Object.keys(arg.getBindingIdentifiers())) {
  98. if (this.scope.getBinding(name) !== path.scope.getBinding(name)) {
  99. return;
  100. }
  101. const exportedNames = this.exports[name];
  102. if (!exportedNames) return;
  103. for (const exportedName of exportedNames) {
  104. exprs.push(this.buildCall(exportedName, _core.types.identifier(name)).expression);
  105. }
  106. }
  107. path.replaceWith(_core.types.sequenceExpression(exprs));
  108. return;
  109. }
  110. if (!arg.isIdentifier()) return;
  111. const name = arg.node.name;
  112. if (this.scope.getBinding(name) !== path.scope.getBinding(name)) return;
  113. const exportedNames = this.exports[name];
  114. if (!exportedNames) return;
  115. let node = path.node;
  116. const isPostUpdateExpression = path.isUpdateExpression({
  117. prefix: false
  118. });
  119. if (isPostUpdateExpression) {
  120. node = _core.types.binaryExpression(node.operator[0], _core.types.unaryExpression("+", _core.types.cloneNode(node.argument)), _core.types.numericLiteral(1));
  121. }
  122. for (const exportedName of exportedNames) {
  123. node = this.buildCall(exportedName, node).expression;
  124. }
  125. if (isPostUpdateExpression) {
  126. node = _core.types.sequenceExpression([node, path.node]);
  127. }
  128. path.replaceWith(node);
  129. }
  130. };
  131. return {
  132. name: "transform-modules-systemjs",
  133. pre() {
  134. this.file.set("@babel/plugin-transform-modules-*", "systemjs");
  135. },
  136. visitor: {
  137. CallExpression(path, state) {
  138. if (_core.types.isImport(path.node.callee)) {
  139. if (!this.file.has("@babel/plugin-proposal-dynamic-import")) {
  140. {
  141. console.warn(MISSING_PLUGIN_WARNING);
  142. }
  143. }
  144. path.replaceWith(_core.types.callExpression(_core.types.memberExpression(_core.types.identifier(state.contextIdent), _core.types.identifier("import")), [(0, _utils.getImportSource)(_core.types, path.node)]));
  145. }
  146. },
  147. MetaProperty(path, state) {
  148. if (path.node.meta.name === "import" && path.node.property.name === "meta") {
  149. path.replaceWith(_core.types.memberExpression(_core.types.identifier(state.contextIdent), _core.types.identifier("meta")));
  150. }
  151. },
  152. ReferencedIdentifier(path, state) {
  153. if (path.node.name === "__moduleName" && !path.scope.hasBinding("__moduleName")) {
  154. path.replaceWith(_core.types.memberExpression(_core.types.identifier(state.contextIdent), _core.types.identifier("id")));
  155. }
  156. },
  157. Program: {
  158. enter(path, state) {
  159. state.contextIdent = path.scope.generateUid("context");
  160. state.stringSpecifiers = new Set();
  161. if (!allowTopLevelThis) {
  162. (0, _helperModuleTransforms.rewriteThis)(path);
  163. }
  164. },
  165. exit(path, state) {
  166. const scope = path.scope;
  167. const exportIdent = scope.generateUid("export");
  168. const {
  169. contextIdent,
  170. stringSpecifiers
  171. } = state;
  172. const exportMap = Object.create(null);
  173. const modules = [];
  174. let beforeBody = [];
  175. const setters = [];
  176. const sources = [];
  177. const variableIds = [];
  178. const removedPaths = [];
  179. function addExportName(key, val) {
  180. exportMap[key] = exportMap[key] || [];
  181. exportMap[key].push(val);
  182. }
  183. function pushModule(source, key, specifiers) {
  184. let module;
  185. modules.forEach(function (m) {
  186. if (m.key === source) {
  187. module = m;
  188. }
  189. });
  190. if (!module) {
  191. modules.push(module = {
  192. key: source,
  193. imports: [],
  194. exports: []
  195. });
  196. }
  197. module[key] = module[key].concat(specifiers);
  198. }
  199. function buildExportCall(name, val) {
  200. return _core.types.expressionStatement(_core.types.callExpression(_core.types.identifier(exportIdent), [_core.types.stringLiteral(name), val]));
  201. }
  202. const exportNames = [];
  203. const exportValues = [];
  204. const body = path.get("body");
  205. for (const path of body) {
  206. if (path.isFunctionDeclaration()) {
  207. beforeBody.push(path.node);
  208. removedPaths.push(path);
  209. } else if (path.isClassDeclaration()) {
  210. variableIds.push(_core.types.cloneNode(path.node.id));
  211. path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(path.node.id), _core.types.toExpression(path.node))));
  212. } else if (path.isImportDeclaration()) {
  213. const source = path.node.source.value;
  214. pushModule(source, "imports", path.node.specifiers);
  215. for (const name of Object.keys(path.getBindingIdentifiers())) {
  216. scope.removeBinding(name);
  217. variableIds.push(_core.types.identifier(name));
  218. }
  219. path.remove();
  220. } else if (path.isExportAllDeclaration()) {
  221. pushModule(path.node.source.value, "exports", path.node);
  222. path.remove();
  223. } else if (path.isExportDefaultDeclaration()) {
  224. const declar = path.get("declaration");
  225. const id = declar.node.id;
  226. if (declar.isClassDeclaration()) {
  227. if (id) {
  228. exportNames.push("default");
  229. exportValues.push(scope.buildUndefinedNode());
  230. variableIds.push(_core.types.cloneNode(id));
  231. addExportName(id.name, "default");
  232. path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(id), _core.types.toExpression(declar.node))));
  233. } else {
  234. exportNames.push("default");
  235. exportValues.push(_core.types.toExpression(declar.node));
  236. removedPaths.push(path);
  237. }
  238. } else if (declar.isFunctionDeclaration()) {
  239. if (id) {
  240. beforeBody.push(declar.node);
  241. exportNames.push("default");
  242. exportValues.push(_core.types.cloneNode(id));
  243. addExportName(id.name, "default");
  244. } else {
  245. exportNames.push("default");
  246. exportValues.push(_core.types.toExpression(declar.node));
  247. }
  248. removedPaths.push(path);
  249. } else {
  250. path.replaceWith(buildExportCall("default", declar.node));
  251. }
  252. } else if (path.isExportNamedDeclaration()) {
  253. const declar = path.get("declaration");
  254. if (declar.node) {
  255. path.replaceWith(declar);
  256. if (path.isFunction()) {
  257. const node = declar.node;
  258. const name = node.id.name;
  259. addExportName(name, name);
  260. beforeBody.push(node);
  261. exportNames.push(name);
  262. exportValues.push(_core.types.cloneNode(node.id));
  263. removedPaths.push(path);
  264. } else if (path.isClass()) {
  265. const name = declar.node.id.name;
  266. exportNames.push(name);
  267. exportValues.push(scope.buildUndefinedNode());
  268. variableIds.push(_core.types.cloneNode(declar.node.id));
  269. path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(declar.node.id), _core.types.toExpression(declar.node))));
  270. addExportName(name, name);
  271. } else {
  272. for (const name of Object.keys(declar.getBindingIdentifiers())) {
  273. addExportName(name, name);
  274. }
  275. }
  276. } else {
  277. const specifiers = path.node.specifiers;
  278. if (specifiers != null && specifiers.length) {
  279. if (path.node.source) {
  280. pushModule(path.node.source.value, "exports", specifiers);
  281. path.remove();
  282. } else {
  283. const nodes = [];
  284. for (const specifier of specifiers) {
  285. const {
  286. local,
  287. exported
  288. } = specifier;
  289. const binding = scope.getBinding(local.name);
  290. const exportedName = getExportSpecifierName(exported, stringSpecifiers);
  291. if (binding && _core.types.isFunctionDeclaration(binding.path.node)) {
  292. exportNames.push(exportedName);
  293. exportValues.push(_core.types.cloneNode(local));
  294. } else if (!binding) {
  295. nodes.push(buildExportCall(exportedName, local));
  296. }
  297. addExportName(local.name, exportedName);
  298. }
  299. path.replaceWithMultiple(nodes);
  300. }
  301. } else {
  302. path.remove();
  303. }
  304. }
  305. }
  306. }
  307. modules.forEach(function (specifiers) {
  308. let setterBody = [];
  309. const target = scope.generateUid(specifiers.key);
  310. for (let specifier of specifiers.imports) {
  311. if (_core.types.isImportNamespaceSpecifier(specifier)) {
  312. setterBody.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", specifier.local, _core.types.identifier(target))));
  313. } else if (_core.types.isImportDefaultSpecifier(specifier)) {
  314. specifier = _core.types.importSpecifier(specifier.local, _core.types.identifier("default"));
  315. }
  316. if (_core.types.isImportSpecifier(specifier)) {
  317. const {
  318. imported
  319. } = specifier;
  320. setterBody.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", specifier.local, _core.types.memberExpression(_core.types.identifier(target), specifier.imported, imported.type === "StringLiteral"))));
  321. }
  322. }
  323. if (specifiers.exports.length) {
  324. const exportNames = [];
  325. const exportValues = [];
  326. let hasExportStar = false;
  327. for (const node of specifiers.exports) {
  328. if (_core.types.isExportAllDeclaration(node)) {
  329. hasExportStar = true;
  330. } else if (_core.types.isExportSpecifier(node)) {
  331. const exportedName = getExportSpecifierName(node.exported, stringSpecifiers);
  332. exportNames.push(exportedName);
  333. exportValues.push(_core.types.memberExpression(_core.types.identifier(target), node.local, _core.types.isStringLiteral(node.local)));
  334. } else {}
  335. }
  336. setterBody = setterBody.concat(constructExportCall(path, _core.types.identifier(exportIdent), exportNames, exportValues, hasExportStar ? _core.types.identifier(target) : null, stringSpecifiers));
  337. }
  338. sources.push(_core.types.stringLiteral(specifiers.key));
  339. setters.push(_core.types.functionExpression(null, [_core.types.identifier(target)], _core.types.blockStatement(setterBody)));
  340. });
  341. let moduleName = (0, _helperModuleTransforms.getModuleName)(this.file.opts, options);
  342. if (moduleName) moduleName = _core.types.stringLiteral(moduleName);
  343. (0, _helperHoistVariables.default)(path, (id, name, hasInit) => {
  344. variableIds.push(id);
  345. if (!hasInit && name in exportMap) {
  346. for (const exported of exportMap[name]) {
  347. exportNames.push(exported);
  348. exportValues.push(scope.buildUndefinedNode());
  349. }
  350. }
  351. }, null);
  352. if (variableIds.length) {
  353. beforeBody.unshift(_core.types.variableDeclaration("var", variableIds.map(id => _core.types.variableDeclarator(id))));
  354. }
  355. if (exportNames.length) {
  356. beforeBody = beforeBody.concat(constructExportCall(path, _core.types.identifier(exportIdent), exportNames, exportValues, null, stringSpecifiers));
  357. }
  358. path.traverse(reassignmentVisitor, {
  359. exports: exportMap,
  360. buildCall: buildExportCall,
  361. scope
  362. });
  363. for (const path of removedPaths) {
  364. path.remove();
  365. }
  366. let hasTLA = false;
  367. path.traverse({
  368. AwaitExpression(path) {
  369. hasTLA = true;
  370. path.stop();
  371. },
  372. Function(path) {
  373. path.skip();
  374. },
  375. noScope: true
  376. });
  377. path.node.body = [buildTemplate({
  378. SYSTEM_REGISTER: _core.types.memberExpression(_core.types.identifier(systemGlobal), _core.types.identifier("register")),
  379. BEFORE_BODY: beforeBody,
  380. MODULE_NAME: moduleName,
  381. SETTERS: _core.types.arrayExpression(setters),
  382. EXECUTE: _core.types.functionExpression(null, [], _core.types.blockStatement(path.node.body), false, hasTLA),
  383. SOURCES: _core.types.arrayExpression(sources),
  384. EXPORT_IDENTIFIER: _core.types.identifier(exportIdent),
  385. CONTEXT_IDENTIFIER: _core.types.identifier(contextIdent)
  386. })];
  387. }
  388. }
  389. }
  390. };
  391. });
  392. exports.default = _default;