@@ -9,8 +9,8 @@ import UserConfig from "./UserConfig.js";
99import GlobalDependencyMap from "./GlobalDependencyMap.js" ;
1010import ExistsCache from "./Util/ExistsCache.js" ;
1111import merge from "./Util/Merge.js" ;
12- import unique from "./Util/Unique.js" ;
1312import eventBus from "./EventBus.js" ;
13+ import ProjectTemplateFormats from "./Util/ProjectTemplateFormats.js" ;
1414
1515const debug = debugUtil ( "Eleventy:TemplateConfig" ) ;
1616const debugDev = debugUtil ( "Dev:Eleventy:TemplateConfig" ) ;
@@ -50,6 +50,8 @@ class EleventyPluginError extends EleventyBaseError {}
5050 * @param {String } projectConfigPath - Path to local project config.
5151 */
5252class TemplateConfig {
53+ #templateFormats;
54+
5355 constructor ( customRootConfig , projectConfigPath ) {
5456 this . userConfig = new UserConfig ( ) ;
5557
@@ -101,6 +103,18 @@ class TemplateConfig {
101103 this . userConfig . directories = directories . getUserspaceInstance ( ) ;
102104 }
103105
106+ /* Setter for TemplateFormats instance */
107+ setTemplateFormats ( templateFormats ) {
108+ this . #templateFormats = templateFormats ;
109+ }
110+
111+ get templateFormats ( ) {
112+ if ( ! this . #templateFormats) {
113+ this . #templateFormats = new ProjectTemplateFormats ( ) ;
114+ }
115+ return this . #templateFormats;
116+ }
117+
104118 /* Backwards compat */
105119 get inputDir ( ) {
106120 return this . directories . input ;
@@ -335,8 +349,6 @@ class TemplateConfig {
335349 this . directories . setViaConfigObject ( exportedConfigObject . dir ) ;
336350 }
337351
338- // TODO (config) sync `exportedConfigObject` to the rest of `userConfig` (e.g. templateFormats)
339-
340352 if ( typeof configDefaultReturn === "function" ) {
341353 localConfig = await configDefaultReturn ( this . userConfig ) ;
342354 } else {
@@ -399,20 +411,28 @@ class TemplateConfig {
399411 }
400412 }
401413
402- // Template Formats:
403- // 1. Root Config (usually defaultConfig.js)
404- // 2. Local Config return object (project .eleventy.js)
405- // 3.
406- let templateFormats = this . rootConfig . templateFormats || [ ] ;
407- if ( localConfig && localConfig . templateFormats ) {
408- templateFormats = localConfig . templateFormats ;
409- delete localConfig . templateFormats ;
414+ // `templateFormats` is an override via `setTemplateFormats`
415+ if ( this . userConfig && this . userConfig . templateFormats ) {
416+ this . templateFormats . setViaConfig ( this . userConfig . templateFormats ) ;
417+ } else if ( localConfig ?. templateFormats || this . rootConfig ?. templateFormats ) {
418+ // Local project config or defaultConfig.js
419+ this . templateFormats . setViaConfig (
420+ localConfig . templateFormats || this . rootConfig ?. templateFormats ,
421+ ) ;
422+ }
423+
424+ // `templateFormatsAdded` is additive via `addTemplateFormats`
425+ if ( this . userConfig && this . userConfig . templateFormatsAdded ) {
426+ this . templateFormats . addViaConfig ( this . userConfig . templateFormatsAdded ) ;
410427 }
411428
412429 let mergedConfig = merge ( { } , this . rootConfig , localConfig ) ;
413430
414431 // Setup a few properties for plugins:
415432
433+ // Set frozen templateFormats
434+ mergedConfig . templateFormats = Object . freeze ( this . templateFormats . getTemplateFormats ( ) ) ;
435+
416436 // Setup pathPrefix set via command line for plugin consumption
417437 if ( this . overrides . pathPrefix ) {
418438 mergedConfig . pathPrefix = this . overrides . pathPrefix ;
@@ -433,9 +453,6 @@ class TemplateConfig {
433453 // But BEFORE the rest of the config options are merged
434454 // this way we can pass directories and other template information to plugins
435455
436- // Temporarily restore templateFormats
437- mergedConfig . templateFormats = templateFormats ;
438-
439456 await this . userConfig . events . emit ( "eleventy.beforeConfig" , this . userConfig ) ;
440457
441458 let benchmarkManager = this . userConfig . benchmarkManager . get ( "Aggregate" ) ;
@@ -444,30 +461,17 @@ class TemplateConfig {
444461 await this . processPlugins ( mergedConfig ) ;
445462 pluginsBench . after ( ) ;
446463
447- delete mergedConfig . templateFormats ;
448-
449464 let eleventyConfigApiMergingObject = this . userConfig . getMergingConfigObject ( ) ;
450465
451- // `templateFormats` is an override via `setTemplateFormats`
452- // `templateFormatsAdded` is additive via `addTemplateFormats`
453- if ( eleventyConfigApiMergingObject && eleventyConfigApiMergingObject . templateFormats ) {
454- templateFormats = eleventyConfigApiMergingObject . templateFormats ;
455- delete eleventyConfigApiMergingObject . templateFormats ;
466+ if ( "templateFormats" in eleventyConfigApiMergingObject ) {
467+ throw new Error (
468+ "Internal error: templateFormats should not return from `getMergingConfigObject`" ,
469+ ) ;
456470 }
457471
458- let templateFormatsAdded = eleventyConfigApiMergingObject . templateFormatsAdded || [ ] ;
459- delete eleventyConfigApiMergingObject . templateFormatsAdded ;
460-
461- templateFormats = unique ( [ ...templateFormats , ...templateFormatsAdded ] ) ;
462-
463- merge ( mergedConfig , eleventyConfigApiMergingObject ) ;
464-
465- // Apply overrides, currently only pathPrefix uses this I think!
472+ // Overrides are only used by pathPrefix
466473 debug ( "Configuration overrides: %o" , this . overrides ) ;
467- merge ( mergedConfig , this . overrides ) ;
468-
469- // Restore templateFormats
470- mergedConfig . templateFormats = templateFormats ;
474+ merge ( mergedConfig , eleventyConfigApiMergingObject , this . overrides ) ;
471475
472476 debug ( "Current configuration: %o" , mergedConfig ) ;
473477
0 commit comments