@ai.all/ds 0.0.0-stage → 0.1.0-beta.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/ALL.md +32 -0
- package/CHANGELOG.md +28 -0
- package/LICENSE +21 -0
- package/README.md +184 -2
- package/bin/ds.js +9 -0
- package/brand/ai-all.svg +5 -0
- package/brand/aiall.svg +11 -0
- package/brand/ds.svg +9 -0
- package/brand/ds7.svg +10 -0
- package/brand/ui.svg +10 -0
- package/docs/ARCHITECTURE.md +38 -0
- package/docs/CONFIG.md +50 -0
- package/docs/CRITICAL-CSS.md +36 -0
- package/docs/DELIVERY.md +43 -0
- package/docs/ECOSYSTEM.md +48 -0
- package/docs/INTEGRATION.md +79 -0
- package/docs/RELEASE.md +22 -0
- package/docs/STATIC-SITES.md +34 -0
- package/ds.config.schema.json +91 -0
- package/nuxt/module.mjs +17 -0
- package/package.json +71 -4
- package/src/build.mjs +18 -0
- package/src/cli.mjs +172 -0
- package/src/config.mjs +231 -0
- package/src/files.mjs +36 -0
- package/src/index.mjs +10 -0
- package/src/nuxt-options.mjs +27 -0
- package/src/optimize.mjs +163 -0
- package/src/report.mjs +102 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# Using DS from DS7 and UI
|
|
2
|
+
|
|
3
|
+
DS is the build-output layer of the AI.ALL ecosystem. DS7 and UI depend on it; DS depends on neither.
|
|
4
|
+
|
|
5
|
+
| Consumer | Uses | For |
|
|
6
|
+
| --- | --- | --- |
|
|
7
|
+
| DS7 | CLI `ds build`, Node API, Nuxt module | `generate:optimized` in initialized projects, size reports next to `ds7 audit perf`. |
|
|
8
|
+
| UI | Nuxt module, Node API | Production minification and compression defaults for the brandbook and Nuxt sites using UI. |
|
|
9
|
+
| A site | CLI or Nuxt module | Optimized static output without extra tooling. |
|
|
10
|
+
|
|
11
|
+
## In a Nuxt site
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install --save-dev @ai.all/ds@next
|
|
15
|
+
npx ds init
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
// nuxt.config.ts
|
|
20
|
+
export default defineNuxtConfig({
|
|
21
|
+
modules: ['@ai.all/ds/nuxt'],
|
|
22
|
+
});
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm run generate:optimized # nuxt generate, then ds optimize on .output/public
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Nuxt module options
|
|
30
|
+
|
|
31
|
+
The module only acts in production builds and never overrides a value the project set itself.
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
export default defineNuxtConfig({
|
|
35
|
+
modules: ['@ai.all/ds/nuxt'],
|
|
36
|
+
ds: {
|
|
37
|
+
minify: true, // vite.build.minify and cssMinify, when unset
|
|
38
|
+
compression: { gzip: true, brotli: true }, // nitro.compressPublicAssets, when unset
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
| Option | Default | Effect | Off |
|
|
44
|
+
| --- | --- | --- | --- |
|
|
45
|
+
| `minify` | `true` | Sets `vite.build.minify` and `vite.build.cssMinify` to `true` if the project left them unset. | `minify: false` |
|
|
46
|
+
| `compression.gzip` / `compression.brotli` | `true` | Sets `nitro.compressPublicAssets` (or its missing keys). | `compression: false`, or set the key to `false` |
|
|
47
|
+
|
|
48
|
+
## Node API
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
import { loadConfig, resolveOutputDirectory, runGenerator, optimizeOutput, analyzeOutput, formatReport } from '@ai.all/ds';
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
| Export | Signature | Returns / throws |
|
|
55
|
+
| --- | --- | --- |
|
|
56
|
+
| `DEFAULT_CONFIG` | object | The config `ds init` writes. |
|
|
57
|
+
| `loadConfig(root, file = 'ds.config.json')` | async | Validated config merged with defaults. Throws when the file is missing, not JSON or invalid. |
|
|
58
|
+
| `validateConfig(object)` | sync | A normalized copy. Throws on the first invalid key. |
|
|
59
|
+
| `resolveOutputDirectory(root, dir)` | async | Real path of `dir`. Throws when it is not a directory inside `root`. |
|
|
60
|
+
| `runGenerator(command, cwd)` | async | Resolves when the command exits with 0, rejects otherwise. No shell. |
|
|
61
|
+
| `optimizeOutput(dir, config.optimization)` | async | `{ htmlFiles, changedHtmlFiles, criticalCssFiles, gzipFiles, brotliFiles, addedOrUpdatedSidecars, initialFileCount, report }` |
|
|
62
|
+
| `analyzeOutput(dir)` | async | `report`: `{ fileCount, totalRawBytes, compressible: { files, rawBytes, gzipBytes, brotliBytes }, groups: { html, css, javascript, other } }`. `.gz` and `.br` files are not counted. |
|
|
63
|
+
| `formatReport(report, label)` | sync | The text table `ds analyze` prints. |
|
|
64
|
+
| `applyProductionDefaults(nuxt, options)` | sync | What the Nuxt module runs; for tools that configure Nuxt themselves. |
|
|
65
|
+
|
|
66
|
+
Typical use from another tool:
|
|
67
|
+
|
|
68
|
+
```js
|
|
69
|
+
const root = process.cwd();
|
|
70
|
+
const config = await loadConfig(root);
|
|
71
|
+
await runGenerator(config.generator.command, root);
|
|
72
|
+
const dir = await resolveOutputDirectory(root, config.generator.outputDir);
|
|
73
|
+
const result = await optimizeOutput(dir, config.optimization);
|
|
74
|
+
console.log(formatReport(result.report, config.generator.outputDir));
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Versions
|
|
78
|
+
|
|
79
|
+
DS is a beta on npm `next`. Consumers pin a range such as `^0.1.0-beta.0` and update DS before DS7 and UI, so the ecosystem moves in the order DS, AI.ALL, UI, DS7.
|
package/docs/RELEASE.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Release process
|
|
2
|
+
|
|
3
|
+
DS is an experimental package. Pre-releases use the npm `next` dist-tag; they must not replace the stable `latest` tag.
|
|
4
|
+
|
|
5
|
+
## Current release
|
|
6
|
+
|
|
7
|
+
`@ai.all/ds@0.1.0-beta.0` is the first pre-release.
|
|
8
|
+
|
|
9
|
+
## How a release runs
|
|
10
|
+
|
|
11
|
+
Pushing a `vX.Y.Z` tag runs `.github/workflows/publish.yml`, the workflow shared by all AI.ALL packages: it checks the tag against `package.json` and `CHANGELOG.md`, runs `npm run verify`, publishes to npm with provenance through trusted publishing (environment `npm`; final versions go to `latest`, `-beta.N` pre-releases to `next`), then creates the GitHub Release from the matching `CHANGELOG.md` section and, for final versions, asks `ai-all-ai/homebrew-tap` to update the formula. **Pushing a tag publishes the package**, so push it only when the release is approved.
|
|
12
|
+
|
|
13
|
+
## Before publishing a pre-release
|
|
14
|
+
|
|
15
|
+
1. Confirm `package.json`, `package-lock.json` if present, and the matching `CHANGELOG.md` entry use the same version.
|
|
16
|
+
2. Run `npm run verify` and inspect `npm pack --dry-run` for the expected files.
|
|
17
|
+
3. Check `npm view @ai.all/ds versions --json` to confirm the version is unused.
|
|
18
|
+
4. Commit the reviewed source and release notes, then create the matching `vVERSION` tag.
|
|
19
|
+
5. Push the tag only with the maintainer's explicit approval; the workflow publishes it.
|
|
20
|
+
6. Confirm `npm view @ai.all/ds@VERSION version dist.tarball` and install the published package in a clean fixture.
|
|
21
|
+
|
|
22
|
+
Do not publish the beta under `latest`, and do not describe it as a stable API. A stable release requires a separate compatibility review and explicit maintainer approval.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# Static generation and optimization
|
|
2
|
+
|
|
3
|
+
## Keep rendering with the framework
|
|
4
|
+
|
|
5
|
+
DS runs a configured generator, then post-processes its output. It does not render routes itself.
|
|
6
|
+
|
|
7
|
+
For Nuxt, `nuxt generate` prerenders routes into `.output/public`. Nuxt discovers pages by crawling links by default; dynamic routes that are not linked need to be listed in `nitro.prerender.routes`. The generator fails on route errors rather than handing DS an incomplete site.
|
|
8
|
+
|
|
9
|
+
For other generators, change `generator.command` and `generator.outputDir`. For example, an Astro project commonly uses `["npm", "run", "build"]` and `dist`. Keep route configuration in the owning framework.
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npx ds init
|
|
13
|
+
npm run generate:optimized
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
`ds init` creates `ds.config.json` and adds `generate:optimized` only when the project already has a `generate` script. The default generator is `npm run generate`; an Astro or other project should edit the command and output directory before running `ds build`.
|
|
17
|
+
|
|
18
|
+
## Build stages
|
|
19
|
+
|
|
20
|
+
1. **Render and bundle:** Nuxt, Vite, Astro or another framework creates the static files. The optional Nuxt module explicitly enables the active Vite JavaScript and CSS minifiers unless the project has set those options itself. Keep minification before content hashes are calculated.
|
|
21
|
+
2. **Optional HTML processing:** DS can minify HTML, but it is off by default because whitespace and framework hydration markers must be verified against the actual application.
|
|
22
|
+
3. **Optional critical CSS:** DS can process selected HTML files with Beasties. Configure exact output paths; start with one page group and verify visual references at the project's target viewports.
|
|
23
|
+
4. **Delivery compression:** DS can write gzip and Brotli sidecars for compressible files. A web server or CDN must be configured to serve the pre-compressed variants.
|
|
24
|
+
5. **Measure:** `ds analyze` reports raw, gzip and Brotli sizes for HTML, CSS, JavaScript and other compressible assets. Compare the same routes and assets before and after each change.
|
|
25
|
+
|
|
26
|
+
DS never rewrites post-hash JS or CSS. This avoids stale hashes, broken source maps and mismatch with framework manifests.
|
|
27
|
+
|
|
28
|
+
## Project-specific checks
|
|
29
|
+
|
|
30
|
+
- Confirm every intended route exists in generated output, including dynamic and localized routes.
|
|
31
|
+
- Test `404.html`, any SPA fallback and canonical URLs on the actual static host.
|
|
32
|
+
- Inspect generated HTML, stylesheets, scripts and compression headers.
|
|
33
|
+
- Run visual, accessibility, hydration and console checks after enabling HTML or CSS transforms.
|
|
34
|
+
- Record representative raw, gzip and Brotli sizes in the consuming project's metrics log.
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"title": "DS build configuration",
|
|
4
|
+
"type": "object",
|
|
5
|
+
"required": ["schemaVersion", "generator", "optimization"],
|
|
6
|
+
"properties": {
|
|
7
|
+
"$schema": {
|
|
8
|
+
"type": "string"
|
|
9
|
+
},
|
|
10
|
+
"schemaVersion": {
|
|
11
|
+
"const": 1
|
|
12
|
+
},
|
|
13
|
+
"generator": {
|
|
14
|
+
"type": "object",
|
|
15
|
+
"required": ["command", "outputDir"],
|
|
16
|
+
"properties": {
|
|
17
|
+
"command": {
|
|
18
|
+
"type": "array",
|
|
19
|
+
"items": {
|
|
20
|
+
"type": "string",
|
|
21
|
+
"minLength": 1
|
|
22
|
+
},
|
|
23
|
+
"minItems": 1
|
|
24
|
+
},
|
|
25
|
+
"outputDir": {
|
|
26
|
+
"type": "string",
|
|
27
|
+
"minLength": 1
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"additionalProperties": false
|
|
31
|
+
},
|
|
32
|
+
"optimization": {
|
|
33
|
+
"type": "object",
|
|
34
|
+
"required": ["html", "criticalCss", "compression"],
|
|
35
|
+
"properties": {
|
|
36
|
+
"html": {
|
|
37
|
+
"type": "object",
|
|
38
|
+
"required": ["minify"],
|
|
39
|
+
"properties": {
|
|
40
|
+
"minify": {
|
|
41
|
+
"type": "boolean"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"additionalProperties": false
|
|
45
|
+
},
|
|
46
|
+
"criticalCss": {
|
|
47
|
+
"type": "object",
|
|
48
|
+
"required": ["enabled", "routes", "allowRules", "preload"],
|
|
49
|
+
"properties": {
|
|
50
|
+
"enabled": {
|
|
51
|
+
"type": "boolean"
|
|
52
|
+
},
|
|
53
|
+
"routes": {
|
|
54
|
+
"type": "array",
|
|
55
|
+
"items": {
|
|
56
|
+
"type": "string",
|
|
57
|
+
"minLength": 1
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
"allowRules": {
|
|
61
|
+
"type": "array",
|
|
62
|
+
"items": {
|
|
63
|
+
"type": "string",
|
|
64
|
+
"minLength": 1
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
"preload": {
|
|
68
|
+
"oneOf": [{ "const": "body" }, { "const": false }]
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
"additionalProperties": false
|
|
72
|
+
},
|
|
73
|
+
"compression": {
|
|
74
|
+
"type": "object",
|
|
75
|
+
"required": ["gzip", "brotli"],
|
|
76
|
+
"properties": {
|
|
77
|
+
"gzip": {
|
|
78
|
+
"type": "boolean"
|
|
79
|
+
},
|
|
80
|
+
"brotli": {
|
|
81
|
+
"type": "boolean"
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
"additionalProperties": false
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
"additionalProperties": false
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
"additionalProperties": false
|
|
91
|
+
}
|
package/nuxt/module.mjs
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { defineNuxtModule } from 'nuxt/kit';
|
|
2
|
+
import { applyProductionDefaults } from '../src/nuxt-options.mjs';
|
|
3
|
+
|
|
4
|
+
export default defineNuxtModule({
|
|
5
|
+
meta: {
|
|
6
|
+
name: '@ai.all/ds',
|
|
7
|
+
configKey: 'ds',
|
|
8
|
+
compatibility: { nuxt: '^4.0.0' },
|
|
9
|
+
},
|
|
10
|
+
defaults: {
|
|
11
|
+
minify: true,
|
|
12
|
+
compression: { gzip: true, brotli: true },
|
|
13
|
+
},
|
|
14
|
+
setup(options, nuxt) {
|
|
15
|
+
applyProductionDefaults(nuxt, options);
|
|
16
|
+
},
|
|
17
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,73 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai.all/ds",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0-beta.1",
|
|
4
|
+
"description": "Static-site build and delivery optimization engine for AI.ALL",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"ai.all",
|
|
7
|
+
"static-site",
|
|
8
|
+
"build",
|
|
9
|
+
"optimization",
|
|
10
|
+
"critical-css",
|
|
11
|
+
"brotli",
|
|
12
|
+
"gzip"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/ai-all-ai/ds#readme",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/ai-all-ai/ds.git"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/ai-all-ai/ds/issues"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"author": "Igor Orlov",
|
|
24
|
+
"type": "module",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": "./src/index.mjs",
|
|
27
|
+
"./nuxt": "./nuxt/module.mjs",
|
|
28
|
+
"./package.json": "./package.json"
|
|
29
|
+
},
|
|
30
|
+
"bin": {
|
|
31
|
+
"ds": "bin/ds.js"
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"bin/",
|
|
35
|
+
"brand/",
|
|
36
|
+
"docs/",
|
|
37
|
+
"nuxt/",
|
|
38
|
+
"src/",
|
|
39
|
+
"ALL.md",
|
|
40
|
+
"CHANGELOG.md",
|
|
41
|
+
"LICENSE",
|
|
42
|
+
"README.md",
|
|
43
|
+
"ds.config.schema.json"
|
|
44
|
+
],
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=20.19.0"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"beasties": "^0.5.7",
|
|
50
|
+
"cross-spawn": "^7.0.6",
|
|
51
|
+
"html-minifier-terser": "^7.2.0"
|
|
52
|
+
},
|
|
53
|
+
"peerDependencies": {
|
|
54
|
+
"nuxt": "^4.0.0"
|
|
55
|
+
},
|
|
56
|
+
"peerDependenciesMeta": {
|
|
57
|
+
"nuxt": {
|
|
58
|
+
"optional": true
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
"publishConfig": {
|
|
62
|
+
"access": "public",
|
|
63
|
+
"registry": "https://registry.npmjs.org/",
|
|
64
|
+
"tag": "next"
|
|
65
|
+
},
|
|
66
|
+
"scripts": {
|
|
67
|
+
"test": "node --test",
|
|
68
|
+
"format": "npx --yes prettier@3.10.1 --write \"**/*.{js,mjs,cjs,json,yml,yaml}\"",
|
|
69
|
+
"format:check": "npx --yes prettier@3.10.1 --check \"**/*.{js,mjs,cjs,json,yml,yaml}\"",
|
|
70
|
+
"prepublishOnly": "npm run format:check && npm test",
|
|
71
|
+
"verify": "npm run format:check && npm test && npm pack --dry-run"
|
|
72
|
+
}
|
|
73
|
+
}
|
package/src/build.mjs
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import spawn from 'cross-spawn';
|
|
2
|
+
|
|
3
|
+
export function runGenerator(command, cwd) {
|
|
4
|
+
const [executable, ...args] = command;
|
|
5
|
+
return new Promise((resolve, reject) => {
|
|
6
|
+
const child = spawn(executable, args, { cwd, stdio: 'inherit' });
|
|
7
|
+
child.once('error', reject);
|
|
8
|
+
child.once('close', (code, signal) => {
|
|
9
|
+
if (code === 0) resolve();
|
|
10
|
+
else
|
|
11
|
+
reject(
|
|
12
|
+
new Error(
|
|
13
|
+
`Generator failed${signal ? ` with signal ${signal}` : ` with exit code ${code}`}.`,
|
|
14
|
+
),
|
|
15
|
+
);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
}
|
package/src/cli.mjs
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import {
|
|
4
|
+
DEFAULT_CONFIG,
|
|
5
|
+
loadConfig,
|
|
6
|
+
resolveOutputDirectory,
|
|
7
|
+
} from './config.mjs';
|
|
8
|
+
import { runGenerator } from './build.mjs';
|
|
9
|
+
import { optimizeOutput } from './optimize.mjs';
|
|
10
|
+
import { analyzeOutput, formatReport } from './report.mjs';
|
|
11
|
+
|
|
12
|
+
const projectRoot = process.cwd();
|
|
13
|
+
const packageJson = JSON.parse(
|
|
14
|
+
await fs.readFile(new URL('../package.json', import.meta.url), 'utf8'),
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
function help() {
|
|
18
|
+
return `DS ${packageJson.version}
|
|
19
|
+
|
|
20
|
+
Usage:
|
|
21
|
+
ds init
|
|
22
|
+
ds build [--config <file>]
|
|
23
|
+
ds optimize [--dir <output>] [--config <file>]
|
|
24
|
+
ds analyze [--dir <output>] [--config <file>] [--json]
|
|
25
|
+
|
|
26
|
+
Commands:
|
|
27
|
+
init Create ds.config.json without overwriting existing files
|
|
28
|
+
build Run the configured static generator, then optimize its output
|
|
29
|
+
optimize Optimize an existing static output directory
|
|
30
|
+
analyze Report raw, gzip and Brotli sizes for the output
|
|
31
|
+
|
|
32
|
+
Critical CSS and HTML minification are opt-in. See README.md for configuration.`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function parseOptions(args) {
|
|
36
|
+
const options = {
|
|
37
|
+
config: 'ds.config.json',
|
|
38
|
+
dir: null,
|
|
39
|
+
json: false,
|
|
40
|
+
positional: [],
|
|
41
|
+
};
|
|
42
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
43
|
+
const argument = args[index];
|
|
44
|
+
if (argument === '--config' || argument === '--dir') {
|
|
45
|
+
const value = args[index + 1];
|
|
46
|
+
if (!value || value.startsWith('--'))
|
|
47
|
+
throw new Error(`${argument} requires a value.`);
|
|
48
|
+
options[argument === '--config' ? 'config' : 'dir'] = value;
|
|
49
|
+
index += 1;
|
|
50
|
+
} else if (argument === '--json') options.json = true;
|
|
51
|
+
else if (argument.startsWith('-'))
|
|
52
|
+
throw new Error(`Unknown option: ${argument}`);
|
|
53
|
+
else options.positional.push(argument);
|
|
54
|
+
}
|
|
55
|
+
return options;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function loadProjectConfig(configFile) {
|
|
59
|
+
return loadConfig(projectRoot, configFile);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function getOutputDirectory(config, options) {
|
|
63
|
+
return resolveOutputDirectory(
|
|
64
|
+
projectRoot,
|
|
65
|
+
options.dir ?? config.generator.outputDir,
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function writeConfig() {
|
|
70
|
+
const configPath = path.join(projectRoot, 'ds.config.json');
|
|
71
|
+
try {
|
|
72
|
+
await fs.access(configPath);
|
|
73
|
+
console.log('Kept existing ds.config.json.');
|
|
74
|
+
} catch (error) {
|
|
75
|
+
if (error.code !== 'ENOENT') throw error;
|
|
76
|
+
await fs.writeFile(
|
|
77
|
+
configPath,
|
|
78
|
+
`${JSON.stringify(DEFAULT_CONFIG, null, 2)}\n`,
|
|
79
|
+
{ flag: 'wx' },
|
|
80
|
+
);
|
|
81
|
+
console.log('Created ds.config.json.');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const packagePath = path.join(projectRoot, 'package.json');
|
|
85
|
+
try {
|
|
86
|
+
const pkg = JSON.parse(await fs.readFile(packagePath, 'utf8'));
|
|
87
|
+
pkg.scripts ??= {};
|
|
88
|
+
if (pkg.scripts.generate && !pkg.scripts['generate:optimized']) {
|
|
89
|
+
pkg.scripts['generate:optimized'] = 'ds build';
|
|
90
|
+
await fs.writeFile(packagePath, `${JSON.stringify(pkg, null, 2)}\n`);
|
|
91
|
+
console.log('Added package script generate:optimized.');
|
|
92
|
+
}
|
|
93
|
+
} catch (error) {
|
|
94
|
+
if (error.code !== 'ENOENT') throw error;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export async function main(args) {
|
|
99
|
+
const command = args[0];
|
|
100
|
+
if (!command || command === '--help' || command === '-h') {
|
|
101
|
+
console.log(help());
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (command === '--version' || command === '-v') {
|
|
105
|
+
console.log(packageJson.version);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if (command === 'init') {
|
|
109
|
+
if (args.length > 1) throw new Error('Usage: ds init');
|
|
110
|
+
await writeConfig();
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const options = parseOptions(args.slice(1));
|
|
115
|
+
if (command === 'build') {
|
|
116
|
+
if (options.positional.length || options.dir || options.json) {
|
|
117
|
+
throw new Error('Usage: ds build [--config <file>]');
|
|
118
|
+
}
|
|
119
|
+
const config = await loadProjectConfig(options.config);
|
|
120
|
+
console.log(`Running: ${config.generator.command.join(' ')}`);
|
|
121
|
+
await runGenerator(config.generator.command, projectRoot);
|
|
122
|
+
const outputDir = await getOutputDirectory(config, options);
|
|
123
|
+
const result = await optimizeOutput(outputDir, config.optimization);
|
|
124
|
+
console.log(
|
|
125
|
+
`Optimized ${result.htmlFiles} HTML files; changed ${result.changedHtmlFiles}; generated or refreshed ${result.addedOrUpdatedSidecars} compression sidecars.`,
|
|
126
|
+
);
|
|
127
|
+
console.log(
|
|
128
|
+
formatReport(result.report, path.relative(projectRoot, outputDir)),
|
|
129
|
+
);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
if (command === 'optimize' || command === 'analyze') {
|
|
133
|
+
if (options.positional.length > 1) {
|
|
134
|
+
throw new Error(
|
|
135
|
+
`Usage: ds ${command} [--dir <output>] [--config <file>]${command === 'analyze' ? ' [--json]' : ''}`,
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
if (options.positional.length && options.dir) {
|
|
139
|
+
throw new Error(
|
|
140
|
+
'Pass the output directory either as a positional argument or with --dir, not both.',
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
if (command === 'optimize' && options.json) {
|
|
144
|
+
throw new Error('The --json option is supported by ds analyze only.');
|
|
145
|
+
}
|
|
146
|
+
const config = await loadProjectConfig(options.config);
|
|
147
|
+
const outputDir = await getOutputDirectory(config, {
|
|
148
|
+
...options,
|
|
149
|
+
dir: options.dir ?? options.positional[0],
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
if (command === 'optimize') {
|
|
153
|
+
const result = await optimizeOutput(outputDir, config.optimization);
|
|
154
|
+
console.log(
|
|
155
|
+
`Optimized ${result.htmlFiles} HTML files; changed ${result.changedHtmlFiles}; generated or refreshed ${result.addedOrUpdatedSidecars} compression sidecars.`,
|
|
156
|
+
);
|
|
157
|
+
console.log(
|
|
158
|
+
formatReport(result.report, path.relative(projectRoot, outputDir)),
|
|
159
|
+
);
|
|
160
|
+
} else {
|
|
161
|
+
const report = await analyzeOutput(outputDir);
|
|
162
|
+
console.log(
|
|
163
|
+
options.json
|
|
164
|
+
? JSON.stringify(report, null, 2)
|
|
165
|
+
: formatReport(report, path.relative(projectRoot, outputDir)),
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
throw new Error(`Unknown command "${command}".\n\n${help()}`);
|
|
172
|
+
}
|