forked from js-tasks-ru/vue-20230320_eugene-kachalin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.js
158 lines (140 loc) · 4.61 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import fs from 'node:fs';
import { extname, join, resolve } from 'node:path';
import { fileURLToPath, URL } from 'node:url';
import vueJsx from '@vitejs/plugin-vue-jsx';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
/**
* @param {string} taskDir - Path to task directory in Taskbook, e.g. 0-module/1-task
* @return {string} Path to task source directory depending on env, e.g. path/src
*/
function joinTaskSourceDir(taskDir) {
if (process.env.TASK_DEV) {
const sourceDirname = process.env.SOLUTION ? 'solution' : 'src';
return `${taskDir}/${sourceDirname}`;
}
return taskDir;
}
/**
* @typedef Task
* @property task {string}
* @property module {string}
* @property path {string}
*/
/**
* Discover all tasks in Taskbook
* @param {string} rootDir - Path to Taskbook root
* @returns {Task[]} Array of tasks data
*/
function discoverTaskDirs(rootDir = __dirname) {
const isDir = (filepath) => fs.lstatSync(filepath).isDirectory();
const getSubDirs = (dir) => fs.readdirSync(dir).filter((name) => isDir(join(dir, name)));
const isModuleOrTaskDir = (dirname) => /^\d+-/.test(dirname);
return getSubDirs(rootDir)
.filter(isModuleOrTaskDir)
.flatMap((module) =>
getSubDirs(join(rootDir, module))
.filter(isModuleOrTaskDir)
.map((task) => ({ module, task, path: `${module}/${task}` })),
);
}
/**
* Generates pages config for building.
* Each page is served by index.html on /module/task.
* @param {Task[]} taskList - Array of tasks data
* @return {Object} Pages config for vite.config.js build.rollupOptions.input
*/
function generatePagesConfig(taskList) {
return taskList.reduce((pages, { module, task, path }) => {
pages[`${module}/${task}`] = resolve(join(__dirname, joinTaskSourceDir(path)), 'index.html');
return pages;
}, {});
}
/**
* Use `rollupOptions.input` pages as bases for multi page SPA fallback.
* Rewrite SPA routes `/<page>/*` to `/<page>/index.html`.
* @returns {import('vite').Plugin}
*/
function devServerMultiPageSpa() {
return {
name: 'dev-server-multi-page-spa',
configureServer(server) {
server.middlewares.use((req, res, next) => {
const rollupOptionsInput = server.config.build.rollupOptions.input;
if (!rollupOptionsInput) {
return next();
}
const pathname = new URL(req.url, 'http://localhost').pathname;
const isFile = !!extname(pathname);
if (isFile) {
return next();
}
const page = Object.keys(rollupOptionsInput).find((page) => pathname.startsWith(`/${page}`));
if (page) {
req.url = `/${page}/index.html`;
}
next();
});
},
};
}
/**
* Rewrites path in taskbook development
* @param {Task[]} tasks
* @returns {import('vite').Plugin|false}
*/
const taskbookDevSourceRewrite = (tasks) => {
// Ignore production
if (!process.env.TASK_DEV) {
return false;
}
return {
name: 'taskbook-dev-source-rewrite',
configureServer(server) {
server.middlewares.use((req, res, next) => {
const pathname = new URL(req.url, 'http://localhost').pathname;
const task = tasks.find(({ module, task }) => pathname.startsWith(`/${module}/${task}/`));
if (task) {
const sourcePath = joinTaskSourceDir(task.path);
if (!req.url.includes(sourcePath)) {
req.url = req.url.replace(task.path, sourcePath);
}
}
next();
});
},
};
};
// All tasks for Taskbook's Index page
const tasks = discoverTaskDirs(__dirname);
export default defineConfig({
plugins: [devServerMultiPageSpa(), taskbookDevSourceRewrite(tasks), vue(), vueJsx()],
resolve: {
alias: [
{
find: 'vue',
replacement: 'vue/dist/vue.esm-bundler.js',
},
// Migration from @vue/cli Taskbook: support for ~@ alias in css
{ find: '~@', replacement: fileURLToPath(new URL('./src', import.meta.url)) },
// Migration from @vue/cli Taskbook: support for @ alias
{ find: '@', replacement: fileURLToPath(new URL('./src', import.meta.url)) },
// Migration from @vue/cli Taskbook: support public assets and icons
{ find: /^\/(assets|icons)\/(.*)/, replacement: '/src/$1/$2' },
],
// Migration from @vue/cli Taskbook: add .vue extension resolve
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
},
define: {
// Inject tasks to generate index page
'import.meta.env.TASKBOOK_TASKS': JSON.stringify(tasks),
},
build: {
rollupOptions: {
input: {
main: resolve(__dirname, 'index.html'),
...generatePagesConfig(tasks),
},
},
},
});