forked from 11ty/eleventy-plugin-vite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
57 lines (46 loc) · 1.87 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
const pkg = require("./package.json");
const EleventyVite = require("./EleventyVite");
module.exports = function(eleventyConfig, options = {}) {
try {
eleventyConfig.versionCheck(pkg["11ty"].compatibility);
} catch(e) {
console.warn( `[11ty] Warning: Eleventy Plugin (${pkg.name}) Compatibility: ${e.message}` );
}
let eleventyVite = new EleventyVite(eleventyConfig.dir.output, options);
// Adds support for automatic publicDir passthrough copy
// vite/rollup will not touch these files and as part of the build will copy them to the root of your output folder
let publicDir = eleventyVite.options.viteOptions?.publicDir || "public";
eleventyConfig.ignores.add(publicDir);
// Use passthrough copy on the public directory
let passthroughCopyObject = {};
passthroughCopyObject[`${publicDir}/`] = "/"
eleventyConfig.addPassthroughCopy(passthroughCopyObject);
// Add temp folder to the ignores
eleventyConfig.ignores.add(eleventyVite.getIgnoreDirectory());
let serverOptions = Object.assign({
module: "@11ty/eleventy-dev-server",
domDiff: false,
}, options.serverOptions);
serverOptions.setup = async () => {
// Use Vite as Middleware
let middleware = await eleventyVite.getServerMiddleware();
return {
middleware: [ middleware ]
}
};
eleventyConfig.setServerOptions(serverOptions);
// Run Vite build
// TODO use `build.write` option to work with json or ndjson outputs
eleventyConfig.on("eleventy.after", async ({ dir, runMode, outputMode, results }) => {
// Skips the Vite build if:
// --serve
// --to=json
// --to=ndjson
// or 0 output files from Eleventy build
// Notably, this *does* run Vite build in --watch mode
if(runMode === "serve" || outputMode === "json" || outputMode === "ndjson" || results.length === 0) {
return;
}
await eleventyVite.runBuild(results);
});
};