-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path.eleventy.js
88 lines (73 loc) · 2.31 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
import EleventyVite from "./EleventyVite.js";
import path from "node:path";
import { createRequire } from "module";
const require = createRequire(import.meta.url);
const pkg = require("./package.json");
/**
* Options which can be passed to eleventy-plugin-vite
* @typedef {Object} EleventyViteOptions
* @property {string} tempFolderName
* @property {import("vite").InlineConfig} [viteOptions]
* @property {Object} [serverOptions]
*/
/**
* @param {import('@11ty/eleventy/src/UserConfig').default} eleventyConfig
* @param {EleventyViteOptions} options
*/
export default function (eleventyConfig, options = {}) {
try {
eleventyConfig.versionCheck(pkg["11ty"].compatibility);
} catch (error) {
eleventyConfig.logger.warn(
`Warning: Eleventy Plugin (${pkg.name}) Compatibility: ${error.message}`,
);
}
const eleventyVite = new EleventyVite(eleventyConfig, options);
const publicDir = eleventyVite.options.viteOptions?.publicDir || "public";
if (!path.relative(eleventyConfig.directories.output, publicDir)) {
throw new Error(
`${EleventyVite.LOGGER_PREFIX} Misconfiguration: Can't use the same directory for 11ty output and vite public directory`,
);
}
// Add publicDir to passthrough copy
eleventyConfig.addPassthroughCopy(publicDir);
// Add tempFolder to ignores
eleventyConfig.ignores.add(eleventyVite.getIgnoreDirectory());
const serverOptions = Object.assign(
{
module: "@11ty/eleventy-dev-server",
domDiff: false,
},
options.serverOptions,
);
serverOptions.setup = async () => {
// Use Vite as Middleware
const viteDevServer = await eleventyVite.getServer();
process.on("SIGINT", async () => {
await viteDevServer.close();
});
return {
middleware: [viteDevServer.middlewares],
};
};
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);
});
}