@apleasantview/eleventy-plugin-baseline 0.1.0-next.33 → 0.1.0-next.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.
Files changed (59) hide show
  1. package/README.md +48 -23
  2. package/core/content-map-store.js +51 -0
  3. package/core/filters/index.js +4 -0
  4. package/core/filters/isString.js +1 -1
  5. package/core/filters/related-posts.js +1 -1
  6. package/core/global-functions/index.js +6 -0
  7. package/core/logging.js +54 -23
  8. package/core/page-context.js +310 -0
  9. package/core/registry.js +110 -0
  10. package/core/schema.js +70 -0
  11. package/core/shortcodes/image.js +8 -3
  12. package/core/shortcodes/index.js +2 -0
  13. package/core/slug-index.js +61 -0
  14. package/core/translation-map-store.js +46 -0
  15. package/core/types.js +73 -0
  16. package/core/utils/helpers.js +75 -0
  17. package/core/utils/pick.js +7 -0
  18. package/core/virtual-dir.js +111 -0
  19. package/core/wikilinks.js +152 -0
  20. package/index.js +364 -0
  21. package/modules/assets/index.js +162 -0
  22. package/modules/assets/processors/esbuild-process.js +68 -0
  23. package/modules/{assets-postcss/process.js → assets/processors/postcss-process.js} +39 -2
  24. package/modules/assets/schema.js +14 -0
  25. package/modules/head/drivers/capo-adapter.js +94 -0
  26. package/modules/head/drivers/posthtml-head-elements.js +140 -0
  27. package/modules/head/index.js +106 -0
  28. package/modules/head/schema.js +42 -0
  29. package/modules/head/utils/alternates.js +11 -0
  30. package/modules/head/utils/dedupe.js +47 -0
  31. package/modules/multilang/index.js +149 -0
  32. package/modules/navigator/index.js +140 -0
  33. package/modules/navigator/schema.js +13 -0
  34. package/modules/{navigator-core → navigator}/templates/navigator-core.html +10 -4
  35. package/{core → modules/navigator/utils}/debug.js +7 -1
  36. package/modules/sitemap/index.js +121 -0
  37. package/modules/{sitemap-core → sitemap}/templates/sitemap-core.html +2 -2
  38. package/modules/{sitemap-core → sitemap}/templates/sitemap-index.html +2 -2
  39. package/modules.js +6 -0
  40. package/package.json +15 -6
  41. package/core/filters.js +0 -9
  42. package/core/globals.js +0 -6
  43. package/core/helpers.js +0 -36
  44. package/core/modules.js +0 -18
  45. package/core/shortcodes.js +0 -3
  46. package/eleventy.config.js +0 -169
  47. package/modules/assets-core/plugins/assets-core.js +0 -197
  48. package/modules/assets-esbuild/process.js +0 -33
  49. package/modules/head-core/drivers/posthtml-head-elements.js +0 -127
  50. package/modules/head-core/plugins/head-core.js +0 -75
  51. package/modules/head-core/utils/head-utils.js +0 -249
  52. package/modules/multilang-core/plugins/multilang-core.js +0 -118
  53. package/modules/navigator-core/plugins/navigator-core.js +0 -57
  54. package/modules/sitemap-core/plugins/sitemap-core.js +0 -88
  55. /package/core/{globals → global-functions}/date.js +0 -0
  56. /package/modules/{assets-postcss/fallback → assets/configs}/postcss.config.js +0 -0
  57. /package/modules/{multilang-core → multilang}/filters/i18n-default-translation.js +0 -0
  58. /package/modules/{multilang-core → multilang}/filters/i18n-translation-in.js +0 -0
  59. /package/modules/{multilang-core → multilang}/filters/i18n-translations-for.js +0 -0
