-
Notifications
You must be signed in to change notification settings - Fork 6
/
.eleventy.js
194 lines (182 loc) 路 6.08 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
183
184
185
186
187
188
189
190
191
192
193
194
const input = 'src'
const output = 'build'
const { rollup } = require('rollup')
const cssPrefixer = require('postcss-prefix-selector')
const cssClean = require('postcss-clean')
const postcss = require('postcss')
const { promisify } = require('util')
const sassCompiler = require('sass')
const sassRender = promisify(sassCompiler.render)
const path = require('path')
const chalk = require('chalk')
const virtual = require('@rollup/plugin-virtual')
const { stringify } = require('javascript-stringify')
const yaml = require('js-yaml')
const joinTrimSlashes = require('./utils/join-trim-slashes')
const dotenv = require('dotenv')
const { readFrontMatter, renderToLayout } = require('./utils/render-to-layout')(
path.resolve(__dirname, input)
)
const livereload = require('livereload')
const liveReloadPort = 35729
const matchPathProperties = (path = '', fileExtension = '', useOutput) => {
const [_, relativePath, fileName] = path.match(
new RegExp(
`.*${useOutput ? output : './' + input}(.*\/|\/)(.*).${fileExtension}`
)
)
return { relativePath, fileName }
}
const templateExtensionConfig = {
read: false, // don't process the file's contents. That's our job!
getData: (inputPath) =>
readFrontMatter(inputPath, {
slinkit: {
scripts: `
<script src="/__main.mjs" defer></script>
${
process.env.MODE === 'dev'
? `<script src="http://localhost:${liveReloadPort}/livereload.js?snipver=1"></script>`
: ``
}
`,
},
}),
compile: (_, inputPath) => (data) => {
const { relativePath } = matchPathProperties(data.page.outputPath, '', true)
if (inputPath.startsWith(`./${input}/_layouts`)) {
return
} else {
return renderToLayout(inputPath, {
...data,
slinkit: {
...data.slinkit,
styles:
`<link rel="stylesheet" href="${path.join(
relativePath,
'__styles.css'
)}">` + (data.slinkit.styles ?? ''),
},
})
}
},
}
module.exports = function (eleventyConfig) {
dotenv.config()
/*
We're processing templates by hand instead of using 11ty
This is mainly for page transitions and style scoping, since we need
to apply unique HTML identifiers to *each layout file* our templates use
*/
eleventyConfig.templateFormats = ['md', 'pug', 'scss', 'mjs', 'html']
eleventyConfig.addPassthroughCopy('assets')
eleventyConfig.addPassthroughCopy('_redirects')
eleventyConfig.addExtension('pug', templateExtensionConfig)
eleventyConfig.addExtension('md', templateExtensionConfig)
eleventyConfig.addDataExtension('yaml', (contents) => yaml.safeLoad(contents))
eleventyConfig.addExtension('mjs', {
read: false,
getData: (inputPath) => {
const { relativePath, fileName } = matchPathProperties(inputPath, 'mjs')
let resolvedPathWithFolder = ''
if (fileName === '_main') {
resolvedPathWithFolder = path.resolve(relativePath, '__main.mjs')
} else if (fileName === 'index') {
resolvedPathWithFolder = path.resolve(relativePath, '__client.mjs')
} else {
resolvedPathWithFolder = path.resolve(
relativePath,
fileName,
'__client.mjs'
)
}
return {
dynamicPermalink: false,
permalink: resolvedPathWithFolder,
}
},
compile: (_, inputPath) => async (data) => {
/* Runs your JS through a bundler called RollupJS.
Check out https://rollupjs.org for a quick guide,
and where to find any plugins you might want */
const bundle = await rollup({
input: inputPath,
})
const { output } = await bundle.generate({ format: 'esm' })
if (output?.length && output[0]?.code) {
const { fileName } = matchPathProperties(inputPath, 'mjs')
if (fileName !== '_main') {
const dataBundle = await rollup({
input: '__data',
plugins: [
virtual({
__data: `export default ${stringify({
...data,
collections: undefined,
})}`,
}),
],
})
await dataBundle.write({
file: data.page.outputPath.replace('__client.mjs', '__data.mjs'),
format: 'esm',
})
}
return output[0].code
} else {
console.log(
chalk.red(`There was a problem generating JS for ${inputPath}`)
)
return
}
},
})
eleventyConfig.addExtension('scss', {
read: false,
outputFileExtension: 'css',
getData: (inputPath) => {
let { relativePath, fileName } = matchPathProperties(inputPath, 'scss')
const pathWithFolder =
fileName === 'index'
? relativePath
: path.resolve(relativePath, fileName)
return {
dynamicPermalink: false,
permalink: path.resolve(pathWithFolder, '__styles.css'),
eleventyExcludeFromCollections: true,
}
},
compile:
(_, inputPath) =>
async ({ page: { outputPath } }) => {
const { css } = await sassRender({ file: inputPath })
const { relativePath } = matchPathProperties(outputPath, 'css', true)
const dataPageAttr = joinTrimSlashes(relativePath)
const postCssPlugins = []
if (process.env.ENV === 'prod') {
// minify CSS for production builds
postCssPlugins.push(cssClean())
}
if (dataPageAttr !== '_layouts') {
// append the data-page attribute for everything *except* global _layout styles
postCssPlugins.push(
cssPrefixer({ prefix: `[data-page="${dataPageAttr}"]` })
)
}
if (!postCssPlugins.length) return css
else {
const postCSSOutput = await postcss(postCssPlugins).process(css)
return postCSSOutput.css
}
},
})
if (process.env.MODE === 'dev') {
const server = livereload.createServer({ port: liveReloadPort }, () =>
console.log(chalk.green('Live reload enabled'))
)
server.watch(output)
}
return {
dir: { input, output },
}
}