@knapsack/package-bundler 4.101.0--canary.8110.15bba7c.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 (67) hide show
  1. package/.lintstagedrc.mjs +11 -0
  2. package/INSTRUCTIONS-FOR-CUSTOMERS.md +130 -0
  3. package/dist/bundle.d.ts +41 -0
  4. package/dist/bundle.d.ts.map +1 -0
  5. package/dist/bundle.js +348 -0
  6. package/dist/bundle.js.map +1 -0
  7. package/dist/cli.d.ts +6 -0
  8. package/dist/cli.d.ts.map +1 -0
  9. package/dist/cli.js +165 -0
  10. package/dist/cli.js.map +1 -0
  11. package/dist/errors.d.ts +63 -0
  12. package/dist/errors.d.ts.map +1 -0
  13. package/dist/errors.js +155 -0
  14. package/dist/errors.js.map +1 -0
  15. package/dist/help-text.d.ts +16 -0
  16. package/dist/help-text.d.ts.map +1 -0
  17. package/dist/help-text.js +180 -0
  18. package/dist/help-text.js.map +1 -0
  19. package/dist/index.d.ts +20 -0
  20. package/dist/index.d.ts.map +1 -0
  21. package/dist/index.js +20 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/log.d.ts +42 -0
  24. package/dist/log.d.ts.map +1 -0
  25. package/dist/log.js +114 -0
  26. package/dist/log.js.map +1 -0
  27. package/dist/metadata.d.ts +112 -0
  28. package/dist/metadata.d.ts.map +1 -0
  29. package/dist/metadata.js +204 -0
  30. package/dist/metadata.js.map +1 -0
  31. package/dist/prompts.d.ts +54 -0
  32. package/dist/prompts.d.ts.map +1 -0
  33. package/dist/prompts.js +125 -0
  34. package/dist/prompts.js.map +1 -0
  35. package/dist/resolve-entries.d.ts +56 -0
  36. package/dist/resolve-entries.d.ts.map +1 -0
  37. package/dist/resolve-entries.js +333 -0
  38. package/dist/resolve-entries.js.map +1 -0
  39. package/dist/resolve-source.d.ts +53 -0
  40. package/dist/resolve-source.d.ts.map +1 -0
  41. package/dist/resolve-source.js +174 -0
  42. package/dist/resolve-source.js.map +1 -0
  43. package/dist/run.d.ts +52 -0
  44. package/dist/run.d.ts.map +1 -0
  45. package/dist/run.js +276 -0
  46. package/dist/run.js.map +1 -0
  47. package/dist/types.d.ts +67 -0
  48. package/dist/types.d.ts.map +1 -0
  49. package/dist/types.js +62 -0
  50. package/dist/types.js.map +1 -0
  51. package/dist/vendor.d.ts +59 -0
  52. package/dist/vendor.d.ts.map +1 -0
  53. package/dist/vendor.js +134 -0
  54. package/dist/vendor.js.map +1 -0
  55. package/dist/verify-bundle.d.ts +59 -0
  56. package/dist/verify-bundle.d.ts.map +1 -0
  57. package/dist/verify-bundle.js +238 -0
  58. package/dist/verify-bundle.js.map +1 -0
  59. package/dist/write-output.d.ts +82 -0
  60. package/dist/write-output.d.ts.map +1 -0
  61. package/dist/write-output.js +214 -0
  62. package/dist/write-output.js.map +1 -0
  63. package/package.json +53 -0
  64. package/readme.md +127 -0
  65. package/tsconfig.json +9 -0
  66. package/tsconfig.test.json +13 -0
  67. package/vitest.config.ts +9 -0
