公共组件、公共样式集合
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.

1 line
68 KiB

2 weeks ago
{"version":3,"file":"index.browser.mjs","sources":["../src/utils.ts","../src/imports-injector.ts","../src/debug-utils.ts","../src/normalize-options.ts","../src/visitors/usage.ts","../src/visitors/entry.ts","../src/browser/dependencies.ts","../src/meta-resolver.ts","../src/index.ts"],"sourcesContent":["import { types as t, template } from \"@babel/core\";\nimport type { NodePath } from \"@babel/traverse\";\nimport type { Utils } from \"./types\";\nimport type ImportsCachedInjector from \"./imports-injector\";\n\nexport function intersection<T>(a: Set<T>, b: Set<T>): Set<T> {\n const result = new Set<T>();\n a.forEach(v => b.has(v) && result.add(v));\n return result;\n}\n\nexport function has(object: any, key: string) {\n return Object.prototype.hasOwnProperty.call(object, key);\n}\n\nfunction resolve(\n path: NodePath,\n resolved: Set<NodePath> = new Set(),\n): NodePath | undefined {\n if (resolved.has(path)) return;\n resolved.add(path);\n\n if (path.isVariableDeclarator()) {\n if (path.get(\"id\").isIdentifier()) {\n return resolve(path.get(\"init\"), resolved);\n }\n } else if (path.isReferencedIdentifier()) {\n const binding = path.scope.getBinding(path.node.name);\n if (!binding) return path;\n if (!binding.constant) return;\n return resolve(binding.path, resolved);\n }\n return path;\n}\n\nfunction resolveId(path: NodePath): string {\n if (\n path.isIdentifier() &&\n !path.scope.hasBinding(path.node.name, /* noGlobals */ true)\n ) {\n return path.node.name;\n }\n\n const resolved = resolve(path);\n if (resolved?.isIdentifier()) {\n return resolved.node.name;\n }\n}\n\nexport function resolveKey(\n path: NodePath<t.Expression | t.PrivateName>,\n computed: boolean = false,\n) {\n const { scope } = path;\n if (path.isStringLiteral()) return path.node.value;\n const isIdentifier = path.isIdentifier();\n if (\n isIdentifier &&\n !(computed || (path.parent as t.MemberExpression).computed)\n ) {\n return path.node.name;\n }\n\n if (\n computed &&\n path.isMemberExpression() &&\n path.get(\"object\").isIdentifier({ name: \"Symbol\" }) &&\n !scope.hasBinding(\"Symbol\", /* noGlobals */ true)\n ) {\n const sym = resolveKey(path.get(\"property\"), path.node.computed);\n if (sym) return \"Symbol.\" + sym;\n }\n\n if (\n isIdentifier\n ? scope.hasBinding(path.node.name, /* noGlobals */ true)\n : path.isPure()\n ) {\n const { value } = path.evaluate();\n if (typeof value === \"string\") return value;\n }\n}\n\nexport function resolveSource(obj: NodePath): {\n id: string | null;\n placement: \"prototype\" | \"static\" | null;\n} {\n if (\n obj.isMemberExpression() &&\n obj.get(\"property\").isIdentifier({ name: \"prototype\" })\n ) {\n const id = resolveId(obj.get(\"object\"));\n\n if (id) {\n return { id, placement: \"prototype\" };\n }\n return { id: null, placement: null };\n }\n\n const id = resolveId(obj);\n if (id) {\n return { id, placement: \"static\" };\n }\n\n const path = resolve(obj);\n switch (path?.type) {\n case \"RegExpLiteral\":\n return { id: \"RegExp\", placement: \"prototype\" };\n case \"FunctionExpression\":\n return { id: \"Function\", placement: \"prototype\" };\n case \"StringLiteral\":\n return { id: \"String\", placement: \"prototype\" };\n case \"NumberLiteral\":\n return { id: \"Number\", placement: \"prototype\" };\n case \"BooleanLiteral\":\n return { id: \"Boolean\", placement: \"prototype\" };\n case \"ObjectExpression\":\n return { id: \"Object\", placement: \"prototype\" };\n case \"ArrayExpression\":\n return { id: \"Array\", placement: \"prototype\" };\n }\n\n return { id: null, placement: null };\n}\n\nexport function getImportSource({ node }: NodePath<t.ImportDeclaration>) {\n if (node.specifiers.length === 0) return node.source.value;\n}\n\nexport function getRequireSource({ node }: NodePath<t.Statement>) {\n if (!t.isExpressionStatement(node)) return;\n const { expression } = node;\n