smartbundle 0.7.3 → 0.8.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/README.md +62 -28
  2. package/__bin__/smartbundle.js +1 -1
  3. package/__do_not_import_directly__/args.js +23 -6
  4. package/__do_not_import_directly__/args.mjs +18 -0
  5. package/__do_not_import_directly__/buildVite.js +7 -7
  6. package/__do_not_import_directly__/buildVite.mjs +20 -0
  7. package/__do_not_import_directly__/createViteConfig.js +14 -14
  8. package/__do_not_import_directly__/{createViteConfig.cjs → createViteConfig.mjs} +14 -14
  9. package/__do_not_import_directly__/error.js +3 -3
  10. package/__do_not_import_directly__/error.mjs +9 -0
  11. package/__do_not_import_directly__/errors.js +3 -3
  12. package/__do_not_import_directly__/{errors.cjs → errors.mjs} +3 -3
  13. package/__do_not_import_directly__/packageJson.js +52 -35
  14. package/__do_not_import_directly__/packageJson.mjs +129 -0
  15. package/__do_not_import_directly__/resolveDirs.js +6 -6
  16. package/__do_not_import_directly__/{resolveDirs.cjs → resolveDirs.mjs} +6 -6
  17. package/__do_not_import_directly__/smartbundle.js +4 -3
  18. package/__do_not_import_directly__/smartbundle.mjs +3 -0
  19. package/__do_not_import_directly__/tasks/binsTask.js +16 -13
  20. package/__do_not_import_directly__/tasks/{binsTask.cjs → binsTask.mjs} +16 -13
  21. package/__do_not_import_directly__/tasks/buildTypesTask/buildTypesTask.js +34 -12
  22. package/__do_not_import_directly__/tasks/buildTypesTask/buildTypesTask.mjs +40 -0
  23. package/__do_not_import_directly__/tasks/buildTypesTask/callTypescript.js +33 -12
  24. package/__do_not_import_directly__/tasks/buildTypesTask/callTypescript.mjs +53 -0
  25. package/__do_not_import_directly__/tasks/copyStaticFilesTask.js +9 -9
  26. package/__do_not_import_directly__/tasks/{copyStaticFilesTask.cjs → copyStaticFilesTask.mjs} +9 -9
  27. package/__do_not_import_directly__/tasks/jsFilesTask.js +6 -6
  28. package/__do_not_import_directly__/tasks/{jsFilesTask.cjs → jsFilesTask.mjs} +6 -6
  29. package/__do_not_import_directly__/tasks/utils.js +3 -3
  30. package/__do_not_import_directly__/tasks/{utils.cjs → utils.mjs} +3 -3
  31. package/__do_not_import_directly__/writePackageJson.js +23 -15
  32. package/__do_not_import_directly__/{writePackageJson.cjs → writePackageJson.mjs} +23 -15
  33. package/package.json +9 -8
  34. package/src/args.d.cts +9 -0
  35. package/src/buildVite.d.cts +16 -0
  36. package/src/createViteConfig.d.cts +12 -0
  37. package/src/error.d.cts +4 -0
  38. package/src/errors.d.cts +28 -0
  39. package/src/index.d.cts +9 -0
  40. package/src/index.js +48 -43
  41. package/src/index.mjs +105 -0
  42. package/src/packageJson.d.cts +89 -0
  43. package/src/resolveDirs.d.cts +12 -0
  44. package/src/run.d.cts +1 -0
  45. package/src/tasks/binsTask.d.cts +9 -0
  46. package/src/tasks/buildTypesTask/buildTypesTask.d.cts +9 -0
  47. package/src/tasks/buildTypesTask/callTypescript.d.cts +8 -0
  48. package/src/tasks/copyStaticFilesTask.d.cts +1 -0
  49. package/src/tasks/jsFilesTask.d.cts +7 -0
  50. package/src/tasks/utils.d.cts +1 -0
  51. package/src/writePackageJson.d.cts +14 -0
  52. package/src/writePackageJson.d.ts +1 -0
  53. package/__do_not_import_directly__/args.cjs +0 -35
  54. package/__do_not_import_directly__/buildVite.cjs +0 -20
  55. package/__do_not_import_directly__/error.cjs +0 -9
  56. package/__do_not_import_directly__/packageJson.cjs +0 -146
  57. package/__do_not_import_directly__/smartbundle.cjs +0 -4
  58. package/__do_not_import_directly__/tasks/buildTypesTask/buildTypesTask.cjs +0 -62
  59. package/__do_not_import_directly__/tasks/buildTypesTask/callTypescript.cjs +0 -68
  60. package/src/index.cjs +0 -100
