版博士V2.0程序
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

chunk-UYEZILGY.cjs 12 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  2. var _chunkGOGBRDVIcjs = require('./chunk-GOGBRDVI.cjs');
  3. // src/core/unplugin.ts
  4. var _minimatch = require('minimatch'); var _minimatch2 = _interopRequireDefault(_minimatch);
  5. var _utils = require('@antfu/utils');
  6. var _unplugin = require('unplugin');
  7. // src/core/ctx.ts
  8. var _path = require('path');
  9. var _fs = require('fs');
  10. var _pluginutils = require('@rollup/pluginutils');
  11. var _localpkg = require('local-pkg');
  12. var _unimport = require('unimport');
  13. var _addons = require('unimport/addons');
  14. var _magicstring = require('magic-string'); var _magicstring2 = _interopRequireDefault(_magicstring);
  15. // src/core/eslintrc.ts
  16. function generateESLintConfigs(imports, eslintrc) {
  17. const eslintConfigs = { globals: {} };
  18. imports.map((i) => {
  19. var _a;
  20. return (_a = i.as) != null ? _a : i.name;
  21. }).filter(Boolean).sort().forEach((name) => {
  22. eslintConfigs.globals[name] = eslintrc.globalsPropValue || true;
  23. });
  24. const jsonBody = JSON.stringify(eslintConfigs, null, 2);
  25. return jsonBody;
  26. }
  27. // src/core/resolvers.ts
  28. function normalizeImport(info, name) {
  29. if (typeof info === "string") {
  30. return {
  31. name: "default",
  32. as: name,
  33. from: info
  34. };
  35. }
  36. if ("path" in info) {
  37. return {
  38. from: info.path,
  39. as: info.name,
  40. name: info.importName,
  41. sideEffects: info.sideEffects
  42. };
  43. }
  44. return _chunkGOGBRDVIcjs.__spreadValues.call(void 0, {
  45. name,
  46. as: name
  47. }, info);
  48. }
  49. async function firstMatchedResolver(resolvers, fullname) {
  50. let name = fullname;
  51. for (const resolver of resolvers) {
  52. if (typeof resolver === "object" && resolver.type === "directive") {
  53. if (name.startsWith("v"))
  54. name = name.slice(1);
  55. else
  56. continue;
  57. }
  58. const resolved = await (typeof resolver === "function" ? resolver(name) : resolver.resolve(name));
  59. if (resolved)
  60. return normalizeImport(resolved, fullname);
  61. }
  62. }
  63. function resolversAddon(resolvers) {
  64. return {
  65. async matchImports(names, matched) {
  66. if (!resolvers.length)
  67. return;
  68. const dynamic = [];
  69. const sideEffects = [];
  70. await Promise.all([...names].map(async (name) => {
  71. const matchedImport = matched.find((i) => i.as === name);
  72. if (matchedImport) {
  73. if ("sideEffects" in matchedImport)
  74. sideEffects.push(..._utils.toArray.call(void 0, matchedImport.sideEffects).map((i) => normalizeImport(i, "")));
  75. return;
  76. }
  77. const resolved = await firstMatchedResolver(resolvers, name);
  78. if (resolved)
  79. dynamic.push(resolved);
  80. if (resolved == null ? void 0 : resolved.sideEffects)
  81. sideEffects.push(..._utils.toArray.call(void 0, resolved == null ? void 0 : resolved.sideEffects).map((i) => normalizeImport(i, "")));
  82. }));
  83. if (dynamic.length) {
  84. this.dynamicImports.push(...dynamic);
  85. this.invalidate();
  86. }
  87. if (dynamic.length || sideEffects.length)
  88. return [...matched, ...dynamic, ...sideEffects];
  89. }
  90. };
  91. }
  92. // src/core/ctx.ts
  93. function createContext(options = {}, root = process.cwd()) {
  94. var _a;
  95. const {
  96. dts: preferDTS = _localpkg.isPackageExists.call(void 0, "typescript"),
  97. cache: isCache = false
  98. } = options;
  99. const dirs = (_a = options.dirs) == null ? void 0 : _a.map((dir) => _path.resolve.call(void 0, root, dir));
  100. const eslintrc = options.eslintrc || {};
  101. eslintrc.enabled = eslintrc.enabled === void 0 ? false : eslintrc.enabled;
  102. eslintrc.filepath = eslintrc.filepath || "./.eslintrc-auto-import.json";
  103. eslintrc.globalsPropValue = eslintrc.globalsPropValue === void 0 ? true : eslintrc.globalsPropValue;
  104. const resolvers = options.resolvers ? [options.resolvers].flat(2) : [];
  105. const cachePath = isCache === false ? false : _path.resolve.call(void 0, root, typeof isCache === "string" ? isCache : "node_modules/.cache/unplugin-auto-import.json");
  106. const injectAtEnd = options.injectAtEnd !== false;
  107. const unimport = _unimport.createUnimport.call(void 0, {
  108. imports: [],
  109. presets: [],
  110. injectAtEnd,
  111. addons: [
  112. ...options.vueTemplate ? [_addons.vueTemplateAddon.call(void 0, )] : [],
  113. resolversAddon(resolvers),
  114. {
  115. declaration(dts2) {
  116. return `${`
  117. /* eslint-disable */
  118. /* prettier-ignore */
  119. // @ts-nocheck
  120. // Generated by unplugin-auto-import
  121. ${dts2}`.trim()}
  122. `;
  123. }
  124. }
  125. ]
  126. });
  127. const importsPromise = flattenImports(options.imports).then((imports) => {
  128. var _a2;
  129. if (!imports.length && !resolvers.length)
  130. console.warn("[auto-import] plugin installed but no imports has defined, see https://github.com/antfu/unplugin-auto-import#configurations for configurations");
  131. (_a2 = options.ignore) == null ? void 0 : _a2.forEach((name) => {
  132. const i = imports.find((i2) => i2.as === name);
  133. if (i)
  134. i.disabled = true;
  135. });
  136. return unimport.getInternalContext().replaceImports(imports);
  137. });
  138. const filter = _pluginutils.createFilter.call(void 0,
  139. options.include || [/\.[jt]sx?$/, /\.vue$/, /\.vue\?vue/, /\.svelte$/],
  140. options.exclude || [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/]
  141. );
  142. const dts = preferDTS === false ? false : preferDTS === true ? _path.resolve.call(void 0, root, "auto-imports.d.ts") : _path.resolve.call(void 0, root, preferDTS);
  143. async function generateDTS(file) {
  144. await importsPromise;
  145. const dir = _path.dirname.call(void 0, file);
  146. return unimport.generateTypeDeclarations({
  147. resolvePath: (i) => {
  148. if (i.from.startsWith(".") || _path.isAbsolute.call(void 0, i.from)) {
  149. const related = _utils.slash.call(void 0, _path.relative.call(void 0, dir, i.from).replace(/\.ts(x)?$/, ""));
  150. return !related.startsWith(".") ? `./${related}` : related;
  151. }
  152. return i.from;
  153. }
  154. });
  155. }
  156. async function generateESLint() {
  157. return generateESLintConfigs(await unimport.getImports(), eslintrc);
  158. }
  159. const writeConfigFilesThrottled = _utils.throttle.call(void 0, 500, writeConfigFiles, { noLeading: false });
  160. const writeFileThrottled = _utils.throttle.call(void 0, 500, writeFile, { noLeading: false });
  161. async function writeFile(filePath, content = "") {
  162. await _fs.promises.mkdir(_path.dirname.call(void 0, filePath), { recursive: true });
  163. return await _fs.promises.writeFile(filePath, content, "utf-8");
  164. }
  165. let lastDTS;
  166. let lastESLint;
  167. async function writeConfigFiles() {
  168. const promises = [];
  169. if (dts) {
  170. promises.push(
  171. generateDTS(dts).then((content) => {
  172. if (content !== lastDTS) {
  173. lastDTS = content;
  174. return writeFile(dts, content);
  175. }
  176. })
  177. );
  178. }
  179. if (eslintrc.enabled && eslintrc.filepath) {
  180. promises.push(
  181. generateESLint().then((content) => {
  182. if (content !== lastESLint) {
  183. lastESLint = `${content}
  184. `;
  185. return writeFile(eslintrc.filepath, content);
  186. }
  187. })
  188. );
  189. }
  190. return Promise.all(promises);
  191. }
  192. async function scanDirs() {
  193. if (dirs == null ? void 0 : dirs.length) {
  194. await unimport.modifyDynamicImports(async (imports) => {
  195. const exports = await _unimport.scanDirExports.call(void 0, dirs, {
  196. filePatterns: ["*.{tsx,jsx,ts,js,mjs,cjs,mts,cts}"]
  197. });
  198. exports.forEach((i) => i.__source = "dir");
  199. return modifyDefaultExportsAlias([
  200. ...imports.filter((i) => i.__source !== "dir"),
  201. ...exports
  202. ], options);
  203. });
  204. }
  205. writeConfigFilesThrottled();
  206. }
  207. async function getCacheData(cache) {
  208. const str = (await _fs.promises.readFile(cache, "utf-8")).trim();
  209. return JSON.parse(str || "{}");
  210. }
  211. async function generateCache() {
  212. if (!cachePath)
  213. return {};
  214. let cacheData = {};
  215. try {
  216. cacheData = await getCacheData(cachePath);
  217. await Promise.allSettled(Object.keys(cacheData).map(async (filePath) => {
  218. try {
  219. const normalizeRoot = root.replaceAll(_path.sep, _path.posix.sep);
  220. await _fs.promises.access(_path.posix.join(normalizeRoot, filePath));
  221. } catch (e) {
  222. Reflect.deleteProperty(cacheData, filePath);
  223. }
  224. }));
  225. await writeFile(cachePath, JSON.stringify(cacheData, null, 2));
  226. } catch (e) {
  227. await writeFile(cachePath, "{}");
  228. }
  229. return cacheData;
  230. }
  231. let isInitialCache = false;
  232. const resolveCachePromise = generateCache();
  233. async function updateCacheImports(id, importList) {
  234. if (!cachePath || isInitialCache && !id)
  235. return;
  236. isInitialCache = true;
  237. const cacheData = await resolveCachePromise;
  238. await unimport.modifyDynamicImports(async (imports) => {
  239. if (id && importList) {
  240. const filePath = _path.posix.normalize(_path.posix.relative(root, id));
  241. importList = importList.filter((i) => {
  242. var _a2;
  243. return ((_a2 = i.name) != null ? _a2 : i.as) && i.name !== "default";
  244. });
  245. if (importList.length)
  246. cacheData[filePath] = importList;
  247. else
  248. delete cacheData[filePath];
  249. writeFileThrottled(cachePath, JSON.stringify(cacheData, null, 2));
  250. return imports.concat(importList);
  251. }
  252. return imports.concat(Object.values(cacheData).reduce((p, n) => p.concat(n), []));
  253. });
  254. }
  255. async function transform(code, id) {
  256. await importsPromise;
  257. const s = new (0, _magicstring2.default)(code);
  258. const res = await unimport.injectImports(s, id);
  259. await updateCacheImports(id, res.imports);
  260. if (!s.hasChanged())
  261. return;
  262. writeConfigFilesThrottled();
  263. return {
  264. code: s.toString(),
  265. map: s.generateMap({ source: id, includeContent: true })
  266. };
  267. }
  268. return {
  269. root,
  270. dirs,
  271. filter,
  272. scanDirs,
  273. updateCacheImports,
  274. writeConfigFiles,
  275. writeConfigFilesThrottled,
  276. transform,
  277. generateDTS,
  278. generateESLint
  279. };
  280. }
  281. async function flattenImports(map) {
  282. const promises = await Promise.all(_utils.toArray.call(void 0, map).map(async (definition) => {
  283. if (typeof definition === "string") {
  284. if (!_chunkGOGBRDVIcjs.presets[definition])
  285. throw new Error(`[auto-import] preset ${definition} not found`);
  286. const preset = _chunkGOGBRDVIcjs.presets[definition];
  287. definition = typeof preset === "function" ? preset() : preset;
  288. }
  289. if ("from" in definition && "imports" in definition) {
  290. return await _unimport.resolvePreset.call(void 0, definition);
  291. } else {
  292. const resolved = [];
  293. for (const mod of Object.keys(definition)) {
  294. for (const id of definition[mod]) {
  295. const meta = {
  296. from: mod
  297. };
  298. if (Array.isArray(id)) {
  299. meta.name = id[0];
  300. meta.as = id[1];
  301. } else {
  302. meta.name = id;
  303. meta.as = id;
  304. }
  305. resolved.push(meta);
  306. }
  307. }
  308. return resolved;
  309. }
  310. }));
  311. return promises.flat();
  312. }
  313. function modifyDefaultExportsAlias(imports, options) {
  314. if (options.defaultExportByFilename) {
  315. imports.forEach((i) => {
  316. var _a, _b, _c;
  317. if (i.name === "default")
  318. i.as = (_c = (_b = (_a = i.from.split("/").pop()) == null ? void 0 : _a.split(".")) == null ? void 0 : _b.shift()) != null ? _c : i.as;
  319. });
  320. }
  321. return imports;
  322. }
  323. // src/core/unplugin.ts
  324. var unplugin_default = _unplugin.createUnplugin.call(void 0, (options) => {
  325. let ctx = createContext(options);
  326. return {
  327. name: "unplugin-auto-import",
  328. enforce: "post",
  329. transformInclude(id) {
  330. return ctx.filter(id);
  331. },
  332. async transform(code, id) {
  333. return ctx.transform(code, id);
  334. },
  335. async buildStart() {
  336. await Promise.all([
  337. ctx.scanDirs(),
  338. ctx.updateCacheImports()
  339. ]);
  340. await ctx.writeConfigFiles();
  341. },
  342. async buildEnd() {
  343. await ctx.writeConfigFiles();
  344. },
  345. vite: {
  346. async handleHotUpdate({ file }) {
  347. var _a;
  348. if ((_a = ctx.dirs) == null ? void 0 : _a.some((glob) => _minimatch2.default.call(void 0, _utils.slash.call(void 0, file), glob)))
  349. await ctx.scanDirs();
  350. },
  351. async configResolved(config) {
  352. if (ctx.root !== config.root) {
  353. ctx = createContext(options, config.root);
  354. await ctx.scanDirs();
  355. }
  356. }
  357. }
  358. };
  359. });
  360. exports.unplugin_default = unplugin_default;