-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.js
127 lines (117 loc) · 3.5 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
import { defineConfig } from 'vite';
import { resolve } from 'path';
import handlebars from 'vite-plugin-handlebars';
import fs from 'fs';
import path from 'path';
const files = [];
function readDirectory(dirPath) {
const items = fs.readdirSync(dirPath);
for (const item of items) {
const itemPath = path.join(dirPath, item);
if (fs.statSync(itemPath).isDirectory()) {
// componentsディレクトリを除外する
if (item === 'components' || item === 'js') {
continue;
}
readDirectory(itemPath);
} else {
// htmlファイル以外を除外する
if (path.extname(itemPath) !== '.html') {
continue;
}
// nameを決定する
let name;
if (dirPath === path.resolve(__dirname, 'src')) {
name = path.parse(itemPath).name;
} else {
const relativePath = path.relative(path.resolve(__dirname, 'src'), dirPath);
const dirName = relativePath.replace(/\//g, '_');
name = `${dirName}_${path.parse(itemPath).name}`;
}
// pathを決定する
const relativePath = path.relative(path.resolve(__dirname, 'src'), itemPath);
const filePath = `/${relativePath}`;
files.push({ name, path: filePath });
}
}
}
readDirectory(path.resolve(__dirname, 'src'));
const inputFiles = {};
for (let i = 0; i < files.length; i++) {
const file = files[i];
inputFiles[file.name] = resolve(__dirname, './src' + file.path );
}
// Handlebars=HTML上で出し分けたい各ページごとの情報
const pageData = {
'/index.html': {
isHome: true,
title: 'Main Page',
},
'/other.html': {
isHome: false,
title: 'Other Page',
},
};
// Cache busting CSS and JS files
const htmlPlugin = () => {
return {
name: 'html-transform',
transformIndexHtml(html) {
// build時のみ動作させる
if(process.env.NODE_ENV !== 'production') {
return;
}
const date = new Date();
const param = date.getFullYear() + date.getMonth() + date.getDate() + date.getHours() + date.getMinutes() + date.getSeconds();
let setParamHtml = html.replace(/(?=.*<link)(?=.*css).*$/gm, match => {
return match.replace(/\.css/, '.css?' + param);
});
return setParamHtml.replace(/(?=.*<script)(?=.*js).*$/gm, match => {
return match.replace(/\.js/, '.js?' + param);
});
}
}
}
export default defineConfig({
server: {
host: true
},
base: './',
root: './src', // 開発ディレクトリ
build: {
outDir: '../dist', // 出力ディレクトリ
rollupOptions: { // 出力設定
output: {
assetFileNames: (assetInfo) => {
let extType = assetInfo.name.split('.')[1];
// Webフォントファイル振り分け
if (/ttf|otf|woff|woff2/i.test(extType)) {
extType = 'font';
}
if (/png|jpe?g|webp|avif|svg|gif|ico/i.test(extType)) {
extType = 'image';
}
if(extType === 'css') {
return `assets/css/style.css`;
}
return `assets/${extType}/[name][extname]`;
},
chunkFileNames: 'assets/js/main.js',
entryFileNames: 'assets/js/[name].js',
},
input: {
index: resolve(__dirname, './src/index.html'),
other: resolve(__dirname, './src/other.html'),
},
},
},
plugins: [
htmlPlugin(),
handlebars({
partialDirectory: resolve(__dirname, './src/components'),
context(pagePath) {
return pageData[pagePath];
},
}),
],
});