forked from scalacenter/scastie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.js
133 lines (123 loc) · 3.3 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
import { spawnSync } from "child_process";
import path from "path";
import { defineConfig, splitVendorChunkPlugin } from "vite";
import { plugin as mdPlugin } from 'vite-plugin-markdown';
import scalaJSPlugin from '@scala-js/vite-plugin-scalajs';
function isDev() {
return process.env.NODE_ENV !== "production";
}
function emitEmbedded() {
return process.env.MODE == "embed"
}
const root = path.resolve('client/src/main/resources', (isDev() ? 'dev' : 'prod'))
const embeddedOptions = {
input: {
embedded: path.resolve(root, 'embedded.js')
},
output: {
entryFileNames: "embedded/[name].js",
assetFileNames: "embedded/[name].[ext]"
}
}
const embeddedLibrary = {
entry: path.resolve(root, 'embedded.js'),
name: "scastie",
formats: ['umd'],
}
const websiteOptions = {
input: {
app: path.resolve(root, 'index.html')
},
output: {
entryFileNames: "[name]-[hash].js",
assetFileNames: "assets/[name]-[hash].[ext]"
}
}
if (!isDev()) {
console.info(`Production bundling: ${emitEmbedded() ? 'embedded' : 'website' }`)
} else {
console.info('Developer mode')
}
const proxy = {
"/metals": {
target: "http://0.0.0.0:8000"
},
"/": {
target: "http://0.0.0.0:9000",
bypass: function(req, res, proxyOptions) {
// regex matching snippet ids
const snippet = /(\/[A-Za-z0-9]{22}|\/[A-Za-z0-9]{22}\/([A-Za-z0-9])*[/(0-9)*])/;
const snippetOld = /(\/[0-9]+)/;
const backendUrls = /(\/api\/|\/login|\/logout|\/*.wasm|\/*.scm)/;
if(!backendUrls.test(req.url)) {
if (snippet.test(req.url) || snippetOld.test(req.url)) {
console.log("index: " + req.url);
return "/";
} else {
if (req.url.startsWith("/try")) {
return "/";
} else {
console.log("other: " + req.url);
return req.url;
}
}
} else {
console.log("proxied: " + req.url);
}
}
}
}
export default defineConfig({
define: {
'process.env.NODE_ENV': `"${process.env.NODE_ENV}"`,
'process.env.MODE': `"${process.env.MODE}"`
},
root: root,
base: isDev() ? '' : '/public/',
plugins: [
scalaJSPlugin({
projectID: 'client'
}),
splitVendorChunkPlugin(),
mdPlugin({
mode: ['html']
}),
],
resolve: {
alias: [
{
find: '@resources',
replacement: path.resolve(__dirname, 'client', 'src', 'main', 'resources'),
},
{
find: '@scastieRoot',
replacement: path.resolve(__dirname),
}
],
},
build: {
outDir: path.resolve(__dirname, 'client', 'dist', 'public'),
rollupOptions: emitEmbedded() ? embeddedOptions : websiteOptions,
emptyOutDir: !emitEmbedded(),
// Embedded is used as a library, in order to support current scastie embedded users.
// It outputs 'umd' module which allows to use <script> tag without specifying its type to "module"
lib: emitEmbedded() ? embeddedLibrary : null,
},
css: {
devSourcemap: true,
preprocessorOptions: {
stylus: { // or stylus, depending on the stylus files extension name you use
imports: [path.resolve(__dirname, 'node_modules', 'highlight.js', 'styles')],
}
}
},
server: {
proxy: proxy,
port: 8080,
strictPort: true,
},
preview: {
port: 8080,
strictPort: true,
}
});