smartbundle 0.8.1 → 0.9.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 (70) hide show
  1. package/__bin__/smartbundle.js +1 -1
  2. package/{__do_not_import_directly__ → __compiled__/cjs/src}/createViteConfig.js +23 -15
  3. package/{src → __compiled__/cjs/src}/index.js +24 -22
  4. package/{__do_not_import_directly__/smartbundle.js → __compiled__/cjs/src/run.js} +1 -1
  5. package/{__do_not_import_directly__ → __compiled__/cjs/src}/tasks/binsTask.js +2 -5
  6. package/{__do_not_import_directly__ → __compiled__/cjs/src}/tasks/buildTypesTask/callTypescript.js +4 -3
  7. package/{src → __compiled__/cjs/src}/tasks/jsFilesTask.d.ts +2 -1
  8. package/__compiled__/cjs/src/tasks/jsFilesTask.js +57 -0
  9. package/{__do_not_import_directly__ → __compiled__/esm/src}/createViteConfig.mjs +23 -15
  10. package/{src → __compiled__/esm/src}/index.mjs +24 -22
  11. package/{__do_not_import_directly__/smartbundle.mjs → __compiled__/esm/src/run.mjs} +1 -1
  12. package/{__do_not_import_directly__ → __compiled__/esm/src}/tasks/binsTask.mjs +2 -5
  13. package/{__do_not_import_directly__ → __compiled__/esm/src}/tasks/buildTypesTask/callTypescript.mjs +4 -3
  14. package/{src → __compiled__/esm/src}/tasks/jsFilesTask.d.mts +2 -1
  15. package/__compiled__/esm/src/tasks/jsFilesTask.mjs +57 -0
  16. package/index.js +1 -0
  17. package/index.mjs +1 -0
  18. package/package.json +10 -10
  19. package/__do_not_import_directly__/tasks/jsFilesTask.js +0 -25
  20. package/__do_not_import_directly__/tasks/jsFilesTask.mjs +0 -25
  21. /package/{src → __compiled__/cjs/src}/args.d.ts +0 -0
  22. /package/{__do_not_import_directly__ → __compiled__/cjs/src}/args.js +0 -0
  23. /package/{src → __compiled__/cjs/src}/buildVite.d.ts +0 -0
  24. /package/{__do_not_import_directly__ → __compiled__/cjs/src}/buildVite.js +0 -0
  25. /package/{src → __compiled__/cjs/src}/createViteConfig.d.ts +0 -0
  26. /package/{src → __compiled__/cjs/src}/error.d.ts +0 -0
  27. /package/{__do_not_import_directly__ → __compiled__/cjs/src}/error.js +0 -0
  28. /package/{src → __compiled__/cjs/src}/errors.d.ts +0 -0
  29. /package/{__do_not_import_directly__ → __compiled__/cjs/src}/errors.js +0 -0
  30. /package/{src → __compiled__/cjs/src}/index.d.ts +0 -0
  31. /package/{src → __compiled__/cjs/src}/packageJson.d.ts +0 -0
  32. /package/{__do_not_import_directly__ → __compiled__/cjs/src}/packageJson.js +0 -0
  33. /package/{src → __compiled__/cjs/src}/resolveDirs.d.ts +0 -0
  34. /package/{__do_not_import_directly__ → __compiled__/cjs/src}/resolveDirs.js +0 -0
  35. /package/{src → __compiled__/cjs/src}/run.d.ts +0 -0
  36. /package/{src → __compiled__/cjs/src}/tasks/binsTask.d.ts +0 -0
  37. /package/{src → __compiled__/cjs/src}/tasks/buildTypesTask/buildTypesTask.d.ts +0 -0
  38. /package/{__do_not_import_directly__ → __compiled__/cjs/src}/tasks/buildTypesTask/buildTypesTask.js +0 -0
  39. /package/{src → __compiled__/cjs/src}/tasks/buildTypesTask/callTypescript.d.ts +0 -0
  40. /package/{src → __compiled__/cjs/src}/tasks/copyStaticFilesTask.d.ts +0 -0
  41. /package/{__do_not_import_directly__ → __compiled__/cjs/src}/tasks/copyStaticFilesTask.js +0 -0
  42. /package/{src → __compiled__/cjs/src}/tasks/utils.d.ts +0 -0
  43. /package/{__do_not_import_directly__ → __compiled__/cjs/src}/tasks/utils.js +0 -0
  44. /package/{src → __compiled__/cjs/src}/writePackageJson.d.ts +0 -0
  45. /package/{__do_not_import_directly__ → __compiled__/cjs/src}/writePackageJson.js +0 -0
  46. /package/{src → __compiled__/esm/src}/args.d.mts +0 -0
  47. /package/{__do_not_import_directly__ → __compiled__/esm/src}/args.mjs +0 -0
  48. /package/{src → __compiled__/esm/src}/buildVite.d.mts +0 -0
  49. /package/{__do_not_import_directly__ → __compiled__/esm/src}/buildVite.mjs +0 -0
  50. /package/{src → __compiled__/esm/src}/createViteConfig.d.mts +0 -0
  51. /package/{src → __compiled__/esm/src}/error.d.mts +0 -0
  52. /package/{__do_not_import_directly__ → __compiled__/esm/src}/error.mjs +0 -0
  53. /package/{src → __compiled__/esm/src}/errors.d.mts +0 -0
  54. /package/{__do_not_import_directly__ → __compiled__/esm/src}/errors.mjs +0 -0
  55. /package/{src → __compiled__/esm/src}/index.d.mts +0 -0
  56. /package/{src → __compiled__/esm/src}/packageJson.d.mts +0 -0
  57. /package/{__do_not_import_directly__ → __compiled__/esm/src}/packageJson.mjs +0 -0
  58. /package/{src → __compiled__/esm/src}/resolveDirs.d.mts +0 -0
  59. /package/{__do_not_import_directly__ → __compiled__/esm/src}/resolveDirs.mjs +0 -0
  60. /package/{src → __compiled__/esm/src}/run.d.mts +0 -0
  61. /package/{src → __compiled__/esm/src}/tasks/binsTask.d.mts +0 -0
  62. /package/{src → __compiled__/esm/src}/tasks/buildTypesTask/buildTypesTask.d.mts +0 -0
  63. /package/{__do_not_import_directly__ → __compiled__/esm/src}/tasks/buildTypesTask/buildTypesTask.mjs +0 -0
  64. /package/{src → __compiled__/esm/src}/tasks/buildTypesTask/callTypescript.d.mts +0 -0
  65. /package/{src → __compiled__/esm/src}/tasks/copyStaticFilesTask.d.mts +0 -0
  66. /package/{__do_not_import_directly__ → __compiled__/esm/src}/tasks/copyStaticFilesTask.mjs +0 -0
  67. /package/{src → __compiled__/esm/src}/tasks/utils.d.mts +0 -0
  68. /package/{__do_not_import_directly__ → __compiled__/esm/src}/tasks/utils.mjs +0 -0
  69. /package/{src → __compiled__/esm/src}/writePackageJson.d.mts +0 -0
  70. /package/{__do_not_import_directly__ → __compiled__/esm/src}/writePackageJson.mjs +0 -0
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env node
2
- import("../__do_not_import_directly__/smartbundle.mjs");
2
+ import("../__compiled__/esm/src/run.mjs");
@@ -4,14 +4,11 @@ const path = require("node:path");
4
4
  require("node:fs/promises");
