-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvite.config.js
More file actions
103 lines (101 loc) · 3.02 KB
/
vite.config.js
File metadata and controls
103 lines (101 loc) · 3.02 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
import path from "path";
import fs from "fs";
import { defineConfig } from "vite";
import VitePluginBrowserSync from "vite-plugin-browser-sync";
import { viteStaticCopy } from "vite-plugin-static-copy";
import { liveReload } from "vite-plugin-live-reload";
import sassGlobImports from "vite-plugin-sass-glob-import";
import svgSpritePlugin from "vite-plugin-svg-sprite-component";
// srcディレクトリ内のすべてのファイルとディレクトリを取得
const srcDirectoryContents = fs.readdirSync(path.resolve(__dirname, "src"));
// assetsフォルダ以外のファイルとディレクトリを抽出
const nonAssetsContents = srcDirectoryContents.filter((item) => item !== "assets");
export default defineConfig({
plugins: [
liveReload("./**/*.php"),
viteStaticCopy({
// assetsフォルダ以外のファイルとディレクトリをコピー
targets: nonAssetsContents.map((item) => ({
src: `src/${item}`,
dest: "",
})),
}),
viteStaticCopy({
targets: [
{
src: path.resolve(__dirname + `/src/assets/static/*`),
dest: "assets/static",
},
],
}),
sassGlobImports(),
svgSpritePlugin(),
VitePluginBrowserSync({
bs: {
port: 3030,
proxy: "localhost:8000",
ui: {
port: 8080,
},
},
}),
],
root: "",
build: {
assetsInlineLimit: 0,
outDir: path.resolve(__dirname, "./dist"),
emptyOutDir: true,
target: "es2018",
rollupOptions: {
input: {
app: path.resolve(__dirname + `/src/assets/app.js`),
"front-page": path.resolve(__dirname + `/src/assets/front-page.js`),
},
output: {
entryFileNames: `assets/js/[name].js`,
chunkFileNames: `assets/js/[name].js`,
assetFileNames: ({ name }) => {
if (/\.(gif|jpeg|jpg|png|svg|webp)$/.test(name ?? "")) {
return "assets/images/[name].[ext]";
}
if (/\.css$/.test(name ?? "")) {
return "assets/css/[name].[ext]";
}
if (/\.js$/.test(name ?? "")) {
return "assets/js/[name].[ext]";
}
return "assets/[name].[ext]";
},
},
},
},
// CSSでローカルと本番で異なる参照をしたい時に設定してください
css: {
preprocessorOptions: {
scss: {
additionalData: `$base-dir: ${(() => {
switch (process.env.NODE_ENV) {
case "local":
return "'http://localhost:3000/src/'";
case "development":
return "'https://dev.example.com/wp-content/themes/theme-name/'";
case "staging":
return "'https://staging.example.com/wp-content/themes/theme-name/'";
case "production":
return "'https://www.example.com/wp-content/themes/theme-name/'";
}
})()};`,
},
},
},
server: {
host: true,
cors: true,
strictPort: true,
port: 3000,
https: false,
hmr: {
host: "localhost",
},
},
});