@@ -1,5 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
1
  function reverseMap(map) {
4
2
  const reversed = /* @__PURE__ */ new Map();
5
3
  for (const [key, value] of map) {
@@ -9,4 +7,6 @@ function reverseMap(map) {
9
7
  }
10
8
  return reversed;
11
9
  }
12
- exports.reverseMap = reverseMap;
10
+ export {
11
+ reverseMap
12
+ };
@@ -1,13 +1,22 @@
1
- import { writeFile } from "node:fs/promises";
2
- import "zod";
3
- import "node:path";
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const fs = require("node:fs/promises");
4
+ require("zod");
5
+ require("node:path");
6
+ function extractValue(value) {
7
+ if (!value) {
8
+ return void 0;
9
+ }
10
+ if (typeof value === "string") {
11
+ return value;
12
+ }
13
+ return value.default;
14
+ }
4
15
  async function writePackageJson(outDir, parsed, { exportsMap, binsMap }) {
5
16
  const allExports = {};
6
17
  for (const [key, value] of exportsMap) {
7
18
  const anExport = {};
8
- if (value.dts) {
9
- anExport.types = value.dts;
10
- }
19
+ anExport.types = value.cdts ?? value.dts;
11
20
  if (value.mjs) {
12
21
  anExport.import = {};
13
22
  if (value.dts) {
@@ -17,12 +26,12 @@ async function writePackageJson(outDir, parsed, { exportsMap, binsMap }) {
17
26
  }
18
27
  if (value.cjs) {
19
28
  anExport.require = {};
20
- if (value.dts) {
21
- anExport.require.types = value.dts;
29
+ if (value.cdts) {
30
+ anExport.require.types = value.cdts;
22
31
  }
23
32
  anExport.require.default = value.cjs;
24
33
  }
25
- anExport.default = value.mjs;
34
+ anExport.default = value.cjs;
26
35
  allExports[key] = anExport;
27
36
  }
28
37
  allExports["./package.json"] = "./package.json";
@@ -30,11 +39,12 @@ async function writePackageJson(outDir, parsed, { exportsMap, binsMap }) {
30
39
  const rootExport = typeof allExports["."] === "object" ? allExports["."] : void 0;
31
40
  const res = {
32
41
  name: parsed.name,
33
- type: "module",
42
+ type: "commonjs",
34
43
  version: parsed.version,
35
44
  bin,
36
45
  types: rootExport == null ? void 0 : rootExport.types,
37
- module: rootExport == null ? void 0 : rootExport.default,
46
+ module: extractValue(rootExport == null ? void 0 : rootExport.import),
47
+ main: extractValue(rootExport == null ? void 0 : rootExport.require),
38
48
  description: parsed.description ?? "",
39
49
  exports: allExports,
40
50
  dependencies: parsed.dependencies ?? void 0,
@@ -57,8 +67,6 @@ async function writePackageJson(outDir, parsed, { exportsMap, binsMap }) {
57
67
  homepage: parsed.homepage,
58
68
  devDependencies: parsed.devDependencies
59
69
  };
60
- await writeFile(`${outDir}/package.json`, JSON.stringify(res, null, 2));
70
+ await fs.writeFile(`${outDir}/package.json`, JSON.stringify(res, null, 2));
61
71
  }
62
- export {
63
- writePackageJson
64
- };
72
+ exports.writePackageJson = writePackageJson;
@@ -1,15 +1,20 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const fs = require("node:fs/promises");
4
- require("zod");
5
- require("node:path");
1
+ import { writeFile } from "node:fs/promises";
2
+ import "zod";
3
+ import "node:path";
4
+ function extractValue(value) {
5
+ if (!value) {
6
+ return void 0;
7
+ }
8
+ if (typeof value === "string") {
9
+ return value;
10
+ }
11
+ return value.default;
12
+ }
6
13
  async function writePackageJson(outDir, parsed, { exportsMap, binsMap }) {
7
14
  const allExports = {};
8
15
  for (const [key, value] of exportsMap) {
9
16
  const anExport = {};
10
- if (value.dts) {
11
- anExport.types = value.dts;
12
- }
17
+ anExport.types = value.cdts ?? value.dts;
13
18
  if (value.mjs) {
14
19
  anExport.import = {};
15
20
  if (value.dts) {
@@ -19,12 +24,12 @@ async function writePackageJson(outDir, parsed, { exportsMap, binsMap }) {
19
24
  }
20
25
  if (value.cjs) {
21
26
  anExport.require = {};
22
- if (value.dts) {
23
- anExport.require.types = value.dts;
27
+ if (value.cdts) {
28
+ anExport.require.types = value.cdts;
24
29
  }
25
30
  anExport.require.default = value.cjs;
26
31
  }
27
- anExport.default = value.mjs;
32
+ anExport.default = value.cjs;
28
33
  allExports[key] = anExport;
29
34
  }
30
35
  allExports["./package.json"] = "./package.json";
@@ -32,11 +37,12 @@ async function writePackageJson(outDir, parsed, { exportsMap, binsMap }) {
32
37
  const rootExport = typeof allExports["."] === "object" ? allExports["."] : void 0;
33
38
  const res = {
34
39
  name: parsed.name,
35
- type: "module",
40
+ type: "commonjs",
36
41
  version: parsed.version,
37
42
  bin,
38
43
  types: rootExport == null ? void 0 : rootExport.types,
39
- module: rootExport == null ? void 0 : rootExport.default,
44
+ module: extractValue(rootExport == null ? void 0 : rootExport.import),
45
+ main: extractValue(rootExport == null ? void 0 : rootExport.require),
40
46
  description: parsed.description ?? "",
41
47
  exports: allExports,
42
48
  dependencies: parsed.dependencies ?? void 0,
@@ -59,6 +65,8 @@ async function writePackageJson(outDir, parsed, { exportsMap, binsMap }) {
59
65
  homepage: parsed.homepage,
60
66
  devDependencies: parsed.devDependencies
61
67
  };
62
- await fs.writeFile(`${outDir}/package.json`, JSON.stringify(res, null, 2));
68
+ await writeFile(`${outDir}/package.json`, JSON.stringify(res, null, 2));
63
69
  }
64
- exports.writePackageJson = writePackageJson;
70
+ export {
71
+ writePackageJson
72
+ };
package/package.json CHANGED
@@ -1,23 +1,24 @@
1
1
  {
2
2
  "name": "smartbundle",
3
- "type": "module",
4
- "version": "0.7.3",
3
+ "type": "commonjs",
4
+ "version": "0.8.0-alpha.0",
5
5
  "bin": {
6
6
  "smartbundle": "__bin__/smartbundle.js"
7
7
  },
8
- "types": "./src/index.d.ts",
9
- "module": "./src/index.js",
8
+ "types": "./src/index.d.cts",
9
+ "module": "./src/index.mjs",
10
+ "main": "./src/index.js",
10
11
  "description": "zero-config bundler for npm packages",
11
12
  "exports": {
12
13
  ".": {
13
- "types": "./src/index.d.ts",
14
+ "types": "./src/index.d.cts",
14
15
  "import": {
15
16
  "types": "./src/index.d.ts",
16
- "default": "./src/index.js"
17
+ "default": "./src/index.mjs"
17
18
  },
18
19
  "require": {
19
- "types": "./src/index.d.ts",
20
- "default": "./src/index.cjs"
20
+ "types": "./src/index.d.cts",
21
+ "default": "./src/index.js"
21
22
  },
22
23
  "default": "./src/index.js"
23
24
  },
package/src/args.d.cts ADDED
@@ -0,0 +1,9 @@
1
+ export declare const args: {
2
+ [x: string]: unknown;
3
+ sourceDir: string;
4
+ packagePath: string;
5
+ outputDir: string;
6
+ _: (string | number)[];
7
+ $0: string;
8
+ };
9
+ export type Args = typeof args;
@@ -0,0 +1,16 @@
1
+ import { type UserConfig } from "vite";
2
+ import { Rollup } from "vite";
3
+ type BuildViteOptions = {
4
+ viteConfig: UserConfig;
5
+ };
6
+ type BuildSuccess = {
7
+ error: false;
8
+ output: Rollup.OutputChunk[];
9
+ };
10
+ type BuildError = {
11
+ error: true;
12
+ errors: Array<string>;
13
+ };
14
+ type BuildResult = BuildSuccess | BuildError;
15
+ export declare function buildVite({ viteConfig, }: BuildViteOptions): Promise<BuildResult>;
16
+ export {};
@@ -0,0 +1,12 @@
1
+ import { type Dirs } from "./resolveDirs.js";
2
+ import { type PackageJson } from "./packageJson.js";
3
+ type CreateViteConfigParam = {
4
+ dirs: Dirs;
5
+ packageJson: PackageJson;
6
+ };
7
+ export declare function createViteConfig({ dirs, packageJson }: CreateViteConfigParam): {
8
+ viteConfig: import("vite").UserConfig;
9
+ entrypoints: Map<string, string>;
10
+ bins: Map<string, string>;
11
+ };
12
+ export {};
@@ -0,0 +1,4 @@
1
+ export declare class BuildError extends Error {
2
+ error: string;
3
+ constructor(error: string);
4
+ }
@@ -0,0 +1,28 @@
1
+ export declare const errors: {
2
+ exportsRequired: string;
3
+ exportsInvalid: string;
4
+ nameRequired: string;
5
+ nameMinLength: string;
6
+ nameMaxLength: string;
7
+ nameStartsIllegalChars: string;
8
+ versionRequired: string;
9
+ privateIsTrue: string;
10
+ descriptionString: string;
11
+ dependenciesInvalid: string;
12
+ binFiled: string;
13
+ rollupError: string;
14
+ typescriptNotFound: string;
15
+ optionalDependenciesInvalid: string;
16
+ repositoryInvalid: string;
17
+ keywordsInvalid: string;
18
+ authorInvalid: string;
19
+ contributorsInvalid: string;
20
+ licenseInvalid: string;
21
+ devDependenciesInvalid: string;
22
+ peerDependenciesInvalid: string;
23
+ enginesInvalid: string;
24
+ browserInvalid: string;
25
+ fundingInvalid: string;
26
+ osInvalid: string;
27
+ cpuInvalid: string;
28
+ };
@@ -0,0 +1,9 @@
1
+ import { type Args } from "./resolveDirs.js";
2
+ export declare function defineViteConfig(args?: Args): Promise<import("vite").UserConfig>;
3
+ export declare function run(args: Args): Promise<{
4
+ error: boolean;
5
+ errors: string[];
6
+ } | {
7
+ error: boolean;
8
+ errors?: undefined;
9
+ }>;
package/src/index.js CHANGED
@@ -1,47 +1,49 @@
1
- import { relative } from "node:path";
2
- import { rm, mkdir } from "node:fs/promises";
3
- import { parsePackageJson } from "../__do_not_import_directly__/packageJson.js";
4
- import { writePackageJson } from "../__do_not_import_directly__/writePackageJson.js";
5
- import { buildVite } from "../__do_not_import_directly__/buildVite.js";
6
- import { resolveDirs } from "../__do_not_import_directly__/resolveDirs.js";
7
- import { createViteConfig } from "../__do_not_import_directly__/createViteConfig.js";
8
- import { copyStaticFilesTask } from "../__do_not_import_directly__/tasks/copyStaticFilesTask.js";
9
- import { buildTypesTask } from "../__do_not_import_directly__/tasks/buildTypesTask/buildTypesTask.js";
10
- import { BuildError } from "../__do_not_import_directly__/error.js";
11
- import { jsFilesTask } from "../__do_not_import_directly__/tasks/jsFilesTask.js";
12
- import { binsTask } from "../__do_not_import_directly__/tasks/binsTask.js";
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const path = require("node:path");
4
+ const fs = require("node:fs/promises");
5
+ const packageJson = require("../__do_not_import_directly__/packageJson.js");
6
+ const writePackageJson = require("../__do_not_import_directly__/writePackageJson.js");
7
+ const buildVite = require("../__do_not_import_directly__/buildVite.js");
8
+ const resolveDirs = require("../__do_not_import_directly__/resolveDirs.js");
9
+ const createViteConfig = require("../__do_not_import_directly__/createViteConfig.js");
10
+ const copyStaticFilesTask = require("../__do_not_import_directly__/tasks/copyStaticFilesTask.js");
11
+ const buildTypesTask = require("../__do_not_import_directly__/tasks/buildTypesTask/buildTypesTask.js");
12
+ const error = require("../__do_not_import_directly__/error.js");
13
+ const jsFilesTask = require("../__do_not_import_directly__/tasks/jsFilesTask.js");
14
+ const binsTask = require("../__do_not_import_directly__/tasks/binsTask.js");
13
15
  function setExports(exportsMap, exportName, mapFn) {
14
16
  const entry = exportsMap.get(exportName) ?? {};
15
17
  exportsMap.set(exportName, mapFn(entry));
16
18
  }
17
19
  async function defineViteConfig(args = {}) {
18
- const dirs = resolveDirs(args);
20
+ const dirs = resolveDirs.resolveDirs(args);
19
21
  const { sourceDir, outDir, packagePath } = dirs;
20
- await rm(outDir, { recursive: true, force: true });
21
- await mkdir(outDir, { recursive: true });
22
- const packageJson = await parsePackageJson({ sourceDir, packagePath });
23
- if (Array.isArray(packageJson)) {
24
- console.error(packageJson);
22
+ await fs.rm(outDir, { recursive: true, force: true });
23
+ await fs.mkdir(outDir, { recursive: true });
24
+ const packageJson$1 = await packageJson.parsePackageJson({ sourceDir, packagePath });
25
+ if (Array.isArray(packageJson$1)) {
26
+ console.error(packageJson$1);
25
27
  throw new Error("Failed to parse package.json");
26
28
  }
27
- const { viteConfig } = createViteConfig({ dirs, packageJson });
29
+ const { viteConfig } = createViteConfig.createViteConfig({ dirs, packageJson: packageJson$1 });
28
30
  return viteConfig;
29
31
  }
30
32
  async function run(args) {
31
- const dirs = resolveDirs(args);
33
+ const dirs = resolveDirs.resolveDirs(args);
32
34
  const { sourceDir, outDir, packagePath, outBinsDir } = dirs;
33
- await rm(outDir, { recursive: true, force: true });
34
- await mkdir(outDir, { recursive: true });
35
- const packageJson = await parsePackageJson({ sourceDir, packagePath });
36
- if (Array.isArray(packageJson)) {
37
- console.log(packageJson);
38
- return { error: true, errors: packageJson };
35
+ await fs.rm(outDir, { recursive: true, force: true });
36
+ await fs.mkdir(outDir, { recursive: true });
37
+ const packageJson$1 = await packageJson.parsePackageJson({ sourceDir, packagePath });
38
+ if (Array.isArray(packageJson$1)) {
39
+ console.log(packageJson$1);
40
+ return { error: true, errors: packageJson$1 };
39
41
  }
40
- const { viteConfig, entrypoints, bins } = createViteConfig({
42
+ const { viteConfig, entrypoints, bins } = createViteConfig.createViteConfig({
41
43
  dirs,
42
- packageJson
44
+ packageJson: packageJson$1
43
45
  });
44
- const outputs = await buildVite({ viteConfig });
46
+ const outputs = await buildVite.buildVite({ viteConfig });
45
47
  if (outputs.error) {
46
48
  return { error: true, errors: outputs.errors };
47
49
  }
@@ -49,24 +51,29 @@ async function run(args) {
49
51
  const exportsMap = /* @__PURE__ */ new Map();
50
52
  const binsMap = /* @__PURE__ */ new Map();
51
53
  const tasksRes = await Promise.allSettled([
52
- copyStaticFilesTask(sourceDir, outDir),
53
- buildTypesTask({
54
+ copyStaticFilesTask.copyStaticFilesTask(sourceDir, outDir),
55
+ buildTypesTask.buildTypesTask({
54
56
  sourceDir,
55
57
  outDir,
56
58
  entrypoints,
57
59
  buildOutput: viteOutput
58
60
  }).then((res) => {
59
- for (const [value, key] of res) {
60
- setExports(exportsMap, key, (entry) => {
61
- entry.dts = "./" + relative(outDir, value);
61
+ for (const [types, source] of res) {
62
+ setExports(exportsMap, source, (entry) => {
63
+ if (types.endsWith(".d.ts")) {
64
+ entry.dts = "./" + path.relative(outDir, types);
65
+ }
66
+ if (types.endsWith(".d.cts")) {
67
+ entry.cdts = "./" + path.relative(outDir, types);
68
+ }
62
69
  return entry;
63
70
  });
64
71
  }
65
72
  }),
66
- jsFilesTask({ buildOutput: viteOutput, entrypoints }).then((res) => {
73
+ jsFilesTask.jsFilesTask({ buildOutput: viteOutput, entrypoints }).then((res) => {
67
74
  for (const [value, key] of res) {
68
75
  setExports(exportsMap, key, (entry) => {
69
- const format = value.endsWith(".cjs") ? "cjs" : "es";
76
+ const format = value.endsWith(".js") ? "cjs" : "es";
70
77
  if (format === "es") {
71
78
  entry.mjs = "./" + value;
72
79
  } else if (format === "cjs") {
@@ -76,7 +83,7 @@ async function run(args) {
76
83
  });
77
84
  }
78
85
  }),
79
- binsTask({ outBinsDir, bins, buildOutput: viteOutput, outDir }).then(
86
+ binsTask.binsTask({ outBinsDir, bins, buildOutput: viteOutput, outDir }).then(
80
87
  (res) => {
81
88
  for (const [value, key] of res) {
82
89
  binsMap.set(key, value);
@@ -84,17 +91,15 @@ async function run(args) {
84
91
  }
85
92
  )
86
93
  ]);
87
- const errors = tasksRes.filter((res) => res.status === "rejected").map((res) => res.reason).filter((res) => res instanceof BuildError).map((res) => res.error);
94
+ const errors = tasksRes.filter((res) => res.status === "rejected").map((res) => res.reason).filter((res) => res instanceof error.BuildError).map((res) => res.error);
88
95
  if (errors.length > 0) {
89
96
  return { error: true, errors };
90
97
  }
91
- await writePackageJson(outDir, packageJson, {
98
+ await writePackageJson.writePackageJson(outDir, packageJson$1, {
92
99
  exportsMap,
93
100
  binsMap
94
101
  });
95
102
  return { error: false };
96
103
  }
97
- export {
98
- defineViteConfig,
99
- run
100
- };
104
+ exports.defineViteConfig = defineViteConfig;
105
+ exports.run = run;
package/src/index.mjs ADDED
@@ -0,0 +1,105 @@
1
+ import { relative } from "node:path";
2
+ import { rm, mkdir } from "node:fs/promises";
3
+ import { parsePackageJson } from "../__do_not_import_directly__/packageJson.mjs";
4
+ import { writePackageJson } from "../__do_not_import_directly__/writePackageJson.mjs";
5
+ import { buildVite } from "../__do_not_import_directly__/buildVite.mjs";
6
+ import { resolveDirs } from "../__do_not_import_directly__/resolveDirs.mjs";
7
+ import { createViteConfig } from "../__do_not_import_directly__/createViteConfig.mjs";
8
+ import { copyStaticFilesTask } from "../__do_not_import_directly__/tasks/copyStaticFilesTask.mjs";
9
+ import { buildTypesTask } from "../__do_not_import_directly__/tasks/buildTypesTask/buildTypesTask.mjs";
10
+ import { BuildError } from "../__do_not_import_directly__/error.mjs";
11
+ import { jsFilesTask } from "../__do_not_import_directly__/tasks/jsFilesTask.mjs";
12
+ import { binsTask } from "../__do_not_import_directly__/tasks/binsTask.mjs";
13
+ function setExports(exportsMap, exportName, mapFn) {
14
+ const entry = exportsMap.get(exportName) ?? {};
15
+ exportsMap.set(exportName, mapFn(entry));
16
+ }
17
+ async function defineViteConfig(args = {}) {
18
+ const dirs = resolveDirs(args);
19
+ const { sourceDir, outDir, packagePath } = dirs;
20
+ await rm(outDir, { recursive: true, force: true });
21
+ await mkdir(outDir, { recursive: true });
22
+ const packageJson = await parsePackageJson({ sourceDir, packagePath });
23
+ if (Array.isArray(packageJson)) {
24
+ console.error(packageJson);
25
+ throw new Error("Failed to parse package.json");
26
+ }
27
+ const { viteConfig } = createViteConfig({ dirs, packageJson });
28
+ return viteConfig;
29
+ }
30
+ async function run(args) {
31
+ const dirs = resolveDirs(args);
32
+ const { sourceDir, outDir, packagePath, outBinsDir } = dirs;
33
+ await rm(outDir, { recursive: true, force: true });
34
+ await mkdir(outDir, { recursive: true });
35
+ const packageJson = await parsePackageJson({ sourceDir, packagePath });
36
+ if (Array.isArray(packageJson)) {
37
+ console.log(packageJson);
38
+ return { error: true, errors: packageJson };
39
+ }
40
+ const { viteConfig, entrypoints, bins } = createViteConfig({
41
+ dirs,
42
+ packageJson
43
+ });
44
+ const outputs = await buildVite({ viteConfig });
45
+ if (outputs.error) {
46
+ return { error: true, errors: outputs.errors };
47
+ }
48
+ const viteOutput = outputs.output;
49
+ const exportsMap = /* @__PURE__ */ new Map();
50
+ const binsMap = /* @__PURE__ */ new Map();
51
+ const tasksRes = await Promise.allSettled([
52
+ copyStaticFilesTask(sourceDir, outDir),
53
+ buildTypesTask({
54
+ sourceDir,
55
+ outDir,
56
+ entrypoints,
57
+ buildOutput: viteOutput
58
+ }).then((res) => {
59
+ for (const [types, source] of res) {
60
+ setExports(exportsMap, source, (entry) => {
61
+ if (types.endsWith(".d.ts")) {
62
+ entry.dts = "./" + relative(outDir, types);
63
+ }
64
+ if (types.endsWith(".d.cts")) {
65
+ entry.cdts = "./" + relative(outDir, types);
66
+ }
67
+ return entry;
68
+ });
69
+ }
70
+ }),
71
+ jsFilesTask({ buildOutput: viteOutput, entrypoints }).then((res) => {
72
+ for (const [value, key] of res) {
73
+ setExports(exportsMap, key, (entry) => {
74
+ const format = value.endsWith(".js") ? "cjs" : "es";
75
+ if (format === "es") {
76
+ entry.mjs = "./" + value;
77
+ } else if (format === "cjs") {
78
+ entry.cjs = "./" + value;
79
+ }
80
+ return entry;
81
+ });
82
+ }
83
+ }),
84
+ binsTask({ outBinsDir, bins, buildOutput: viteOutput, outDir }).then(
85
+ (res) => {
86
+ for (const [value, key] of res) {
87
+ binsMap.set(key, value);
88
+ }
89
+ }
90
+ )
91
+ ]);
92
+ const errors = tasksRes.filter((res) => res.status === "rejected").map((res) => res.reason).filter((res) => res instanceof BuildError).map((res) => res.error);
93
+ if (errors.length > 0) {
94
+ return { error: true, errors };
95
+ }
96
+ await writePackageJson(outDir, packageJson, {
97
+ exportsMap,
98
+ binsMap
99
+ });
100
+ return { error: false };
101
+ }
102
+ export {
103
+ defineViteConfig,
104
+ run
105
+ };
@@ -0,0 +1,89 @@
1
+ import z from "zod";
2
+ declare function createPackageJsonSchema(sourceDir: string): z.ZodObject<{
3
+ exports: z.ZodOptional<z.ZodEffects<z.ZodUnion<[z.ZodEffects<z.ZodString, Map<string, string>, string>, z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodString>, Map<string, string>, Record<string, string>>]>, Map<string, string>, string | Record<string, string>>>;
4
+ name: z.ZodEffects<z.ZodString, string, string>;
5
+ version: z.ZodString;
6
+ private: z.ZodEffects<z.ZodBoolean, boolean, boolean>;
7
+ description: z.ZodOptional<z.ZodString>;
8
+ dependencies: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
9
+ optionalDependencies: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
10
+ bin: z.ZodOptional<z.ZodEffects<z.ZodUnion<[z.ZodEffects<z.ZodString, Map<string, string>, string>, z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodString>, Map<string, string>, Record<string, string>>]>, Map<string, string>, string | Record<string, string>>>;
11
+ repository: z.ZodOptional<z.ZodAny>;
12
+ keywords: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
13
+ author: z.ZodOptional<z.ZodAny>;
14
+ maintainers: z.ZodOptional<z.ZodAny>;
15
+ contributors: z.ZodOptional<z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>, "many">>;
16
+ license: z.ZodOptional<z.ZodAny>;
17
+ devDependencies: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
18
+ peerDependencies: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
19
+ engines: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
20
+ browser: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodRecord<z.ZodString, z.ZodString>]>>;
21
+ bugs: z.ZodOptional<z.ZodAny>;
22
+ funding: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>]>>;
23
+ os: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
24
+ cpu: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
25
+ sideEffects: z.ZodOptional<z.ZodAny>;
26
+ unpkg: z.ZodOptional<z.ZodAny>;
27
+ homepage: z.ZodOptional<z.ZodAny>;
28
+ }, "strip", z.ZodTypeAny, {
29
+ exports?: Map<string, string>;
30
+ name?: string;
31
+ version?: string;
32
+ private?: boolean;
33
+ description?: string;
34
+ dependencies?: Record<string, string>;
35
+ optionalDependencies?: Record<string, string>;
36
+ bin?: Map<string, string>;
37
+ repository?: any;
38
+ keywords?: string[];
39
+ author?: any;
40
+ contributors?: (string | {})[];
41
+ license?: any;
42
+ devDependencies?: Record<string, string>;
43
+ peerDependencies?: Record<string, string>;
44
+ engines?: Record<string, string>;
45
+ browser?: string | Record<string, string>;
46
+ funding?: string | {};
47
+ os?: string[];
48
+ cpu?: string[];
49
+ maintainers?: any;
50
+ bugs?: any;
51
+ sideEffects?: any;
52
+ unpkg?: any;
53
+ homepage?: any;
54
+ }, {
55
+ exports?: string | Record<string, string>;
56
+ name?: string;
57
+ version?: string;
58
+ private?: boolean;
59
+ description?: string;
60
+ dependencies?: Record<string, string>;
61
+ optionalDependencies?: Record<string, string>;
62
+ bin?: string | Record<string, string>;
63
+ repository?: any;
64
+ keywords?: string[];
65
+ author?: any;
66
+ contributors?: (string | {})[];
67
+ license?: any;
68
+ devDependencies?: Record<string, string>;
69
+ peerDependencies?: Record<string, string>;
70
+ engines?: Record<string, string>;
71
+ browser?: string | Record<string, string>;
72
+ funding?: string | {};
73
+ os?: string[];
74
+ cpu?: string[];
75
+ maintainers?: any;
76
+ bugs?: any;
77
+ sideEffects?: any;
78
+ unpkg?: any;
79
+ homepage?: any;
80
+ }>;
81
+ type PackageJsonSchema = ReturnType<typeof createPackageJsonSchema>;
82
+ export type PackageJson = z.infer<PackageJsonSchema>;
83
+ type Errors = Array<string>;
84
+ type ParsePackageJsonArg = {
85
+ packagePath: string;
86
+ sourceDir: string;
87
+ };
88
+ export declare function parsePackageJson({ sourceDir, packagePath, }: ParsePackageJsonArg): Promise<PackageJson | Errors>;
89
+ export {};
@@ -0,0 +1,12 @@
1
+ export type Args = {
2
+ sourceDir?: string;
3
+ packagePath?: string;
4
+ outputDir?: string;
5
+ };
6
+ export declare function resolveDirs(args: Args): {
7
+ sourceDir: string;
8
+ packagePath: string;
9
+ outDir: string;
10
+ outBinsDir: string;
11
+ };
12
+ export type Dirs = ReturnType<typeof resolveDirs>;
package/src/run.d.cts ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,9 @@
1
+ import { type Rollup } from "vite";
2
+ type BinsTaskOption = {
3
+ buildOutput: Rollup.OutputChunk[];
4
+ bins: Map<string, string>;
5
+ outBinsDir: string;
6
+ outDir: string;
7
+ };
8
+ export declare function binsTask({ buildOutput, bins, outBinsDir, outDir, }: BinsTaskOption): Promise<Map<string, string>>;
9
+ export {};
@@ -0,0 +1,9 @@
1
+ import { type Rollup } from "vite";
2
+ type BuildTypesTaskOption = {
3
+ buildOutput: Rollup.OutputChunk[];
4
+ entrypoints: Map<string, string>;
5
+ sourceDir: string;
6
+ outDir: string;
7
+ };
8
+ export declare function buildTypesTask({ buildOutput, entrypoints, sourceDir, outDir, }: BuildTypesTaskOption): Promise<Map<string, string>>;
9
+ export {};