-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
173 lines (155 loc) · 5.01 KB
/
index.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const path = require('path');
const mime = require('mime-types');
const fs = require('fs');
let Config;
function resolver(from, to) {
return path.resolve(from, to);
}
function getPageRoots() {
const roots = [];
const { pageDir } = Config;
fs.readdirSync(resolver(Config.root, pageDir)).forEach(page => {
roots.push(resolver(Config.root, `${pageDir}${path.sep}${page}${path.sep}${page}.html`));
});
return roots;
}
function getPageNames() {
const pageNames = [];
const { pageDir } = Config;
fs.readdirSync(resolver(Config.root, pageDir)).forEach(page => {
pageNames.push(page);
});
return pageNames;
}
function rewritePages() {
const rules = [];
const { pageDir, pageName, mimeCheck, defaultPageName } = Config;
fs.readdirSync(resolver(Config.root, pageDir)).forEach(page => {
rules.push({
from: new RegExp(`^\/${page}\/${pageName}$`),
to: `/${pageDir}/${page}/${page}.html`
});
rules.push({
from: new RegExp(`^\/${page}.html$`),
to: `/${pageDir}/${page}/${page}.html`
});
rules.push({
from: new RegExp(`^\/${page}$`),
to: `/${pageDir}/${page}/${page}.html`
});
rules.push({
from: new RegExp(`^\/${page}\/$`),
to: `/${pageDir}/${page}/${page}.html`
});
rules.push({
from: new RegExp(`^\/${page}\/.+$`),
to: `/${pageDir}/${page}/${page}.html`
});
});
if (!Config.interceptor) {
rules.push({
from: new RegExp('^\/$'),
to: `/${pageDir}/${defaultPageName}.html`
});
rules.push({
from: new RegExp('^$'),
to: `/${pageDir}/${defaultPageName}.html`
});
}
return (req, res, next) => {
if (Config.interceptor) {
Config.interceptor(req, res, next);
}
const name = req.url;
if (mimeCheck) {
res.setHeader('Content-Type', mime.lookup(name));
}
const mimeType = mime.lookup(name);
if (mimeType != 'image/png') {
for (const rule in rules) {
if (name.match(rules[rule].from)) {
req.url = rules[rule].to;
res.statusCode = 302;
break;
}
}
}
return next();
};
}
module.exports = (interceptor = null, addServerConfiguration = [], userConfig = {}) => {
const defaultConfig = {
pageDir: 'pages',
pageName: 'index.html',
mimeCheck: true,
...userConfig
};
return {
name: 'vite-plugin-shiwaforce-mpa',
enforce: 'pre',
config(config) {
Config = config;
config.root = config.root || process.cwd();
config.defaultPageName = 'index';
config.pageDir = defaultConfig.pageDir;
config.removePageDirs = defaultConfig.removePageDirs;
config.pageName = defaultConfig.pageName;
config.pageNames = getPageNames();
config.mimeCheck = defaultConfig.mimeCheck;
config.server = config.server || {};
config.build = config.build || {};
config.build.outDir = config.build.outDir || 'dist';
config.build.rollupOptions = config.build.rollupOptions || {};
config.build.rollupOptions.input = getPageRoots();
config.build.assetsDir = defaultConfig.assetsDir || '';
config.interceptor = interceptor;
config.serverConfigurations = addServerConfiguration;
},
configureServer({ middlewares: app }) {
const { serverConfigurations } = Config;
for (const serverConfiguration of serverConfigurations) {
app.use(serverConfiguration());
}
app.use(rewritePages());
},
closeBundle() {
const { pageDir, pageNames } = Config;
// Change html-s to relative paths
fs.readdirSync(resolver(Config.build.outDir, pageDir)).forEach(page => {
const currentPagePlace = resolver(Config.build.outDir, `${pageDir}${path.sep}${page}${path.sep}${page}.html`);
const currentPageData = fs.readFileSync(currentPagePlace, 'utf8');
const result = currentPageData.replace(/[=]"\//g, '="/static/' + page + '/');
fs.writeFileSync(currentPagePlace, result, 'utf8');
});
// Rename page.html to index.html
fs.readdirSync(resolver(Config.build.outDir, pageDir)).forEach(page => {
const currentPagePlace = resolver(Config.build.outDir, `${pageDir}${path.sep}${page}${path.sep}${page}.html`);
const newPagePlace = resolver(Config.build.outDir, `${pageDir}${path.sep}${page}${path.sep}index.html`);
fs.renameSync(currentPagePlace, newPagePlace);
});
// Move files to proper directory
fs.readdirSync(resolver(Config.build.outDir, '.')).forEach(file => {
const filePath = resolver(Config.build.outDir, file);
if (!fs.lstatSync(filePath).isDirectory()) {
const fileName = file.split('.')[0];
const matchedPageIndex = pageNames.indexOf(fileName);
const hasPageMatch = matchedPageIndex > -1;
const moveFolder = pageNames[matchedPageIndex];
if (hasPageMatch) {
// Move files
const newFilePlace = resolver(Config.build.outDir, `${pageDir}${path.sep}${moveFolder}${path.sep}${file}`);
fs.renameSync(filePath, newFilePlace);
} else {
// Copy files
for (const pageName of pageNames) {
const newFilePlace = resolver(Config.build.outDir, `${pageDir}${path.sep}${pageName}${path.sep}${file}`);
fs.copyFileSync(filePath, newFilePlace);
}
fs.rmSync(filePath);
}
}
});
console.log('The files are moved to the dist!');
}
};
};