-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
182 lines (161 loc) · 5.71 KB
/
.eleventy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const { EleventyI18nPlugin } = require("@11ty/eleventy");
const CleanCSS = require("clean-css");
const htmlmin = require("html-minifier");
const cagovBuildSystem = require("@cagov/11ty-build-system");
const config = require("./config/index.js");
const {
copyFolderRecursiveSync,
} = require("./src/js/eleventy/sync-static-content.js");
const {
renderPostLists,
renderWordpressPostTitleDate,
setDefaultAttributes,
} = require("./src/js/eleventy/post-list/render.js");
const { renderEventLists } = require("./src/js/eleventy/event-list/render.js");
const {
pagePath,
relativePath,
i18n,
} = require("./src/js/eleventy/filters.js");
module.exports = function eleventyBuild(eleventyConfig) {
eleventyConfig.htmlTemplateEngine = "njk";
// Copy content from static bundle to gitignored folder in 11ty directory for local processing
eleventyConfig.setUseGitIgnore(false);
copyFolderRecursiveSync(
config.staticContentPaths.posts,
config.build.eleventy_content
);
copyFolderRecursiveSync(
config.staticContentPaths.pages,
config.build.eleventy_content
);
copyFolderRecursiveSync(
config.staticContentPaths.menu,
config.build.eleventy_content
);
copyFolderRecursiveSync(
config.staticContentPaths.redirects,
config.build.eleventy_content
);
// Register ca.gov 11ty build system.
eleventyConfig.addPlugin(cagovBuildSystem, {
processors: {
sass: {
watch: ["src/css/**", "src/components/**"],
output: "dist/index.css",
options: {
file: "src/css/index.scss",
includePaths: ["./src/css"],
},
},
esbuild: {
watch: ["src/js/**", "src/js/**/*"],
options: {
entryPoints: ["src/js/index.js"],
bundle: true,
minify: true,
outfile: "dist/site.js",
},
},
},
});
eleventyConfig.setBrowserSyncConfig({
notify: true,
watch: true,
});
// https://www.11ty.dev/docs/plugins/i18n/ canary version docs
eleventyConfig.addPlugin(EleventyI18nPlugin, {
// any valid BCP 47-compatible language tag is supported
defaultLanguage: "en",
});
// 11ty filters:
eleventyConfig.addFilter(
"cssmin",
(code) => new CleanCSS({}).minify(code).styles
);
eleventyConfig.addFilter("i18n", i18n);
eleventyConfig.addFilter("pagePath", pagePath);
eleventyConfig.addFilter("relativePath", relativePath);
// Used in announcements.njk
eleventyConfig.addFilter("displayPostInfo", (item) =>
renderWordpressPostTitleDate(item.data, setDefaultAttributes())
);
eleventyConfig.addTransform("htmlTransforms", (html, outputPath) => {
// !outputPath || // Do we really need this??
if (outputPath.endsWith(".html")) {
// Render post-list components
if (html.includes("cagov-post-list")) {
html = renderPostLists(html);
}
// Render posts with event web component
if (html.includes("cagov-event-post-list")) {
html = renderEventLists(html);
}
// Remove WP auto-lazy images for the homepage banner.
if (html.includes('<img loading="lazy" class="cagov-featured-image"')) {
html = html.replace(
'<img loading="lazy" class="cagov-featured-image"',
'<img class="cagov-featured-image"'
);
}
// Replace any domain from replace list with the canonical url for the current build.
// For this example.ca.gov instance, there are multiple URL sources coming from different backend systems
config.build.replace_urls.forEach((rootPath) => {
if (html !== undefined && html.includes(rootPath)) {
html = html.replace(
new RegExp(rootPath, "g"),
config.build.canonical_site_url
);
}
return false;
});
// Read a list of full media paths for any links that should be relative to this instance.
// Replace with the local media folder.
// Note: do not change the media folder without a corresponding update to the redirects (using Redirection plugin in editor).
// jbum - this should not be necessary
// config.build.media_replace_urls.forEach((mediaPath) => {
// if (html !== undefined && html.includes(mediaPath)) {
// html = html.replace(
// new RegExp(mediaPath, "g"),
// `/${config.build.docs_media}/`
// );
// }
// return false;
// });
// Patch for glitch/issue with absolute url permalinks for og meta - likely resulting from custom eleventy absolutePath filter
// jbum this should not be necessary
// if (html !== undefined && html.includes("//wp-content/uploads/")) {
// html = html.replace(
// new RegExp("//wp-content/uploads/", "g"),
// `/${config.build.docs_media}/`
// );
// }
// Minify HTML
html = htmlmin.minify(html, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true,
});
}
return html;
});
// Copy media assets folder from static site to built site
// 11ty copy assets
eleventyConfig.addPassthroughCopy({ "src/assets/": "assets" });
eleventyConfig.addPassthroughCopy({ "src/css/fonts/": "fonts" }); // Required location by cagov code
// jbum removed this because it should not be necessary
// eleventyConfig.addPassthroughCopy({
// [config.staticContentPaths.media]: config.build.docs_media,
// });
eleventyConfig.addPassthroughCopy({ "dist/*": "/" });
return {
htmlTemplateEngine: "njk",
markdownTemplateEngine: "md",
templateFormats: ["html", "njk", "11ty.js", "md"],
dir: {
input: config.build.eleventy_input,
output: config.build.eleventy_output,
layouts: config.build.eleventy_layouts,
},
};
};