5
5
  require("zod");
6
6
  const vite = require("vite");
7
- function mapToObject(map, bins) {
7
+ function mapToObject(map) {
8
8
  const obj = {};
9
9
  for (const [key, value] of map) {
10
10
  obj[key] = value;
11
11
  }
12
- for (const [key, value] of bins) {
13
- obj[key] = value;
14
- }
15
12
  return obj;
16
13
  }
17
14
  function createExternalDepValidator(packageJson) {
@@ -57,9 +54,11 @@ function createViteConfig({ dirs, packageJson }) {
57
54
  bins.set(key, path.join(sourceDir, value));
58
55
  }
59
56
  }
57
+ const mergedEntries = new Map([...entrypoints, ...bins]);
60
58
  const depsValidator = createExternalDepValidator(packageJson);
61
59
  const viteConfig = vite.defineConfig({
62
60
  publicDir: false,
61
+ root: sourceDir,
63
62
  build: {
64
63
  outDir,
65
64
  write: true,
@@ -71,28 +70,37 @@ function createViteConfig({ dirs, packageJson }) {
71
70
  mangle: false
72
71
  },
73
72
  lib: {
74
- entry: mapToObject(entrypoints, bins),
73
+ entry: mapToObject(mergedEntries),
75
74
  formats: ["es", "cjs"],
76
75
  fileName: (format, entryName) => {
77
- const entrypoint = entrypoints.get(entryName);
76
+ const entrypoint = mergedEntries.get(entryName);
78
77
  if (!entrypoint) {
79
- const noExt2 = entryName.replace(/\.[^.]+$/, "");
80
- return "__do_not_import_directly__/" + noExt2 + (format === "es" ? ".mjs" : ".js");
78
+ const noExt = entryName.replace(/\.[^.]+$/, "");
79
+ if (format === "es") {
80
+ return path.join("__compiled__", "esm", `${noExt}.mjs`);
81
+ } else {
82
+ return path.join("__compiled__", "cjs", `${noExt}.js`);
83
+ }
81
84
  }
82
- const relativePath = path.relative(sourceDir, entrypoint);
83
- const noExt = relativePath.replace(/\.[^.]+$/, "");
84
85
  if (format === "es") {
85
- return `${noExt}.mjs`;
86
- }
87
- if (format === "cjs") {
88
- return `${noExt}.js`;
86
+ return path.join(
87
+ "__compiled__",
88
+ "esm",
89
+ path.relative(sourceDir, entrypoint).replace(/\.[^.]+$/, "") + ".mjs"
90
+ );
91
+ } else {
92
+ return path.join(
93
+ "__compiled__",
94
+ "cjs",
95
+ path.relative(sourceDir, entrypoint).replace(/\.[^.]+$/, "") + ".js"
96
+ );
89
97
  }
90
- return noExt;
91
98
  }
92
99
  },
93
100
  rollupOptions: {
94
101
  external: depsValidator,
95
102
  output: {
103
+ preserveModulesRoot: sourceDir,
96
104
  exports: "named",
97
105
  preserveModules: true
98
106
  }
@@ -2,16 +2,16 @@
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const path = require("node:path");
4
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");
5
+ const packageJson = require("./packageJson.js");
6
+ const writePackageJson = require("./writePackageJson.js");
7
+ const buildVite = require("./buildVite.js");
8
+ const resolveDirs = require("./resolveDirs.js");
9
+ const createViteConfig = require("./createViteConfig.js");
10
+ const copyStaticFilesTask = require("./tasks/copyStaticFilesTask.js");
11
+ const buildTypesTask = require("./tasks/buildTypesTask/buildTypesTask.js");
12
+ const error = require("./error.js");
13
+ const jsFilesTask = require("./tasks/jsFilesTask.js");
14
+ const binsTask = require("./tasks/binsTask.js");
15
15
  function setExports(exportsMap, exportName, mapFn) {
16
16
  const entry = exportsMap.get(exportName) ?? {};
17
17
  exportsMap.set(exportName, mapFn(entry));
@@ -70,19 +70,21 @@ async function run(args) {
70
70
  });
71
71
  }
72
72
  }),
73
- jsFilesTask.jsFilesTask({ buildOutput: viteOutput, entrypoints }).then((res) => {
74
- for (const [value, key] of res) {
75
- setExports(exportsMap, key, (entry) => {
76
- const format = value.endsWith(".js") ? "cjs" : "es";
77
- if (format === "es") {
78
- entry.mjs = "./" + value;
79
- } else if (format === "cjs") {
80
- entry.cjs = "./" + value;
81
- }
82
- return entry;
83
- });
73
+ jsFilesTask.jsFilesTask({ buildOutput: viteOutput, entrypoints, outDir }).then(
74
+ (res) => {
75
+ for (const [filePath, name] of res) {
76
+ setExports(exportsMap, name, (entry) => {
77
+ const format = filePath.endsWith(".js") ? "cjs" : "es";
78
+ if (format === "es") {
79
+ entry.mjs = "./" + filePath;
80
+ } else if (format === "cjs") {
81
+ entry.cjs = "./" + filePath;
82
+ }
83
+ return entry;
84
+ });
85
+ }
84
86
  }
85
- }),
87
+ ),
86
88
  binsTask.binsTask({ outBinsDir, bins, buildOutput: viteOutput, outDir }).then(
87
89
  (res) => {
88
90
  for (const [value, key] of res) {
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  const args = require("./args.js");
3
- const _ = require("../src/index.js");
3
+ const _ = require("./index.js");
4
4
  _.run(args.args);
@@ -20,7 +20,7 @@ async function binsTask({
20
20
  if (el.facadeModuleId == null) {
21
21
  continue;
22
22
  }
23
- if (el.fileName.endsWith(".cjs")) {
23
+ if (el.fileName.endsWith(".js")) {
24
24
  continue;
25
25
  }
26
26
  const binsNames = reversedEntrypoints.get(el.facadeModuleId);
@@ -28,10 +28,7 @@ async function binsTask({
28
28
  continue;
29
29
  }
30
30
  for (const binName of binsNames) {
31
- const totalPath = path.relative(
32
- outBinsDir,
33
- path.join(outDir, el.fileName.replace(/js$/, "mjs"))
34
- );
31
+ const totalPath = path.relative(outBinsDir, path.join(outDir, el.fileName));
35
32
  const execPath = path.join(outBinsDir, `${binName}.js`);
36
33
  await fs.writeFile(
37
34
  execPath,
@@ -57,13 +57,14 @@ async function callTypescript({
57
57
  const program = ts.createProgram(files, parsedCommandLine.options, host);
58
58
  program.emit(void 0, (fileName, data) => {
59
59
  const relativePath = path__namespace.relative(sourceDir, fileName);
60
- const finalPath = path__namespace.join(outDir, relativePath);
61
- const esmFinalPath = finalPath.replace(/\.d\.ts$/, ".d.mts");
62
60
  const sourceFileName = fileName.replace(/\.d\.ts$/, ".ts");
61
+ const finalEsmPath = path__namespace.join(outDir, "__compiled__", "esm", relativePath);
62
+ const esmFinalPath = finalEsmPath.replace(/\.d\.ts$/, ".d.mts");
63
63
  sourceToDtsMap.set(esmFinalPath, sourceFileName);
64
64
  fs__namespace.mkdirSync(path__namespace.dirname(esmFinalPath), { recursive: true });
65
65
  fs__namespace.writeFileSync(esmFinalPath, data);
66
- const cjsFinalPath = finalPath.replace(/\.d\.ts$/, ".d.ts");
66
+ const finalCjsPath = path__namespace.join(outDir, "__compiled__", "cjs", relativePath);
67
+ const cjsFinalPath = finalCjsPath.replace(/\.d\.ts$/, ".d.ts");
67
68
  fs__namespace.writeFileSync(cjsFinalPath, data);
68
69
  sourceToDtsMap.set(cjsFinalPath, sourceFileName);
69
70
  });
@@ -2,6 +2,7 @@ import { type Rollup } from "vite";
2
2
  type JsFilesTaskOption = {
3
3
  buildOutput: Rollup.OutputChunk[];
4
4
  entrypoints: Map<string, string>;
5
+ outDir: string;
5
6
  };
6
- export declare function jsFilesTask({ buildOutput, entrypoints, }: JsFilesTaskOption): Promise<Map<string, string>>;
7
+ export declare function jsFilesTask({ buildOutput, entrypoints, outDir, }: JsFilesTaskOption): Promise<Map<string, string>>;
7
8
  export {};
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ require("vite");
4
+ const path = require("node:path");
5
+ const fs = require("node:fs/promises");
6
+ function findCompiledPath(buildOutput, originalFile, type) {
7
+ for (const el of buildOutput) {
8
+ if (el.facadeModuleId === originalFile) {
9
+ if (type === "js" && el.fileName.endsWith(".js")) {
10
+ return {
11
+ path: el.fileName,
12
+ hasDefault: el.exports.includes("default")
13
+ };
14
+ }
15
+ if (type === "mjs" && el.fileName.endsWith(".mjs")) {
16
+ return {
17
+ path: el.fileName,
18
+ hasDefault: el.exports.includes("default")
19
+ };
20
+ }
21
+ }
22
+ }
23
+ return { path: "", hasDefault: false };
24
+ }
25
+ async function jsFilesTask({
26
+ buildOutput,
27
+ entrypoints,
28
+ outDir
29
+ }) {
30
+ const res = /* @__PURE__ */ new Map();
31
+ for (const [name, filePath] of entrypoints) {
32
+ const totalName = path.join(outDir, name);
33
+ const esmName = path.join(totalName, "index.mjs");
34
+ const esmCompiled = findCompiledPath(buildOutput, filePath, "mjs");
35
+ const esmPath = path.relative(totalName, path.join(outDir, esmCompiled.path));
36
+ await fs.mkdir(path.dirname(esmName), { recursive: true });
37
+ let esmContent = `export * from "./${esmPath}";
38
+ `;
39
+ if (esmCompiled.hasDefault) {
40
+ esmContent += `import Default from "./${esmPath}";
41
+ export default Default;
42
+ `;
43
+ }
44
+ await fs.writeFile(esmName, esmContent);
45
+ res.set(path.relative(outDir, path.join(path.dirname(esmName), esmPath)), name);
46
+ const cjsName = path.join(totalName, "index.js");
47
+ const cjsCompiled = findCompiledPath(buildOutput, filePath, "js");
48
+ const cjsPath = path.relative(totalName, path.join(outDir, cjsCompiled.path));
49
+ await fs.mkdir(path.dirname(esmName), { recursive: true });
50
+ const cjsContent = `module.exports = require("./${cjsPath}");
51
+ `;
52
+ await fs.writeFile(cjsName, cjsContent);
53
+ res.set(path.relative(outDir, path.join(path.dirname(cjsName), cjsPath)), name);
54
+ }
55
+ return res;
56
+ }
57
+ exports.jsFilesTask = jsFilesTask;
@@ -2,14 +2,11 @@ import { join, relative } from "node:path";
2
2
  import "node:fs/promises";
3
3
  import "zod";
4
4
  import { defineConfig } from "vite";
5
- function mapToObject(map, bins) {
5
+ function mapToObject(map) {
6
6
  const obj = {};
7
7
  for (const [key, value] of map) {
8
8
  obj[key] = value;
9
9
  }
10
- for (const [key, value] of bins) {
11
- obj[key] = value;
12
- }
13
10
  return obj;
14
11
  }
15
12
  function createExternalDepValidator(packageJson) {
@@ -55,9 +52,11 @@ function createViteConfig({ dirs, packageJson }) {
55
52
  bins.set(key, join(sourceDir, value));
56
53
  }
57
54
  }
55
+ const mergedEntries = new Map([...entrypoints, ...bins]);
58
56
  const depsValidator = createExternalDepValidator(packageJson);
59
57
  const viteConfig = defineConfig({
60
58
  publicDir: false,
59
+ root: sourceDir,
61
60
  build: {
62
61
  outDir,
63
62
  write: true,
@@ -69,28 +68,37 @@ function createViteConfig({ dirs, packageJson }) {
69
68
  mangle: false
70
69
  },
71
70
  lib: {
72
- entry: mapToObject(entrypoints, bins),
71
+ entry: mapToObject(mergedEntries),
73
72
  formats: ["es", "cjs"],
74
73
  fileName: (format, entryName) => {
75
- const entrypoint = entrypoints.get(entryName);
74
+ const entrypoint = mergedEntries.get(entryName);
76
75
  if (!entrypoint) {
77
- const noExt2 = entryName.replace(/\.[^.]+$/, "");
78
- return "__do_not_import_directly__/" + noExt2 + (format === "es" ? ".mjs" : ".js");
76
+ const noExt = entryName.replace(/\.[^.]+$/, "");
77
+ if (format === "es") {
78
+ return join("__compiled__", "esm", `${noExt}.mjs`);
79
+ } else {
80
+ return join("__compiled__", "cjs", `${noExt}.js`);
81
+ }
79
82
  }
80
- const relativePath = relative(sourceDir, entrypoint);
81
- const noExt = relativePath.replace(/\.[^.]+$/, "");
82
83
  if (format === "es") {
83
- return `${noExt}.mjs`;
84
- }
85
- if (format === "cjs") {
86
- return `${noExt}.js`;
84
+ return join(
85
+ "__compiled__",
86
+ "esm",
87
+ relative(sourceDir, entrypoint).replace(/\.[^.]+$/, "") + ".mjs"
88
+ );
89
+ } else {
90
+ return join(
91
+ "__compiled__",
92
+ "cjs",
93
+ relative(sourceDir, entrypoint).replace(/\.[^.]+$/, "") + ".js"
94
+ );
87
95
  }
88
- return noExt;
89
96
  }
90
97
  },
91
98
  rollupOptions: {
92
99
  external: depsValidator,
93
100
  output: {
101
+ preserveModulesRoot: sourceDir,
94
102
  exports: "named",
95
103
  preserveModules: true
96
104
  }
@@ -1,15 +1,15 @@
1
1
  import { relative } from "node:path";
2
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";
3
+ import { parsePackageJson } from "./packageJson.mjs";
4
+ import { writePackageJson } from "./writePackageJson.mjs";
5
+ import { buildVite } from "./buildVite.mjs";
6
+ import { resolveDirs } from "./resolveDirs.mjs";
7
+ import { createViteConfig } from "./createViteConfig.mjs";
8
+ import { copyStaticFilesTask } from "./tasks/copyStaticFilesTask.mjs";
9
+ import { buildTypesTask } from "./tasks/buildTypesTask/buildTypesTask.mjs";
10
+ import { BuildError } from "./error.mjs";
11
+ import { jsFilesTask } from "./tasks/jsFilesTask.mjs";
12
+ import { binsTask } from "./tasks/binsTask.mjs";
13
13
  function setExports(exportsMap, exportName, mapFn) {
14
14
  const entry = exportsMap.get(exportName) ?? {};
15
15
  exportsMap.set(exportName, mapFn(entry));
@@ -68,19 +68,21 @@ async function run(args) {
68
68
  });
69
69
  }
70
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
- });
71
+ jsFilesTask({ buildOutput: viteOutput, entrypoints, outDir }).then(
72
+ (res) => {
73
+ for (const [filePath, name] of res) {
74
+ setExports(exportsMap, name, (entry) => {
75
+ const format = filePath.endsWith(".js") ? "cjs" : "es";
76
+ if (format === "es") {
77
+ entry.mjs = "./" + filePath;
78
+ } else if (format === "cjs") {
79
+ entry.cjs = "./" + filePath;
80
+ }
81
+ return entry;
82
+ });
83
+ }
82
84
  }
83
- }),
85
+ ),
84
86
  binsTask({ outBinsDir, bins, buildOutput: viteOutput, outDir }).then(
85
87
  (res) => {
86
88
  for (const [value, key] of res) {
@@ -1,3 +1,3 @@
1
1
  import { args } from "./args.mjs";
2
- import { run } from "../src/index.mjs";
2
+ import { run } from "./index.mjs";
3
3
  run(args);
@@ -18,7 +18,7 @@ async function binsTask({
18
18
  if (el.facadeModuleId == null) {
19
19
  continue;
20
20
  }
21
- if (el.fileName.endsWith(".cjs")) {
21
+ if (el.fileName.endsWith(".js")) {
22
22
  continue;
23
23
  }
24
24
  const binsNames = reversedEntrypoints.get(el.facadeModuleId);
@@ -26,10 +26,7 @@ async function binsTask({
26
26
  continue;
27
27
  }
28
28
  for (const binName of binsNames) {
29
- const totalPath = relative(
30
- outBinsDir,
31
- join(outDir, el.fileName.replace(/js$/, "mjs"))
32
- );
29
+ const totalPath = relative(outBinsDir, join(outDir, el.fileName));
33
30
  const execPath = join(outBinsDir, `${binName}.js`);
34
31
  await writeFile(
35
32
  execPath,
@@ -37,13 +37,14 @@ async function callTypescript({
37
37
  const program = ts.createProgram(files, parsedCommandLine.options, host);
38
38
  program.emit(void 0, (fileName, data) => {
39
39
  const relativePath = path.relative(sourceDir, fileName);
40
- const finalPath = path.join(outDir, relativePath);
41
- const esmFinalPath = finalPath.replace(/\.d\.ts$/, ".d.mts");
42
40
  const sourceFileName = fileName.replace(/\.d\.ts$/, ".ts");
41
+ const finalEsmPath = path.join(outDir, "__compiled__", "esm", relativePath);
42
+ const esmFinalPath = finalEsmPath.replace(/\.d\.ts$/, ".d.mts");
43
43
  sourceToDtsMap.set(esmFinalPath, sourceFileName);
44
44
  fs.mkdirSync(path.dirname(esmFinalPath), { recursive: true });
45
45
  fs.writeFileSync(esmFinalPath, data);
46
- const cjsFinalPath = finalPath.replace(/\.d\.ts$/, ".d.ts");
46
+ const finalCjsPath = path.join(outDir, "__compiled__", "cjs", relativePath);
47
+ const cjsFinalPath = finalCjsPath.replace(/\.d\.ts$/, ".d.ts");
47
48
  fs.writeFileSync(cjsFinalPath, data);
48
49
  sourceToDtsMap.set(cjsFinalPath, sourceFileName);
49
50
  });
@@ -2,6 +2,7 @@ import { type Rollup } from "vite";
2
2
  type JsFilesTaskOption = {
3
3
  buildOutput: Rollup.OutputChunk[];
4
4
  entrypoints: Map<string, string>;
5
+ outDir: string;
5
6
  };
6
- export declare function jsFilesTask({ buildOutput, entrypoints, }: JsFilesTaskOption): Promise<Map<string, string>>;
7
+ export declare function jsFilesTask({ buildOutput, entrypoints, outDir, }: JsFilesTaskOption): Promise<Map<string, string>>;
7
8
  export {};
@@ -0,0 +1,57 @@
1
+ import "vite";
2
+ import { join, relative, dirname } from "node:path";
3
+ import { mkdir, writeFile } from "node:fs/promises";
4
+ function findCompiledPath(buildOutput, originalFile, type) {
5
+ for (const el of buildOutput) {
6
+ if (el.facadeModuleId === originalFile) {
7
+ if (type === "js" && el.fileName.endsWith(".js")) {
8
+ return {
9
+ path: el.fileName,
10
+ hasDefault: el.exports.includes("default")
11
+ };
12
+ }
13
+ if (type === "mjs" && el.fileName.endsWith(".mjs")) {
14
+ return {
15
+ path: el.fileName,
16
+ hasDefault: el.exports.includes("default")
17
+ };
18
+ }
19
+ }
20
+ }
21
+ return { path: "", hasDefault: false };
22
+ }
23
+ async function jsFilesTask({
24
+ buildOutput,
25
+ entrypoints,
26
+ outDir
27
+ }) {
28
+ const res = /* @__PURE__ */ new Map();
29
+ for (const [name, filePath] of entrypoints) {
30
+ const totalName = join(outDir, name);
31
+ const esmName = join(totalName, "index.mjs");
32
+ const esmCompiled = findCompiledPath(buildOutput, filePath, "mjs");
33
+ const esmPath = relative(totalName, join(outDir, esmCompiled.path));
34
+ await mkdir(dirname(esmName), { recursive: true });
35
+ let esmContent = `export * from "./${esmPath}";
36
+ `;
37
+ if (esmCompiled.hasDefault) {
38
+ esmContent += `import Default from "./${esmPath}";
39
+ export default Default;
40
+ `;
41
+ }
42
+ await writeFile(esmName, esmContent);
43
+ res.set(relative(outDir, join(dirname(esmName), esmPath)), name);
44
+ const cjsName = join(totalName, "index.js");
45
+ const cjsCompiled = findCompiledPath(buildOutput, filePath, "js");
46
+ const cjsPath = relative(totalName, join(outDir, cjsCompiled.path));
47
+ await mkdir(dirname(esmName), { recursive: true });
48
+ const cjsContent = `module.exports = require("./${cjsPath}");
49
+ `;
50
+ await writeFile(cjsName, cjsContent);
51
+ res.set(relative(outDir, join(dirname(cjsName), cjsPath)), name);
52
+ }
53
+ return res;
54
+ }
55
+ export {
56
+ jsFilesTask
57
+ };
package/index.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require("./__compiled__/cjs/src/index.js");
package/index.mjs ADDED
@@ -0,0 +1 @@
1
+ export * from "./__compiled__/esm/src/index.mjs";
package/package.json CHANGED
@@ -1,26 +1,26 @@
1
1
  {
2
2
  "name": "smartbundle",
3
3
  "type": "commonjs",
4
- "version": "0.8.1",
4
+ "version": "0.9.0",
5
5
  "bin": {
6
6
  "smartbundle": "__bin__/smartbundle.js"
7
7
  },
8
- "types": "./src/index.d.ts",
9
- "module": "./src/index.mjs",
10
- "main": "./src/index.js",
8
+ "types": "./__compiled__/cjs/src/index.d.ts",
9
+ "module": "./__compiled__/esm/src/index.mjs",
10
+ "main": "./__compiled__/cjs/src/index.js",
11
11
  "description": "zero-config bundler for npm packages",
12
12
  "exports": {
13
13
  ".": {
14
14
  "import": {
15
- "types": "./src/index.d.mts",
16
- "default": "./src/index.mjs"
15
+ "types": "./__compiled__/esm/src/index.d.mts",
16
+ "default": "./__compiled__/esm/src/index.mjs"
17
17
  },
18
18
  "require": {
19
- "types": "./src/index.d.ts",
20
- "default": "./src/index.js"
19
+ "types": "./__compiled__/cjs/src/index.d.ts",
20
+ "default": "./__compiled__/cjs/src/index.js"
21
21
  },
22
- "types": "./src/index.d.ts",
23
- "default": "./src/index.js"
22
+ "types": "./__compiled__/cjs/src/index.d.ts",
23
+ "default": "./__compiled__/cjs/src/index.js"
24
24
  },
25
25
  "./package.json": "./package.json"
26
26
  },
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- require("vite");
4
- const utils = require("./utils.js");
5
- async function jsFilesTask({
6
- buildOutput,
7
- entrypoints
8
- }) {
9
- const reversedEntrypoints = utils.reverseMap(entrypoints);
10
- const res = /* @__PURE__ */ new Map();
11
- for (const el of buildOutput) {
12
- if (el.facadeModuleId == null) {
13
- continue;
14
- }
15
- const exportPath = reversedEntrypoints.get(el.facadeModuleId);
16
- if (!exportPath) {
17
- continue;
18
- }
19
- for (const path of exportPath) {
20
- res.set(el.fileName, path);
21
- }
22
- }
23
- return res;
24
- }
25
- exports.jsFilesTask = jsFilesTask;
@@ -1,25 +0,0 @@
1
- import "vite";
2
- import { reverseMap } from "./utils.mjs";
3
- async function jsFilesTask({
4
- buildOutput,
5
- entrypoints
6
- }) {
7
- const reversedEntrypoints = reverseMap(entrypoints);
8
- const res = /* @__PURE__ */ new Map();
9
- for (const el of buildOutput) {
10
- if (el.facadeModuleId == null) {
11
- continue;
12
- }
13
- const exportPath = reversedEntrypoints.get(el.facadeModuleId);
14
- if (!exportPath) {
15
- continue;
16
- }
17
- for (const path of exportPath) {
18
- res.set(el.fileName, path);
19
- }
20
- }
21
- return res;
22
- }
23
- export {
24
- jsFilesTask
25
- };
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes