-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathvite.config.js
76 lines (70 loc) · 2.74 KB
/
vite.config.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
import { defineConfig } from "vite";
import fs from "fs/promises";
import path from "path";
import { greenwoodPluginStandardCss } from "@greenwood/cli/src/plugins/resource/plugin-standard-css.js";
import { greenwoodPluginImportRaw } from "@greenwood/plugin-import-raw";
import { readAndMergeConfig } from "@greenwood/cli/src/lifecycles/config.js";
import { initContext } from "@greenwood/cli/src/lifecycles/context.js";
// bootstrap custom plugin transforms from Greenwood
const config = await readAndMergeConfig();
const context = await initContext({ config });
const compilation = { context, config };
const standardCssResource = greenwoodPluginStandardCss.provider(compilation);
const rawResource = greenwoodPluginImportRaw()[0].provider(compilation);
// Vite doesn't support import attributes :/
// https://github.com/vitejs/vite/pull/15654
// https://github.com/vitejs/vite/issues/14674
// https://github.com/vitejs/vite/pull/8937
// https://github.com/vitejs/vite/issues/12140
// for now, bail out of any standard CSS handling
// https://github.com/vitejs/vite/issues/15322#issuecomment-1858878697
function transformConstructableStylesheetsPlugin() {
return {
name: "transform-constructable-stylesheets",
enforce: "pre",
resolveId: (id, importer) => {
if (
importer?.indexOf("/src/components/") >= 0 &&
id.endsWith(".css") &&
!id.endsWith(".module.css")
) {
// add .type so Constructable Stylesheets are not precessed by Vite's default pipeline
return path.join(path.dirname(importer), `${id}.type`);
}
},
load: async (id) => {
if (id.endsWith(".css.type")) {
const filename = id.slice(0, -5);
const contents = await fs.readFile(filename, "utf-8");
const url = new URL(`file://${id.replace(".type", "")}`);
// "coerce" native conststructable stylesheets into inline JS so Vite / Rollup do not complain
const request = new Request(url, {
headers: {
Accept: "text/javascript",
},
});
const response = await standardCssResource.intercept(url, request, new Response(contents));
const body = await response.text();
return body;
}
},
};
}
function transformRawImports() {
return {
name: "transform-raw-imports",
enforce: "pre",
load: async (id) => {
if (id.endsWith("?type=raw")) {
const filename = id.slice(0, -9);
const contents = await fs.readFile(filename, "utf-8");
const response = await rawResource.intercept(null, null, new Response(contents));
const body = await response.text();
return body;
}
},
};
}
export default defineConfig({
plugins: [transformConstructableStylesheetsPlugin(), transformRawImports()],
});