-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathvite.config.js
More file actions
90 lines (77 loc) · 2.78 KB
/
Copy pathvite.config.js
File metadata and controls
90 lines (77 loc) · 2.78 KB
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
import { copyFileSync, mkdirSync, readdirSync, rmSync } from 'node:fs';
import { basename, dirname, extname, resolve } from 'node:path';
import { defineConfig } from 'vite';
const themeRoot = import.meta.dirname;
const blocksDirectory = resolve(themeRoot, 'blocks');
const buildDirectory = resolve(themeRoot, '.vite-build');
function findFiles(directory, extension) {
return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {
const path = resolve(directory, entry.name);
if (entry.isDirectory()) {
return findFiles(path, extension);
}
return entry.isFile() && extname(entry.name) === extension ? [path] : [];
});
}
function addBlockEntries(entries, extension, outputDirectory) {
for (const path of findFiles(blocksDirectory, extension)) {
if (path.endsWith('.min.js')) {
continue;
}
const name = basename(path, extension);
const outputName = `${outputDirectory}/${name}`;
if (entries[outputName]) {
throw new Error(`Multiple block assets would compile to ${outputName}`);
}
entries[outputName] = path;
}
}
function publishThemeAssets() {
return {
name: 'publish-theme-assets',
writeBundle(options, bundle) {
for (const output of Object.values(bundle)) {
const source = resolve(options.dir, output.fileName);
const destination = resolve(themeRoot, output.fileName);
mkdirSync(dirname(destination), { recursive: true });
copyFileSync(source, destination);
}
},
closeBundle() {
rmSync(buildDirectory, { force: true, recursive: true });
},
};
}
export default defineConfig(({ mode }) => {
const entries = {
style: resolve(themeRoot, 'assets/styles/style.scss'),
'css/editor-styles': resolve(themeRoot, 'assets/styles/admin/_editor.scss'),
'js/scripts.min': resolve(themeRoot, 'assets/scripts/scripts.js'),
};
addBlockEntries(entries, '.js', 'js/blocks');
addBlockEntries(entries, '.scss', 'css/blocks');
return {
plugins: [publishThemeAssets()],
build: {
cssCodeSplit: true,
cssTarget: 'chrome61',
minify: mode === 'development' ? false : 'oxc',
outDir: buildDirectory,
rollupOptions: {
input: entries,
output: {
assetFileNames: '[name][extname]',
entryFileNames: '[name].js',
},
},
sourcemap: mode === 'development',
},
css: {
preprocessorOptions: {
scss: {
loadPaths: [themeRoot],
},
},
},
};
});