-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
114 lines (105 loc) · 3.11 KB
/
vite.config.js
File metadata and controls
114 lines (105 loc) · 3.11 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { defineConfig } from 'vite';
import fullReload from 'vite-plugin-full-reload';
const themeRoot = fileURLToPath(new URL('.', import.meta.url));
const mainEntry = path.resolve(themeRoot, 'src/main.js');
const blocksRoot = path.resolve(themeRoot, 'blocks');
// Discover block entry files at /blocks/*/index.js and map to blocks/[name].
const getBlockEntries = () => {
if (!fs.existsSync(blocksRoot)) {
return {};
}
return fs.readdirSync(blocksRoot, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.reduce((entries, dirent) => {
const entryPath = path.resolve(blocksRoot, dirent.name, 'index.js');
if (fs.existsSync(entryPath)) {
entries[`blocks/${dirent.name}`] = entryPath;
}
return entries;
}, {});
};
const getBlockCssOutput = (assetInfo) => {
if (!assetInfo || !Array.isArray(assetInfo.originalFileNames)) {
return null;
}
const blockSource = assetInfo.originalFileNames.find((name) => (
typeof name === 'string' && name.replace(/\\/g, '/').includes('/blocks/')
));
if (!blockSource) {
return null;
}
const normalized = blockSource.replace(/\\/g, '/');
const match = normalized.match(/\/blocks\/([^/]+)\//);
if (!match) {
return null;
}
return `blocks/${match[1]}.css`;
};
export default defineConfig({
// Use ./src as the project root for dev/build.
root: 'src',
plugins: [
fullReload([
'**/*.php',
'../**/*.php',
]),
],
server: {
// Local dev server port.
port: 5175,
strictPort: true,
// Allow Vite to serve files from the theme root (blocks live outside /src).
fs: {
allow: [themeRoot],
},
},
css: {
// Enable CSS sourcemaps in dev (build maps handled by Sass CLI).
devSourcemap: true,
preprocessorOptions: {
scss: {
// Allow Sass to emit source maps during dev.
sourceMap: true,
sourceMapIncludeSources: true,
},
},
},
build: {
// Emit build output into the theme's /dist folder.
outDir: '../dist',
emptyOutDir: true,
// Keep JS sourcemaps for debugging.
sourcemap: true,
rollupOptions: {
// Use WordPress-provided packages at runtime instead of bundling them.
external: [
'@wordpress/blocks',
'@wordpress/block-editor',
'@wordpress/components',
'@wordpress/element',
],
// Multi-entry: main theme bundle + one entry per block.
input: {
main: mainEntry,
...getBlockEntries(),
},
output: {
// Keep entry names stable and readable.
entryFileNames: (chunk) => (
chunk.name === 'main' ? 'main.js' : `${chunk.name}.js`
),
chunkFileNames: 'chunks/[name].js',
assetFileNames: (assetInfo) => {
// Preserve per-entry CSS names (e.g. blocks/hero.css).
if (assetInfo.name && assetInfo.name.endsWith('.css')) {
return getBlockCssOutput(assetInfo) || 'assets/[name][extname]';
}
return 'assets/[name][extname]';
},
},
},
},
});