-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathvite.config.js
More file actions
145 lines (128 loc) · 5.39 KB
/
Copy pathvite.config.js
File metadata and controls
145 lines (128 loc) · 5.39 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
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
/*
* Vite is used to compile and minify JS and Sass.
*
* Assets are compiled from src/App/assets into the public directory:
* js/*.js - one file per entry below, plus the shared js/vendor.js
* css/app.css - compiled from scss/index.scss (imported by js/index.js)
* fonts/* - emitted for every font referenced by the stylesheets
* images/app/ - copied verbatim from App/assets/images
*
* So please, DO NOT MANUALLY ADD ASSETS TO THE PUBLIC DIRECTORY!
*/
import { defineConfig } from 'vite';
import { viteStaticCopy } from 'vite-plugin-static-copy';
import fs from 'fs';
import path from 'path';
const projectRoot = import.meta.dirname;
const assetsPath = 'App/assets';
const imagesPath = `${assetsPath}/images`;
/*
* The public directory is never emptied, so a production build has to clear out
* the source maps a previous development build left behind. Without this they
* would linger next to the minified assets they no longer describe.
*/
const removeStaleSourceMaps = () => ({
name: 'remove-stale-source-maps',
closeBundle() {
for (const directory of ['js', 'css']) {
const directoryPath = path.resolve(projectRoot, 'public', directory);
if (! fs.existsSync(directoryPath)) {
continue;
}
for (const file of fs.readdirSync(directoryPath)) {
if (file.endsWith('.map')) {
fs.unlinkSync(path.join(directoryPath, file));
}
}
}
},
});
/*
* Each entry becomes public/js/<name>.js and is loaded by a template as
* <script type="module">. Code shared between them (jQuery, Bootstrap,
* Chart.js, ...) is emitted once as a shared chunk.
*/
const entries = {
app: `${assetsPath}/js/index.js`,
admin: `${assetsPath}/js/components/_admin.js`,
table_settings: `${assetsPath}/js/components/_table_settings.js`,
user: `${assetsPath}/js/components/_user.js`,
};
export default defineConfig(({ mode }) => {
const isDevelopment = mode === 'development';
return {
// Look for source files under src, mirroring the asset paths above.
root: path.resolve(projectRoot, 'src'),
plugins: [
viteStaticCopy({
targets: [
{
src: `${imagesPath}/**/*`,
dest: 'images/app',
// Globs only match files, so the images directory has to
// be walked recursively and its leading path segments
// dropped to keep the nested directories intact.
rename: { stripBase: imagesPath.split('/').length },
},
],
}),
! isDevelopment && removeStaleSourceMaps(),
],
build: {
outDir: path.resolve(projectRoot, 'public'),
// public/ also holds index.php, robots.txt and uploads/, so it must
// never be wiped. Compiled assets use stable names and overwrite in
// place instead.
emptyOutDir: false,
/*
* Development builds are readable and mapped, production builds are
* minified with no map. The maps are written as separate .map files
* (ignored by git) rather than inlined, so that the committed
* bundles never carry a base64 copy of their own sources.
*
* Only `npm run prod` produces assets meant to be committed.
*/
sourcemap: isDevelopment,
minify: ! isDevelopment,
rollupOptions: {
input: entries,
output: {
entryFileNames: 'js/[name].js',
chunkFileNames: 'js/[name].js',
assetFileNames: (assetInfo) => {
const fileName = assetInfo.names?.[0] ?? assetInfo.name ?? '';
const sourcePath = assetInfo.originalFileNames?.[0] ?? '';
if (/\.css$/i.test(fileName)) {
return 'css/[name][extname]';
}
// An SVG may be either a font or an image, so anything
// living in an image directory is not treated as a font.
if (
/\.(woff2?|eot|ttf|otf|svg)$/i.test(fileName)
&& ! /(^|\/)(images?|img)\//i.test(sourcePath)
) {
return 'fonts/[name][extname]';
}
return 'assets/[name][extname]';
},
/*
* Chunking is deliberately left to Rollup. Forcing the
* dependencies into a chunk of their own reorders them ahead
* of the entry that sets window.jQuery, which breaks
* jquery-sparkline: it reads the global instead of importing
* jQuery, so it throws while the bundle is still loading.
*/
},
},
},
css: {
preprocessorOptions: {
scss: {
// Bootstrap 5.3 still uses @import internally; silence the
// Dart Sass deprecation warnings it emits.
quietDeps: true,
},
},
},
};
});