cook-ssg 2.1.3 → 2.2.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/README.md +48 -20
- package/package.json +2 -5
- package/scripts/build.js +91 -26
- package/scripts/dev.js +46 -16
- package/scripts/plugins/apply-template.js +8 -17
- package/scripts/plugins/auto-components.js +2 -2
- package/scripts/plugins/bundle.js +6 -3
- package/scripts/plugins/copy-src.js +1 -1
- package/scripts/plugins/create-dir-from-file.js +4 -2
- package/scripts/plugins/custom-plugins.js +1 -1
- package/scripts/plugins/declarative-shadow-dom.js +2 -2
- package/scripts/plugins/escape-code-blocks.js +22 -1
- package/scripts/plugins/fingerprint-assets.js +133 -13
- package/scripts/plugins/generate-formats.js +49 -39
- package/scripts/plugins/generate-fragments.js +24 -14
- package/scripts/plugins/generate-sitemap-xml.js +38 -15
- package/scripts/plugins/minify-src.js +98 -72
- package/scripts/plugins/optimize-images.js +80 -26
- package/scripts/plugins/process-markdown.js +12 -25
- package/scripts/plugins/repeat-collection.js +70 -45
- package/scripts/plugins/replace-components.js +82 -60
- package/scripts/plugins/replace-external-link-protocol.js +16 -31
- package/scripts/plugins/replace-include.js +34 -19
- package/scripts/plugins/replace-inline.js +6 -3
- package/scripts/plugins/replace-template-strings.js +2 -50
- package/scripts/plugins/set-active-links.js +57 -26
- package/scripts/utils/config/config.js +29 -5
- package/scripts/utils/get-src/get-src.js +9 -45
- package/scripts/utils/interpolate/interpolate.js +83 -0
- package/scripts/utils/logger/logger.js +8 -3
- package/scripts/utils/site-pages/site-pages.js +37 -0
- package/scripts/utils/source-file/source-file.js +64 -0
- package/scripts/utils/user-data/user-data.js +45 -0
- package/scripts/utils/util/util.js +88 -46
- package/patches/jsdom-fetch-fix.js +0 -52
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
A static site build system with templating, bundling, and optimization features.
|
|
4
4
|
|
|
5
|
-
**Version:** 2.
|
|
5
|
+
**Version:** 2.1.3 (canonical version lives in `package.json`)
|
|
6
6
|
**License:** MIT
|
|
7
7
|
**Node.js:** >=18.0.0
|
|
8
8
|
|
|
@@ -19,7 +19,15 @@ npm install
|
|
|
19
19
|
npm run dev
|
|
20
20
|
```
|
|
21
21
|
|
|
22
|
-
This builds the site locally and runs it at `localhost:3000`, using BrowserSync for live reloading.
|
|
22
|
+
This builds the site locally and runs it at `localhost:3000`, using BrowserSync for live reloading. Rebuilds run in-process (no cold Node start per save), and a build error on one page is reported without stopping the dev server or the rest of the build.
|
|
23
|
+
|
|
24
|
+
When installed as a package, the `cook` CLI is also available:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
cook build # Production build
|
|
28
|
+
cook dev # Build + live-reload dev server
|
|
29
|
+
cook dev --port N # Dev server on a custom port (default: 3000)
|
|
30
|
+
```
|
|
23
31
|
|
|
24
32
|
### Project Structure
|
|
25
33
|
|
|
@@ -109,33 +117,53 @@ if (process.env.NODE_ENV === 'development') {
|
|
|
109
117
|
}
|
|
110
118
|
```
|
|
111
119
|
|
|
120
|
+
### Other Flags
|
|
121
|
+
|
|
122
|
+
| Flag | Effect |
|
|
123
|
+
|------|--------|
|
|
124
|
+
| `LOGGER=true` | Verbose per-plugin logging (spinners, timings, per-file detail). Errors and warnings always print, with or without this flag. |
|
|
125
|
+
| `MINIFY=false` | Skip HTML/CSS/JS minification in a production build (e.g. for a readable staging build). |
|
|
126
|
+
| `BUNDLE=true` | Force CSS/JS bundling in development mode (normally production-only). |
|
|
127
|
+
|
|
112
128
|
---
|
|
113
129
|
|
|
114
130
|
## Build Process
|
|
115
131
|
|
|
116
|
-
The build process performs the following steps:
|
|
132
|
+
The build process performs the following steps, in this order:
|
|
117
133
|
|
|
118
134
|
1. **Creates `/dist`** - Removes existing dist folder and recreates it
|
|
119
|
-
2. **Copies `/src` to `/dist`** - All source files are copied (excludes `.md` files)
|
|
120
|
-
3. **Processes Markdown** - Converts `.md` files to `.html` with front matter support
|
|
121
|
-
4. **
|
|
122
|
-
5. **
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
135
|
+
2. **Copies `/src` to `/dist`** - All source files are copied (excludes `.md` files and `buildOnlyPaths`)
|
|
136
|
+
3. **Processes Markdown** - Converts `.md` files to `.html` with front matter support; groups pages into `collections.*` by directory
|
|
137
|
+
4. **Generates image derivatives** - Responsive AVIF/WebP variants at configured widths, with a cross-build disk cache (`images` config)
|
|
138
|
+
5. **Runs custom-user `before` plugins** - Runs once before file processing
|
|
139
|
+
6. **Converts pages to directories** - `about.html` becomes `about/index.html` for extensionless URLs
|
|
140
|
+
7. **Loops through each allowed file**, in this order per page:
|
|
141
|
+
* Parses YAML front matter from HTML pages
|
|
142
|
+
* Escapes `<code-block data-escape>` content (restored at the end of the loop)
|
|
143
|
+
* Runs any custom-user `default` plugins
|
|
144
|
+
* Applies layout templates (`[data-template]` attribute or front matter `layout`)
|
|
145
|
+
* Replaces include placeholders (`[data-include]`, `[include]`, `<include-file>`)
|
|
146
|
+
* Repeats elements for each item in collections (`[data-repeat]`, nesting supported)
|
|
147
|
+
* Replaces custom elements and `[data-component]` elements with templates (nesting supported; flat or Declarative Shadow DOM)
|
|
148
|
+
* Replaces ES6 template strings (`${}`) with front matter + global data
|
|
149
|
+
* Rewrites `<img>` to responsive `<picture>` elements
|
|
150
|
+
* Detects custom elements and manages component JS loading (`autoComponents` config)
|
|
151
|
+
* Pre-renders Declarative Shadow DOM from a manifest (`dsd` config)
|
|
152
|
+
* Adds missing `http://` protocol to `www.`/`cdn.` links
|
|
129
153
|
* Replaces inline placeholders (`[data-inline]`) with external source (production only)
|
|
130
|
-
* Sets `<a>` tags
|
|
131
|
-
*
|
|
132
|
-
* Stores link/script files marked for bundling (`[bundle]`)
|
|
154
|
+
* Sets `<a>` tags whose `[href]` resolves to the current page as active (`class="active"`), ancestors as `class="active-parent"`
|
|
155
|
+
* Stores link/script files marked for bundling (`[data-bundle]`)
|
|
133
156
|
* Minifies HTML, CSS, and JS source (production only)
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
157
|
+
8. **Generates fragments** - A sibling `_fragment.html` per page for SPA-style navigation (`fragments` config)
|
|
158
|
+
9. **Generates formats** - Markdown copies under `/md/`, JSON under `/api/`, Atom feed, and `llms.txt` (`formats` config)
|
|
159
|
+
10. **Creates `sitemap.xml`** - Auto-generated in `/dist` (fragments excluded)
|
|
160
|
+
11. **Creates bundled files** - `.css` and `.js` bundles are written
|
|
161
|
+
12. **Fingerprints assets** - Opt-in content-hash filenames with HTML and JS-import rewriting (`fingerprint` config)
|
|
162
|
+
13. **Runs custom-user `after` plugins** - Runs once after file processing
|
|
163
|
+
|
|
164
|
+
A page that fails during the loop is reported (with its path) and skipped; the rest of the site still builds, a failure summary prints at the end, and the process exits non-zero so CI catches it.
|
|
137
165
|
|
|
138
|
-
Many build settings can be customized in your project's `/config/main.js
|
|
166
|
+
Many build settings can be customized in your project's `/config/main.js` — see `scripts/utils/config/config.js` for every default. Note that array values in your config **replace** the defaults entirely.
|
|
139
167
|
|
|
140
168
|
---
|
|
141
169
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cook-ssg",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "Cook your static pages into a beautifully performing site. Static site generation the web platform way.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -15,12 +15,10 @@
|
|
|
15
15
|
"files": [
|
|
16
16
|
"bin/",
|
|
17
17
|
"scripts/",
|
|
18
|
-
"patches/",
|
|
19
18
|
"LICENSE",
|
|
20
19
|
"README.md"
|
|
21
20
|
],
|
|
22
21
|
"scripts": {
|
|
23
|
-
"postinstall": "node patches/jsdom-fetch-fix.js",
|
|
24
22
|
"build": "node scripts/build.js",
|
|
25
23
|
"dev": "NODE_ENV=development npm run build && node scripts/dev.js",
|
|
26
24
|
"dev:prod": "NODE_ENV=production npm run build && npx http-server ./dist -p 3000",
|
|
@@ -61,11 +59,10 @@
|
|
|
61
59
|
"fs-extra": "^11.3.2",
|
|
62
60
|
"gray-matter": "^4.0.3",
|
|
63
61
|
"html-minifier-terser": "^7.2.0",
|
|
64
|
-
"jsdom": "^29.1.0",
|
|
65
62
|
"linkedom": "^0.18.12",
|
|
66
63
|
"lodash.clonedeep": "^4.5.0",
|
|
67
64
|
"lodash.merge": "^4.6.2",
|
|
68
|
-
"marked": "^
|
|
65
|
+
"marked": "^18.0.6",
|
|
69
66
|
"rimraf": "^6.1.2",
|
|
70
67
|
"sharp": "^0.34.5",
|
|
71
68
|
"terser": "^5.46.1"
|
package/scripts/build.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// -----------------------------
|
|
3
3
|
import chalk from 'chalk';
|
|
4
4
|
import fs from 'fs-extra';
|
|
5
|
-
import
|
|
5
|
+
import path from 'node:path';
|
|
6
6
|
import { pathToFileURL } from 'node:url';
|
|
7
7
|
|
|
8
8
|
// UTILS
|
|
@@ -27,7 +27,7 @@ import processMarkdown from './plugins/process-markdown.js';
|
|
|
27
27
|
import repeatCollection from './plugins/repeat-collection.js';
|
|
28
28
|
import replaceComponents from './plugins/replace-components.js';
|
|
29
29
|
import applyTemplate from './plugins/apply-template.js';
|
|
30
|
-
import escapeCodeBlocks from './plugins/escape-code-blocks.js';
|
|
30
|
+
import escapeCodeBlocks, { restoreCodeBlocks } from './plugins/escape-code-blocks.js';
|
|
31
31
|
import prerenderCodeBlocks from './plugins/prerender-code-blocks.js';
|
|
32
32
|
import processHtmlFrontmatter from './plugins/process-html-frontmatter.js';
|
|
33
33
|
import replaceInclude from './plugins/replace-include.js';
|
|
@@ -39,6 +39,9 @@ import setActiveLinks from './plugins/set-active-links.js';
|
|
|
39
39
|
// GET SOURCE
|
|
40
40
|
import { getDynamicFiles, getSrcConfig, getSrcFiles } from './utils/get-src/get-src.js';
|
|
41
41
|
|
|
42
|
+
// SHARED POST-LOOP PAGE SNAPSHOT
|
|
43
|
+
import { collectSitePages } from './utils/site-pages/site-pages.js';
|
|
44
|
+
|
|
42
45
|
// CONFIG
|
|
43
46
|
// Combined user overrides on top of internal defaults
|
|
44
47
|
import config, { plugins } from './utils/config/config.js';
|
|
@@ -60,15 +63,9 @@ const store = {
|
|
|
60
63
|
// Get user's data config object. We pass it into plugins (when necessary)
|
|
61
64
|
// so that additional data can be added to it from plugins instead of needing
|
|
62
65
|
// to manually define everything from the start in `/config/data.js`
|
|
63
|
-
// Note:
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
let data = {};
|
|
67
|
-
if (userDataPathExists) {
|
|
68
|
-
const dataModule = await import(pathToFileURL(userDataPath));
|
|
69
|
-
const exported = dataModule.default || dataModule;
|
|
70
|
-
data = typeof exported === 'function' ? await exported() : exported;
|
|
71
|
-
}
|
|
66
|
+
// Note: Loaded via the shared user-data module so every consumer
|
|
67
|
+
// (including get-src's getDynamicFiles) sees the SAME instance.
|
|
68
|
+
import data from './utils/user-data/user-data.js';
|
|
72
69
|
|
|
73
70
|
|
|
74
71
|
// BUILD
|
|
@@ -110,12 +107,28 @@ class Build {
|
|
|
110
107
|
await createDirFromFile({files, allowType: ['.html'] });
|
|
111
108
|
|
|
112
109
|
// THE FILES LOOP
|
|
110
|
+
// A failing page is recorded and skipped — one bad page must not
|
|
111
|
+
// kill the other N-1. Failures are reported after the loop and the
|
|
112
|
+
// process exits non-zero so CI/deploys still catch them.
|
|
113
|
+
const failedPages = [];
|
|
113
114
|
await runFileLoop(files, fileLoop);
|
|
114
115
|
async function fileLoop(fileName) {
|
|
116
|
+
let file;
|
|
117
|
+
try {
|
|
118
|
+
await processPage(fileName);
|
|
119
|
+
} catch (err) {
|
|
120
|
+
const pagePath = (file && file.path) || fileName.path || fileName;
|
|
121
|
+
failedPages.push({ page: pagePath, error: err });
|
|
122
|
+
console.warn(chalk.yellow(`\n[build] Page failed: ${pagePath}`));
|
|
123
|
+
console.warn(` ${err && err.message ? err.message : err}`);
|
|
124
|
+
}
|
|
125
|
+
return fileName;
|
|
126
|
+
|
|
127
|
+
async function processPage(fileName) {
|
|
115
128
|
// Read and store the target file source.
|
|
116
129
|
// We'll pass the string around between the plugins
|
|
117
130
|
// then write back the updated/modified source to the file at the end
|
|
118
|
-
|
|
131
|
+
file = await getSrcConfig({fileName});
|
|
119
132
|
|
|
120
133
|
// PLUGIN: Parse YAML front matter from HTML files
|
|
121
134
|
// Strips front matter from source, stores on file.frontMatter
|
|
@@ -177,14 +190,8 @@ class Build {
|
|
|
177
190
|
await minifySrc({file, disallowType: ['.json', '.webmanifest']});
|
|
178
191
|
|
|
179
192
|
// Restore base64-encoded code-block content from escape-code-blocks plugin.
|
|
180
|
-
// Must run LAST — after all
|
|
181
|
-
|
|
182
|
-
file.src = file.src.replace(/<code-block([^>]*) data-cook-esc="([^"]+)"([^>]*)><\/code-block>/g,
|
|
183
|
-
(_, before, b64, after) => {
|
|
184
|
-
const decoded = Buffer.from(b64, 'base64').toString('utf-8');
|
|
185
|
-
return `<code-block${before}${after}>${decoded}</code-block>`;
|
|
186
|
-
});
|
|
187
|
-
}
|
|
193
|
+
// Must run LAST — after all DOM-based plugins have finished processing.
|
|
194
|
+
file.src = restoreCodeBlocks(file.src);
|
|
188
195
|
|
|
189
196
|
// PLUGIN: Pre-render <code-block> with Declarative Shadow DOM
|
|
190
197
|
// Runs after restore — code-block content is decoded and ready for highlighting.
|
|
@@ -192,15 +199,18 @@ class Build {
|
|
|
192
199
|
|
|
193
200
|
// Write new, modified source back to the file
|
|
194
201
|
await updatePage(file);
|
|
195
|
-
|
|
196
|
-
return fileName;
|
|
202
|
+
}
|
|
197
203
|
}
|
|
198
204
|
|
|
205
|
+
// SHARED PAGE SNAPSHOT: one dist walk + one parse per built page,
|
|
206
|
+
// consumed read-only by fragments and formats below
|
|
207
|
+
const sitePages = await collectSitePages();
|
|
208
|
+
|
|
199
209
|
// PLUGIN: Generate content-only fragments for SPA navigation
|
|
200
|
-
await generateFragments();
|
|
210
|
+
await generateFragments({ pages: sitePages });
|
|
201
211
|
|
|
202
212
|
// PLUGIN: Generate multi-format output (markdown, JSON, feed, llms.txt)
|
|
203
|
-
await generateFormats({store, data});
|
|
213
|
+
await generateFormats({store, data, pages: sitePages});
|
|
204
214
|
|
|
205
215
|
// PLUGIN: Create `sitemap.xml` in the created `/dist` folder
|
|
206
216
|
await generateSitemap();
|
|
@@ -216,9 +226,64 @@ class Build {
|
|
|
216
226
|
|
|
217
227
|
// CUSTOM PLUGINS: Run custom user plugins after file loop
|
|
218
228
|
await customPlugins({store, data, plugins: plugins.after, log: 'After' });
|
|
229
|
+
|
|
230
|
+
// FAILURE SUMMARY
|
|
231
|
+
// Report pages that failed during the loop and mark the build as
|
|
232
|
+
// failed (non-zero exit) without having aborted the other pages.
|
|
233
|
+
if (failedPages.length) {
|
|
234
|
+
console.error(chalk.bold.red(`\n[build] ${failedPages.length} page(s) failed:`));
|
|
235
|
+
failedPages.forEach(({ page, error }) => {
|
|
236
|
+
console.error(chalk.red(` ✖ ${page}`), chalk.gray(error && error.message ? error.message : String(error)));
|
|
237
|
+
});
|
|
238
|
+
process.exitCode = 1;
|
|
239
|
+
}
|
|
219
240
|
}
|
|
220
241
|
|
|
221
242
|
}
|
|
222
243
|
|
|
223
|
-
|
|
224
|
-
|
|
244
|
+
/**
|
|
245
|
+
* Run one build. Exported so the dev server can rebuild IN-PROCESS —
|
|
246
|
+
* module imports, config, and template caches stay warm instead of
|
|
247
|
+
* paying a cold Node start per file save.
|
|
248
|
+
*
|
|
249
|
+
* Per-build state is reset here:
|
|
250
|
+
* - bundle groups and dynamic pages always (they accumulate per build)
|
|
251
|
+
* - template/include/component caches only when NOT a single-page
|
|
252
|
+
* fast-path rebuild (a build-only file change means they're stale)
|
|
253
|
+
*/
|
|
254
|
+
export async function runBuild() {
|
|
255
|
+
const isSinglePageRebuild = Util.validatePageChange();
|
|
256
|
+
if (!isSinglePageRebuild) {
|
|
257
|
+
delete store.cachedIncludes;
|
|
258
|
+
delete store.cachedComponents;
|
|
259
|
+
delete store.cachedLayouts;
|
|
260
|
+
delete store.cachedInline;
|
|
261
|
+
}
|
|
262
|
+
store.bundle = { css: {}, js: {} };
|
|
263
|
+
// Collections are fully rebuilt by processMarkdown every run — stale
|
|
264
|
+
// entries would otherwise accumulate (duplicate blog posts on the
|
|
265
|
+
// second in-process build)
|
|
266
|
+
delete store.collections;
|
|
267
|
+
if (data.dynamicPages) data.dynamicPages = [];
|
|
268
|
+
|
|
269
|
+
await new Build().init();
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// Run the build when executed directly (`node scripts/build.js`,
|
|
273
|
+
// `cook build`) — NOT when imported by the dev server or tests.
|
|
274
|
+
const isMain = process.argv[1]
|
|
275
|
+
&& import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href;
|
|
276
|
+
|
|
277
|
+
if (isMain) {
|
|
278
|
+
(async () => {
|
|
279
|
+
try {
|
|
280
|
+
await runBuild();
|
|
281
|
+
} catch (err) {
|
|
282
|
+
// Truly unrecoverable (config, dist creation, plugin crash outside
|
|
283
|
+
// the per-page contract): report clearly and fail the process.
|
|
284
|
+
console.error(chalk.bold.red('\n[build] Fatal error'));
|
|
285
|
+
console.error(err);
|
|
286
|
+
process.exit(1);
|
|
287
|
+
}
|
|
288
|
+
})();
|
|
289
|
+
}
|
package/scripts/dev.js
CHANGED
|
@@ -2,13 +2,12 @@
|
|
|
2
2
|
// -----------------------------
|
|
3
3
|
import browserSyncLib from 'browser-sync';
|
|
4
4
|
import chalk from 'chalk';
|
|
5
|
-
import { execSync } from 'node:child_process';
|
|
6
5
|
import { existsSync } from 'node:fs';
|
|
7
6
|
import path from 'node:path';
|
|
8
|
-
import { fileURLToPath } from 'node:url';
|
|
9
7
|
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
// In-process rebuilds: no cold Node process per save — module imports,
|
|
9
|
+
// config, and (for single-page rebuilds) template caches stay warm
|
|
10
|
+
import { runBuild } from './build.js';
|
|
12
11
|
|
|
13
12
|
const browserSync = browserSyncLib.create('Dev Server');
|
|
14
13
|
|
|
@@ -101,19 +100,50 @@ browserSync.emitter.on('init', () => {
|
|
|
101
100
|
// Watch changes to `/src` files and run the build process to copy
|
|
102
101
|
// to `/dist` equivalent. This way we can run livereload on `/dist` to
|
|
103
102
|
// view replaced includes, and to avoid mutating `/src` files directly.
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
103
|
+
// REBUILD QUEUE
|
|
104
|
+
// One rebuild at a time; a save during a rebuild queues the LATEST
|
|
105
|
+
// change and runs once the current rebuild finishes.
|
|
106
|
+
let building = false;
|
|
107
|
+
let queuedFile = null;
|
|
108
|
+
|
|
109
|
+
async function rebuild(file) {
|
|
110
|
+
if (building) {
|
|
111
|
+
queuedFile = file;
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
building = true;
|
|
115
|
+
|
|
116
|
+
// DEV_CHANGED_PAGE tells the build whether to run the whole pipeline
|
|
117
|
+
// or just this changed file (see validatePageChange)
|
|
118
|
+
process.env.DEV_CHANGED_PAGE = file;
|
|
119
|
+
let ok = true;
|
|
120
|
+
try {
|
|
121
|
+
await runBuild();
|
|
122
|
+
} catch (err) {
|
|
123
|
+
// A failed rebuild must NOT take down the dev server
|
|
124
|
+
ok = false;
|
|
125
|
+
console.error(chalk.red(`\n[dev] Rebuild failed for ${file} — fix the error and save again.`));
|
|
126
|
+
console.error(err && err.message ? err.message : err);
|
|
127
|
+
} finally {
|
|
128
|
+
delete process.env.DEV_CHANGED_PAGE;
|
|
129
|
+
building = false;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (ok) {
|
|
115
133
|
// Reload changed page
|
|
116
134
|
const fileDist = file.replace(`${srcPath}/`, `${distPath}/`);
|
|
117
135
|
browserSync.reload(fileDist);
|
|
118
|
-
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Run the latest queued change, if any arrived mid-build
|
|
139
|
+
if (queuedFile) {
|
|
140
|
+
const next = queuedFile;
|
|
141
|
+
queuedFile = null;
|
|
142
|
+
rebuild(next);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
watchFiles.forEach(watchPath => {
|
|
147
|
+
// Watch `/src` files for changes
|
|
148
|
+
browserSync.watch(`${srcPath}${watchPath}`).on('change', file => { rebuild(file); });
|
|
119
149
|
});
|
|
@@ -20,6 +20,7 @@ import { promises as fs } from 'node:fs';
|
|
|
20
20
|
import { existsSync } from 'node:fs';
|
|
21
21
|
import path from 'node:path';
|
|
22
22
|
import Util from '../utils/util/util.js';
|
|
23
|
+
import { renderTemplate } from '../utils/interpolate/interpolate.js';
|
|
23
24
|
|
|
24
25
|
// Config
|
|
25
26
|
import { srcPath, distPath } from '../utils/config/config.js';
|
|
@@ -192,24 +193,14 @@ class ApplyTemplate {
|
|
|
192
193
|
* @returns {string} Merged HTML
|
|
193
194
|
*/
|
|
194
195
|
applyTemplate(template, pageData) {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
//
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
// Exception: unmatched ${slot:...} placeholders are removed (no content provided for that slot)
|
|
203
|
-
html = html.replace(/\$\{([^}]+)\}/g, (match, key) => {
|
|
204
|
-
const trimmedKey = key.trim();
|
|
205
|
-
if (trimmedKey === 'content') return match; // Already replaced
|
|
206
|
-
if (pageData[trimmedKey] !== undefined) return pageData[trimmedKey];
|
|
207
|
-
// Remove unfilled slot placeholders rather than leaving them in output
|
|
208
|
-
if (trimmedKey.startsWith('slot:')) return '';
|
|
209
|
-
return match;
|
|
196
|
+
// Shared interpolator: single pass over the LAYOUT only, callback
|
|
197
|
+
// insertion (page content with $-patterns and ${...} in code samples
|
|
198
|
+
// is never scanned), unmatched vars deferred to replaceTemplateStrings,
|
|
199
|
+
// unfilled ${slot:...} removed. ${content} always resolves (empty
|
|
200
|
+
// string when the page produced none).
|
|
201
|
+
return renderTemplate(template, { ...pageData, content: pageData.content || '' }, {
|
|
202
|
+
removeUnmatchedSlots: true,
|
|
210
203
|
});
|
|
211
|
-
|
|
212
|
-
return html;
|
|
213
204
|
}
|
|
214
205
|
|
|
215
206
|
|
|
@@ -67,7 +67,7 @@ class AutoComponents {
|
|
|
67
67
|
await this.loadManifest();
|
|
68
68
|
|
|
69
69
|
// Parse HTML for custom elements
|
|
70
|
-
const dom = Util.
|
|
70
|
+
const dom = Util.fileDom(this.file);
|
|
71
71
|
const document = dom.window.document;
|
|
72
72
|
|
|
73
73
|
// Find all custom element tags in the document
|
|
@@ -99,7 +99,7 @@ class AutoComponents {
|
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
// Store updated file source
|
|
102
|
-
this.file
|
|
102
|
+
Util.commitDom(this.file, dom);
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
/**
|
|
@@ -81,7 +81,7 @@ class Bundle {
|
|
|
81
81
|
const { file } = this;
|
|
82
82
|
|
|
83
83
|
// Make source traversable with JSDOM
|
|
84
|
-
const dom = Util.
|
|
84
|
+
const dom = Util.fileDom(file);
|
|
85
85
|
const document = dom.window.document;
|
|
86
86
|
|
|
87
87
|
// Find `<link>` or `<script>` tags with the `[data-bundle]` or `[bundle]` attributes
|
|
@@ -101,7 +101,7 @@ class Bundle {
|
|
|
101
101
|
this.groupSrcAndInsertBundle(scripts, 'js');
|
|
102
102
|
|
|
103
103
|
// Store updated file source
|
|
104
|
-
|
|
104
|
+
Util.commitDom(file, dom);
|
|
105
105
|
|
|
106
106
|
// END LOGGING
|
|
107
107
|
this.endAddLog();
|
|
@@ -270,7 +270,10 @@ class Bundle {
|
|
|
270
270
|
return src;
|
|
271
271
|
}
|
|
272
272
|
catch (err) {
|
|
273
|
-
|
|
273
|
+
// Missing/broken bundle asset: warn and contribute nothing to the
|
|
274
|
+
// bundle rather than aborting the whole build
|
|
275
|
+
Util.customWarn(err, `bundle: could not read source from: ${path} (skipped)`);
|
|
276
|
+
return '';
|
|
274
277
|
}
|
|
275
278
|
}
|
|
276
279
|
|
|
@@ -34,7 +34,7 @@ class CopySrc {
|
|
|
34
34
|
// Show terminal message: Start
|
|
35
35
|
Logger.persist.header(`\nCopied Updated Page`);
|
|
36
36
|
// Copy changed page to `/dist` only
|
|
37
|
-
await fs.copy(changedSrcPath, changedDistPath).catch(err => Util.
|
|
37
|
+
await fs.copy(changedSrcPath, changedDistPath).catch(err => Util.customWarn(err, 'CopySrc.init(): Page Change'));
|
|
38
38
|
// Show terminal message
|
|
39
39
|
Logger.persist.success(`/${changedSrcPath} copied to /${changedDistPath}`);
|
|
40
40
|
}
|
|
@@ -111,8 +111,10 @@ class CreateDirFromFile {
|
|
|
111
111
|
const matchedExcludePathLen = matchedExcludePath.length;
|
|
112
112
|
if (matchedExcludePathLen) return;
|
|
113
113
|
|
|
114
|
-
// Get file path without extension
|
|
115
|
-
|
|
114
|
+
// Get file path without the trailing `.html` extension only —
|
|
115
|
+
// dots elsewhere in the path (node.js-guide.html, v1.2/page.html)
|
|
116
|
+
// are part of real file/directory names.
|
|
117
|
+
const filePath = file.path.replace(/\.html$/, '');
|
|
116
118
|
|
|
117
119
|
// CREATE NEW DIRECTORY IN /DIST
|
|
118
120
|
await fs.mkdirp(filePath);
|
|
@@ -96,7 +96,7 @@ class CustomPlugins {
|
|
|
96
96
|
await new plugin[Object.keys(plugin)[0]]({file, data}).init();
|
|
97
97
|
}
|
|
98
98
|
catch (err) {
|
|
99
|
-
Util.
|
|
99
|
+
Util.customWarn(err, `Plugin: ${this.plugins[index]}.js`)
|
|
100
100
|
}
|
|
101
101
|
|
|
102
102
|
// GO TO NEXT PLUGIN
|
|
@@ -68,7 +68,7 @@ class DeclarativeShadowDom {
|
|
|
68
68
|
if (!this.store._dsdManifest) return;
|
|
69
69
|
|
|
70
70
|
// Parse HTML
|
|
71
|
-
const dom = Util.
|
|
71
|
+
const dom = Util.fileDom(this.file);
|
|
72
72
|
const document = dom.window.document;
|
|
73
73
|
|
|
74
74
|
// Find all custom elements that have DSD templates
|
|
@@ -93,7 +93,7 @@ class DeclarativeShadowDom {
|
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
if (injected > 0) {
|
|
96
|
-
this.file
|
|
96
|
+
Util.commitDom(this.file, dom);
|
|
97
97
|
this.store._dsdStats.total += injected;
|
|
98
98
|
}
|
|
99
99
|
}
|
|
@@ -28,7 +28,8 @@ class EscapeCodeBlocks {
|
|
|
28
28
|
const pattern = /(<code-block\b[^>]*\bdata-escape\b[^>]*>)([\s\S]*?)(<\/code-block>)/gi;
|
|
29
29
|
|
|
30
30
|
this.file.src = this.file.src.replace(pattern, (match, openTag, content, closeTag) => {
|
|
31
|
-
|
|
31
|
+
// Remove data-escape whether bare or valued (data-escape="true")
|
|
32
|
+
const cleanedTag = openTag.replace(/\s*\bdata-escape(?:="[^"]*")?/, '');
|
|
32
33
|
const trimmed = content.replace(/^\n+/, '').replace(/\n+$/, '');
|
|
33
34
|
const escaped = this.escapeHtml(trimmed);
|
|
34
35
|
const encoded = Buffer.from(escaped).toString('base64');
|
|
@@ -51,4 +52,24 @@ class EscapeCodeBlocks {
|
|
|
51
52
|
}
|
|
52
53
|
}
|
|
53
54
|
|
|
55
|
+
/**
|
|
56
|
+
* Restore base64-encoded code-block content (the counterpart of the
|
|
57
|
+
* escape pass above). Must run AFTER all DOM-based plugins are done
|
|
58
|
+
* with the page. Lives here so escape/restore stay in one place.
|
|
59
|
+
* @param {string} src - Page source
|
|
60
|
+
* @returns {string}
|
|
61
|
+
*/
|
|
62
|
+
export function restoreCodeBlocks(src) {
|
|
63
|
+
if (!src.includes('data-cook-esc')) return src;
|
|
64
|
+
// [^"]* (not +): an empty code block encodes to data-cook-esc=""
|
|
65
|
+
// and must still be restored/cleaned
|
|
66
|
+
return src.replace(
|
|
67
|
+
/<code-block\b([^>]*?)\s*\bdata-cook-esc="([^"]*)"([^>]*)>([\s\S]*?)<\/code-block>/gi,
|
|
68
|
+
(match, before, b64, after) => {
|
|
69
|
+
const decoded = Buffer.from(b64, 'base64').toString('utf-8');
|
|
70
|
+
return `<code-block${before}${after}>${decoded}</code-block>`;
|
|
71
|
+
}
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
54
75
|
export default EscapeCodeBlocks.export;
|