smartbundle 0.0.6 → 0.2.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.
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const yargs = require("yargs");
4
+ const process = require("node:process");
5
+ function _interopNamespaceDefault(e) {
6
+ const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
7
+ if (e) {
8
+ for (const k in e) {
9
+ if (k !== "default") {
10
+ const d = Object.getOwnPropertyDescriptor(e, k);
11
+ Object.defineProperty(n, k, d.get ? d : {
12
+ enumerable: true,
13
+ get: () => e[k]
14
+ });
15
+ }
16
+ }
17
+ }
18
+ n.default = e;
19
+ return Object.freeze(n);
20
+ }
21
+ const process__namespace = /* @__PURE__ */ _interopNamespaceDefault(process);
22
+ const args = yargs(process__namespace.argv.slice(2)).option("sourceDir", {
23
+ alias: "s",
24
+ type: "string",
25
+ describe: "path to the project directory. Default: current working directory"
26
+ }).option("packagePath", {
27
+ alias: "p",
28
+ type: "string",
29
+ describe: "path to the package.json. Default: cwd()/package.json"
30
+ }).option("outputDir", {
31
+ alias: "o",
32
+ type: "string",
33
+ describe: "path to the output directory. Default: cwd()/dist"
34
+ }).help("help").parseSync();
35
+ exports.args = args;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const errors = {
4
+ exportsRequired: "The `exports` field is string. Please, verify the value. More info: https://nodejs.org/api/packages.html#package-entry-points",
5
+ exportsInvalid: "The `exports` field must be a path to entrypoint. Please, verify the value. More info: https://nodejs.org/api/packages.html#package-entry-points",
6
+ nameRequired: "The `name` field is required string. Please, verify the value. More info: https://docs.npmjs.com/cli/v10/configuring-npm/package-json#name",
7
+ nameMinLength: 'Min length of "name" is 1 character. More info: https://docs.npmjs.com/cli/v10/configuring-npm/package-json#name',
8
+ nameMaxLength: 'Max length of "name" is 214 characters. More info: https://docs.npmjs.com/cli/v10/configuring-npm/package-json#name',
9
+ nameStartsIllegalChars: "Name cannot start with `_` or `.` if it is not a scoped package. More info: https://docs.npmjs.com/cli/v10/configuring-npm/package-json#name",
10
+ versionRequired: "The `version` field is required string. Please, verify the value. More info: https://docs.npmjs.com/cli/v10/configuring-npm/package-json#version",
11
+ privateIsTrue: "The `private` field must be `true` for avoiding accidental publish. More info: https://docs.npmjs.com/cli/v10/configuring-npm/package-json#private",
12
+ descriptionString: "The `description` field must be a string. Please, verify the value. More info: https://docs.npmjs.com/cli/v10/configuring-npm/package-json#description",
13
+ dependenciesInvalid: "The `dependencies` field must be an Object<string, string>. Please, verify the value. More info: https://docs.npmjs.com/cli/v10/configuring-npm/package-json#dependencies",
14
+ binString: "The `bin` field must be a string. Please, verify the value. More info: https://docs.npmjs.com/cli/v10/configuring-npm/package-json#bin",
15
+ rollupError: "An error occurred while building the package. Please, report it to the issues on GitHub"
16
+ };
17
+ exports.errors = errors;
@@ -0,0 +1,174 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const node_path = require("node:path");
4
+ const fs = require("node:fs/promises");
5
+ const packageJson = require("./packageJson.cjs");
6
+ const vite = require("vite");
7
+ const writePackageJson = require("./writePackageJson.cjs");
8
+ const rollupts = require("@rollup/plugin-typescript");
9
+ const errors = require("./errors.cjs");
10
+ function myResolve(path1, path2) {
11
+ if (node_path.isAbsolute(path2)) {
12
+ return path2;
13
+ }
14
+ return node_path.join(path1, path2);
15
+ }
16
+ function mapToObject(map) {
17
+ const obj = {};
18
+ for (const [key, value] of map) {
19
+ obj[key] = value;
20
+ }
21
+ return obj;
22
+ }
23
+ function reverseMap(map) {
24
+ const reversed = /* @__PURE__ */ new Map();
25
+ for (const [key, value] of map) {
26
+ const noExtValue = value.replace(/\.[^.]+$/, "");
27
+ const arr = reversed.get(noExtValue) ?? [];
28
+ arr.push(key);
29
+ reversed.set(noExtValue, arr);
30
+ }
31
+ return reversed;
32
+ }
33
+ function setExports(exportsMap, exportName, mapFn) {
34
+ const entry = exportsMap.get(exportName) ?? {};
35
+ exportsMap.set(exportName, mapFn(entry));
36
+ }
37
+ async function run(args) {
38
+ var _a;
39
+ const sourceDir = myResolve(process.cwd(), args.sourceDir ?? ".");
40
+ const packagePath = myResolve(sourceDir, args.packagePath ?? "./package.json");
41
+ const outDir = myResolve(process.cwd(), args.outputDir ?? "./dist");
42
+ await fs.rm(outDir, { recursive: true, force: true });
43
+ await fs.mkdir(outDir, { recursive: true });
44
+ const packageJson$1 = await packageJson.parsePackageJson({ sourceDir, packagePath });
45
+ if (Array.isArray(packageJson$1)) {
46
+ console.log(packageJson$1);
47
+ return { error: true, errors: packageJson$1 };
48
+ }
49
+ const entrypoints = /* @__PURE__ */ new Map();
50
+ if (packageJson$1.exports) {
51
+ const mainEntry = node_path.join(sourceDir, packageJson$1.exports);
52
+ entrypoints.set(".", mainEntry);
53
+ }
54
+ if (packageJson$1.bin) {
55
+ const binEntry = node_path.join(sourceDir, packageJson$1.bin);
56
+ entrypoints.set("__bin__", binEntry);
57
+ }
58
+ const hasTs = [...entrypoints.values()].some((entry) => entry.endsWith(".ts"));
59
+ const typescript = hasTs ? (
60
+ // @ts-expect-error
61
+ rollupts({
62
+ compilerOptions: {
63
+ rootDir: sourceDir,
64
+ declaration: true,
65
+ emitDeclarationOnly: true,
66
+ declarationDir: outDir
67
+ }
68
+ })
69
+ ) : false;
70
+ const outputs = await vite.build({
71
+ publicDir: false,
72
+ build: {
73
+ outDir,
74
+ write: true,
75
+ minify: false,
76
+ emptyOutDir: true,
77
+ assetsInlineLimit: 0,
78
+ terserOptions: {
79
+ compress: false,
80
+ mangle: false
81
+ },
82
+ lib: {
83
+ entry: mapToObject(entrypoints),
84
+ formats: ["es", "cjs"],
85
+ fileName: (format, entryName) => {
86
+ const entrypoint = entrypoints.get(entryName);
87
+ if (!entrypoint) {
88
+ const noExt2 = entryName.replace(/\.[^.]+$/, "");
89
+ return "__do_not_import_directly__/" + noExt2 + (format === "es" ? ".js" : ".cjs");
90
+ }
91
+ const relativePath = node_path.relative(sourceDir, entrypoint);
92
+ const noExt = relativePath.replace(/\.[^.]+$/, "");
93
+ if (format === "es") {
94
+ return `${noExt}.js`;
95
+ }
96
+ if (format === "cjs") {
97
+ return `${noExt}.cjs`;
98
+ }
99
+ return noExt;
100
+ }
101
+ },
102
+ rollupOptions: {
103
+ plugins: [typescript],
104
+ external: (id, parentId, isResolved) => {
105
+ if (id === packageJson$1.name) {
106
+ return true;
107
+ }
108
+ if (id.startsWith("node:")) {
109
+ return true;
110
+ }
111
+ if (packageJson$1.dependencies) {
112
+ return id in packageJson$1.dependencies;
113
+ }
114
+ return false;
115
+ },
116
+ output: {
117
+ preserveModules: true
118
+ }
119
+ }
120
+ }
121
+ });
122
+ if (!Array.isArray(outputs)) {
123
+ return { error: true, errors: [errors.errors.rollupError] };
124
+ }
125
+ const exportsMap = /* @__PURE__ */ new Map();
126
+ const reversedEntrypoints = reverseMap(entrypoints);
127
+ for (const { output } of outputs) {
128
+ for (const el of output) {
129
+ switch (el.type) {
130
+ case "chunk":
131
+ const noExtPath = (_a = el.facadeModuleId) == null ? void 0 : _a.replace(/\.[^.]+$/, "");
132
+ if (noExtPath == null) {
133
+ continue;
134
+ }
135
+ const exportPath = reversedEntrypoints.get(noExtPath);
136
+ if (!exportPath) {
137
+ continue;
138
+ }
139
+ for (const path of exportPath) {
140
+ setExports(exportsMap, path, (entry) => {
141
+ const format = el.fileName.endsWith(".cjs") ? "cjs" : "es";
142
+ if (format === "es") {
143
+ entry.mjs = "./" + el.fileName;
144
+ } else if (format === "cjs") {
145
+ entry.cjs = "./" + el.fileName;
146
+ }
147
+ return entry;
148
+ });
149
+ }
150
+ break;
151
+ case "asset":
152
+ if (el.fileName.endsWith(".d.ts")) {
153
+ const noExtPath2 = node_path.join(sourceDir, el.fileName.replace(/\.d\.ts$/, ""));
154
+ const exportPath2 = reversedEntrypoints.get(noExtPath2);
155
+ if (!exportPath2) {
156
+ continue;
157
+ }
158
+ for (const path of exportPath2) {
159
+ setExports(exportsMap, path, (entry) => {
160
+ entry.mdts = "./" + el.fileName;
161
+ return entry;
162
+ });
163
+ }
164
+ }
165
+ break;
166
+ }
167
+ }
168
+ }
169
+ await writePackageJson.writePackageJson(outDir, packageJson$1, {
170
+ exportsMap
171
+ });
172
+ return { error: false };
173
+ }
174
+ exports.run = run;
@@ -1,10 +1,10 @@
1
1
  import { join, relative, isAbsolute } from "node:path";
