makepack 1.7.18 → 1.7.20

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "makepack",
3
- "version": "1.7.18",
3
+ "version": "1.7.20",
4
4
  "type": "module",
5
5
  "description": "A CLI tool to create, build, and manage JavaScript, TypeScript, React, and React-TypeScript libraries for npm projects.",
6
6
  "categories": [
@@ -0,0 +1,6 @@
1
+ export default {
2
+ input: {
3
+ hello: './src/test/hello.ts',
4
+ mod: './src/mod/info/test.ts',
5
+ },
6
+ };
@@ -6,49 +6,17 @@ import json from "@rollup/plugin-json";
6
6
  import terser from "@rollup/plugin-terser";
7
7
  import path from "path";
8
8
  import fs from "fs/promises";
9
- import ts from "typescript";
10
9
  import { loadRollupConfig, loadViteConfig } from "../../helpers.js";
11
-
12
- const MAX_DIR_CONCURRENCY = 16;
10
+ import dts from "rollup-plugin-dts";
13
11
  const MAX_FILE_COPY_CONCURRENCY = 32;
14
12
 
13
+ // --------------------- Helpers ---------------------
15
14
  function isCodeFile(filename) {
16
15
  return /\.(ts|tsx|js|jsx|cjs|mjs)$/i.test(filename);
17
16
  }
18
17
 
19
18
  function isSkippedDir(name) {
20
- return name === "node_modules" || name === ".git" || name === ".next";
21
- }
22
-
23
- async function getEntriesBatch(root) {
24
- const entries = {};
25
- const dirs = [root];
26
-
27
- async function worker() {
28
- while (dirs.length) {
29
- const dir = dirs.shift();
30
- const items = await fs.readdir(dir, { withFileTypes: true });
31
-
32
- for (const item of items) {
33
- const full = path.join(dir, item.name);
34
- if (item.isDirectory()) dirs.push(full);
35
- else if (isCodeFile(item.name) && !item.name.endsWith(".d.ts")) {
36
- // Skip type-only files heuristically
37
- const content = await fs.readFile(full, "utf-8");
38
- const typeOnly = /^\s*(export\s+)?(type|interface|enum|declare)/m.test(content);
39
- if (!typeOnly) {
40
- const name = path.relative(root, full).replace(/\.(ts|tsx|js|jsx)$/, "");
41
- entries[name] = full;
42
- }
43
- }
44
- }
45
- }
46
- }
47
-
48
- const workers = Array.from({ length: MAX_DIR_CONCURRENCY }, () => worker());
49
- await Promise.all(workers);
50
-
51
- return entries;
19
+ return ["node_modules", ".git", ".next"].includes(name);
52
20
  }
53
21
 
54
22
  // --------------------- Parallel asset copy ---------------------
@@ -60,7 +28,7 @@ async function copyAssetsBatched(rootdir, outdir) {
60
28
  for (const item of items) {
61
29
  const full = path.join(dir, item.name);
62
30
  const rel = path.relative(rootdir, full);
63
- if (rel.split(path.sep).some(p => isSkippedDir(p))) continue;
31
+ if (rel.split(path.sep).some(isSkippedDir)) continue;
64
32
 
65
33
  if (item.isDirectory()) await walk(full);
66
34
  else if (!isCodeFile(item.name)) queue.push({ src: full, rel });
@@ -82,47 +50,55 @@ async function copyAssetsBatched(rootdir, outdir) {
82
50
  await Promise.all(workers);
83
51
  }
84
52
 
85
- // --------------------- Generate .d.ts programmatically ---------------------
86
- async function generateDeclarations(rootDir, outDir) {
87
- const tsFiles = [];
53
+ /**
54
+ * Normalize rollup input (string | array | object)
55
+ * returns [{ entry, outdir }]
56
+ */
57
+ function mapEntriesToOutdirs(entries, rootdir, baseOutdir) {
58
+ const absRoot = path.resolve(process.cwd(), rootdir);
59
+ const absOut = path.resolve(process.cwd(), baseOutdir);
88
60
 
89
- async function walk(dir) {
90
- const items = await fs.readdir(dir, { withFileTypes: true });
91
- for (const item of items) {
92
- const full = path.join(dir, item.name);
93
- if (item.isDirectory()) await walk(full);
94
- else if (/\.(ts|tsx)$/.test(item.name)) tsFiles.push(full);
95
- }
61
+ /** normalize to array of absolute entry paths */
62
+ let entryList = [];
63
+
64
+ // string
65
+ if (typeof entries === "string") {
66
+ entryList = [entries];
67
+ }
68
+
69
+ // array
70
+ else if (Array.isArray(entries)) {
71
+ entryList = entries;
96
72
  }
97
73
 
98
- await walk(rootDir);
74
+ // object { name: path }
75
+ else if (entries && typeof entries === "object") {
76
+ entryList = Object.values(entries);
77
+ }
99
78
 
100
- if (!tsFiles.length) return;
79
+ return entryList.map(entry => {
80
+ const absEntry = path.isAbsolute(entry)
81
+ ? entry
82
+ : path.resolve(process.cwd(), entry);
101
83
 
102
- const options = {
103
- declaration: true,
104
- emitDeclarationOnly: true,
105
- outDir: outDir,
106
- rootDir: rootDir,
107
- moduleResolution: ts.ModuleResolutionKind.NodeJs,
108
- target: ts.ScriptTarget.ES2017,
109
- module: ts.ModuleKind.ESNext,
110
- esModuleInterop: true,
111
- skipLibCheck: true,
112
- };
84
+ // relative path from rootdir (e.g. anydir/entry.ts)
85
+ const rel = path.relative(absRoot, absEntry);
113
86
 
114
- const program = ts.createProgram(tsFiles, options);
115
- program.emit();
87
+ // directory only (e.g. anydir)
88
+ const subdir = path.dirname(rel);
89
+
90
+ return {
91
+ entry: absEntry,
92
+ outdir: subdir === "." ? absOut : path.join(absOut, subdir),
93
+ };
94
+ });
116
95
  }
117
96
 
118
- // --------------------- Main bundler ---------------------
119
- async function bundler(args, spinner) {
97
+ // --------------------- Main Bundler ---------------------
98
+ async function bundler(args, spinner, child = false) {
120
99
  const rootdir = args.rootdir;
121
100
  const outdir = args.outdir;
122
101
 
123
- const entries = await getEntriesBatch(rootdir);
124
- const isTs = Object.values(entries).some(f => f.endsWith(".ts") || f.endsWith(".tsx"));
125
-
126
102
  const viteConfig = await loadViteConfig();
127
103
  const rollupConfig = await loadRollupConfig();
128
104
  const viteRollupConfig = viteConfig?.build?.rollupOptions || {};
@@ -130,7 +106,7 @@ async function bundler(args, spinner) {
130
106
 
131
107
  const config = {
132
108
  ...rollupConfig,
133
- input: { ...entries },
109
+ input: args.entry,
134
110
  external: id => {
135
111
  if (rollupConfig && typeof rollupConfig.external === "function") {
136
112
  if (rollupConfig.external(id)) return true;
@@ -166,6 +142,8 @@ async function bundler(args, spinner) {
166
142
 
167
143
  const bundle = await rollup(config);
168
144
 
145
+
146
+
169
147
  // --------------------- Output formats ---------------------
170
148
  const outputs = [];
171
149
  if (!args.format || args.format === "both") {
@@ -175,7 +153,7 @@ async function bundler(args, spinner) {
175
153
  sourcemap: args.sourcemap,
176
154
  preserveModules: true,
177
155
  preserveModulesRoot: rootdir,
178
- entryFileNames: "[name].mjs"
156
+ entryFileNames: "[name].mjs",
179
157
  });
180
158
  outputs.push({
181
159
  dir: outdir,
@@ -183,7 +161,7 @@ async function bundler(args, spinner) {
183
161
  sourcemap: args.sourcemap,
184
162
  preserveModules: true,
185
163
  preserveModulesRoot: rootdir,
186
- entryFileNames: "[name].cjs"
164
+ entryFileNames: "[name].cjs",
187
165
  });
188
166
  } else if (args.format === "esm" || args.format === "cjs") {
189
167
  outputs.push({
@@ -192,7 +170,7 @@ async function bundler(args, spinner) {
192
170
  sourcemap: args.sourcemap,
193
171
  preserveModules: true,
194
172
  preserveModulesRoot: rootdir,
195
- entryFileNames: args.format === "esm" ? "[name].mjs" : "[name].cjs"
173
+ entryFileNames: args.format === "esm" ? "[name].mjs" : "[name].cjs",
196
174
  });
197
175
  } else if (args.format === "iife" || args.format === "umd") {
198
176
  outputs.push({
@@ -200,7 +178,7 @@ async function bundler(args, spinner) {
200
178
  format: args.format,
201
179
  name: args.name || "Bundle",
202
180
  sourcemap: args.sourcemap,
203
- entryFileNames: "[name].js"
181
+ entryFileNames: "[name].js",
204
182
  });
205
183
  }
206
184
 
@@ -210,13 +188,42 @@ async function bundler(args, spinner) {
210
188
 
211
189
  await bundle.close();
212
190
 
191
+
192
+ if (!child && rollupConfig && rollupConfig.input) {
193
+ const mapentries = mapEntriesToOutdirs(rollupConfig.input, rootdir, outdir)
194
+ if (mapentries.length > 1) {
195
+ spinner.text = `📦 Bundling ${mapentries.length} entries...`;
196
+ }
197
+
198
+ for (const { entry } of mapentries) {
199
+ await bundler({
200
+ ...args,
201
+ entry,
202
+ outdir,
203
+ }, spinner, true);
204
+ }
205
+
206
+ }
207
+
213
208
  // --------------------- Copy assets ---------------------
214
- await copyAssetsBatched(rootdir, outdir);
209
+ if (!child) {
210
+ spinner.text = "📁 Copying non-code assets...";
211
+ await copyAssetsBatched(rootdir, outdir);
212
+ }
215
213
 
216
- // --------------------- Generate TypeScript declarations ---------------------
217
- if (isTs && args.declaration) {
218
- spinner.text = "📄 Generating TypeScript declarations programmatically...";
219
- await generateDeclarations(rootdir, outdir);
214
+ if (args.declaration) {
215
+ spinner.text = "Generating TypeScript declarations..."
216
+ const bundlets = await rollup({
217
+ ...config,
218
+ plugins: [dts()],
219
+ });
220
+ await bundlets.write({
221
+ format: "esm",
222
+ preserveModules: true,
223
+ preserveModulesRoot: args.rootdir,
224
+ dir: path.join(args.outdir),
225
+ });
226
+ await bundlets.close();
220
227
  }
221
228
  }
222
229
 
@@ -20,6 +20,23 @@ const build = async (args) => {
20
20
 
21
21
  const outdir = path.join(process.cwd(), '.mpack');
22
22
  const rootdir = path.join(process.cwd(), 'src');
23
+ let entry = '';
24
+ let entryts = path.join(rootdir, 'index.ts');
25
+ let entryjs = path.join(rootdir, 'index.js');
26
+ let entrytsx = path.join(rootdir, 'index.tsx');
27
+ let entryjsx = path.join(rootdir, 'index.jsx');
28
+
29
+ if (fs.existsSync(entryts)) {
30
+ entry = "index.ts";
31
+ } else if (fs.existsSync(entryjs)) {
32
+ entry = "index.js";
33
+ } else if (fs.existsSync(entrytsx)) {
34
+ entry = "index.tsx";
35
+ } else if (fs.existsSync(entryjsx)) {
36
+ entry = "index.jsx";
37
+ } else {
38
+ throw new Error("No entry file found in src directory. Please provide an index.ts or index.js file.");
39
+ }
23
40
 
24
41
  args = {
25
42
  format: args.format || 'both',
@@ -29,6 +46,7 @@ const build = async (args) => {
29
46
  declaration: beBool('declaration'),
30
47
  outdir,
31
48
  rootdir,
49
+ entry: path.join(rootdir, entry),
32
50
  };
33
51
 
34
52
  const spinner = ora('✨ Building your package...\n').start();
@@ -49,6 +67,12 @@ const build = async (args) => {
49
67
  const pkgjson = await fs.readJson(pkgPath);
50
68
  delete pkgjson.scripts;
51
69
  delete pkgjson.type;
70
+ delete pkgjson.devDependencies;
71
+ delete pkgjson.jest;
72
+ delete pkgjson.prettier;
73
+ delete pkgjson.eslintConfig;
74
+ delete pkgjson.vite;
75
+ delete pkgjson.rollup;
52
76
  await fs.writeJson(path.join(outdir, 'package.json'), pkgjson, { spaces: 2 });
53
77
  } else {
54
78
  spinner.fail(concolor.red('package.json not found!'));
package/src/helpers.js CHANGED
@@ -1,4 +1,8 @@
1
1
  import child_process from 'child_process'
2
+ import fs from 'fs/promises';
3
+ import path from 'path';
4
+ import { pathToFileURL } from 'url';
5
+
2
6
 
3
7
  export const execSync = (command, option = {}) => {
4
8
  try {
@@ -53,9 +57,6 @@ export const logger = {
53
57
  }
54
58
  };
55
59
 
56
- import fs from 'fs/promises';
57
- import path from 'path';
58
- import { pathToFileURL } from 'url';
59
60
 
60
61
  /**
61
62
  * Load full Vite config object from root
package/src/index.js CHANGED
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env node
1
+ #!/usr/bin/env node
2
2
 
3
3
  import { Command } from "commander";
4
4
  import start from "./actions/start/index.js";