portapack 0.2.1
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/.eslintrc.json +9 -0
- package/.github/workflows/ci.yml +73 -0
- package/.github/workflows/deploy-pages.yml +56 -0
- package/.prettierrc +9 -0
- package/.releaserc.js +29 -0
- package/CHANGELOG.md +21 -0
- package/README.md +288 -0
- package/commitlint.config.js +36 -0
- package/dist/cli/cli-entry.js +1694 -0
- package/dist/cli/cli-entry.js.map +1 -0
- package/dist/index.d.ts +275 -0
- package/dist/index.js +1405 -0
- package/dist/index.js.map +1 -0
- package/docs/.vitepress/config.ts +89 -0
- package/docs/.vitepress/sidebar-generator.ts +73 -0
- package/docs/cli.md +117 -0
- package/docs/code-of-conduct.md +65 -0
- package/docs/configuration.md +151 -0
- package/docs/contributing.md +107 -0
- package/docs/demo.md +46 -0
- package/docs/deployment.md +132 -0
- package/docs/development.md +168 -0
- package/docs/getting-started.md +106 -0
- package/docs/index.md +40 -0
- package/docs/portapack-transparent.png +0 -0
- package/docs/portapack.jpg +0 -0
- package/docs/troubleshooting.md +107 -0
- package/examples/main.ts +118 -0
- package/examples/sample-project/index.html +12 -0
- package/examples/sample-project/logo.png +1 -0
- package/examples/sample-project/script.js +1 -0
- package/examples/sample-project/styles.css +1 -0
- package/jest.config.ts +124 -0
- package/jest.setup.cjs +211 -0
- package/nodemon.json +11 -0
- package/output.html +1 -0
- package/package.json +161 -0
- package/site-packed.html +1 -0
- package/src/cli/cli-entry.ts +28 -0
- package/src/cli/cli.ts +139 -0
- package/src/cli/options.ts +151 -0
- package/src/core/bundler.ts +201 -0
- package/src/core/extractor.ts +618 -0
- package/src/core/minifier.ts +233 -0
- package/src/core/packer.ts +191 -0
- package/src/core/parser.ts +115 -0
- package/src/core/web-fetcher.ts +292 -0
- package/src/index.ts +262 -0
- package/src/types.ts +163 -0
- package/src/utils/font.ts +41 -0
- package/src/utils/logger.ts +139 -0
- package/src/utils/meta.ts +100 -0
- package/src/utils/mime.ts +90 -0
- package/src/utils/slugify.ts +70 -0
- package/test-output.html +0 -0
- package/tests/__fixtures__/sample-project/index.html +5 -0
- package/tests/unit/cli/cli-entry.test.ts +104 -0
- package/tests/unit/cli/cli.test.ts +230 -0
- package/tests/unit/cli/options.test.ts +316 -0
- package/tests/unit/core/bundler.test.ts +287 -0
- package/tests/unit/core/extractor.test.ts +1129 -0
- package/tests/unit/core/minifier.test.ts +414 -0
- package/tests/unit/core/packer.test.ts +193 -0
- package/tests/unit/core/parser.test.ts +540 -0
- package/tests/unit/core/web-fetcher.test.ts +374 -0
- package/tests/unit/index.test.ts +339 -0
- package/tests/unit/utils/font.test.ts +81 -0
- package/tests/unit/utils/logger.test.ts +275 -0
- package/tests/unit/utils/meta.test.ts +70 -0
- package/tests/unit/utils/mime.test.ts +96 -0
- package/tests/unit/utils/slugify.test.ts +71 -0
- package/tsconfig.build.json +11 -0
- package/tsconfig.jest.json +17 -0
- package/tsconfig.json +20 -0
- package/tsup.config.ts +71 -0
- package/typedoc.json +28 -0
package/dist/index.d.ts
ADDED
@@ -0,0 +1,275 @@
|
|
1
|
+
/**
|
2
|
+
* @file types.ts
|
3
|
+
*
|
4
|
+
* @description
|
5
|
+
* Centralized types used across the PortaPack CLI, API, core modules, and bundling pipeline.
|
6
|
+
*
|
7
|
+
* This file defines:
|
8
|
+
* - Asset structure
|
9
|
+
* - HTML parsing result
|
10
|
+
* - Bundling options and metadata
|
11
|
+
* - Page structures for recursive bundling
|
12
|
+
* - CLI execution output format
|
13
|
+
*/
|
14
|
+
/**
|
15
|
+
* Represents a single discovered, downloaded, or embedded asset.
|
16
|
+
* This includes JS, CSS, images, fonts, etc.
|
17
|
+
*/
|
18
|
+
interface Asset {
|
19
|
+
type: 'css' | 'js' | 'image' | 'font' | 'video' | 'audio' | 'other';
|
20
|
+
/** The resolved or original URL of the asset */
|
21
|
+
url: string;
|
22
|
+
/** Inlined or fetched content */
|
23
|
+
content?: string;
|
24
|
+
/** Font-specific metadata for font-face usage */
|
25
|
+
fontMeta?: {
|
26
|
+
familyName: string;
|
27
|
+
weight?: number;
|
28
|
+
style?: 'normal' | 'italic' | 'oblique';
|
29
|
+
format?: string;
|
30
|
+
};
|
31
|
+
}
|
32
|
+
/**
|
33
|
+
* Represents raw HTML and any linked/discovered assets.
|
34
|
+
* Result of the parsing stage.
|
35
|
+
*/
|
36
|
+
interface ParsedHTML {
|
37
|
+
htmlContent: string;
|
38
|
+
assets: Asset[];
|
39
|
+
}
|
40
|
+
/**
|
41
|
+
* Represents a single page crawled during recursive bundling.
|
42
|
+
* Used as input for the multi-page bundler.
|
43
|
+
*/
|
44
|
+
interface PageEntry {
|
45
|
+
/** Full resolved URL of the crawled page */
|
46
|
+
url: string;
|
47
|
+
/** Raw HTML content of the crawled page */
|
48
|
+
html: string;
|
49
|
+
}
|
50
|
+
/**
|
51
|
+
* Configuration options provided by the user via CLI or API call.
|
52
|
+
* Controls various aspects of the bundling process.
|
53
|
+
*/
|
54
|
+
interface BundleOptions {
|
55
|
+
/** Embed all discovered assets as data URIs (default: true) */
|
56
|
+
embedAssets?: boolean;
|
57
|
+
/** Enable HTML minification using html-minifier-terser (default: true) */
|
58
|
+
minifyHtml?: boolean;
|
59
|
+
/** Enable CSS minification using clean-css (default: true) */
|
60
|
+
minifyCss?: boolean;
|
61
|
+
/** Enable JavaScript minification using terser (default: true) */
|
62
|
+
minifyJs?: boolean;
|
63
|
+
/** Base URL for resolving relative links, especially for remote fetches or complex local structures */
|
64
|
+
baseUrl?: string;
|
65
|
+
/** Enable verbose logging during CLI execution */
|
66
|
+
verbose?: boolean;
|
67
|
+
/** Skip writing output file to disk (CLI dry-run mode) */
|
68
|
+
dryRun?: boolean;
|
69
|
+
/** Enable recursive crawling. If a number, specifies max depth. If true, uses default depth. */
|
70
|
+
recursive?: number | boolean;
|
71
|
+
/** Optional output file path override (CLI uses this) */
|
72
|
+
output?: string;
|
73
|
+
/** Log level for the internal logger */
|
74
|
+
logLevel?: LogLevel;
|
75
|
+
}
|
76
|
+
declare enum LogLevel {
|
77
|
+
NONE = 0,// No logging (equivalent to 'silent')
|
78
|
+
ERROR = 1,// Only errors
|
79
|
+
WARN = 2,// Errors and warnings
|
80
|
+
INFO = 3,// Errors, warnings, and info (Default)
|
81
|
+
DEBUG = 4
|
82
|
+
}
|
83
|
+
type LogLevelName = 'debug' | 'info' | 'warn' | 'error' | 'silent' | 'none';
|
84
|
+
/**
|
85
|
+
* Summary statistics and metadata returned after the packing/bundling process completes.
|
86
|
+
*/
|
87
|
+
interface BundleMetadata {
|
88
|
+
/** Source HTML file path or URL */
|
89
|
+
input: string;
|
90
|
+
/** Total number of unique assets discovered (CSS, JS, images, fonts etc.) */
|
91
|
+
assetCount: number;
|
92
|
+
/** Final output HTML size in bytes */
|
93
|
+
outputSize: number;
|
94
|
+
/** Elapsed build time in milliseconds */
|
95
|
+
buildTimeMs: number;
|
96
|
+
/** If recursive bundling was performed, the number of pages successfully crawled and included */
|
97
|
+
pagesBundled?: number;
|
98
|
+
/** Any non-critical errors or warnings encountered during bundling (e.g., asset fetch failure) */
|
99
|
+
errors?: string[];
|
100
|
+
}
|
101
|
+
/**
|
102
|
+
* Standard result object returned from the main public API functions.
|
103
|
+
*/
|
104
|
+
interface BuildResult {
|
105
|
+
/** The final generated HTML string */
|
106
|
+
html: string;
|
107
|
+
/** Metadata summarizing the build process */
|
108
|
+
metadata: BundleMetadata;
|
109
|
+
}
|
110
|
+
/** CLI-specific options extending BundleOptions. */
|
111
|
+
interface CLIOptions extends BundleOptions {
|
112
|
+
/** Input file or URL (positional). */
|
113
|
+
input?: string;
|
114
|
+
/** Max depth for recursive crawling (numeric alias for recursive). */
|
115
|
+
maxDepth?: number;
|
116
|
+
minify?: boolean;
|
117
|
+
}
|
118
|
+
/**
|
119
|
+
* Result object specifically for the CLI runner, capturing output streams and exit code.
|
120
|
+
*/
|
121
|
+
interface CLIResult {
|
122
|
+
/** Captured content written to stdout */
|
123
|
+
stdout?: string;
|
124
|
+
/** Captured content written to stderr */
|
125
|
+
stderr?: string;
|
126
|
+
/** Final exit code intended for the process (0 for success, non-zero for errors) */
|
127
|
+
exitCode: number;
|
128
|
+
}
|
129
|
+
|
130
|
+
/**
|
131
|
+
* @file src/utils/logger.ts
|
132
|
+
* @description Provides a standardized logging utility with configurable levels (based on an enum)
|
133
|
+
* to control output verbosity throughout the application (core, API, CLI).
|
134
|
+
*/
|
135
|
+
|
136
|
+
/**
|
137
|
+
* A simple logger class that allows filtering messages based on severity levels.
|
138
|
+
* Uses standard console methods (debug, info, warn, error) for output.
|
139
|
+
*/
|
140
|
+
declare class Logger {
|
141
|
+
/** The current minimum log level required for a message to be output. */
|
142
|
+
level: LogLevel;
|
143
|
+
/**
|
144
|
+
* Creates a new Logger instance.
|
145
|
+
* Defaults to LogLevel.INFO if no level is provided.
|
146
|
+
*
|
147
|
+
* @param {LogLevel} [level=LogLevel.INFO] - The initial log level for this logger instance.
|
148
|
+
* Must be one of the values from the LogLevel enum.
|
149
|
+
*/
|
150
|
+
constructor(level?: LogLevel);
|
151
|
+
/**
|
152
|
+
* Updates the logger's current level. Messages below this level will be suppressed.
|
153
|
+
*
|
154
|
+
* @param {LogLevel} level - The new log level to set. Must be a LogLevel enum member.
|
155
|
+
*/
|
156
|
+
setLevel(level: LogLevel): void;
|
157
|
+
/**
|
158
|
+
* Logs a debug message if the current log level is DEBUG or higher.
|
159
|
+
*
|
160
|
+
* @param {string} message - The debug message string.
|
161
|
+
*/
|
162
|
+
debug(message: string): void;
|
163
|
+
/**
|
164
|
+
* Logs an informational message if the current log level is INFO or higher.
|
165
|
+
*
|
166
|
+
* @param {string} message - The informational message string.
|
167
|
+
*/
|
168
|
+
info(message: string): void;
|
169
|
+
/**
|
170
|
+
* Logs a warning message if the current log level is WARN or higher.
|
171
|
+
*
|
172
|
+
* @param {string} message - The warning message string.
|
173
|
+
*/
|
174
|
+
warn(message: string): void;
|
175
|
+
/**
|
176
|
+
* Logs an error message if the current log level is ERROR or higher.
|
177
|
+
*
|
178
|
+
* @param {string} message - The error message string.
|
179
|
+
*/
|
180
|
+
error(message: string): void;
|
181
|
+
/**
|
182
|
+
* Static factory method to create a Logger instance based on a simple boolean `verbose` flag.
|
183
|
+
*
|
184
|
+
* @static
|
185
|
+
* @param {{ verbose?: boolean }} [options={}] - An object potentially containing a `verbose` flag.
|
186
|
+
* @returns {Logger} A new Logger instance set to LogLevel.DEBUG if options.verbose is true,
|
187
|
+
* otherwise set to LogLevel.INFO.
|
188
|
+
*/
|
189
|
+
static fromVerboseFlag(options?: {
|
190
|
+
verbose?: boolean;
|
191
|
+
}): Logger;
|
192
|
+
/**
|
193
|
+
* Static factory method to create a Logger instance based on a LogLevel string name.
|
194
|
+
* Useful for creating a logger from config files or environments variables.
|
195
|
+
*
|
196
|
+
* @static
|
197
|
+
* @param {string | undefined} levelName - The name of the log level (e.g., 'debug', 'info', 'warn', 'error', 'silent'/'none'). Case-insensitive.
|
198
|
+
* @param {LogLevel} [defaultLevel=LogLevel.INFO] - The level to use if levelName is invalid or undefined.
|
199
|
+
* @returns {Logger} A new Logger instance set to the corresponding LogLevel.
|
200
|
+
*/
|
201
|
+
static fromLevelName(levelName?: string, defaultLevel?: LogLevel): Logger;
|
202
|
+
}
|
203
|
+
|
204
|
+
/**
|
205
|
+
* @file src/index.ts
|
206
|
+
* @description
|
207
|
+
* Main public API for the PortaPack library.
|
208
|
+
* Provides functions to create portable HTML files from local paths or URLs,
|
209
|
+
* including single-page fetching, recursive site crawling, and multi-page bundling.
|
210
|
+
* It coordinates calls to various core modules (parser, extractor, minifier, packer, web-fetcher, bundler).
|
211
|
+
*/
|
212
|
+
|
213
|
+
/**
|
214
|
+
* Generates a single, portable HTML file from a local file path or a remote URL.
|
215
|
+
*
|
216
|
+
* - **For local files:** Reads the file, parses it, discovers linked assets (CSS, JS, images, fonts),
|
217
|
+
* fetches/reads asset content, optionally embeds assets as data URIs (default),
|
218
|
+
* optionally minifies HTML/CSS/JS (default), and packs everything into a single HTML string.
|
219
|
+
* - **For remote URLs:** Fetches the HTML content of the single specified URL using the core web-fetcher.
|
220
|
+
* *Note: This does not process/embed assets for single remote URLs; it returns the fetched HTML as-is.*
|
221
|
+
*
|
222
|
+
* @export
|
223
|
+
* @param {string} input - The local file path or remote http(s) URL of the HTML document.
|
224
|
+
* @param {BundleOptions} [options={}] - Configuration options controlling embedding, minification,
|
225
|
+
* base URL, logging level, etc. See `BundleOptions` type for details.
|
226
|
+
* @param {Logger} [loggerInstance] - Optional pre-configured logger instance to use.
|
227
|
+
* @returns {Promise<BuildResult>} A promise resolving to an object containing the final HTML string
|
228
|
+
* and metadata (`BundleMetadata`) about the bundling process (input, size, time, assets, errors).
|
229
|
+
* @throws {Error} Throws errors if file reading, parsing, required asset fetching, or processing fails critically.
|
230
|
+
*/
|
231
|
+
declare function generatePortableHTML(input: string, options?: BundleOptions, loggerInstance?: Logger): Promise<BuildResult>;
|
232
|
+
/**
|
233
|
+
* Crawls a website starting from a given URL up to a specified depth,
|
234
|
+
* bundles all discovered internal HTML pages into a single multi-page file,
|
235
|
+
* and returns the result.
|
236
|
+
*
|
237
|
+
* @export
|
238
|
+
* @param {string} url - The entry point URL to start crawling. Must be http or https.
|
239
|
+
* @param {number} [depth=1] - The maximum link depth to crawl (1 means only the starting page).
|
240
|
+
* @param {BundleOptions} [options={}] - Configuration options. Primarily used for `logLevel`.
|
241
|
+
* @param {Logger} [loggerInstance] - Optional pre-configured logger instance to use.
|
242
|
+
* @returns {Promise<BuildResult>} A promise resolving to an object containing the bundled multi-page HTML string
|
243
|
+
* and metadata (`BundleMetadata`) about the crawl and bundling process.
|
244
|
+
* @throws {Error} Throws errors if the initial URL is invalid, crawling fails, or bundling fails.
|
245
|
+
*/
|
246
|
+
declare function generateRecursivePortableHTML(url: string, depth?: number, options?: BundleOptions, loggerInstance?: Logger): Promise<BuildResult>;
|
247
|
+
/**
|
248
|
+
* Fetches the HTML content of a single remote URL using the core web-fetcher.
|
249
|
+
* This function acts as a public wrapper, primarily adding standardized timing and metadata.
|
250
|
+
* It does *not* process assets within the fetched HTML.
|
251
|
+
*
|
252
|
+
* @export
|
253
|
+
* @param {string} url - The remote http(s) URL to fetch.
|
254
|
+
* @param {BundleOptions} [options={}] - Configuration options, mainly for `logLevel`.
|
255
|
+
* @param {Logger} [loggerInstance] - Optional pre-configured logger instance to use.
|
256
|
+
* @returns {Promise<BuildResult>} A promise resolving to the BuildResult containing the fetched HTML
|
257
|
+
* and metadata from the fetch operation.
|
258
|
+
* @throws {Error} Propagates errors directly from the core fetching function or if URL is invalid.
|
259
|
+
*/
|
260
|
+
declare function fetchAndPackWebPage(url: string, options?: BundleOptions, loggerInstance?: Logger): Promise<BuildResult>;
|
261
|
+
/**
|
262
|
+
* Bundles an array of pre-fetched/generated HTML pages into a single static HTML file
|
263
|
+
* using `<template>` tags and a simple client-side hash-based router.
|
264
|
+
* This function does not perform any asset processing on the input HTML strings.
|
265
|
+
*
|
266
|
+
* @export
|
267
|
+
* @param {PageEntry[]} pages - An array of page objects, where each object has a `url` (for slug generation)
|
268
|
+
* and `html` (the content for that page).
|
269
|
+
* @param {BundleOptions} [options={}] - Configuration options, primarily used for `logLevel`.
|
270
|
+
* @param {Logger} [loggerInstance] - Optional pre-configured logger instance.
|
271
|
+
* @returns {string} A single HTML string representing the bundled multi-page document.
|
272
|
+
*/
|
273
|
+
declare function bundleMultiPageHTML(pages: PageEntry[], options?: BundleOptions, loggerInstance?: Logger): string;
|
274
|
+
|
275
|
+
export { type Asset, type BuildResult, type BundleMetadata, type BundleOptions, type CLIOptions, type CLIResult, LogLevel, type LogLevelName, type PageEntry, type ParsedHTML, bundleMultiPageHTML, fetchAndPackWebPage, generatePortableHTML, generateRecursivePortableHTML };
|