forked from 11ty/eleventy-plugin-vite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EleventyVite.js
84 lines (70 loc) · 2.49 KB
/
EleventyVite.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
const { promises: fsp } = require("fs");
const path = require("path");
const lodashMerge = require("lodash.merge");
const DEFAULT_OPTIONS = {
tempFolderName: ".11ty-vite",
viteOptions: {
resolve:{
alias:{
// Allow references to `node_modules` directly for bundling.
'/node_modules': path.resolve(".", 'node_modules')
// Note that bare module specifiers are also supported
},
},
clearScreen: false,
appType: "mpa",
server: {
mode: "development",
middlewareMode: true,
},
build: {
mode: "production",
rollupOptions: {}, // we use this to inject input for MPA build below
}
}
}
class EleventyVite {
constructor(outputDir, pluginOptions = {}) {
this.outputDir = outputDir;
this.options = lodashMerge({}, DEFAULT_OPTIONS, pluginOptions);
}
async getServerMiddleware() {
let viteOptions = lodashMerge({}, this.options.viteOptions);
viteOptions.root = this.outputDir;
const { createServer } = await import('vite');
let vite = await createServer(viteOptions);
return vite.middlewares;
}
getIgnoreDirectory() {
return path.join(this.options.tempFolderName, "**");
}
async runBuild(input) {
let tmp = path.resolve(".", this.options.tempFolderName);
await fsp.mkdir(tmp, { recursive: true });
await fsp.rename(this.outputDir, tmp);
try {
let viteOptions = lodashMerge({}, this.options.viteOptions);
viteOptions.root = tmp;
viteOptions.build.rollupOptions.input = input
.filter(entry => !!entry.outputPath) // filter out `false` serverless routes
.filter(entry => (entry.outputPath || "").endsWith(".html")) // only html output
.map(entry => {
if(!entry.outputPath.startsWith(this.outputDir + path.sep)) {
throw new Error(`Unexpected output path (was not in output directory ${this.outputDir}): ${entry.outputPath}`);
}
return path.resolve(tmp, entry.outputPath.substr(this.outputDir.length + path.sep.length));
});
viteOptions.build.outDir = path.resolve(".", this.outputDir);
const { build } = await import('vite');
await build(viteOptions);
} catch(e) {
console.warn( `[11ty] Encountered a Vite build error, restoring original Eleventy output to ${this.outputDir}`, e );
await fsp.rename(tmp, this.outputDir);
throw e;
} finally {
// remove the tmp dir
await fsp.rm(tmp, { recursive: true });
}
}
}
module.exports = EleventyVite;