2
2
  import { rm, mkdir } from "node:fs/promises";
3
- import { parsePackageJson } from "./packageJson.mjs";
3
+ import { parsePackageJson } from "./packageJson.js";
4
4
  import { build } from "vite";
5
- import { writePackageJson } from "./writePackageJson.mjs";
5
+ import { writePackageJson } from "./writePackageJson.js";
6
6
  import rollupts from "@rollup/plugin-typescript";
7
- import { errors } from "./errors.mjs";
7
+ import { errors } from "./errors.js";
8
8
  function myResolve(path1, path2) {
9
9
  if (isAbsolute(path2)) {
10
10
  return path2;
@@ -58,7 +58,9 @@ async function run(args) {
58
58
  // @ts-expect-error
59
59
  rollupts({
60
60
  compilerOptions: {
61
+ rootDir: sourceDir,
61
62
  declaration: true,
63
+ emitDeclarationOnly: true,
62
64
  declarationDir: outDir
63
65
  }
64
66
  })
@@ -77,17 +79,20 @@ async function run(args) {
77
79
  },
78
80
  lib: {
79
81
  entry: mapToObject(entrypoints),
80
- formats: ["es"],
82
+ formats: ["es", "cjs"],
81
83
  fileName: (format, entryName) => {
82
84
  const entrypoint = entrypoints.get(entryName);
83
85
  if (!entrypoint) {
84
86
  const noExt2 = entryName.replace(/\.[^.]+$/, "");
85
- return "__do_not_import_directly__/" + noExt2 + ".mjs";
87
+ return "__do_not_import_directly__/" + noExt2 + (format === "es" ? ".js" : ".cjs");
86
88
  }
87
89
  const relativePath = relative(sourceDir, entrypoint);
88
90
  const noExt = relativePath.replace(/\.[^.]+$/, "");
89
91
  if (format === "es") {
90
- return `${noExt}.mjs`;
92
+ return `${noExt}.js`;
93
+ }
94
+ if (format === "cjs") {
95
+ return `${noExt}.cjs`;
91
96
  }
92
97
  return noExt;
93
98
  }
@@ -131,7 +136,12 @@ async function run(args) {
131
136
  }
132
137
  for (const path of exportPath) {
133
138
  setExports(exportsMap, path, (entry) => {
134
- entry.mjs = "./" + el.fileName;
139
+ const format = el.fileName.endsWith(".cjs") ? "cjs" : "es";
140
+ if (format === "es") {
141
+ entry.mjs = "./" + el.fileName;
142
+ } else if (format === "cjs") {
143
+ entry.cjs = "./" + el.fileName;
144
+ }
135
145
  return entry;
136
146
  });
137
147
  }
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const fs = require("node:fs/promises");
4
+ const z = require("zod");
5
+ const errors = require("./errors.cjs");
6
+ const node_path = require("node:path");
7
+ function _interopNamespaceDefault(e) {
8
+ const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
9
+ if (e) {
10
+ for (const k in e) {
11
+ if (k !== "default") {
12
+ const d = Object.getOwnPropertyDescriptor(e, k);
13
+ Object.defineProperty(n, k, d.get ? d : {
14
+ enumerable: true,
15
+ get: () => e[k]
16
+ });
17
+ }
18
+ }
19
+ }
20
+ n.default = e;
21
+ return Object.freeze(n);
22
+ }
23
+ const fs__namespace = /* @__PURE__ */ _interopNamespaceDefault(fs);
24
+ async function fileExists(filePath) {
25
+ try {
26
+ const stats = await fs__namespace.stat(filePath);
27
+ return stats.isFile();
28
+ } catch (error) {
29
+ return false;
30
+ }
31
+ }
32
+ function dependencies(errorText) {
33
+ return z.record(z.string({ message: errorText }), { message: errorText }).optional();
34
+ }
35
+ function createPackageJsonSchema(sourceDir) {
36
+ return z.object({
37
+ exports: z.string({ message: errors.errors.exportsRequired }).refine((exports2) => {
38
+ const mainFinalPath = node_path.join(sourceDir, exports2);
39
+ return fileExists(mainFinalPath);
40
+ }, errors.errors.exportsInvalid).optional(),
41
+ name: z.string({ message: errors.errors.nameRequired }).min(1, errors.errors.nameMinLength).max(214, errors.errors.nameMaxLength).refine((name) => ["_", "."].every((start) => !name.startsWith(start)), errors.errors.nameStartsIllegalChars),
42
+ version: z.string({ message: errors.errors.versionRequired }),
43
+ private: z.boolean({ message: errors.errors.privateIsTrue }).refine((value) => value, errors.errors.privateIsTrue),
44
+ description: z.string({ message: errors.errors.descriptionString }).optional(),
45
+ dependencies: dependencies(errors.errors.dependenciesInvalid),
46
+ bin: z.string({ message: errors.errors.binString }).optional()
47
+ });
48
+ }
49
+ async function parsePackageJson({ sourceDir, packagePath }) {
50
+ const packageString = await fs__namespace.readFile(packagePath, "utf-8");
51
+ const rawJson = JSON.parse(packageString);
52
+ const packageJsonSchema = createPackageJsonSchema(sourceDir);
53
+ const packageJson = await packageJsonSchema.safeParseAsync(rawJson);
54
+ if (!packageJson.success) {
55
+ return packageJson.error.errors.map((error) => error.message);
56
+ }
57
+ return packageJson.data;
58
+ }
59
+ exports.parsePackageJson = parsePackageJson;
@@ -1,6 +1,6 @@
1
1
  import * as fs from "node:fs/promises";
2
2
  import z from "zod";
3
- import { errors } from "./errors.mjs";
3
+ import { errors } from "./errors.js";
4
4
  import { join } from "node:path";
5
5
  async function fileExists(filePath) {
6
6
  try {
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const fs = require("node:fs/promises");
4
+ async function writePackageJson(outDir, parsed, { exportsMap }) {
5
+ var _a, _b, _c;
6
+ const allExports = {};
7
+ for (const [key, value] of exportsMap) {
8
+ if (key === "__bin__") {
9
+ continue;
10
+ }
11
+ const anExport = {};
12
+ if (value.mjs || value.mdts) {
13
+ anExport.import = {
14
+ types: value.mdts,
15
+ default: value.mjs
16
+ };
17
+ }
18
+ if (value.cjs || value.cdts) {
19
+ anExport.require = {
20
+ types: value.cdts,
21
+ default: value.cjs
22
+ };
23
+ }
24
+ anExport.types = value.mdts;
25
+ anExport.default = value.mjs;
26
+ allExports[key] = anExport;
27
+ }
28
+ const res = {
29
+ name: parsed.name,
30
+ type: "module",
31
+ version: parsed.version,
32
+ bin: (_a = exportsMap.get("__bin__")) == null ? void 0 : _a.mjs,
33
+ types: (_b = allExports["."]) == null ? void 0 : _b.types,
34
+ module: (_c = allExports["."]) == null ? void 0 : _c.default,
35
+ description: parsed.description ?? "",
36
+ exports: allExports,
37
+ dependencies: parsed.dependencies ?? void 0
38
+ };
39
+ await fs.writeFile(`${outDir}/package.json`, JSON.stringify(res, null, 2));
40
+ }
41
+ exports.writePackageJson = writePackageJson;
@@ -6,15 +6,20 @@ async function writePackageJson(outDir, parsed, { exportsMap }) {
6
6
  if (key === "__bin__") {
7
7
  continue;
8
8
  }
9
- const anExport = {
10
- types: value.mdts
11
- };
9
+ const anExport = {};
12
10
  if (value.mjs || value.mdts) {
13
11
  anExport.import = {
14
12
  types: value.mdts,
15
13
  default: value.mjs
16
14
  };
17
15
  }
16
+ if (value.cjs || value.cdts) {
17
+ anExport.require = {
18
+ types: value.cdts,
19
+ default: value.cjs
20
+ };
21
+ }
22
+ anExport.types = value.mdts;
18
23
  anExport.default = value.mjs;
19
24
  allExports[key] = anExport;
20
25
  }
package/package.json CHANGED
@@ -1,19 +1,22 @@
1
1
  {
2
2
  "name": "smartbundle",
3
3
  "type": "module",
4
- "version": "0.0.6",
5
- "bin": "./src/bin.mjs",
4
+ "version": "0.2.0-alpha.0",
5
+ "bin": "./src/bin.js",
6
6
  "types": "./src/run.d.ts",
7
- "module": "./src/run.mjs",
7
+ "module": "./src/run.js",
8
8
  "description": "",
9
9
  "exports": {
10
10
  ".": {
11
- "types": "./src/run.d.ts",
12
11
  "import": {
13
12
  "types": "./src/run.d.ts",
14
- "default": "./src/run.mjs"
13
+ "default": "./src/run.js"
14
+ },
15
+ "require": {
16
+ "default": "./src/run.cjs"
15
17
  },
16
- "default": "./src/run.mjs"
18
+ "types": "./src/run.d.ts",
19
+ "default": "./src/run.js"
17
20
  }
18
21
  },
19
22
  "dependencies": {
package/src/bin.cjs ADDED
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
18
+ // If the importer is in node compatibility mode or this is not an ESM
19
+ // file that has been converted to a CommonJS file using a Babel-
20
+ // compatible transform (i.e. "__esModule" has not been set), then set
21
+ // "default" to the CommonJS "module.exports" for node compatibility.
22
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
23
+ mod
24
+ ));
25
+ import("smartbundle");
package/src/bin.js ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ import("smartbundle");
@@ -0,0 +1,2 @@
1
+ declare const _default: 1;
2
+ export default _default;
package/src/run.cjs ADDED
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ const args = require("../__do_not_import_directly__/args.cjs");
3
+ const index = require("../__do_not_import_directly__/index.cjs");
4
+ index.run(args.args);
package/src/run.d.ts CHANGED
@@ -1 +1 @@
1
- export declare function main(): void;
1
+ export {};
package/src/run.js ADDED
@@ -0,0 +1,3 @@
1
+ import { args } from "../__do_not_import_directly__/args.js";
2
+ import { run } from "../__do_not_import_directly__/index.js";
3
+ run(args);
@@ -2,6 +2,8 @@ import { PackageJson } from "./packageJson.js";
2
2
  export type ExportsObject = {
3
3
  mjs?: string;
4
4
  mdts?: string;
5
+ cjs?: string;
6
+ cdts?: string;
5
7
  };
6
8
  type BuildResult = {
7
9
  exportsMap: Map<string, ExportsObject>;
package/src/bin.mjs DELETED
@@ -1,2 +0,0 @@
1
- #!/usr/bin/env node
2
- import("smartbundle").then(({ main }) => main());
package/src/run.mjs DELETED
@@ -1,6 +0,0 @@
1
- import { args } from "../__do_not_import_directly__/args.mjs";
2
- import { run } from "../__do_not_import_directly__/index.mjs";
3
- function main() {
4
- run(args);
5
- }
6
- export { main };