@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.
- package/.lintstagedrc.mjs +11 -0
- package/INSTRUCTIONS-FOR-CUSTOMERS.md +130 -0
- package/dist/bundle.d.ts +41 -0
- package/dist/bundle.d.ts.map +1 -0
- package/dist/bundle.js +348 -0
- package/dist/bundle.js.map +1 -0
- package/dist/cli.d.ts +6 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +165 -0
- package/dist/cli.js.map +1 -0
- package/dist/errors.d.ts +63 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +155 -0
- package/dist/errors.js.map +1 -0
- package/dist/help-text.d.ts +16 -0
- package/dist/help-text.d.ts.map +1 -0
- package/dist/help-text.js +180 -0
- package/dist/help-text.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/log.d.ts +42 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +114 -0
- package/dist/log.js.map +1 -0
- package/dist/metadata.d.ts +112 -0
- package/dist/metadata.d.ts.map +1 -0
- package/dist/metadata.js +204 -0
- package/dist/metadata.js.map +1 -0
- package/dist/prompts.d.ts +54 -0
- package/dist/prompts.d.ts.map +1 -0
- package/dist/prompts.js +125 -0
- package/dist/prompts.js.map +1 -0
- package/dist/resolve-entries.d.ts +56 -0
- package/dist/resolve-entries.d.ts.map +1 -0
- package/dist/resolve-entries.js +333 -0
- package/dist/resolve-entries.js.map +1 -0
- package/dist/resolve-source.d.ts +53 -0
- package/dist/resolve-source.d.ts.map +1 -0
- package/dist/resolve-source.js +174 -0
- package/dist/resolve-source.js.map +1 -0
- package/dist/run.d.ts +52 -0
- package/dist/run.d.ts.map +1 -0
- package/dist/run.js +276 -0
- package/dist/run.js.map +1 -0
- package/dist/types.d.ts +67 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +62 -0
- package/dist/types.js.map +1 -0
- package/dist/vendor.d.ts +59 -0
- package/dist/vendor.d.ts.map +1 -0
- package/dist/vendor.js +134 -0
- package/dist/vendor.js.map +1 -0
- package/dist/verify-bundle.d.ts +59 -0
- package/dist/verify-bundle.d.ts.map +1 -0
- package/dist/verify-bundle.js +238 -0
- package/dist/verify-bundle.js.map +1 -0
- package/dist/write-output.d.ts +82 -0
- package/dist/write-output.d.ts.map +1 -0
- package/dist/write-output.js +214 -0
- package/dist/write-output.js.map +1 -0
- package/package.json +53 -0
- package/readme.md +127 -0
- package/tsconfig.json +9 -0
- package/tsconfig.test.json +13 -0
- package/vitest.config.ts +9 -0
package/dist/cli.js
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Argument parsing and process lifecycle. All real work lives in `run.ts`.
|
|
4
|
+
*/
|
|
5
|
+
import { readFileSync } from 'node:fs';
|
|
6
|
+
import { dirname, join } from 'node:path';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
8
|
+
import { Command } from 'commander';
|
|
9
|
+
import pc from 'picocolors';
|
|
10
|
+
import { formatError, isBundlerError } from './errors.js';
|
|
11
|
+
import { buildHelpText } from './help-text.js';
|
|
12
|
+
import { createBundlerLogger, flushTelemetry, isTelemetryEnabled, } from './log.js';
|
|
13
|
+
import { collectAnswers, validateOptions, } from './prompts.js';
|
|
14
|
+
import { resolveSource } from './resolve-source.js';
|
|
15
|
+
import { defaultOutputDirFor, run } from './run.js';
|
|
16
|
+
function readOwnVersion() {
|
|
17
|
+
try {
|
|
18
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
19
|
+
const pkg = JSON.parse(readFileSync(join(here, '..', 'package.json'), 'utf8'));
|
|
20
|
+
return pkg.version ?? '0.0.0';
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return '0.0.0';
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function collect(value, previous) {
|
|
27
|
+
return [...previous, value];
|
|
28
|
+
}
|
|
29
|
+
async function main() {
|
|
30
|
+
const version = readOwnVersion();
|
|
31
|
+
const program = new Command();
|
|
32
|
+
program
|
|
33
|
+
.name('knapsack-package-bundler')
|
|
34
|
+
.description('Bundle an already-built npm package into a self-contained copy with its dependencies inlined')
|
|
35
|
+
.version(version, '-V, --version')
|
|
36
|
+
.argument('[source]', 'Package directory or npm spec. Defaults to the current directory.')
|
|
37
|
+
.option('--out-dir <path>', 'Where to write the output folder')
|
|
38
|
+
.option('--entry <file>', 'Bundle this file instead of auto-detecting (repeatable)', collect, [])
|
|
39
|
+
.option('--external <pkg>', 'Do not inline this package (repeatable)', collect, [])
|
|
40
|
+
.option('--inline <pkg>', 'Force this package to be inlined (repeatable)', collect, [])
|
|
41
|
+
.option('--strategy <mode>', 'inline | vendor', 'inline')
|
|
42
|
+
.option('--treeshake <mode>', 'safe | none', 'safe')
|
|
43
|
+
.option('--include-peer-deps', 'Inline peer dependencies too', false)
|
|
44
|
+
.option('--strict', 'Treat warnings as errors', false)
|
|
45
|
+
.option('--force', 'Overwrite a non-empty output folder', false)
|
|
46
|
+
.option('--no-telemetry', 'Do not report anything to Knapsack')
|
|
47
|
+
.option('--yes', 'Accept all defaults; never prompt', false)
|
|
48
|
+
.option('--json', 'Print a machine-readable result to stdout', false)
|
|
49
|
+
.option('--verbose', 'Print full build output', false)
|
|
50
|
+
.addHelpText('after', buildHelpText())
|
|
51
|
+
.allowExcessArguments(false);
|
|
52
|
+
program.parse();
|
|
53
|
+
const flags = program.opts();
|
|
54
|
+
const [source] = program.args;
|
|
55
|
+
const options = {
|
|
56
|
+
source,
|
|
57
|
+
outDir: flags.outDir,
|
|
58
|
+
entry: flags.entry ?? [],
|
|
59
|
+
external: flags.external ?? [],
|
|
60
|
+
inline: flags.inline ?? [],
|
|
61
|
+
strategy: flags.strategy,
|
|
62
|
+
treeshake: flags.treeshake,
|
|
63
|
+
includePeerDeps: Boolean(flags.includePeerDeps),
|
|
64
|
+
strict: Boolean(flags.strict),
|
|
65
|
+
force: Boolean(flags.force),
|
|
66
|
+
telemetry: flags.telemetry,
|
|
67
|
+
json: Boolean(flags.json),
|
|
68
|
+
verbose: Boolean(flags.verbose),
|
|
69
|
+
};
|
|
70
|
+
const telemetryEnabled = isTelemetryEnabled({
|
|
71
|
+
noTelemetryFlag: !options.telemetry,
|
|
72
|
+
});
|
|
73
|
+
const logger = createBundlerLogger({
|
|
74
|
+
verbose: options.verbose,
|
|
75
|
+
telemetryEnabled,
|
|
76
|
+
version,
|
|
77
|
+
});
|
|
78
|
+
logger.debug('Starting', {
|
|
79
|
+
evt: { name: 'packageBundler.start', outcome: 'unknown' },
|
|
80
|
+
});
|
|
81
|
+
try {
|
|
82
|
+
// Fail on bad input before doing anything, and report every problem at once.
|
|
83
|
+
validateOptions(options);
|
|
84
|
+
// Resolve the source before prompting so the questions can name the real package
|
|
85
|
+
// and the real default output folder. The result is handed to `run`, so an npm
|
|
86
|
+
// spec is never installed twice.
|
|
87
|
+
const resolvedSource = resolveSource({
|
|
88
|
+
input: options.source,
|
|
89
|
+
verbose: options.verbose,
|
|
90
|
+
});
|
|
91
|
+
const { pkgJson } = resolvedSource;
|
|
92
|
+
const packageLabel = `${pkgJson.name ?? '(unnamed package)'}${pkgJson.version ? `@${pkgJson.version}` : ''}`;
|
|
93
|
+
const answered = flags.yes
|
|
94
|
+
? options
|
|
95
|
+
: await collectAnswers({
|
|
96
|
+
options,
|
|
97
|
+
suggestedOutDir: defaultOutputDirFor(pkgJson),
|
|
98
|
+
packageLabel,
|
|
99
|
+
});
|
|
100
|
+
const result = await run({
|
|
101
|
+
options: answered,
|
|
102
|
+
logger,
|
|
103
|
+
bundlerVersion: version,
|
|
104
|
+
resolvedSource,
|
|
105
|
+
});
|
|
106
|
+
if (options.json) {
|
|
107
|
+
process.stdout.write(`${JSON.stringify({
|
|
108
|
+
ok: true,
|
|
109
|
+
outputDir: result.outputDir,
|
|
110
|
+
bundleDir: result.bundleDir,
|
|
111
|
+
selfContained: result.metadata.verification.selfContained,
|
|
112
|
+
warnings: result.metadata.warnings.length,
|
|
113
|
+
metadata: result.metadata,
|
|
114
|
+
}, null, 2)}\n`);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
const { metadata } = result;
|
|
118
|
+
process.stdout.write([
|
|
119
|
+
'',
|
|
120
|
+
`${pc.green(pc.bold('✔ Bundle ready'))}`,
|
|
121
|
+
'',
|
|
122
|
+
` ${pc.bold(result.outputDir)}`,
|
|
123
|
+
'',
|
|
124
|
+
` Self-contained: ${metadata.verification.selfContained ? pc.green('yes') : pc.red('no')}`,
|
|
125
|
+
` Entry points: ${metadata.entryPoints.length}`,
|
|
126
|
+
` Inlined deps: ${metadata.dependencies.inlined.length}`,
|
|
127
|
+
` Warnings: ${metadata.warnings.length > 0 ? pc.yellow(String(metadata.warnings.length)) : '0'}`,
|
|
128
|
+
'',
|
|
129
|
+
` ${pc.dim('Read README.md in that folder before sending it on.')}`,
|
|
130
|
+
'',
|
|
131
|
+
].join('\n'));
|
|
132
|
+
}
|
|
133
|
+
await flushTelemetry();
|
|
134
|
+
process.exit(0);
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
logger.error(isBundlerError(error) ? error.message : 'Unexpected failure', {
|
|
138
|
+
evt: {
|
|
139
|
+
name: 'packageBundler.complete',
|
|
140
|
+
outcome: 'failure',
|
|
141
|
+
...(isBundlerError(error) ? { errorCode: error.code } : {}),
|
|
142
|
+
},
|
|
143
|
+
...(error instanceof Error ? { error } : {}),
|
|
144
|
+
});
|
|
145
|
+
if (options.json) {
|
|
146
|
+
process.stdout.write(`${JSON.stringify({
|
|
147
|
+
ok: false,
|
|
148
|
+
code: isBundlerError(error) ? error.code : 'ERR_UNKNOWN',
|
|
149
|
+
message: error instanceof Error ? error.message : String(error),
|
|
150
|
+
details: isBundlerError(error) ? error.details : [],
|
|
151
|
+
recovery: isBundlerError(error) ? error.recovery : [],
|
|
152
|
+
}, null, 2)}\n`);
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
process.stderr.write(`\n${formatError(error)}`);
|
|
156
|
+
}
|
|
157
|
+
if (options.verbose && error instanceof Error && error.stack) {
|
|
158
|
+
process.stderr.write(`\n${pc.dim(error.stack)}\n`);
|
|
159
|
+
}
|
|
160
|
+
await flushTelemetry();
|
|
161
|
+
process.exit(1);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
await main();
|
|
165
|
+
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AACA;;GAEG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,MAAM,YAAY,CAAC;AAC5B,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EACL,mBAAmB,EACnB,cAAc,EACd,kBAAkB,GACnB,MAAM,UAAU,CAAC;AAClB,OAAO,EACL,cAAc,EACd,eAAe,GAEhB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,mBAAmB,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAEpD,SAAS,cAAc;IACrB,IAAI,CAAC;QACH,MAAM,IAAI,GAAG,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;QACr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C7D,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACrD,CAAC;QAED,MAAM,cAAc,EAAE,CAAC;QACvB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;AACH,CAAC;AAED,MAAM,IAAI,EAAE,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every failure mode this CLI can hit, as a closed set of codes. Each code carries the
|
|
3
|
+
* recovery steps that get shown to the user and listed in `--help`, so the terminal, the
|
|
4
|
+
* help text, and the Datadog event all describe a failure the same way.
|
|
5
|
+
*/
|
|
6
|
+
export declare const ERROR_CODES: {
|
|
7
|
+
/** The path given isn't a directory, or doesn't contain a package.json. */
|
|
8
|
+
readonly ERR_NO_PACKAGE: "ERR_NO_PACKAGE";
|
|
9
|
+
/** package.json exists but can't be parsed. */
|
|
10
|
+
readonly ERR_BAD_PACKAGE_JSON: "ERR_BAD_PACKAGE_JSON";
|
|
11
|
+
/** The package's build output is missing — we bundle built code, we don't build it. */
|
|
12
|
+
readonly ERR_NOT_BUILT: "ERR_NOT_BUILT";
|
|
13
|
+
/** No entry points could be derived from exports/main/module/bin. */
|
|
14
|
+
readonly ERR_NO_ENTRIES: "ERR_NO_ENTRIES";
|
|
15
|
+
/** An npm spec couldn't be fetched, or its dependencies couldn't be installed. */
|
|
16
|
+
readonly ERR_NPM_RESOLVE: "ERR_NPM_RESOLVE";
|
|
17
|
+
/** Vite/Rollup failed. */
|
|
18
|
+
readonly ERR_BUILD_FAILED: "ERR_BUILD_FAILED";
|
|
19
|
+
/**
|
|
20
|
+
* The build succeeded but the output still imports things that won't exist on the
|
|
21
|
+
* target machine. Vite externalizes unresolvable bare imports silently in SSR mode, so
|
|
22
|
+
* this is the check that turns a broken-looking-fine bundle into a real failure.
|
|
23
|
+
*/
|
|
24
|
+
readonly ERR_NOT_SELF_CONTAINED: "ERR_NOT_SELF_CONTAINED";
|
|
25
|
+
/** An emitted chunk isn't valid JavaScript. */
|
|
26
|
+
readonly ERR_INVALID_OUTPUT: "ERR_INVALID_OUTPUT";
|
|
27
|
+
/** The output directory exists and isn't empty. */
|
|
28
|
+
readonly ERR_OUTPUT_NOT_EMPTY: "ERR_OUTPUT_NOT_EMPTY";
|
|
29
|
+
/** Required options were missing and there's no TTY to prompt on. */
|
|
30
|
+
readonly ERR_NEEDS_INPUT: "ERR_NEEDS_INPUT";
|
|
31
|
+
/** `--strict` was set and the run produced warnings. */
|
|
32
|
+
readonly ERR_STRICT_WARNINGS: "ERR_STRICT_WARNINGS";
|
|
33
|
+
};
|
|
34
|
+
export type ErrorCode = (typeof ERROR_CODES)[keyof typeof ERROR_CODES];
|
|
35
|
+
/**
|
|
36
|
+
* A failure with a stable code, a plain-language cause, and recovery steps.
|
|
37
|
+
* `details` holds any extra lines worth printing between the two (e.g. the specifiers
|
|
38
|
+
* that leaked out of a bundle).
|
|
39
|
+
*/
|
|
40
|
+
export declare class BundlerError extends Error {
|
|
41
|
+
readonly code: ErrorCode;
|
|
42
|
+
readonly details: readonly string[];
|
|
43
|
+
readonly cause?: unknown;
|
|
44
|
+
constructor({ code, message, details, cause, }: {
|
|
45
|
+
code: ErrorCode;
|
|
46
|
+
message: string;
|
|
47
|
+
details?: readonly string[];
|
|
48
|
+
cause?: unknown;
|
|
49
|
+
});
|
|
50
|
+
get recovery(): readonly string[];
|
|
51
|
+
}
|
|
52
|
+
export declare function isBundlerError(error: unknown): error is BundlerError;
|
|
53
|
+
/** Recovery steps for every code, for the `--help` error table. */
|
|
54
|
+
export declare function getAllRecoverySteps(): readonly {
|
|
55
|
+
readonly code: ErrorCode;
|
|
56
|
+
readonly steps: readonly string[];
|
|
57
|
+
}[];
|
|
58
|
+
/**
|
|
59
|
+
* Render a failure for a human: what happened, any specifics, then what to do about it.
|
|
60
|
+
* Goes to stderr so `--json` output on stdout stays parseable.
|
|
61
|
+
*/
|
|
62
|
+
export declare function formatError(error: unknown): string;
|
|
63
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,eAAO,MAAM,WAAW;IACtB,2EAA2E;;IAE3E,+CAA+C;;IAE/C,uFAAuF;;IAEvF,qEAAqE;;IAErE,kFAAkF;;IAElF,0BAA0B;;IAE1B;;;;OAIG;;IAEH,+CAA+C;;IAE/C,mDAAmD;;IAEnD,qEAAqE;;IAErE,wDAAwD;;CAEhD,CAAC;AAEX,MAAM,MAAM,SAAS,GAAG,CAAC,OAAO,WAAW,CAAC,CAAC,MAAM,OAAO,WAAW,CAAC,CAAC;AAwDvE;;;;GAIG;AACH,qBAAa,YAAa,SAAQ,KAAK;IACrC,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IAEzB,QAAQ,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,CAAC;IAEpC,SAAkB,KAAK,CAAC,EAAE,OAAO,CAAC;gBAEtB,EACV,IAAI,EACJ,OAAO,EACP,OAAY,EACZ,KAAK,GACN,EAAE;QACD,IAAI,EAAE,SAAS,CAAC;QAChB,OAAO,EAAE,MAAM,CAAC;QAChB,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;QAC5B,KAAK,CAAC,EAAE,OAAO,CAAC;KACjB;IAQD,IAAI,QAAQ,IAAI,SAAS,MAAM,EAAE,CAEhC;CACF;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,YAAY,CAEpE;AAED,mEAAmE;AACnE,wBAAgB,mBAAmB,IAAI,SAAS;IAC9C,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,CAAC;CACnC,EAAE,CAKF;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAiClD"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every failure mode this CLI can hit, as a closed set of codes. Each code carries the
|
|
3
|
+
* recovery steps that get shown to the user and listed in `--help`, so the terminal, the
|
|
4
|
+
* help text, and the Datadog event all describe a failure the same way.
|
|
5
|
+
*/
|
|
6
|
+
import pc from 'picocolors';
|
|
7
|
+
export const ERROR_CODES = {
|
|
8
|
+
/** The path given isn't a directory, or doesn't contain a package.json. */
|
|
9
|
+
ERR_NO_PACKAGE: 'ERR_NO_PACKAGE',
|
|
10
|
+
/** package.json exists but can't be parsed. */
|
|
11
|
+
ERR_BAD_PACKAGE_JSON: 'ERR_BAD_PACKAGE_JSON',
|
|
12
|
+
/** The package's build output is missing — we bundle built code, we don't build it. */
|
|
13
|
+
ERR_NOT_BUILT: 'ERR_NOT_BUILT',
|
|
14
|
+
/** No entry points could be derived from exports/main/module/bin. */
|
|
15
|
+
ERR_NO_ENTRIES: 'ERR_NO_ENTRIES',
|
|
16
|
+
/** An npm spec couldn't be fetched, or its dependencies couldn't be installed. */
|
|
17
|
+
ERR_NPM_RESOLVE: 'ERR_NPM_RESOLVE',
|
|
18
|
+
/** Vite/Rollup failed. */
|
|
19
|
+
ERR_BUILD_FAILED: 'ERR_BUILD_FAILED',
|
|
20
|
+
/**
|
|
21
|
+
* The build succeeded but the output still imports things that won't exist on the
|
|
22
|
+
* target machine. Vite externalizes unresolvable bare imports silently in SSR mode, so
|
|
23
|
+
* this is the check that turns a broken-looking-fine bundle into a real failure.
|
|
24
|
+
*/
|
|
25
|
+
ERR_NOT_SELF_CONTAINED: 'ERR_NOT_SELF_CONTAINED',
|
|
26
|
+
/** An emitted chunk isn't valid JavaScript. */
|
|
27
|
+
ERR_INVALID_OUTPUT: 'ERR_INVALID_OUTPUT',
|
|
28
|
+
/** The output directory exists and isn't empty. */
|
|
29
|
+
ERR_OUTPUT_NOT_EMPTY: 'ERR_OUTPUT_NOT_EMPTY',
|
|
30
|
+
/** Required options were missing and there's no TTY to prompt on. */
|
|
31
|
+
ERR_NEEDS_INPUT: 'ERR_NEEDS_INPUT',
|
|
32
|
+
/** `--strict` was set and the run produced warnings. */
|
|
33
|
+
ERR_STRICT_WARNINGS: 'ERR_STRICT_WARNINGS',
|
|
34
|
+
};
|
|
35
|
+
/** Recovery steps per code. Rendered on failure and tabulated in `--help`. */
|
|
36
|
+
const RECOVERY = {
|
|
37
|
+
ERR_NO_PACKAGE: [
|
|
38
|
+
'Point at a directory that contains a package.json, e.g. `package-bundler ./my-design-system`.',
|
|
39
|
+
'Or give an npm package instead: `package-bundler my-design-system@1.2.3`.',
|
|
40
|
+
'Run with no arguments to use the current directory.',
|
|
41
|
+
],
|
|
42
|
+
ERR_BAD_PACKAGE_JSON: [
|
|
43
|
+
'Check the file is valid JSON — `node -e "require(\'./package.json\')"` will point at the syntax error.',
|
|
44
|
+
'Trailing commas and comments are not valid in package.json.',
|
|
45
|
+
],
|
|
46
|
+
ERR_NOT_BUILT: [
|
|
47
|
+
"Run the package's own build first (usually `npm run build` or `pnpm build`), then re-run this.",
|
|
48
|
+
'This tool bundles compiled output; it deliberately does not compile your source.',
|
|
49
|
+
'If your build output lives somewhere unusual, name an entry explicitly: `--entry ./lib/index.js`.',
|
|
50
|
+
],
|
|
51
|
+
ERR_NO_ENTRIES: [
|
|
52
|
+
'Add an `exports`, `main`, or `module` field to package.json so we know what to bundle.',
|
|
53
|
+
'Or bypass detection entirely: `--entry ./dist/index.js` (repeatable).',
|
|
54
|
+
],
|
|
55
|
+
ERR_NPM_RESOLVE: [
|
|
56
|
+
'Check the package name and version exist: `npm view <name>@<version> version`.',
|
|
57
|
+
'For a private registry, make sure you are logged in (`npm whoami`) and `.npmrc` is configured.',
|
|
58
|
+
'Re-run with `--verbose` to see the full npm output.',
|
|
59
|
+
'If you already have the package locally, pass its directory instead of the npm spec.',
|
|
60
|
+
],
|
|
61
|
+
ERR_BUILD_FAILED: [
|
|
62
|
+
'Re-run with `--verbose` for the full Vite/Rollup output.',
|
|
63
|
+
'If a specific dependency cannot be bundled, exclude it: `--external <package>`. It gets copied into the bundle instead.',
|
|
64
|
+
'To skip bundling altogether and just vendor every dependency, use `--strategy vendor`.',
|
|
65
|
+
],
|
|
66
|
+
ERR_NOT_SELF_CONTAINED: [
|
|
67
|
+
'The listed packages could not be inlined — usually because they are not installed. Run your install (`npm install` / `pnpm install`) and try again.',
|
|
68
|
+
'If a listed package is meant to be supplied by whoever consumes the bundle, declare it in `peerDependencies` and it will be treated as external on purpose.',
|
|
69
|
+
'To ship a listed package as real files alongside the bundle: `--external <package>`.',
|
|
70
|
+
'To vendor everything and skip inlining: `--strategy vendor`.',
|
|
71
|
+
],
|
|
72
|
+
ERR_INVALID_OUTPUT: [
|
|
73
|
+
'This is a bug in the bundler, not your package — please report it with the build log from the output folder.',
|
|
74
|
+
'Workaround: `--strategy vendor` avoids the bundling step entirely.',
|
|
75
|
+
],
|
|
76
|
+
ERR_OUTPUT_NOT_EMPTY: [
|
|
77
|
+
'Pass `--force` to overwrite it.',
|
|
78
|
+
'Or choose somewhere else with `--out-dir <path>`.',
|
|
79
|
+
],
|
|
80
|
+
ERR_NEEDS_INPUT: [
|
|
81
|
+
'Supply the missing options as flags (see the list above), or run in an interactive terminal to be prompted.',
|
|
82
|
+
'Add `--yes` to accept every default without prompting.',
|
|
83
|
+
],
|
|
84
|
+
ERR_STRICT_WARNINGS: [
|
|
85
|
+
'Review the warnings above and address them, or drop `--strict` to let the run succeed with warnings recorded in the metadata.',
|
|
86
|
+
],
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* A failure with a stable code, a plain-language cause, and recovery steps.
|
|
90
|
+
* `details` holds any extra lines worth printing between the two (e.g. the specifiers
|
|
91
|
+
* that leaked out of a bundle).
|
|
92
|
+
*/
|
|
93
|
+
export class BundlerError extends Error {
|
|
94
|
+
code;
|
|
95
|
+
details;
|
|
96
|
+
cause;
|
|
97
|
+
constructor({ code, message, details = [], cause, }) {
|
|
98
|
+
super(message);
|
|
99
|
+
this.name = 'BundlerError';
|
|
100
|
+
this.code = code;
|
|
101
|
+
this.details = details;
|
|
102
|
+
this.cause = cause;
|
|
103
|
+
}
|
|
104
|
+
get recovery() {
|
|
105
|
+
return RECOVERY[this.code];
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
export function isBundlerError(error) {
|
|
109
|
+
return error instanceof BundlerError;
|
|
110
|
+
}
|
|
111
|
+
/** Recovery steps for every code, for the `--help` error table. */
|
|
112
|
+
export function getAllRecoverySteps() {
|
|
113
|
+
return Object.values(ERROR_CODES).map((code) => ({
|
|
114
|
+
code,
|
|
115
|
+
steps: RECOVERY[code],
|
|
116
|
+
}));
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Render a failure for a human: what happened, any specifics, then what to do about it.
|
|
120
|
+
* Goes to stderr so `--json` output on stdout stays parseable.
|
|
121
|
+
*/
|
|
122
|
+
export function formatError(error) {
|
|
123
|
+
const lines = [];
|
|
124
|
+
if (isBundlerError(error)) {
|
|
125
|
+
lines.push(`${pc.red(pc.bold('✖ Failed'))} ${pc.dim(`[${error.code}]`)}`);
|
|
126
|
+
lines.push('');
|
|
127
|
+
lines.push(` ${error.message}`);
|
|
128
|
+
if (error.details.length > 0) {
|
|
129
|
+
lines.push('');
|
|
130
|
+
for (const detail of error.details) {
|
|
131
|
+
lines.push(` ${pc.yellow('•')} ${detail}`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
lines.push('');
|
|
135
|
+
lines.push(` ${pc.bold('How to fix it')}`);
|
|
136
|
+
for (const step of error.recovery) {
|
|
137
|
+
lines.push(` ${pc.cyan('→')} ${step}`);
|
|
138
|
+
}
|
|
139
|
+
if (error.cause instanceof Error && error.cause.message) {
|
|
140
|
+
lines.push('');
|
|
141
|
+
lines.push(` ${pc.dim(`Underlying error: ${error.cause.message}`)}`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
146
|
+
lines.push(`${pc.red(pc.bold('✖ Unexpected failure'))}`);
|
|
147
|
+
lines.push('');
|
|
148
|
+
lines.push(` ${message}`);
|
|
149
|
+
lines.push('');
|
|
150
|
+
lines.push(` ${pc.cyan('→')} Re-run with --verbose for a stack trace, and please report this.`);
|
|
151
|
+
}
|
|
152
|
+
lines.push('');
|
|
153
|
+
return lines.join('\n');
|
|
154
|
+
}
|
|
155
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,MAAM,YAAY,CAAC;AAE5B,MAAM,CAAC,MAAM,WAAW,GAAG;IACzB,2EAA2E;IAC3E,cAAc,EAAE,gBAAgB;IAChC,+CAA+C;IAC/C,oBAAoB,EAAE,sBAAsB;IAC5C,uFAAuF;IACvF,aAAa,EAAE,eAAe;IAC9B,qEAAqE;IACrE,cAAc,EAAE,gBAAgB;IAChC,kFAAkF;IAClF,eAAe,EAAE,iBAAiB;IAClC,0BAA0B;IAC1B,gBAAgB,EAAE,kBAAkB;IACpC;;;;OAIG;IACH,sBAAsB,EAAE,wBAAwB;IAChD,+CAA+C;IAC/C,kBAAkB,EAAE,oBAAoB;IACxC,mDAAmD;IACnD,oBAAoB,EAAE,sBAAsB;IAC5C,qEAAqE;IACrE,eAAe,EAAE,iBAAiB;IAClC,wDAAwD;IACxD,mBAAmB,EAAE,qBAAqB;CAClC,CAAC;AAIX,8EAA8E;AAC9E,MAAM,QAAQ,GAAyC;IACrD,cAAc,EAAE;QACd,+FAA+F;QAC/F,2EAA2E;QAC3E,qDAAqD;KACtD;IACD,oBAAoB,EAAE;QACpB,wGAAwG;QACxG,6DAA6D;KAC9D;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,KAAK,IAAI,KAAK,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC;YACxD,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YACf,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,GAAG,CAAC,qBAAqB,KAAK,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC;QACxE,CAAC;IACH,CAAC;SAAM,CAAC;QACN,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACvE,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,sBAAsB,CAAC,CAAC,EAAE,CAAC,CAAC;QACzD,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACf,KAAK,CAAC,IAAI,CAAC,KAAK,OAAO,EAAE,CAAC,CAAC;QAC3B,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACf,KAAK,CAAC,IAAI,CACR,KAAK,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,mEAAmE,CACrF,CAAC;IACJ,CAAC;IACD,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACf,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `--help` body.
|
|
3
|
+
*
|
|
4
|
+
* Written for two readers at once. A human wants the one command that solves their
|
|
5
|
+
* problem; an LLM driving this tool wants exhaustive, unambiguous statements it can act
|
|
6
|
+
* on without guessing — exact flag names, what each default is, and what to do about
|
|
7
|
+
* each failure.
|
|
8
|
+
*
|
|
9
|
+
* Both are served by the same text rather than two, because a divergent help text is
|
|
10
|
+
* worse than a verbose one: the moment they disagree, one of them is lying.
|
|
11
|
+
*
|
|
12
|
+
* The error table is generated from `getAllRecoverySteps()` so this and the terminal
|
|
13
|
+
* output cannot drift apart.
|
|
14
|
+
*/
|
|
15
|
+
export declare function buildHelpText(): string;
|
|
16
|
+
//# sourceMappingURL=help-text.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"help-text.d.ts","sourceRoot":"","sources":["../src/help-text.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAaH,wBAAgB,aAAa,IAAI,MAAM,CA2JtC"}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `--help` body.
|
|
3
|
+
*
|
|
4
|
+
* Written for two readers at once. A human wants the one command that solves their
|
|
5
|
+
* problem; an LLM driving this tool wants exhaustive, unambiguous statements it can act
|
|
6
|
+
* on without guessing — exact flag names, what each default is, and what to do about
|
|
7
|
+
* each failure.
|
|
8
|
+
*
|
|
9
|
+
* Both are served by the same text rather than two, because a divergent help text is
|
|
10
|
+
* worse than a verbose one: the moment they disagree, one of them is lying.
|
|
11
|
+
*
|
|
12
|
+
* The error table is generated from `getAllRecoverySteps()` so this and the terminal
|
|
13
|
+
* output cannot drift apart.
|
|
14
|
+
*/
|
|
15
|
+
import { getAllRecoverySteps } from './errors.js';
|
|
16
|
+
function renderErrorTable() {
|
|
17
|
+
return getAllRecoverySteps()
|
|
18
|
+
.map(({ code, steps }) => {
|
|
19
|
+
const body = steps.map((step) => ` - ${step}`).join('\n');
|
|
20
|
+
return ` ${code}\n${body}`;
|
|
21
|
+
})
|
|
22
|
+
.join('\n\n');
|
|
23
|
+
}
|
|
24
|
+
export function buildHelpText() {
|
|
25
|
+
return `
|
|
26
|
+
WHAT THIS DOES
|
|
27
|
+
|
|
28
|
+
Takes a package that has already been built and produces a self-contained copy with
|
|
29
|
+
its runtime dependencies inlined, so it runs on a machine where node_modules is
|
|
30
|
+
absent. Output is always ESM. Nothing is minified.
|
|
31
|
+
|
|
32
|
+
It does NOT build your package. Run your own build first. The input is your compiled
|
|
33
|
+
output (usually ./dist) — never your source.
|
|
34
|
+
|
|
35
|
+
Typical use: sending a design system to Knapsack for evaluation.
|
|
36
|
+
|
|
37
|
+
EXAMPLES
|
|
38
|
+
|
|
39
|
+
# Bundle the package in the current directory (prompts for anything missing)
|
|
40
|
+
npx @knapsack/package-bundler
|
|
41
|
+
|
|
42
|
+
# Bundle a package in a specific directory
|
|
43
|
+
npx @knapsack/package-bundler ./packages/design-system
|
|
44
|
+
|
|
45
|
+
# Bundle a package straight from npm, by name and version
|
|
46
|
+
npx @knapsack/package-bundler @acme/design-system@2.4.1
|
|
47
|
+
|
|
48
|
+
# Fully non-interactive (for CI, scripts, or an AI agent)
|
|
49
|
+
npx @knapsack/package-bundler ./packages/design-system --yes --out-dir ./out --json
|
|
50
|
+
|
|
51
|
+
# A dependency won't bundle: ship it as real files alongside instead
|
|
52
|
+
npx @knapsack/package-bundler . --external sharp --external playwright
|
|
53
|
+
|
|
54
|
+
# Give up on inlining entirely and just vendor every dependency
|
|
55
|
+
npx @knapsack/package-bundler . --strategy vendor
|
|
56
|
+
|
|
57
|
+
ARGUMENTS
|
|
58
|
+
|
|
59
|
+
[source] A directory, or an npm package spec.
|
|
60
|
+
Defaults to the current directory.
|
|
61
|
+
Treated as a path when it starts with . / or ~, or when a
|
|
62
|
+
directory of that name exists; otherwise as an npm spec.
|
|
63
|
+
|
|
64
|
+
OPTIONS
|
|
65
|
+
|
|
66
|
+
--out-dir <path> Where to write the output folder.
|
|
67
|
+
Default: a new folder in ~/Downloads, or your temp directory
|
|
68
|
+
if you have no Downloads folder.
|
|
69
|
+
--entry <file> Bundle this file instead of auto-detecting from package.json.
|
|
70
|
+
Repeatable. Skips exports/main/bin detection entirely.
|
|
71
|
+
--external <pkg> Do not inline this package; copy its real files into
|
|
72
|
+
bundle/node_modules/ instead. Repeatable.
|
|
73
|
+
--inline <pkg> Force this package to be inlined even though it would be kept
|
|
74
|
+
external by default (e.g. react). Repeatable.
|
|
75
|
+
--strategy <mode> "inline" (default) bundles dependencies into the output.
|
|
76
|
+
"vendor" skips bundling and copies every dependency as files.
|
|
77
|
+
--treeshake <mode> "safe" (default) removes only unreachable code.
|
|
78
|
+
"none" keeps everything, including code that can never run.
|
|
79
|
+
"safe" is recommended: "none" pulls in dead platform-specific
|
|
80
|
+
branches that can fail to resolve or misbehave at runtime.
|
|
81
|
+
--include-peer-deps Inline peer dependencies too. Off by default, because inlining
|
|
82
|
+
a framework gives consumers a second copy of it.
|
|
83
|
+
--strict Treat warnings and unexpected import failures as errors.
|
|
84
|
+
--force Overwrite the output folder if it already exists.
|
|
85
|
+
--no-telemetry Do not send anything to Knapsack. See PRIVACY below.
|
|
86
|
+
--yes Accept every default; never prompt.
|
|
87
|
+
--json Print a machine-readable result to stdout. Logs go to stderr,
|
|
88
|
+
so stdout stays parseable.
|
|
89
|
+
--verbose Print full build output, including every warning.
|
|
90
|
+
-V, --version Print the version.
|
|
91
|
+
-h, --help Print this help.
|
|
92
|
+
|
|
93
|
+
DEFAULTS WORTH KNOWING
|
|
94
|
+
|
|
95
|
+
- Output is ESM, whatever your package emits. There is no --format flag.
|
|
96
|
+
- Peer dependencies stay OUT of the bundle. Whoever consumes it provides them. They
|
|
97
|
+
are listed by name and version in the output README and metadata.
|
|
98
|
+
- react, react-dom, vue, svelte and @angular/* stay out even when listed under
|
|
99
|
+
"dependencies" rather than "peerDependencies", which design systems often do. Two
|
|
100
|
+
copies of a framework in one process is a broken app. Override with --inline react.
|
|
101
|
+
- Wildcard exports ("./*") are expanded to the files they actually match.
|
|
102
|
+
- Every run ends with a self-containment check (see VERIFICATION).
|
|
103
|
+
|
|
104
|
+
RUNBOOKS
|
|
105
|
+
|
|
106
|
+
Sending a design system to Knapsack
|
|
107
|
+
1. Build your package the way you normally do (npm run build).
|
|
108
|
+
2. cd into the package directory (the one with package.json).
|
|
109
|
+
3. npx @knapsack/package-bundler
|
|
110
|
+
4. Accept the defaults. The output folder appears in ~/Downloads.
|
|
111
|
+
5. Check the top of the output — it says whether the bundle is self-contained.
|
|
112
|
+
6. Zip the folder and send it.
|
|
113
|
+
|
|
114
|
+
Bundling a package you do not have checked out
|
|
115
|
+
1. npx @knapsack/package-bundler <name>@<version>
|
|
116
|
+
The package and its production dependencies are installed into a temp directory
|
|
117
|
+
first. Install scripts are skipped.
|
|
118
|
+
2. If the package is in a private registry, make sure npm is authenticated
|
|
119
|
+
(npm whoami) before running.
|
|
120
|
+
|
|
121
|
+
A build fails, or the result is not self-contained
|
|
122
|
+
1. Re-run with --verbose and read the warnings.
|
|
123
|
+
2. The error names the packages that could not be inlined. Most commonly they are
|
|
124
|
+
simply not installed — run your install and try again.
|
|
125
|
+
3. If a package genuinely cannot be bundled (native binaries, dynamic requires),
|
|
126
|
+
exclude it: --external <package>. It gets copied into the bundle as real files,
|
|
127
|
+
so the result still runs.
|
|
128
|
+
4. If several packages resist, use --strategy vendor to skip inlining altogether.
|
|
129
|
+
|
|
130
|
+
Driving this from a script or an AI agent
|
|
131
|
+
1. Always pass a source argument and --yes so nothing prompts.
|
|
132
|
+
2. Pass --json. Machine-readable result on stdout; logs and errors on stderr.
|
|
133
|
+
3. Exit code 0 means success. Non-zero means failure, and the JSON on stdout
|
|
134
|
+
carries { "ok": false, "code": "ERR_...", "recovery": [...] }.
|
|
135
|
+
4. Every error code and its recovery steps are listed under ERRORS below.
|
|
136
|
+
|
|
137
|
+
VERIFICATION
|
|
138
|
+
|
|
139
|
+
Every run checks the bundle before reporting success, in a temporary directory with
|
|
140
|
+
no reachable node_modules. These fail the run:
|
|
141
|
+
|
|
142
|
+
- An emitted file that Node cannot parse.
|
|
143
|
+
- A dynamic-require stub, which would throw when reached at runtime.
|
|
144
|
+
- Any remaining import that is not a Node built-in, a declared peer dependency, or
|
|
145
|
+
a package present in bundle/node_modules/.
|
|
146
|
+
|
|
147
|
+
That last check is the important one. It proves nothing in the bundle will reach for
|
|
148
|
+
a package that does not exist on the target machine.
|
|
149
|
+
|
|
150
|
+
Each entry point is then actually imported. Failures here are reported but do NOT
|
|
151
|
+
fail the run, because a UI component library usually cannot be imported in plain
|
|
152
|
+
Node — its framework peer is intentionally missing and its components may touch
|
|
153
|
+
browser globals at module scope. Use --strict to treat these as failures too.
|
|
154
|
+
|
|
155
|
+
PRIVACY
|
|
156
|
+
|
|
157
|
+
The tool reports errors and basic run outcomes to Knapsack so we can fix failures
|
|
158
|
+
you hit. That is: the error code, the bundler version, your Node version and OS, and
|
|
159
|
+
timing. It does NOT send your source code, your dependency names, your package name,
|
|
160
|
+
file contents, or anything from inside the bundle.
|
|
161
|
+
|
|
162
|
+
Turn it off with --no-telemetry, or by setting KS_TELEMETRY=no or DO_NOT_TRACK=1.
|
|
163
|
+
|
|
164
|
+
OUTPUT
|
|
165
|
+
|
|
166
|
+
<out-dir>/
|
|
167
|
+
README.md What the bundle is, what it needs, what was verified
|
|
168
|
+
bundle-metadata.json Full provenance, machine-readable
|
|
169
|
+
package.original.json Your package.json, verbatim
|
|
170
|
+
build-log.txt Everything the bundler printed
|
|
171
|
+
bundle/ The self-contained package
|
|
172
|
+
|
|
173
|
+
ERRORS
|
|
174
|
+
|
|
175
|
+
Each failure prints a code and how to recover. The full set:
|
|
176
|
+
|
|
177
|
+
${renderErrorTable()}
|
|
178
|
+
`.trimEnd();
|
|
179
|
+
}
|
|
180
|
+
//# sourceMappingURL=help-text.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"help-text.js","sourceRoot":"","sources":["../src/help-text.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAElD,SAAS,gBAAgB;IACvB,OAAO,mBAAmB,EAAE;SACzB,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE,EAAE;QACvB,MAAM,IAAI,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,WAAW,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC/D,OAAO,KAAK,IAAI,KAAK,IAAI,EAAE,CAAC;IAC9B,CAAC,CAAC;SACD,IAAI,CAAC,MAAM,CAAC,CAAC;AAClB,CAAC;AAED,MAAM,UAAU,aAAa;IAC3B,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAwJP,gBAAgB,EAAE;CACnB,CAAC,OAAO,EAAE,CAAC;AACZ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @knapsack/package-bundler
|
|
3
|
+
*
|
|
4
|
+
* Programmatic entry point. The CLI in `cli.ts` is a thin shell over `run()`; anything
|
|
5
|
+
* that wants to bundle a package from code should call this instead of shelling out.
|
|
6
|
+
*/
|
|
7
|
+
export { run, decideExternals, type RunResult } from './run.js';
|
|
8
|
+
export { bundlePackage, type BundleResult, type EmittedImport, } from './bundle.js';
|
|
9
|
+
export { resolveSource, type ResolvedSource, type SourceOrigin, } from './resolve-source.js';
|
|
10
|
+
export { resolveEntries, type SourcePackageJson } from './resolve-entries.js';
|
|
11
|
+
export { verifyBundle, toPackageName, type VerifyReport, type ClassifiedImport, } from './verify-bundle.js';
|
|
12
|
+
export { vendorPackages, findPackageDir, type VendorResult } from './vendor.js';
|
|
13
|
+
export { buildMetadata, buildOutputDirName, defaultOutputParent, renderBundleReadme, collectGitProvenance, type BundleMetadata, } from './metadata.js';
|
|
14
|
+
export { buildBundlePackageJson, fixBinShebang, hoistUseClient, } from './write-output.js';
|
|
15
|
+
export { validateOptions, validateSource, validateOutDir, validateStrategy, validateTreeshake, type BundleOptions, } from './prompts.js';
|
|
16
|
+
export { BundlerError, isBundlerError, formatError, getAllRecoverySteps, ERROR_CODES, type ErrorCode, } from './errors.js';
|
|
17
|
+
export { buildHelpText } from './help-text.js';
|
|
18
|
+
export { createBundlerLogger, isTelemetryEnabled, type BundlerLogger, } from './log.js';
|
|
19
|
+
export { EXTERNAL_REASONS, EXTERNAL_REASON_LABELS, NEVER_INLINE_BY_DEFAULT, BUILD_TIME_CSS_PACKAGES, type BundleWarning, type EntryPoint, type ExternalPackage, type ExternalReason, } from './types.js';
|
|
20
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,GAAG,EAAE,eAAe,EAAE,KAAK,SAAS,EAAE,MAAM,UAAU,CAAC;AAChE,OAAO,EACL,aAAa,EACb,KAAK,YAAY,EACjB,KAAK,aAAa,GACnB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,aAAa,EACb,KAAK,cAAc,EACnB,KAAK,YAAY,GAClB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,cAAc,EAAE,KAAK,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAC9E,OAAO,EACL,YAAY,EACZ,aAAa,EACb,KAAK,YAAY,EACjB,KAAK,gBAAgB,GACtB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,KAAK,YAAY,EAAE,MAAM,aAAa,CAAC;AAChF,OAAO,EACL,aAAa,EACb,kBAAkB,EAClB,mBAAmB,EACnB,kBAAkB,EAClB,oBAAoB,EACpB,KAAK,cAAc,GACpB,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,sBAAsB,EACtB,aAAa,EACb,cAAc,GACf,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,eAAe,EACf,cAAc,EACd,cAAc,EACd,gBAAgB,EAChB,iBAAiB,EACjB,KAAK,aAAa,GACnB,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