@@ -0,0 +1,152 @@
1
+ import { slugify } from './utils/helpers.js';
2
+
3
+ /**
4
+ * Wikilinks (runtime substrate)
5
+ *
6
+ * MediaWiki-style inline link syntax for body markdown. Recognises
7
+ * [[slug]], [[slug#anchor]], [[slug:lang]], [[slug|alias]], and any
8
+ * combination. Resolves slugs against the slug index and, when a lang
9
+ * suffix is given, hops to the requested translation. Misses render as
10
+ * the original literal text.
11
+ *
12
+ * Architecture layer:
13
+ * runtime substrate
14
+ *
15
+ * System role:
16
+ * Markdown-it plugin registered by the composition root via
17
+ * amendLibrary('md', ...). Reads the slug index, page-context registry,
18
+ * and translation-map store; writes nothing back.
19
+ *
20
+ * Lifecycle:
21
+ * transform-time → parses [[...]] in body markdown and emits link or
22
+ * text tokens
23
+ *
24
+ * Why this exists:
25
+ * Markdown-it has no wiki-link syntax. Eleventy resolves all
26
+ * eleventyComputed values before any body renders, so the slug index
27
+ * and translation map are complete by the time this rule fires.
28
+ * Resolution is deterministic without a two-pass build.
29
+ *
30
+ * Scope:
31
+ * Owns syntax parsing, slug-to-href resolution, the lang hop, and link
32
+ * rendering with class/lang/hreflang attributes.
33
+ * Does not own slug derivation (page-context), index population, or the
34
+ * translation-map shape (multilang module).
35
+ *
36
+ * Data flow:
37
+ * markdown-it inline state → slug index + page context + translation map → link tokens
38
+ *
39
+ * @param {import('markdown-it').default} md
40
+ * @param {Object} deps
41
+ * @param {{getBySlug: (slug: string) => string | null}} deps.slugIndex
42
+ * @param {{getByKey: (url: string) => any}} deps.pageContextRegistry
43
+ * @param {{get: () => Record<string, Record<string, {url: string, title?: string}>> | null}} [deps.translationMapStore]
44
+ */
45
+ export function wikilinks(md, { slugIndex, pageContextRegistry, translationMapStore } = {}) {
46
+ if (!slugIndex || !pageContextRegistry) {
47
+ throw new Error('wikilinks plugin requires { slugIndex, pageContextRegistry }');
48
+ }
49
+
50
+ function parse(inner) {
51
+ // [[target|alias]]
52
+ const pipeIdx = inner.indexOf('|');
53
+ const target = pipeIdx === -1 ? inner : inner.slice(0, pipeIdx);
54
+ const alias = pipeIdx === -1 ? null : inner.slice(pipeIdx + 1).trim() || null;
55
+
56
+ // [[slug-or-langed#anchor]]
57
+ const hashIdx = target.indexOf('#');
58
+ const slugAndLang = hashIdx === -1 ? target : target.slice(0, hashIdx);
59
+ const rawAnchor = hashIdx === -1 ? null : target.slice(hashIdx + 1).trim() || null;
60
+ const anchor = rawAnchor ? slugify(rawAnchor) : null;
61
+
62
+ // [[slug:lang]]
63
+ const colonIdx = slugAndLang.indexOf(':');
64
+ const rawSlug = colonIdx === -1 ? slugAndLang : slugAndLang.slice(0, colonIdx);
65
+ const rawLang = colonIdx === -1 ? null : slugAndLang.slice(colonIdx + 1).trim() || null;
66
+
67
+ return { rawSlug: rawSlug.trim(), rawLang, anchor, alias };
68
+ }
69
+
70
+ function resolve({ rawSlug, rawLang, anchor, alias }) {
71
+ const slug = slugify(rawSlug);
72
+ if (!slug) return null;
73
+
74
+ const url = slugIndex.getBySlug(slug);
75
+ if (!url) return null;
76
+
77
+ const ctx = pageContextRegistry.getByKey(url);
78
+ const defaultTitle = ctx?.entry?.title ?? rawSlug;
79
+
80
+ const withAnchor = (u) => (anchor ? `${u}#${anchor}` : u);
81
+
82
+ if (!rawLang) {
83
+ return {
84
+ href: withAnchor(url),
85
+ label: alias ?? defaultTitle,
86
+ lang: null
87
+ };
88
+ }
89
+
90
+ const lang = rawLang.toLowerCase();
91
+ const translationKey = ctx?.page?.locale?.translationKey;
92
+ if (!translationKey) return null;
93
+
94
+ const map = translationMapStore?.get?.();
95
+ const entry = map?.[translationKey]?.[lang];
96
+ if (!entry) return null;
97
+
98
+ return {
99
+ href: withAnchor(entry.url),
100
+ label: alias ?? entry.title ?? defaultTitle,
101
+ lang
102
+ };
103
+ }
104
+
105
+ function tokenize(state, silent) {
106
+ const start = state.pos;
107
+ const src = state.src;
108
+
109
+ if (src.charCodeAt(start) !== 0x5b /* [ */) return false;
110
+ if (src.charCodeAt(start + 1) !== 0x5b) return false;
111
+
112
+ const end = src.indexOf(']]', start + 2);
113
+ if (end === -1) return false;
114
+
115
+ const inner = src.slice(start + 2, end);
116
+ // Reject nested brackets to keep the rule unambiguous.
117
+ if (inner.includes('[') || inner.includes(']')) return false;
118
+
119
+ if (silent) {
120
+ state.pos = end + 2;
121
+ return true;
122
+ }
123
+
124
+ const literal = src.slice(start, end + 2);
125
+ const parsed = parse(inner);
126
+ const result = resolve(parsed);
127
+
128
+ if (!result) {
129
+ const t = state.push('text', '', 0);
130
+ t.content = literal;
131
+ } else {
132
+ const open = state.push('link_open', 'a', 1);
133
+ const attrs = [
134
+ ['href', result.href],
135
+ ['class', 'wikilink']
136
+ ];
137
+ if (result.lang) {
138
+ attrs.push(['lang', result.lang]);
139
+ attrs.push(['hreflang', result.lang]);
140
+ }
141
+ open.attrs = attrs;
142
+ const text = state.push('text', '', 0);
143
+ text.content = result.label;
144
+ state.push('link_close', 'a', -1);
145
+ }
146
+
147
+ state.pos = end + 2;
148
+ return true;
149
+ }
150
+
151
+ md.inline.ruler.before('link', 'wikilink', tokenize);
152
+ }
package/index.js ADDED
@@ -0,0 +1,364 @@
1
+ import 'dotenv/config';
2
+ import { createRequire } from 'node:module';
3
+
4
+ import { HtmlBasePlugin } from '@11ty/eleventy';
5
+ import { eleventyImageOnRequestDuringServePlugin } from '@11ty/eleventy-img';
6
+
7
+ import { createLogger } from './core/logging.js';
8
+ import { createContentMapStore } from './core/content-map-store.js';
9
+ import { createTranslationMapStore } from './core/translation-map-store.js';
10
+ import { createSlugIndex } from './core/slug-index.js';
11
+ import { registerVirtualDir } from './core/virtual-dir.js';
12
+ import { registerPageContext } from './core/page-context.js';
13
+ import { wikilinks } from './core/wikilinks.js';
14
+ import { settingsSchema } from './core/schema.js';
15
+
16
+ import { registerGlobals } from './core/global-functions/index.js';
17
+ import { markdownFilter, relatedPostsFilter, isStringFilter } from './core/filters/index.js';
18
+ import { imageShortcode } from './core/shortcodes/index.js';
19
+ import { assetsCore, headCore, multilangCore, navigatorCore, sitemapCore } from './modules.js';
20
+
21
+ const __require = createRequire(import.meta.url);
22
+ const { name, version } = __require('./package.json');
23
+
24
+ const mode = process.env.ELEVENTY_ENV;
25
+ const isDev = mode === 'development';
26
+ const isProd = mode === 'production';
27
+
28
+ const LEGACY_OPTION_KEYS = [
29
+ 'verbose',
30
+ 'enableNavigatorTemplate',
31
+ 'enableSitemapTemplate',
32
+ 'assetsESBuild',
33
+ 'multilingual'
34
+ ];
35
+
36
+ // Whitelist of reserved global data keys used internally across the plugin.
37
+ // Positive side effect is they all get listed in order and merge data to the same key.
38
+ // Also prevents name collision with filters.
39
+ const INTERNAL_KEYS = [
40
+ '_baseline',
41
+ '_assets',
42
+ '_head',
43
+ '_multilang',
44
+ '_navigator',
45
+ '_sitemap',
46
+ '_snapshot',
47
+ '_pageContext'
48
+ ];
49
+
50
+ /**
51
+ * Detect legacy single-object plugin invocation.
52
+ *
53
+ * The original plugin API accepted a single merged configuration object.
54
+ * This helper detects that shape and enables safe normalization into
55
+ * the current (settings, options) contract.
56
+ *
57
+ * NOTE: arguments.length is required because default parameters mask arity.
58
+ */
59
+ function looksLikeLegacyOptions(firstArg, argsLength) {
60
+ if (argsLength >= 2) return false;
61
+ if (!firstArg || typeof firstArg !== 'object') return false;
62
+ return LEGACY_OPTION_KEYS.some((key) => key in firstArg);
63
+ }
64
+
65
+ /**
66
+ * Normalize legacy plugin input into the current structured contract.
67
+ *
68
+ * - settings → site identity (content + SEO concerns)
69
+ * - options → runtime behavior flags
70
+ */
71
+ function splitLegacyOptions(legacy) {
72
+ const { defaultLanguage, languages, ...rest } = legacy;
73
+ return {
74
+ settings: { defaultLanguage, languages },
75
+ options: rest
76
+ };
77
+ }
78
+
79
+ /**
80
+ * Baseline (composition root)
81
+ *
82
+ * Eleventy plugin entry point. Normalises user input, builds the runtime
83
+ * substrate (stores + page-context registry), and registers feature modules
84
+ * in deterministic order.
85
+ *
86
+ * Architecture layer:
87
+ * composition root
88
+ *
89
+ * System role:
90
+ * The single place that wires settings + options into state, attaches
91
+ * lifecycle stores, registers the page-context registry, and hands a
92
+ * uniform module context to each module. No feature behaviour lives here.
93
+ *
94
+ * Lifecycle:
95
+ * build-time → legacy-shape detection, state computation, virtual dir
96
+ * registration, store and page-context registration, module
97
+ * wiring
98
+ *
99
+ * Why this exists:
100
+ * Modules need a stable, normalised input contract and a shared runtime
101
+ * surface. Centralising the wiring keeps activation rules, option
102
+ * inference, and registration order in one auditable place.
103
+ *
104
+ * Scope:
105
+ * Owns the legacy-shape compatibility shim, state computation, runtime
106
+ * store creation, page-context registration, and the module registry.
107
+ * Does not own any feature behaviour; modules implement that.
108
+ *
109
+ * Data flow:
110
+ * settings + options → state → runtime stores + page-context registry →
111
+ * modules
112
+ *
113
+ * Typedefs (BaselineSettings, BaselineOptions, BaselineState, BaselineContext)
114
+ * live in core/types.js.
115
+ *
116
+ * @param {import('./core/types.js').BaselineSettings} [settings]
117
+ * @param {import('./core/types.js').BaselineOptions} [options]
118
+ */
119
+ export default function baseline(settings = {}, options = {}) {
120
+ // --- Legacy compatibility layer ---
121
+ const argsLength = arguments.length;
122
+ const wasLegacy = looksLikeLegacyOptions(settings, argsLength);
123
+
124
+ if (wasLegacy) {
125
+ const split = splitLegacyOptions(settings);
126
+ settings = split.settings;
127
+ options = split.options;
128
+ }
129
+
130
+ // Base logger outputs regardless of options.
131
+ const baseLog = createLogger(null, { verbose: true });
132
+
133
+ // Scoped logging.
134
+ function scopedLog(name) {
135
+ return createLogger(name, { verbose: options.verbose });
136
+ }
137
+
138
+ if (wasLegacy) {
139
+ baseLog.info('DEPRECATED: single-object plugin arg. Use baseline(settings, options) instead.');
140
+ }
141
+
142
+ // Validate configuration shape (non-fatal).
143
+ const parsed = settingsSchema.safeParse(settings);
144
+ if (!parsed.success) {
145
+ for (const issue of parsed.error.issues) {
146
+ baseLog.info('settings:', `${issue.path.join('.')} — ${issue.message}`);
147
+ }
148
+ }
149
+
150
+ baseLog.info('Eleventy Baseline', version);
151
+
152
+ /**
153
+ * Eleventy plugin initializer.
154
+ *
155
+ * This function is executed during Eleventy configuration time and
156
+ * composes global APIs, filters, shortcodes, and feature modules.
157
+ */
158
+ const plugin = async function (eleventyConfig) {
159
+ // --- Eleventy compatibility check ---
160
+ try {
161
+ eleventyConfig.versionCheck('>=3.0');
162
+ } catch (e) {
163
+ baseLog.error('Eleventy version mismatch:', e.message);
164
+ }
165
+
166
+ INTERNAL_KEYS.forEach((key) => {
167
+ eleventyConfig.addGlobalData(key, {});
168
+ });
169
+
170
+ const env = {
171
+ name: 'Eleventy Baseline',
172
+ package: name,
173
+ version,
174
+ mode
175
+ };
176
+
177
+ eleventyConfig.addGlobalData('_baseline', {
178
+ env
179
+ });
180
+
181
+ if (!settings.url) {
182
+ baseLog.warn('settings.url missing — canonical URLs will be relative');
183
+ }
184
+
185
+ registerGlobals(eleventyConfig);
186
+
187
+ eleventyConfig.addPlugin(HtmlBasePlugin, {
188
+ baseHref: process.env.URL || eleventyConfig.pathPrefix
189
+ });
190
+
191
+ // --- State layer (authoritative configuration) ---
192
+ const hasImageTransformPlugin = eleventyConfig.hasPlugin('eleventyImageTransformPlugin');
193
+
194
+ const state = {
195
+ settings: {
196
+ title: settings.title,
197
+ tagline: settings.tagline,
198
+ url: settings.url,
199
+ noindex: settings.noindex ?? false,
200
+ defaultLanguage: settings.defaultLanguage,
201
+ languages: settings.languages,
202
+ head: settings.head
203
+ },
204
+
205
+ options: {
206
+ verbose: options.verbose ?? false,
207
+ multilang: options.multilingual ?? false,
208
+ sitemap: options.sitemap ?? options.enableSitemapTemplate ?? true,
209
+ navigator: options.navigator ?? options.enableNavigatorTemplate ?? isDev,
210
+ head: {
211
+ titleSeparator: options.head?.titleSeparator,
212
+ showGenerator: options.head?.showGenerator
213
+ },
214
+ assets: {
215
+ esbuild: options.assets?.esbuild ?? options.assetsESBuild ?? {}
216
+ }
217
+ }
218
+ };
219
+
220
+ eleventyConfig.addGlobalData('_baseline', {
221
+ features: {
222
+ ...state.options,
223
+ hasImageTransformPlugin
224
+ }
225
+ });
226
+
227
+ // --- Virtual directories ---
228
+ registerVirtualDir(eleventyConfig, {
229
+ key: 'assets'
230
+ });
231
+
232
+ const publicDir = registerVirtualDir(eleventyConfig, {
233
+ key: 'public',
234
+ outputDir: ''
235
+ });
236
+
237
+ const directories = {
238
+ input: eleventyConfig.directories?.input,
239
+ output: eleventyConfig.directories?.output,
240
+ includes: eleventyConfig.directories?.includes,
241
+ data: eleventyConfig.directories?.data,
242
+ assets: eleventyConfig.directories?.assets,
243
+ public: eleventyConfig.directories?.public
244
+ };
245
+
246
+ eleventyConfig.addPassthroughCopy({ [publicDir.input]: '/' });
247
+
248
+ // Add paths to global.
249
+ eleventyConfig.addGlobalData('_baseline', {
250
+ paths: {
251
+ ...directories
252
+ }
253
+ });
254
+
255
+ // --- Draft filtering (build-time concern) ---
256
+ if (!eleventyConfig.preprocessors.drafts) {
257
+ eleventyConfig.addPreprocessor('drafts', '*', (data) => {
258
+ if (data.draft && process.env.ELEVENTY_RUN_MODE === 'build') {
259
+ return false;
260
+ }
261
+ });
262
+ }
263
+
264
+ // --- Runtime stores (self-attach their lifecycle listeners) ---
265
+ const contentMapStore = createContentMapStore(eleventyConfig);
266
+ const translationMapStore = createTranslationMapStore(eleventyConfig);
267
+ const slugIndex = createSlugIndex(eleventyConfig);
268
+
269
+ // --- Module helpers (derived state) ---
270
+ const helpers = {};
271
+
272
+ // --- Core context (lazy access layer) ---
273
+ const coreContext = {
274
+ env,
275
+ state,
276
+ runtime: {
277
+ get contentMap() {
278
+ return contentMapStore.get();
279
+ },
280
+ translationMap: translationMapStore,
281
+ slugIndex
282
+ },
283
+ directories,
284
+ helpers
285
+ };
286
+
287
+ // Page context registry
288
+ const pageContextRegistry = registerPageContext(eleventyConfig, coreContext);
289
+
290
+ // Wikilinks: [[slug]] / [[slug | lang]] in body markdown.
291
+ eleventyConfig.amendLibrary('md', (md) => {
292
+ md.use(wikilinks, { slugIndex, pageContextRegistry, translationMapStore });
293
+ });
294
+
295
+ coreContext.snapshots = {
296
+ contentMap: () => contentMapStore.snapshot(),
297
+ pageContext: () => pageContextRegistry.snapshot()
298
+ };
299
+
300
+ // --- Module activation ---
301
+ const features = {
302
+ multilang: Boolean(state.options.multilang),
303
+ sitemap: Boolean(state.options.sitemap),
304
+ navigator: Boolean(state.options.navigator),
305
+ head: true,
306
+ assets: true
307
+ };
308
+
309
+ // --- Module registry ---
310
+ const moduleRegistry = [
311
+ { when: features.multilang, name: 'multilang', plugin: multilangCore },
312
+ { when: features.sitemap, name: 'sitemap', plugin: sitemapCore },
313
+ { name: 'navigator', plugin: navigatorCore },
314
+ { when: features.head, name: 'head', plugin: headCore, consumes: { pageContext: true } },
315
+ { when: features.assets, name: 'assets', plugin: assetsCore }
316
+ ];
317
+
318
+ for (const entry of moduleRegistry) {
319
+ const { when = true, name, plugin, consumes = {} } = entry;
320
+ if (!when) continue;
321
+ const moduleContext = {
322
+ ...coreContext,
323
+ log: scopedLog(name),
324
+ resolvePageContext: consumes.pageContext ? pageContextRegistry : null
325
+ };
326
+
327
+ eleventyConfig.addPlugin(plugin, moduleContext);
328
+ }
329
+
330
+ // --- Filters ---
331
+ eleventyConfig.addFilter('markdownify', markdownFilter);
332
+ eleventyConfig.addFilter('relatedPosts', relatedPostsFilter);
333
+ eleventyConfig.addFilter('isString', isStringFilter);
334
+
335
+ // --- Shortcodes ---
336
+ eleventyConfig.addShortcode('image', imageShortcode);
337
+
338
+ // --- Dev image pipeline ---
339
+ eleventyConfig.addPlugin(eleventyImageOnRequestDuringServePlugin);
340
+ };
341
+
342
+ // Set a named function identity so eleventyConfig.hasPlugin() can detect this plugin.
343
+ Object.defineProperty(plugin, 'name', { value: name });
344
+ return plugin;
345
+ }
346
+
347
+ /**
348
+ * Eleventy directory configuration (external contract)
349
+ *
350
+ * Defines input/output structure for the build system.
351
+ */
352
+ export const config = {
353
+ dir: {
354
+ input: 'src',
355
+ output: 'dist',
356
+ data: '_data',
357
+ includes: '_includes',
358
+ assets: 'assets',
359
+ public: 'static'
360
+ },
361
+ htmlTemplateEngine: 'njk',
362
+ markdownTemplateEngine: 'njk',
363
+ templateFormats: ['html', 'njk', 'md']
364
+ };
@@ -0,0 +1,162 @@
1
+ import path from 'node:path';
2
+ import { TemplatePath } from '@11ty/eleventy-utils';
3
+
4
+ import { optionsSchema } from './schema.js';
5
+ import assetsESbuild from './processors/esbuild-process.js';
6
+ import assetsPostCSS from './processors/postcss-process.js';
7
+
8
+ /**
9
+ * Assets (module)
10
+ *
11
+ * Asset pipeline integration. Wires Eleventy’s template formats to esbuild
12
+ * and PostCSS through compile guards that allow only declared entrypoints,
13
+ * and exposes inline filters for critical-path assets.
14
+ *
15
+ * Architecture layer:
16
+ * module
17
+ *
18
+ * System role:
19
+ * Bridge between Eleventy’s template system and the external asset
20
+ * processors. Reads `directories.assets` from the virtual-dir substrate.
21
+ *
22
+ * Lifecycle:
23
+ * build-time → register js/css formats, compile guards, watch target, and
24
+ * inline filters; guards run per-entrypoint during compile
25
+ *
26
+ * Why this exists:
27
+ * Eleventy treats every .js and .css file as a template. Without compile
28
+ * guards, 11tydata.js files and non-entry assets would either pollute the
29
+ * template graph or trigger the wrong processor.
30
+ *
31
+ * Scope:
32
+ * Owns template format registration, compile guards, watch wiring, and the
33
+ * inline filters (inlinePostCSS, inlineESbuild).
34
+ * Does not own the processors themselves (assets/processors/) or
35
+ * `directories.assets` resolution (core/virtual-dir.js).
36
+ *
37
+ * Data flow:
38
+ * assets/{js,css}/index.{js,css} entrypoints → compile guard →
39
+ * esbuild/PostCSS processor → output
40
+ *
41
+ * @param {import("@11ty/eleventy").UserConfig} eleventyConfig
42
+ * @param {Object} moduleContext
43
+ */
44
+ export function assetsCore(eleventyConfig, moduleContext) {
45
+ const { state, directories, log } = moduleContext;
46
+ const { settings, options } = state;
47
+
48
+ // Structural-only options check: log on mismatch, do not throw.
49
+ const parsed = optionsSchema.safeParse(options.assets);
50
+ if (!parsed.success) {
51
+ for (const issue of parsed.error.issues) {
52
+ log.info('options:', `${issue.path.join('.')} — ${issue.message}`);
53
+ }
54
+ }
55
+
56
+ const inputDirectory = directories.input;
57
+ const assetsDirectory = directories.assets;
58
+ const jsDirectory = `${assetsDirectory}js/`;
59
+ const cssDirectory = `${assetsDirectory}css/`;
60
+
61
+ const esbuildOptions = options.assets.esbuild || {};
62
+ const dataFiles = `${inputDirectory}**/*.11tydata.js`;
63
+ const watchGlob = TemplatePath.join(assetsDirectory, '**/*.{css,js,svg,png,jpeg,jpg,webp,gif,avif}');
64
+
65
+ if (!assetsDirectory) {
66
+ log.warn('eleventyConfig.directories.assets is unset; registerVirtualDir must run before this plugin.');
67
+ return;
68
+ }
69
+
70
+ // Watch common asset formats so edits trigger reloads during --serve.
71
+ eleventyConfig.addWatchTarget(watchGlob);
72
+
73
+ // --- JS (esbuild) ---
74
+ // Register js as a template format. Only index.js files under assets/js/
75
+ // are compiled; everything else (11tydata.js, non-entry scripts) is skipped
76
+ // by the compile guard. The inline filter wraps the same process function.
77
+ // Defaults (minify, target) live in assets-esbuild/process.js.
78
+
79
+ eleventyConfig.addTemplateFormats('js');
80
+
81
+ // Prevent Eleventy from processing 11tydata.js files as templates.
82
+ // The compile guard below also filters these, but without this ignore
83
+ // Eleventy still enters them into the template graph (data cascade,
84
+ // permalink computation) before compile gets a chance to reject them.
85
+ eleventyConfig.ignores.add(dataFiles);
86
+
87
+ eleventyConfig.addExtension('js', {
88
+ outputFileExtension: 'js',
89
+ useLayouts: false,
90
+ read: false,
91
+ compileOptions: {
92
+ permalink: true,
93
+ cache: true
94
+ },
95
+ // Compile guard: only process index.js files under the assets js directory.
96
+ // Returning undefined skips the file without error.
97
+ compile: async function (_inputContent, inputPath) {
98
+ if (
99
+ inputPath.includes('11tydata.js') ||
100
+ !inputPath.startsWith(jsDirectory) ||
101
+ path.basename(inputPath) !== 'index.js'
102
+ ) {
103
+ return;
104
+ }
105
+
106
+ return async () => assetsESbuild(inputPath, esbuildOptions);
107
+ }
108
+ });
109
+
110
+ // Inline filter: bundle a JS file and wrap in <script> tags.
111
+ // Accepts per-call esbuild options (merged with defaults in process.js).
112
+ // Eleventy's addAsyncFilter handles the Nunjucks callback bridge,
113
+ // so this is a plain async function.
114
+ eleventyConfig.addAsyncFilter('inlineESbuild', async function (inputPath, opts = {}) {
115
+ try {
116
+ const js = await assetsESbuild(inputPath, opts);
117
+ return `<script>${js}</script>`;
118
+ } catch {
119
+ // Non-fatal: return an error comment so the build doesn't break.
120
+ return `<script>/* Error processing JS */</script>`;
121
+ }
122
+ });
123
+
124
+ // --- CSS (PostCSS) ---
125
+ // Register css as a template format. Only index.css files under assets/css/
126
+ // are compiled; non-entry CSS is skipped. Reads from disk (read: false) —
127
+ // the process function owns its own I/O. Config loading and caching live
128
+ // in assets-postcss/process.js.
129
+
130
+ eleventyConfig.addTemplateFormats('css');
131
+
132
+ eleventyConfig.addExtension('css', {
133
+ outputFileExtension: 'css',
134
+ useLayouts: false,
135
+ read: false,
136
+ compileOptions: {
137
+ permalink: true,
138
+ cache: true
139
+ },
140
+ // Compile guard: only process index.css files under the assets css directory.
141
+ compile: async function (_inputContent, inputPath) {
142
+ if (!inputPath.startsWith(cssDirectory) || path.basename(inputPath) !== 'index.css') {
143
+ return;
144
+ }
145
+
146
+ return async () => assetsPostCSS(inputPath);
147
+ }
148
+ });
149
+
150
+ // Inline filter: process a CSS file through PostCSS and wrap in <style> tags.
151
+ // Eleventy's addAsyncFilter handles the Nunjucks callback bridge,
152
+ // so this is a plain async function.
153
+ eleventyConfig.addAsyncFilter('inlinePostCSS', async function (inputPath) {
154
+ try {
155
+ const css = await assetsPostCSS(inputPath);
156
+ return `<style>${css}</style>`;
157
+ } catch {
158
+ // Non-fatal: return an error comment so the build doesn't break.
159
+ return `<style>/* Error processing CSS */</style>`;
160
+ }
161
+ });
162
+ }