e540718949e8b26a4cca69520f67d2f8aa1a030ceb7ab2b10ca38a11bf5aa26416d2e3ddf9f716f75e9040732eac9e18f2a8064540a1d7de13c8ea24768c7b 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { promises } from 'fs';
  2. import { createUnplugin } from 'unplugin';
  3. import { createFilter } from '@rollup/pluginutils';
  4. import MagicString from 'magic-string';
  5. import { d as createUnimport, s as scanDirExports } from './shared/unimport.3e17b72a.mjs';
  6. import 'fast-glob';
  7. import 'pathe';
  8. import 'mlly';
  9. import 'scule';
  10. import './shared/unimport.db0843be.mjs';
  11. import 'strip-literal';
  12. import 'local-pkg';
  13. import 'os';
  14. import 'pkg-types';
  15. const defaultIncludes = [/\.[jt]sx?$/, /\.vue$/, /\.vue\?vue/, /\.svelte$/];
  16. const defaultExcludes = [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/];
  17. function toArray(x) {
  18. return x == null ? [] : Array.isArray(x) ? x : [x];
  19. }
  20. const unplugin = createUnplugin((options = {}) => {
  21. const ctx = createUnimport(options);
  22. const filter = createFilter(
  23. toArray(options.include || []).length ? options.include : defaultIncludes,
  24. options.exclude || defaultExcludes
  25. );
  26. const dts = options.dts === true ? "unimport.d.ts" : options.dts;
  27. return {
  28. name: "unimport",
  29. enforce: "post",
  30. transformInclude(id) {
  31. return filter(id);
  32. },
  33. async transform(code, id) {
  34. const s = new MagicString(code);
  35. await ctx.injectImports(s, id);
  36. if (!s.hasChanged()) {
  37. return;
  38. }
  39. return {
  40. code: s.toString(),
  41. map: s.generateMap()
  42. };
  43. },
  44. async buildStart() {
  45. if (options.dirs?.length) {
  46. await ctx.modifyDynamicImports(async (imports) => {
  47. imports.push(...await scanDirExports(options.dirs));
  48. });
  49. }
  50. if (dts) {
  51. return promises.writeFile(dts, await ctx.generateTypeDeclarations(), "utf-8");
  52. }
  53. }
  54. };
  55. });
  56. export { unplugin as default, defaultExcludes, defaultIncludes };