@webstir-io/webstir-frontend 0.1.40
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/LICENSE +21 -0
- package/README.md +158 -0
- package/dist/assets/assetManifest.d.ts +16 -0
- package/dist/assets/assetManifest.js +31 -0
- package/dist/assets/imageOptimizer.d.ts +6 -0
- package/dist/assets/imageOptimizer.js +93 -0
- package/dist/assets/precompression.d.ts +1 -0
- package/dist/assets/precompression.js +21 -0
- package/dist/builders/contentBuilder.d.ts +2 -0
- package/dist/builders/contentBuilder.js +1052 -0
- package/dist/builders/cssBuilder.d.ts +2 -0
- package/dist/builders/cssBuilder.js +439 -0
- package/dist/builders/htmlBuilder.d.ts +2 -0
- package/dist/builders/htmlBuilder.js +430 -0
- package/dist/builders/index.d.ts +2 -0
- package/dist/builders/index.js +14 -0
- package/dist/builders/jsBuilder.d.ts +2 -0
- package/dist/builders/jsBuilder.js +300 -0
- package/dist/builders/staticAssetsBuilder.d.ts +2 -0
- package/dist/builders/staticAssetsBuilder.js +158 -0
- package/dist/builders/types.d.ts +12 -0
- package/dist/builders/types.js +1 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +105 -0
- package/dist/config/manifest.d.ts +7 -0
- package/dist/config/manifest.js +17 -0
- package/dist/config/paths.d.ts +3 -0
- package/dist/config/paths.js +11 -0
- package/dist/config/schema.d.ts +413 -0
- package/dist/config/schema.js +44 -0
- package/dist/config/setup.d.ts +2 -0
- package/dist/config/setup.js +12 -0
- package/dist/config/workspace.d.ts +2 -0
- package/dist/config/workspace.js +131 -0
- package/dist/config/workspaceManifest.d.ts +23 -0
- package/dist/config/workspaceManifest.js +1 -0
- package/dist/core/constants.d.ts +70 -0
- package/dist/core/constants.js +70 -0
- package/dist/core/diagnostics.d.ts +15 -0
- package/dist/core/diagnostics.js +21 -0
- package/dist/core/index.d.ts +3 -0
- package/dist/core/index.js +3 -0
- package/dist/core/pages.d.ts +6 -0
- package/dist/core/pages.js +23 -0
- package/dist/hooks.d.ts +19 -0
- package/dist/hooks.js +115 -0
- package/dist/html/criticalCss.d.ts +4 -0
- package/dist/html/criticalCss.js +192 -0
- package/dist/html/htmlSecurity.d.ts +5 -0
- package/dist/html/htmlSecurity.js +73 -0
- package/dist/html/lazyLoad.d.ts +6 -0
- package/dist/html/lazyLoad.js +21 -0
- package/dist/html/pageScaffold.d.ts +10 -0
- package/dist/html/pageScaffold.js +51 -0
- package/dist/html/resourceHints.d.ts +7 -0
- package/dist/html/resourceHints.js +64 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/modes/ssg/index.d.ts +4 -0
- package/dist/modes/ssg/index.js +4 -0
- package/dist/modes/ssg/metadata.d.ts +5 -0
- package/dist/modes/ssg/metadata.js +50 -0
- package/dist/modes/ssg/routing.d.ts +2 -0
- package/dist/modes/ssg/routing.js +186 -0
- package/dist/modes/ssg/seo.d.ts +4 -0
- package/dist/modes/ssg/seo.js +208 -0
- package/dist/modes/ssg/validation.d.ts +3 -0
- package/dist/modes/ssg/validation.js +27 -0
- package/dist/modes/ssg/views.d.ts +2 -0
- package/dist/modes/ssg/views.js +236 -0
- package/dist/operations.d.ts +5 -0
- package/dist/operations.js +102 -0
- package/dist/pipeline.d.ts +7 -0
- package/dist/pipeline.js +71 -0
- package/dist/provider.d.ts +2 -0
- package/dist/provider.js +176 -0
- package/dist/types.d.ts +61 -0
- package/dist/types.js +1 -0
- package/dist/utils/changedFile.d.ts +8 -0
- package/dist/utils/changedFile.js +26 -0
- package/dist/utils/fs.d.ts +11 -0
- package/dist/utils/fs.js +39 -0
- package/dist/utils/hash.d.ts +1 -0
- package/dist/utils/hash.js +5 -0
- package/dist/utils/pagePaths.d.ts +5 -0
- package/dist/utils/pagePaths.js +36 -0
- package/dist/utils/pathMatch.d.ts +3 -0
- package/dist/utils/pathMatch.js +29 -0
- package/dist/watch/frontendFiles.d.ts +3 -0
- package/dist/watch/frontendFiles.js +25 -0
- package/dist/watch/hotUpdateTracker.d.ts +51 -0
- package/dist/watch/hotUpdateTracker.js +205 -0
- package/dist/watch/pipelineHelpers.d.ts +26 -0
- package/dist/watch/pipelineHelpers.js +177 -0
- package/dist/watch/types.d.ts +27 -0
- package/dist/watch/types.js +1 -0
- package/dist/watch/watchCoordinator.d.ts +36 -0
- package/dist/watch/watchCoordinator.js +551 -0
- package/dist/watch/watchDaemon.d.ts +17 -0
- package/dist/watch/watchDaemon.js +127 -0
- package/dist/watch/watchReporter.d.ts +21 -0
- package/dist/watch/watchReporter.js +64 -0
- package/package.json +92 -0
- package/scripts/publish.sh +101 -0
- package/scripts/smoke.mjs +35 -0
- package/scripts/update-contract.sh +121 -0
- package/src/assets/assetManifest.ts +51 -0
- package/src/assets/imageOptimizer.ts +112 -0
- package/src/assets/precompression.ts +25 -0
- package/src/builders/contentBuilder.ts +1400 -0
- package/src/builders/cssBuilder.ts +552 -0
- package/src/builders/htmlBuilder.ts +540 -0
- package/src/builders/index.ts +16 -0
- package/src/builders/jsBuilder.ts +358 -0
- package/src/builders/staticAssetsBuilder.ts +174 -0
- package/src/builders/types.ts +15 -0
- package/src/cli.ts +108 -0
- package/src/config/manifest.ts +24 -0
- package/src/config/paths.ts +14 -0
- package/src/config/schema.ts +49 -0
- package/src/config/setup.ts +14 -0
- package/src/config/workspace.ts +150 -0
- package/src/config/workspaceManifest.ts +27 -0
- package/src/core/constants.ts +73 -0
- package/src/core/diagnostics.ts +40 -0
- package/src/core/index.ts +3 -0
- package/src/core/pages.ts +31 -0
- package/src/hooks.ts +175 -0
- package/src/html/criticalCss.ts +214 -0
- package/src/html/htmlSecurity.ts +86 -0
- package/src/html/lazyLoad.ts +30 -0
- package/src/html/pageScaffold.ts +70 -0
- package/src/html/resourceHints.ts +91 -0
- package/src/index.ts +5 -0
- package/src/modes/ssg/index.ts +4 -0
- package/src/modes/ssg/metadata.ts +63 -0
- package/src/modes/ssg/routing.ts +230 -0
- package/src/modes/ssg/seo.ts +261 -0
- package/src/modes/ssg/validation.ts +37 -0
- package/src/modes/ssg/views.ts +309 -0
- package/src/operations.ts +138 -0
- package/src/pipeline.ts +88 -0
- package/src/provider.ts +249 -0
- package/src/types.ts +67 -0
- package/src/utils/changedFile.ts +39 -0
- package/src/utils/fs.ts +48 -0
- package/src/utils/hash.ts +6 -0
- package/src/utils/pagePaths.ts +43 -0
- package/src/utils/pathMatch.ts +36 -0
- package/src/watch/frontendFiles.ts +32 -0
- package/src/watch/hotUpdateTracker.ts +285 -0
- package/src/watch/pipelineHelpers.ts +242 -0
- package/src/watch/types.ts +23 -0
- package/src/watch/watchCoordinator.ts +666 -0
- package/src/watch/watchDaemon.ts +144 -0
- package/src/watch/watchReporter.ts +98 -0
- package/tests/add-page-defaults.test.js +64 -0
- package/tests/content-pages.test.js +81 -0
- package/tests/css-app-imports.test.js +64 -0
- package/tests/css-page-imports.test.js +100 -0
- package/tests/diagnostics.test.js +48 -0
- package/tests/features.test.js +63 -0
- package/tests/hooks.test.js +71 -0
- package/tests/provider.integration.test.js +137 -0
- package/tests/ssg-defaults.test.js +201 -0
- package/tests/ssg-guardrails.test.js +69 -0
- package/tsconfig.json +27 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Electric Coding LLC and contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# @webstir-io/webstir-frontend
|
|
2
|
+
|
|
3
|
+
Frontend build and publish toolkit for Webstir workspaces. The package bundles the HTML/CSS/JS pipeline, scaffolding helpers, and module provider used by the Webstir CLI and installers.
|
|
4
|
+
|
|
5
|
+
## Status
|
|
6
|
+
|
|
7
|
+
- Experimental provider for the Webstir ecosystem — pipeline details and configuration surfaces may change between releases.
|
|
8
|
+
- Best suited for exploration and demos today; do not rely on it as a hardened production frontend pipeline yet.
|
|
9
|
+
|
|
10
|
+
## Quick Start
|
|
11
|
+
|
|
12
|
+
1. **Install the package**
|
|
13
|
+
```bash
|
|
14
|
+
npm install @webstir-io/webstir-frontend
|
|
15
|
+
```
|
|
16
|
+
2. **Run a build**
|
|
17
|
+
```bash
|
|
18
|
+
npx webstir-frontend build --workspace /absolute/path/to/workspace
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Requires Node.js **20.18.x** or newer.
|
|
22
|
+
|
|
23
|
+
## Workspace Layout
|
|
24
|
+
|
|
25
|
+
The provider assumes the standard Webstir workspace shape:
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
workspace/
|
|
29
|
+
src/frontend/
|
|
30
|
+
app/
|
|
31
|
+
pages/
|
|
32
|
+
images/
|
|
33
|
+
fonts/
|
|
34
|
+
media/
|
|
35
|
+
frontend.config.json # optional feature flag overrides
|
|
36
|
+
webstir.config.mjs # optional hook definitions
|
|
37
|
+
build/frontend/... # generated build artifacts
|
|
38
|
+
dist/frontend/... # publish-ready assets
|
|
39
|
+
.webstir/manifest.json # pipeline manifest emitted on each run
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## CLI Commands
|
|
43
|
+
|
|
44
|
+
Binary name: `webstir-frontend`. All commands require `--workspace` pointing to the absolute workspace root.
|
|
45
|
+
|
|
46
|
+
| Command | Description | Useful options |
|
|
47
|
+
|---------|-------------|----------------|
|
|
48
|
+
| `build` | Runs the development pipeline (incremental safe). | `--changed-file <path>` to scope rebuilds. |
|
|
49
|
+
| `publish` | Produces optimized assets under `dist/frontend`. | `--mode <bundle\|ssg>` (SSG preview). |
|
|
50
|
+
| `rebuild` | Incremental rebuild triggered by a file change. | `--changed-file <path>` to pass the changed file. |
|
|
51
|
+
| `add-page <name>` | Scaffolds a page (HTML/CSS/TS) inside `src/frontend/pages`. | — |
|
|
52
|
+
| `watch-daemon` | Persistent watcher + HMR coordinator. | `--no-auto-start`, `--verbose`, `--hmr-verbose`. |
|
|
53
|
+
|
|
54
|
+
### Feature Flags
|
|
55
|
+
|
|
56
|
+
`frontend.config.json` enables or disables pipeline features:
|
|
57
|
+
|
|
58
|
+
```jsonc
|
|
59
|
+
{
|
|
60
|
+
"features": {
|
|
61
|
+
"htmlSecurity": true,
|
|
62
|
+
"imageOptimization": true,
|
|
63
|
+
"precompression": false
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Lifecycle Hooks
|
|
69
|
+
|
|
70
|
+
Hooks live in `webstir.config.mjs` (or `.js`/`.cjs`) at the workspace root:
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
export const hooks = {
|
|
74
|
+
pipeline: {
|
|
75
|
+
beforeAll({ mode }) {
|
|
76
|
+
console.info(`[webstir] starting ${mode} pipeline`);
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
builders: {
|
|
80
|
+
assets: {
|
|
81
|
+
after({ config }) {
|
|
82
|
+
// custom post-processing
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## API Usage
|
|
90
|
+
|
|
91
|
+
The package exports a `ModuleProvider` compatible with `@webstir-io/module-contract`:
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
import { frontendProvider } from '@webstir-io/webstir-frontend';
|
|
95
|
+
|
|
96
|
+
const result = await frontendProvider.build({
|
|
97
|
+
workspaceRoot: '/absolute/path/to/workspace',
|
|
98
|
+
env: { WEBSTIR_MODULE_MODE: 'publish' }
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
console.log(result.manifest.entryPoints);
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
- `frontendProvider.metadata` surfaces id/version compatibility.
|
|
105
|
+
- `frontendProvider.resolveWorkspace` returns canonical source/build/test paths.
|
|
106
|
+
- `frontendProvider.build` executes the pipeline and returns artifacts + manifest.
|
|
107
|
+
|
|
108
|
+
## SSG Preview
|
|
109
|
+
|
|
110
|
+
When invoked as:
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
npx webstir-frontend publish --workspace /absolute/path/to/workspace --mode ssg
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
the provider:
|
|
117
|
+
|
|
118
|
+
- Runs the normal publish pipeline to populate `dist/frontend/**`.
|
|
119
|
+
- Creates static-friendly `index.html` aliases (root and per-page).
|
|
120
|
+
- When `package.json` includes `webstir.moduleManifest.views` with `renderMode: 'ssg'` and `staticPaths`, uses those paths to add additional `index.html` aliases under `dist/frontend/**`.
|
|
121
|
+
|
|
122
|
+
## Maintainer Workflow
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
npm install
|
|
126
|
+
npm run clean # remove dist artifacts
|
|
127
|
+
npm run build # TypeScript → dist/
|
|
128
|
+
npm run test # Node --test against compiled output
|
|
129
|
+
npm run smoke # scaffolds a temp workspace and runs build/publish
|
|
130
|
+
# Release helper (bumps version, pushes tags to trigger release workflow)
|
|
131
|
+
npm run release -- patch
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
GitHub Actions should run `npm ci`, `npm run clean`, `npm run build`, `npm run test`, and `npm run smoke` before publishing. The release workflow publishes to npm with trusted publishing (`id-token: write` + provenance).
|
|
135
|
+
|
|
136
|
+
CI notes
|
|
137
|
+
- Package CI runs clean + build + tests + smoke on PRs and main.
|
|
138
|
+
|
|
139
|
+
## Troubleshooting
|
|
140
|
+
|
|
141
|
+
- **“404 Not Found” when installing `@webstir-io/module-contract`** — verify the dependency has been published to npm and re-generate `package-lock.json` against npmjs.
|
|
142
|
+
- **“No frontend test files found”** — the `test` script expects files under `tests/**/*.test.js` after build.
|
|
143
|
+
- **Missing entry points in manifest** — confirm `build/frontend` contains at least one `.js`/`.mjs` bundle; the provider falls back to `build/app/index.js` and emits a warning if empty.
|
|
144
|
+
|
|
145
|
+
## Community & Support
|
|
146
|
+
|
|
147
|
+
- Code of Conduct: https://github.com/webstir-io/.github/blob/main/CODE_OF_CONDUCT.md
|
|
148
|
+
- Contributing guidelines: https://github.com/webstir-io/.github/blob/main/CONTRIBUTING.md
|
|
149
|
+
- Security policy and disclosure process: https://github.com/webstir-io/.github/blob/main/SECURITY.md
|
|
150
|
+
- Support expectations and contact channels: https://github.com/webstir-io/.github/blob/main/SUPPORT.md
|
|
151
|
+
|
|
152
|
+
## Third-Party Notices
|
|
153
|
+
|
|
154
|
+
Webstir Frontend depends on third-party libraries and data sets (including `sharp`/libvips and `caniuse-lite`) under their respective licenses. See [THIRD_PARTY_NOTICES.md](./THIRD_PARTY_NOTICES.md) for a summary of notable licenses and attribution.
|
|
155
|
+
|
|
156
|
+
## License
|
|
157
|
+
|
|
158
|
+
MIT © Webstir
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface PageAssetManifest {
|
|
2
|
+
js?: string;
|
|
3
|
+
css?: string;
|
|
4
|
+
}
|
|
5
|
+
export interface AssetManifest {
|
|
6
|
+
pages: Record<string, PageAssetManifest>;
|
|
7
|
+
shared?: SharedAssets;
|
|
8
|
+
}
|
|
9
|
+
export interface SharedAssets {
|
|
10
|
+
css?: string;
|
|
11
|
+
js?: string;
|
|
12
|
+
}
|
|
13
|
+
export declare function updatePageManifest(directory: string, pageName: string, updater: (value: PageAssetManifest) => void): Promise<void>;
|
|
14
|
+
export declare function readPageManifest(directory: string, pageName: string): Promise<PageAssetManifest>;
|
|
15
|
+
export declare function updateSharedAssets(directory: string, updater: (value: SharedAssets) => void): Promise<void>;
|
|
16
|
+
export declare function readSharedAssets(directory: string): Promise<SharedAssets | null>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { readJson, writeJson, ensureDir } from '../utils/fs.js';
|
|
3
|
+
const MANIFEST_FILENAME = 'manifest.json';
|
|
4
|
+
export async function updatePageManifest(directory, pageName, updater) {
|
|
5
|
+
const manifestPath = path.join(directory, MANIFEST_FILENAME);
|
|
6
|
+
await ensureDir(directory);
|
|
7
|
+
const manifest = (await readJson(manifestPath)) ?? { pages: {} };
|
|
8
|
+
const pageManifest = manifest.pages[pageName] ?? {};
|
|
9
|
+
updater(pageManifest);
|
|
10
|
+
manifest.pages[pageName] = pageManifest;
|
|
11
|
+
await writeJson(manifestPath, manifest);
|
|
12
|
+
}
|
|
13
|
+
export async function readPageManifest(directory, pageName) {
|
|
14
|
+
const manifestPath = path.join(directory, MANIFEST_FILENAME);
|
|
15
|
+
const manifest = (await readJson(manifestPath)) ?? { pages: {} };
|
|
16
|
+
return manifest.pages[pageName] ?? {};
|
|
17
|
+
}
|
|
18
|
+
export async function updateSharedAssets(directory, updater) {
|
|
19
|
+
const manifestPath = path.join(directory, MANIFEST_FILENAME);
|
|
20
|
+
await ensureDir(directory);
|
|
21
|
+
const manifest = (await readJson(manifestPath)) ?? { pages: {} };
|
|
22
|
+
const shared = manifest.shared ?? {};
|
|
23
|
+
updater(shared);
|
|
24
|
+
manifest.shared = shared;
|
|
25
|
+
await writeJson(manifestPath, manifest);
|
|
26
|
+
}
|
|
27
|
+
export async function readSharedAssets(directory) {
|
|
28
|
+
const manifestPath = path.join(directory, MANIFEST_FILENAME);
|
|
29
|
+
const manifest = await readJson(manifestPath);
|
|
30
|
+
return manifest?.shared ?? null;
|
|
31
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export interface ImageDimensions {
|
|
2
|
+
width: number;
|
|
3
|
+
height: number;
|
|
4
|
+
}
|
|
5
|
+
export declare function optimizeImages(sourceDir: string, destinationDir: string, files?: string[]): Promise<void>;
|
|
6
|
+
export declare function getImageDimensions(filePath: string): Promise<ImageDimensions | null>;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import sharp from 'sharp';
|
|
3
|
+
import { glob } from 'glob';
|
|
4
|
+
import { copy, ensureDir, emptyDir, pathExists, remove } from '../utils/fs.js';
|
|
5
|
+
import { EXTENSIONS } from '../core/constants.js';
|
|
6
|
+
const TRANSCODABLE_EXTENSIONS = new Set([
|
|
7
|
+
EXTENSIONS.png,
|
|
8
|
+
EXTENSIONS.jpg,
|
|
9
|
+
EXTENSIONS.jpeg
|
|
10
|
+
]);
|
|
11
|
+
export async function optimizeImages(sourceDir, destinationDir, files) {
|
|
12
|
+
if (!(await pathExists(sourceDir))) {
|
|
13
|
+
await emptyDir(destinationDir);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
if (!files || files.length === 0) {
|
|
17
|
+
await emptyDir(destinationDir);
|
|
18
|
+
const allFiles = await glob('**/*', { cwd: sourceDir, nodir: true });
|
|
19
|
+
await Promise.all(allFiles.map(async (relative) => processImage(sourceDir, destinationDir, relative)));
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
await ensureDir(destinationDir);
|
|
23
|
+
await Promise.all(files.map(async (relative) => processImage(sourceDir, destinationDir, relative, true)));
|
|
24
|
+
}
|
|
25
|
+
export async function getImageDimensions(filePath) {
|
|
26
|
+
try {
|
|
27
|
+
const metadata = await sharp(filePath).metadata();
|
|
28
|
+
if (typeof metadata.width === 'number' && typeof metadata.height === 'number') {
|
|
29
|
+
return { width: metadata.width, height: metadata.height };
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
// Ignore errors – the caller can continue without dimensions.
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
function replaceExtension(filePath, extension) {
|
|
38
|
+
const parsed = path.parse(filePath);
|
|
39
|
+
return path.join(parsed.dir, `${parsed.name}${extension}`);
|
|
40
|
+
}
|
|
41
|
+
async function createWebpVariant(sourcePath, destinationPath) {
|
|
42
|
+
try {
|
|
43
|
+
await sharp(sourcePath)
|
|
44
|
+
.webp({ quality: 75 })
|
|
45
|
+
.toFile(destinationPath);
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
// Ignore failures; fall back to original image only.
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
async function createAvifVariant(sourcePath, destinationPath) {
|
|
52
|
+
try {
|
|
53
|
+
await sharp(sourcePath)
|
|
54
|
+
.avif({ quality: 45 })
|
|
55
|
+
.toFile(destinationPath);
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
// Ignore failures; fall back to original image only.
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
async function processImage(sourceDir, destinationDir, relative, incremental = false) {
|
|
62
|
+
const sourcePath = path.join(sourceDir, relative);
|
|
63
|
+
const destinationPath = path.join(destinationDir, relative);
|
|
64
|
+
if (!(await pathExists(sourcePath))) {
|
|
65
|
+
await removeVariants(destinationPath, true);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
await ensureDir(path.dirname(destinationPath));
|
|
69
|
+
await copy(sourcePath, destinationPath);
|
|
70
|
+
const extension = path.extname(sourcePath).toLowerCase();
|
|
71
|
+
if (!TRANSCODABLE_EXTENSIONS.has(extension)) {
|
|
72
|
+
if (incremental) {
|
|
73
|
+
await removeVariants(destinationPath, false);
|
|
74
|
+
}
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (incremental) {
|
|
78
|
+
await removeVariants(destinationPath, false);
|
|
79
|
+
}
|
|
80
|
+
await Promise.all([
|
|
81
|
+
createWebpVariant(sourcePath, replaceExtension(destinationPath, EXTENSIONS.webp)),
|
|
82
|
+
createAvifVariant(sourcePath, replaceExtension(destinationPath, EXTENSIONS.avif))
|
|
83
|
+
]);
|
|
84
|
+
}
|
|
85
|
+
async function removeVariants(destinationPath, includeBase) {
|
|
86
|
+
const targets = [replaceExtension(destinationPath, EXTENSIONS.webp), replaceExtension(destinationPath, EXTENSIONS.avif)];
|
|
87
|
+
if (includeBase) {
|
|
88
|
+
targets.push(destinationPath);
|
|
89
|
+
}
|
|
90
|
+
await Promise.all(targets.map(async (target) => {
|
|
91
|
+
await remove(target).catch(() => undefined);
|
|
92
|
+
}));
|
|
93
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function createCompressedVariants(filePath: string): Promise<void>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { createReadStream, createWriteStream } from 'node:fs';
|
|
2
|
+
import { constants as zlibConstants, createBrotliCompress, createGzip } from 'node:zlib';
|
|
3
|
+
export async function createCompressedVariants(filePath) {
|
|
4
|
+
await Promise.all([
|
|
5
|
+
compress(filePath, '.br', () => createBrotliCompress({ params: { [zlibConstants.BROTLI_PARAM_QUALITY]: 11 } })),
|
|
6
|
+
compress(filePath, '.gz', () => createGzip({ level: zlibConstants.Z_BEST_COMPRESSION }))
|
|
7
|
+
]);
|
|
8
|
+
}
|
|
9
|
+
async function compress(source, extension, factory) {
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
const destination = `${source}${extension}`;
|
|
12
|
+
const readStream = createReadStream(source);
|
|
13
|
+
const writeStream = createWriteStream(destination);
|
|
14
|
+
const compressor = factory();
|
|
15
|
+
readStream.on('error', reject);
|
|
16
|
+
writeStream.on('error', reject);
|
|
17
|
+
compressor.on('error', reject);
|
|
18
|
+
writeStream.on('close', resolve);
|
|
19
|
+
readStream.pipe(compressor).pipe(writeStream);
|
|
20
|
+
});
|
|
21
|
+
}
|