@@ -0,0 +1,125 @@
1
+ /**
2
+ * Interactive question flow, and the validation shared with the non-interactive path.
3
+ *
4
+ * The validators live here rather than inside the prompt callbacks so the same rules
5
+ * apply whether an answer arrived by prompt or by flag. A CI run and a customer at a
6
+ * terminal get identical verdicts on the same input.
7
+ */
8
+ import { existsSync, readdirSync, statSync } from 'node:fs';
9
+ import { isAbsolute, join, resolve } from 'node:path';
10
+ import * as p from '@clack/prompts';
11
+ import { BundlerError, ERROR_CODES } from './errors.js';
12
+ export const validateSource = (value) => {
13
+ if (!value)
14
+ return undefined; // means "current directory"
15
+ const looksLikePath = value.startsWith('.') ||
16
+ value.startsWith('/') ||
17
+ value.startsWith('~') ||
18
+ isAbsolute(value);
19
+ if (!looksLikePath)
20
+ return undefined; // an npm spec; validated when we install it
21
+ const path = resolve(process.cwd(), value.replace(/^~/, process.env.HOME ?? '~'));
22
+ if (!existsSync(path))
23
+ return `No such directory: ${path}`;
24
+ if (!statSync(path).isDirectory()) {
25
+ return `That is a file, not a directory. Point at the folder containing package.json.`;
26
+ }
27
+ if (!existsSync(join(path, 'package.json'))) {
28
+ return `No package.json in ${path}`;
29
+ }
30
+ return undefined;
31
+ };
32
+ export const validateOutDir = ({ force }) => (value) => {
33
+ if (!value)
34
+ return undefined;
35
+ const path = resolve(process.cwd(), value);
36
+ if (!existsSync(path))
37
+ return undefined;
38
+ if (!statSync(path).isDirectory())
39
+ return `${path} exists and is a file.`;
40
+ if (readdirSync(path).length > 0 && !force) {
41
+ return `${path} already exists and is not empty. Pass --force to overwrite it.`;
42
+ }
43
+ return undefined;
44
+ };
45
+ export const validateStrategy = (value) => value === 'inline' || value === 'vendor'
46
+ ? undefined
47
+ : `--strategy must be "inline" or "vendor", got "${value}".`;
48
+ export const validateTreeshake = (value) => value === 'safe' || value === 'none'
49
+ ? undefined
50
+ : `--treeshake must be "safe" or "none", got "${value}".`;
51
+ /**
52
+ * Check everything we can before doing any work, and report all problems at once.
53
+ * Finding out about the third bad flag only after fixing the first two is a bad way to
54
+ * spend someone's afternoon.
55
+ */
56
+ export function validateOptions(options) {
57
+ const problems = [];
58
+ const checks = [
59
+ validateSource(options.source),
60
+ validateOutDir({ force: options.force })(options.outDir),
61
+ validateStrategy(options.strategy),
62
+ validateTreeshake(options.treeshake),
63
+ ];
64
+ for (const problem of checks) {
65
+ if (problem)
66
+ problems.push(problem);
67
+ }
68
+ const conflicting = options.external.filter((name) => options.inline.includes(name));
69
+ if (conflicting.length > 0) {
70
+ problems.push(`These packages are in both --external and --inline: ${conflicting.join(', ')}`);
71
+ }
72
+ for (const entry of options.entry) {
73
+ const path = resolve(process.cwd(), entry);
74
+ if (!existsSync(path))
75
+ problems.push(`--entry file does not exist: ${entry}`);
76
+ }
77
+ if (problems.length > 0) {
78
+ throw new BundlerError({
79
+ code: ERROR_CODES.ERR_NEEDS_INPUT,
80
+ message: `${problems.length} problem${problems.length === 1 ? '' : 's'} with the options you passed.`,
81
+ details: problems,
82
+ });
83
+ }
84
+ }
85
+ function isInteractive() {
86
+ return Boolean(process.stdin.isTTY && process.stdout.isTTY);
87
+ }
88
+ /**
89
+ * Fill in anything the flags didn't cover.
90
+ *
91
+ * With `--yes`, or with no TTY, this returns the flags untouched — a CI job or an agent
92
+ * must never block on a prompt it cannot answer. Defaults are all safe, so there is
93
+ * nothing a prompt could ask that would stop the run.
94
+ */
95
+ export async function collectAnswers({ options, suggestedOutDir, packageLabel, }) {
96
+ if (options.json || !isInteractive())
97
+ return options;
98
+ p.intro('Knapsack package bundler');
99
+ p.log.info(`Bundling ${packageLabel}\nThis reads your built output and inlines its dependencies. It does not run your build.`);
100
+ const answers = {};
101
+ if (!options.outDir) {
102
+ const outDir = await p.text({
103
+ message: 'Where should the bundle go?',
104
+ placeholder: suggestedOutDir,
105
+ defaultValue: suggestedOutDir,
106
+ validate: (value) => validateOutDir({ force: options.force })(value || undefined),
107
+ });
108
+ if (p.isCancel(outDir)) {
109
+ p.cancel('Cancelled.');
110
+ process.exit(130);
111
+ }
112
+ answers.outDir = outDir || suggestedOutDir;
113
+ }
114
+ const proceed = await p.confirm({
115
+ message: `Bundle ${packageLabel} into ${answers.outDir ?? options.outDir}?`,
116
+ initialValue: true,
117
+ });
118
+ if (p.isCancel(proceed) || !proceed) {
119
+ p.cancel('Cancelled.');
120
+ process.exit(130);
121
+ }
122
+ return { ...options, ...answers };
123
+ }
124
+ export const prompts = p;
125
+ //# sourceMappingURL=prompts.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prompts.js","sourceRoot":"","sources":["../src/prompts.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACtD,OAAO,KAAK,CAAC,MAAM,gBAAgB,CAAC;AACpC,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,aAA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GAAwB,EAAE,CAAC;IAExC,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;QACpB,MAAM,MAAM,GAAG,MAAM,CAAC,CAAC,IAAI,CAAC;YAC1B,OAAO,EAAE,6BAA6B;YACtC,WAAW,EAAE,eAAe;YAC5B,YAAY,EAAE,eAAe;YAC7B,QAAQ,EAAE,CAAC,KAAK,EAAE,EAAE,CAClB,cAAc,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC,KAAK,IAAI,SAAS,CAAC;SAC/D,CAAC,CAAC;QACH,IAAI,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;YACvB,CAAC,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;YACvB,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACpB,CAAC;QACD,OAAO,CAAC,MAAM,GAAG,MAAM,IAAI,eAAe,CAAC;IAC7C,CAAC;IAED,MAAM,OAAO,GAAG,MAAM,CAAC,CAAC,OAAO,CAAC;QAC9B,OAAO,EAAE,UAAU,YAAY,SAAS,OAAO,CAAC,MAAM,IAAI,OAAO,CAAC,MAAM,GAAG;QAC3E,YAAY,EAAE,IAAI;KACnB,CAAC,CAAC;IACH,IAAI,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;QACpC,CAAC,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;QACvB,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACpB,CAAC;IAED,OAAO,EAAE,GAAG,OAAO,EAAE,GAAG,OAAO,EAAE,CAAC;AACpC,CAAC;AAED,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,CAAC"}
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Work out what to bundle, from the package's own declaration of what it exposes.
3
+ *
4
+ * The rule is "point at dist, not src" — but `dist` is a convention, not a name. What
5
+ * actually identifies the built output is `exports` / `main` / `module` / `bin`, so we
6
+ * read those and follow them wherever they lead.
7
+ */
8
+ import type { EntryPoint } from './types.js';
9
+ /** The subset of package.json this module reads. */
10
+ export interface SourcePackageJson {
11
+ readonly name?: string;
12
+ readonly version?: string;
13
+ readonly type?: string;
14
+ readonly license?: string;
15
+ readonly engines?: Record<string, string>;
16
+ readonly main?: string;
17
+ readonly module?: string;
18
+ readonly exports?: unknown;
19
+ readonly bin?: string | Record<string, string>;
20
+ readonly dependencies?: Record<string, string>;
21
+ readonly peerDependencies?: Record<string, string>;
22
+ readonly optionalDependencies?: Record<string, string>;
23
+ }
24
+ /**
25
+ * Turn a subpath into a stable output name that cannot escape the output directory.
26
+ *
27
+ * The name becomes `[name].mjs` in Rollup's output, so a `..` segment surviving here
28
+ * would write outside `--out-dir`. `exports` keys and `bin` names are attacker-controlled
29
+ * on the `npx package-bundler <npm-spec>` path — that is registry metadata, not the
30
+ * operator's own tree — so every traversal segment is dropped rather than trusted.
31
+ */
32
+ export declare function toEntryName(subpath: string): string;
33
+ /**
34
+ * Is this file inside the package we were asked to bundle?
35
+ *
36
+ * `exports` and `bin` targets come from package.json, which on the npm path is
37
+ * untrusted. A `./../../secrets/key.js` target would otherwise pull a file from outside
38
+ * the package into the bundle — and into the zip the customer is told to send us.
39
+ */
40
+ export declare function isInsidePackage({ pkgDir, file, }: {
41
+ pkgDir: string;
42
+ file: string;
43
+ }): boolean;
44
+ /**
45
+ * Collect every entry point the package declares.
46
+ *
47
+ * Order matters only for readability of the result; duplicates are collapsed by
48
+ * output name, first declaration winning.
49
+ */
50
+ export declare function resolveEntries({ pkgDir, pkgJson, entryOverrides, }: {
51
+ pkgDir: string;
52
+ pkgJson: SourcePackageJson;
53
+ /** `--entry` values, which bypass detection entirely. */
54
+ entryOverrides?: readonly string[];
55
+ }): readonly EntryPoint[];
56
+ //# sourceMappingURL=resolve-entries.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolve-entries.d.ts","sourceRoot":"","sources":["../src/resolve-entries.ts"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IACD,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,iBAAiB,CAAC;IAC3B,yDAAyD;IACzD,cAAc,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACpC,GAAG,SAAS,UAAU,EAAE,CAwKxB"}
@@ -0,0 +1,333 @@
1
+ /**
2
+ * Work out what to bundle, from the package's own declaration of what it exposes.
3
+ *
4
+ * The rule is "point at dist, not src" — but `dist` is a convention, not a name. What
5
+ * actually identifies the built output is `exports` / `main` / `module` / `bin`, so we
6
+ * read those and follow them wherever they lead.
7
+ */
8
+ import { existsSync, readdirSync, statSync } from 'node:fs';
9
+ import { isAbsolute, join, relative, resolve, sep } from 'node:path';
10
+ import { BundlerError, ERROR_CODES } from './errors.js';
11
+ /**
12
+ * Conditions we follow when walking an `exports` map, best-first. `import` before
13
+ * `require` because output is always ESM; `default` last as the catch-all.
14
+ */
15
+ const PREFERRED_CONDITIONS = [
16
+ 'node',
17
+ 'import',
18
+ 'module',
19
+ 'require',
20
+ 'default',
21
+ ];
22
+ /** Conditions that lead somewhere we never want to bundle. */
23
+ const IGNORED_CONDITIONS = ['types', 'typings'];
24
+ /**
25
+ * Reduce one `exports` entry to a single file path. Handles the string form, the
26
+ * conditions-object form, and arrays (first resolvable wins).
27
+ */
28
+ function resolveConditionalTarget(value) {
29
+ if (typeof value === 'string')
30
+ return value;
31
+ if (Array.isArray(value)) {
32
+ for (const item of value) {
33
+ const resolved = resolveConditionalTarget(item);
34
+ if (resolved)
35
+ return resolved;
36
+ }
37
+ return undefined;
38
+ }
39
+ if (value && typeof value === 'object') {
40
+ const record = value;
41
+ for (const condition of PREFERRED_CONDITIONS) {
42
+ if (condition in record) {
43
+ const resolved = resolveConditionalTarget(record[condition]);
44
+ if (resolved)
45
+ return resolved;
46
+ }
47
+ }
48
+ // Fall through to any remaining condition we don't have an opinion about, so an
49
+ // unusual custom condition still beats giving up.
50
+ for (const [condition, nested] of Object.entries(record)) {
51
+ if (IGNORED_CONDITIONS.includes(condition))
52
+ continue;
53
+ if (PREFERRED_CONDITIONS.includes(condition))
54
+ continue;
55
+ const resolved = resolveConditionalTarget(nested);
56
+ if (resolved)
57
+ return resolved;
58
+ }
59
+ }
60
+ return undefined;
61
+ }
62
+ /** Files a wildcard `exports` pattern should never pull in. */
63
+ function isBundlableFile(file) {
64
+ if (file.endsWith('.d.ts') ||
65
+ file.endsWith('.d.cts') ||
66
+ file.endsWith('.d.mts')) {
67
+ return false;
68
+ }
69
+ if (file.endsWith('.map'))
70
+ return false;
71
+ return /\.(js|mjs|cjs|jsx)$/.test(file);
72
+ }
73
+ function listFilesRecursively(dir) {
74
+ const found = [];
75
+ const walk = (current) => {
76
+ for (const dirent of readdirSync(current, { withFileTypes: true })) {
77
+ const full = join(current, dirent.name);
78
+ if (dirent.isDirectory()) {
79
+ if (dirent.name === 'node_modules')
80
+ continue;
81
+ walk(full);
82
+ }
83
+ else if (dirent.isFile()) {
84
+ found.push(full);
85
+ }
86
+ }
87
+ };
88
+ walk(dir);
89
+ return found;
90
+ }
91
+ /**
92
+ * Expand a wildcard target like `./dist/*.mjs` into the files it actually matches.
93
+ *
94
+ * A fixed entry list cannot reproduce `"./*"`, so without this a package using wildcard
95
+ * exports would get a silently narrower public surface than it declared — broken for
96
+ * consumers at import time, with nothing failing at build time to warn us.
97
+ */
98
+ function expandWildcardTarget({ pkgDir, target, }) {
99
+ const [beforeStar, afterStar = ''] = target.split('*');
100
+ const prefix = beforeStar.replace(/^\.\//, '');
101
+ // Walk the deepest directory the prefix commits us to.
102
+ const prefixDir = prefix.includes('/')
103
+ ? prefix.slice(0, prefix.lastIndexOf('/'))
104
+ : '';
105
+ const searchRoot = join(pkgDir, prefixDir);
106
+ if (!existsSync(searchRoot) || !statSync(searchRoot).isDirectory())
107
+ return [];
108
+ const matches = [];
109
+ for (const absolute of listFilesRecursively(searchRoot)) {
110
+ const relativePath = `./${relative(pkgDir, absolute).split(sep).join('/')}`;
111
+ if (!relativePath.startsWith(beforeStar))
112
+ continue;
113
+ if (afterStar && !relativePath.endsWith(afterStar))
114
+ continue;
115
+ if (!isBundlableFile(relativePath))
116
+ continue;
117
+ const star = relativePath.slice(beforeStar.length, afterStar ? relativePath.length - afterStar.length : undefined);
118
+ if (!star)
119
+ continue;
120
+ matches.push({ star, file: relativePath });
121
+ }
122
+ return matches;
123
+ }
124
+ /**
125
+ * Turn a subpath into a stable output name that cannot escape the output directory.
126
+ *
127
+ * The name becomes `[name].mjs` in Rollup's output, so a `..` segment surviving here
128
+ * would write outside `--out-dir`. `exports` keys and `bin` names are attacker-controlled
129
+ * on the `npx package-bundler <npm-spec>` path — that is registry metadata, not the
130
+ * operator's own tree — so every traversal segment is dropped rather than trusted.
131
+ */
132
+ export function toEntryName(subpath) {
133
+ if (subpath === '.')
134
+ return 'index';
135
+ const withoutExtension = subpath
136
+ .replace(/^\.\//, '')
137
+ .replace(/\.[^./]+$/, '');
138
+ const safeSegments = withoutExtension
139
+ .split('/')
140
+ .filter((segment) => segment && segment !== '.' && segment !== '..');
141
+ return safeSegments.join('/') || 'index';
142
+ }
143
+ /**
144
+ * Is this file inside the package we were asked to bundle?
145
+ *
146
+ * `exports` and `bin` targets come from package.json, which on the npm path is
147
+ * untrusted. A `./../../secrets/key.js` target would otherwise pull a file from outside
148
+ * the package into the bundle — and into the zip the customer is told to send us.
149
+ */
150
+ export function isInsidePackage({ pkgDir, file, }) {
151
+ const rel = relative(pkgDir, file);
152
+ return rel !== '' && !rel.startsWith('..') && !isAbsolute(rel);
153
+ }
154
+ /** Does package.json point at code at all, regardless of whether it's there? */
155
+ function hasDeclaredEntryPoints(pkgJson) {
156
+ return Boolean(pkgJson.exports || pkgJson.main || pkgJson.module || pkgJson.bin);
157
+ }
158
+ /**
159
+ * Explain the empty result. The common cause by far is an unbuilt package, and saying so
160
+ * is far more useful than "no entries found".
161
+ */
162
+ function describeWhyNoEntries({ pkgDir, pkgJson, }) {
163
+ const details = [];
164
+ const declared = [
165
+ pkgJson.main && `main: ${pkgJson.main}`,
166
+ pkgJson.module && `module: ${pkgJson.module}`,
167
+ pkgJson.exports ? 'exports: (present)' : undefined,
168
+ ].filter((value) => Boolean(value));
169
+ if (declared.length === 0) {
170
+ details.push('package.json declares no exports, main, or module field.');
171
+ }
172
+ else {
173
+ details.push(`package.json declares ${declared.join(', ')}.`);
174
+ details.push('None of those files exist on disk — the package looks unbuilt.');
175
+ }
176
+ const buildDirs = ['dist', 'build', 'lib', 'out', 'es', 'esm'].filter((dir) => existsSync(join(pkgDir, dir)));
177
+ details.push(buildDirs.length > 0
178
+ ? `Found these build-looking directories: ${buildDirs.join(', ')}`
179
+ : 'No dist/, build/, lib/, or out/ directory found either.');
180
+ return details;
181
+ }
182
+ /**
183
+ * Collect every entry point the package declares.
184
+ *
185
+ * Order matters only for readability of the result; duplicates are collapsed by
186
+ * output name, first declaration winning.
187
+ */
188
+ export function resolveEntries({ pkgDir, pkgJson, entryOverrides = [], }) {
189
+ const entries = [];
190
+ const seenNames = new Set();
191
+ const escaped = [];
192
+ const add = (entry, { allowOutsidePackage = false } = {}) => {
193
+ if (seenNames.has(entry.name))
194
+ return;
195
+ if (!existsSync(entry.sourceFile))
196
+ return;
197
+ // Manifest-declared targets must stay inside the package. `--entry` is exempt: the
198
+ // operator typed that path themselves, so pointing outside is a deliberate choice.
199
+ if (!allowOutsidePackage &&
200
+ !isInsidePackage({ pkgDir, file: entry.sourceFile })) {
201
+ escaped.push(entry.sourceFile);
202
+ return;
203
+ }
204
+ seenNames.add(entry.name);
205
+ entries.push(entry);
206
+ };
207
+ const toAbsolute = (file) => isAbsolute(file) ? file : resolve(pkgDir, file);
208
+ if (entryOverrides.length > 0) {
209
+ for (const [index, override] of entryOverrides.entries()) {
210
+ const absolute = toAbsolute(override);
211
+ if (!existsSync(absolute)) {
212
+ throw new BundlerError({
213
+ code: ERROR_CODES.ERR_NO_ENTRIES,
214
+ message: `The entry you specified does not exist: ${override}`,
215
+ details: [`Looked for it at ${absolute}`],
216
+ });
217
+ }
218
+ const name = toEntryName(`./${relative(pkgDir, absolute).split(sep).join('/')}`);
219
+ add({
220
+ name,
221
+ sourceFile: absolute,
222
+ // `--entry` is repeatable, so only the first can own the package root.
223
+ // Giving them all `.` means each overwrites the last in the exports map,
224
+ // leaving `main` and `exports` pointing at different files.
225
+ subpath: index === 0 ? '.' : `./${name}`,
226
+ fromWildcard: false,
227
+ }, { allowOutsidePackage: true });
228
+ }
229
+ return entries;
230
+ }
231
+ const { exports: exportsField } = pkgJson;
232
+ if (exportsField &&
233
+ typeof exportsField === 'object' &&
234
+ !Array.isArray(exportsField)) {
235
+ const record = exportsField;
236
+ // A bare conditions object (no subpath keys) is shorthand for `.`.
237
+ const hasSubpathKeys = Object.keys(record).some((key) => key.startsWith('.'));
238
+ const subpathMap = hasSubpathKeys ? record : { '.': record };
239
+ for (const [subpath, value] of Object.entries(subpathMap)) {
240
+ if (!subpath.startsWith('.'))
241
+ continue;
242
+ // package.json self-references carry no code.
243
+ if (subpath === './package' || subpath === './package.json')
244
+ continue;
245
+ const target = resolveConditionalTarget(value);
246
+ if (!target || !target.startsWith('.'))
247
+ continue;
248
+ if (subpath.includes('*') || target.includes('*')) {
249
+ for (const { star, file } of expandWildcardTarget({ pkgDir, target })) {
250
+ add({
251
+ name: toEntryName(subpath.replace('*', star)),
252
+ sourceFile: toAbsolute(file),
253
+ subpath: subpath.replace('*', star),
254
+ fromWildcard: true,
255
+ });
256
+ }
257
+ continue;
258
+ }
259
+ if (!isBundlableFile(target))
260
+ continue;
261
+ add({
262
+ name: toEntryName(subpath),
263
+ sourceFile: toAbsolute(target),
264
+ subpath,
265
+ fromWildcard: false,
266
+ });
267
+ }
268
+ }
269
+ else if (typeof exportsField === 'string') {
270
+ add({
271
+ name: 'index',
272
+ sourceFile: toAbsolute(exportsField),
273
+ subpath: '.',
274
+ fromWildcard: false,
275
+ });
276
+ }
277
+ // `main`/`module` only matter when `exports` didn't already cover the root.
278
+ if (!seenNames.has('index')) {
279
+ const legacy = pkgJson.module || pkgJson.main;
280
+ if (legacy && isBundlableFile(legacy)) {
281
+ add({
282
+ name: 'index',
283
+ sourceFile: toAbsolute(legacy),
284
+ subpath: '.',
285
+ fromWildcard: false,
286
+ });
287
+ }
288
+ }
289
+ const { bin } = pkgJson;
290
+ if (typeof bin === 'string') {
291
+ add({
292
+ name: `bin/${pkgJson.name?.split('/').pop() ?? 'cli'}`,
293
+ sourceFile: toAbsolute(bin),
294
+ subpath: '.',
295
+ binName: pkgJson.name?.split('/').pop() ?? 'cli',
296
+ fromWildcard: false,
297
+ });
298
+ }
299
+ else if (bin && typeof bin === 'object') {
300
+ for (const [binName, file] of Object.entries(bin)) {
301
+ add({
302
+ name: `bin/${binName}`,
303
+ sourceFile: toAbsolute(file),
304
+ subpath: '.',
305
+ binName,
306
+ fromWildcard: false,
307
+ });
308
+ }
309
+ }
310
+ if (escaped.length > 0 && entries.length > 0) {
311
+ // Not fatal — the rest of the package is still bundleable — but the operator has to
312
+ // know a declared entry point was refused rather than silently missing.
313
+ process.emitWarning(`package.json declares ${escaped.length} entry point(s) outside the package directory; they were skipped: ${escaped.join(', ')}`, 'KnapsackPackageBundlerWarning');
314
+ }
315
+ if (entries.length === 0) {
316
+ // Two very different problems produce zero entries, and they need different advice.
317
+ // If the manifest declared entry points and the files simply are not there, the
318
+ // package is unbuilt — by far the most common way to end up here — and telling the
319
+ // user to "add an exports field" would send them the wrong way entirely.
320
+ const declaredButMissing = hasDeclaredEntryPoints(pkgJson);
321
+ throw new BundlerError({
322
+ code: declaredButMissing
323
+ ? ERROR_CODES.ERR_NOT_BUILT
324
+ : ERROR_CODES.ERR_NO_ENTRIES,
325
+ message: declaredButMissing
326
+ ? `${pkgJson.name ?? 'This package'} declares entry points, but none of those files exist on disk.`
327
+ : `No entry points found for ${pkgJson.name ?? 'this package'}.`,
328
+ details: describeWhyNoEntries({ pkgDir, pkgJson }),
329
+ });
330
+ }
331
+ return entries;
332
+ }
333
+ //# sourceMappingURL=resolve-entries.js.map
@@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BACZ,OAAO;gBACP,YAAY,EAAE,KAAK;aACpB,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IAED,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC7C,oFAAoF;QACpF,wEAAwE;QACxE,OAAO,CAAC,WAAW,CACjB,yBAAyB,OAAO,CAAC,MAAM,qEAAqE,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,EAChI,+BAA+B,CAChC,CAAC;IACJ,CAAC;IAED,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACzB,oFAAoF;QACpF,gFAAgF;QAChF,mFAAmF;QACnF,yEAAyE;QACzE,MAAM,kBAAkB,GAAG,sBAAsB,CAAC,OAAO,CAAC,CAAC;QAC3D,MAAM,IAAI,YAAY,CAAC;YACrB,IAAI,EAAE,kBAAkB;gBACtB,CAAC,CAAC,WAAW,CAAC,aAAa;gBAC3B,CAAC,CAAC,WAAW,CAAC,cAAc;YAC9B,OAAO,EAAE,kBAAkB;gBACzB,CAAC,CAAC,GAAG,OAAO,CAAC,IAAI,IAAI,cAAc,gEAAgE;gBACnG,CAAC,CAAC,6BAA6B,OAAO,CAAC,IAAI,IAAI,cAAc,GAAG;YAClE,OAAO,EAAE,oBAAoB,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;SACnD,CAAC,CAAC;IACL,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC"}
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Turn whatever the user gave us into a directory containing a built package with its
3
+ * dependencies installed.
4
+ *
5
+ * Two shapes of input:
6
+ * - a directory (or nothing at all, meaning the current directory)
7
+ * - an npm spec, which we install into a temp directory
8
+ */
9
+ import type { SourcePackageJson } from './resolve-entries.js';
10
+ /** Where the package we're bundling came from. */
11
+ export type SourceOrigin = {
12
+ readonly kind: 'directory';
13
+ readonly path: string;
14
+ } | {
15
+ readonly kind: 'npm';
16
+ readonly spec: string;
17
+ readonly resolvedVersion: string;
18
+ readonly installedTo: string;
19
+ };
20
+ export interface ResolvedSource {
21
+ /** Directory holding the package to bundle. */
22
+ readonly pkgDir: string;
23
+ readonly pkgJson: SourcePackageJson;
24
+ readonly origin: SourceOrigin;
25
+ }
26
+ /**
27
+ * Expand a leading `~` to the user's home directory.
28
+ *
29
+ * `path.resolve` does not do this — the shell normally would, but the value can also
30
+ * arrive from a prompt or a config file where no shell has touched it. Without this a
31
+ * `~/...` path passes validation and then resolves to `<cwd>/~/...`, failing with a
32
+ * confusing "path does not exist".
33
+ */
34
+ export declare function expandTilde(input: string): string;
35
+ /**
36
+ * Does this look like a filesystem path rather than a package name?
37
+ *
38
+ * Explicit path syntax always wins. Otherwise a bare word is treated as a package name
39
+ * only when there's no directory by that name — so `package-bundler my-lib` picks up a
40
+ * local `./my-lib` if one exists, which is what someone in a monorepo expects.
41
+ */
42
+ export declare function looksLikePath(input: string): boolean;
43
+ /**
44
+ * Recover the installed package's name from its spec. Handles `name`, `name@version`,
45
+ * `@scope/name` and `@scope/name@version`.
46
+ */
47
+ export declare function packageNameFromSpec(spec: string): string;
48
+ export declare function resolveSource({ input, verbose, }: {
49
+ /** Raw positional argument, or undefined to mean the current directory. */
50
+ input?: string;
51
+ verbose: boolean;
52
+ }): ResolvedSource;
53
+ //# sourceMappingURL=resolve-source.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolve-source.d.ts","sourceRoot":"","sources":["../src/resolve-source.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAOH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAE9D,kDAAkD;AAClD,MAAM,MAAM,YAAY,GACpB;IAAE,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GACrD;IACE,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC;IACrB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IACjC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;CAC9B,CAAC;AAEN,MAAM,WAAW,cAAc;IAC7B,+CAA+C;IAC/C,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,OAAO,EAAE,iBAAiB,CAAC;IACpC,QAAQ,CAAC,MAAM,EAAE,YAAY,CAAC;CAC/B;AAuBD;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAIjD;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAKpD;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAOxD;AAyFD,wBAAgB,aAAa,CAAC,EAC5B,KAAK,EACL,OAAO,GACR,EAAE;IACD,2EAA2E;IAC3E,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;CAClB,GAAG,cAAc,CAwCjB"}