-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.mjs
128 lines (104 loc) · 4.93 KB
/
main.mjs
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
import svelte from 'rollup-plugin-svelte'
import resolve from '@rollup/plugin-node-resolve'
import http from "http"
import { rollup } from 'rollup';
const Entry = Java.type("net.razshare.svelte3jssr.Entry")
const NodeProxy = Java.type("net.razshare.svelte3jssr.proxies.NodeProxy")
const SvelteProxy = Java.type("net.razshare.svelte3jssr.proxies.SvelteProxy")
const JavaProxy = Java.type("net.razshare.svelte3jssr.proxies.JavaProxy")
// FLAGS START
////////////////////////////////////////////////////
//If this is enabled the server will cache in compiled ".svelte" files, making it faster to server.
const USE_CACHE = true
//If this is enabled, the server will include props in the prerendered version of the component.
//This will make it easier to SEO.
//Make no mistake the JS bundle will still contain your data and render it properly if this flag is disabled.
const PREPRENDER_PROPS = true
////////////////////////////////////////////////////
// FLAGS END
const compiledComponents = {}
// This following instructions give Java access to some NodeJS functions and their context.
// API STARTS
////////////////////////////////////////////////////
NodeProxy.setRequire((url)=>{
return importScripts(url)
})
NodeProxy.setJson(JSON)
SvelteProxy.setCompiler(async (source,generate)=>{
if(USE_CACHE && compiledComponents[`${source}::${generate}`]) return compiledComponents[`${source}::${generate}`]
const bundle = await rollup({
input:source,
plugins:[
svelte({
// By default, all ".svelte" files are compiled
//extensions: ['.my-custom-extension'],
// You can restrict which files are compiled
// using `include` and `exclude`
include: 'src/main/svelte/**/*.svelte',
// Optionally, preprocess components with svelte.preprocess:
// https://svelte.dev/docs#svelte_preprocess
preprocess: {
style: ({ content }) => {
return transformStyles(content);
}
},
// Emit CSS as "files" for other plugins to process. default is true
emitCss: false,
// Warnings are normally passed straight to Rollup. You can
// optionally handle them here, for example to squelch
// warnings with a particular code
onwarn: (warning, handler) => {
// e.g. don't warn on <marquee> elements, cos they're cool
if (warning.code === 'a11y-distracting-elements') return;
// let Rollup handle all other warnings normally
handler(warning);
},
// You can pass any of the Svelte compiler options
compilerOptions: {
// By default, the client-side compiler is used. You
// can also use the server-side rendering compiler
generate,
// ensure that extra attributes are added to head
// elements for hydration (used with generate: 'ssr')
hydratable: true,
// You can optionally set 'customElement' to 'true' to compile
// your components to custom elements (aka web elements)
customElement: false
}
}),
// see NOTICE below
resolve({ browser: true }),
// ...
]
})
const { output } = await bundle.generate({
format:'iife'
})
compiledComponents[`${source}::${generate}`] = output[0].code
return output[0].code
})
SvelteProxy.setRender((code,props)=>{
const Component = eval(code)
console.log("############################################")
console.log("code:",Component.render.toString())
console.log("############################################")
return Component.render(PREPRENDER_PROPS?props:{})
})
////////////////////////////////////////////////////
// API ENDS
/**
* I'm running a node js server here but you can just initialize your
* own Java server inside src/main/java/Entry.java instead.
*
* It will probably be hard to spin off a Spring Boot server because Spring Boot does some code transformations that are not possible in GraalVM, at least not with the default Spring Boot project setup.
* However since then this came out: https://spring.io/blog/2020/04/16/spring-tips-the-graalvm-native-image-builder-feature
*
* That being said you should be perfectly able to run a Quarkus web server using the same familiar Spring API.
* For more information refer to: https://quarkus.io/
*/
new Entry() //initialize Java app.
http
.createServer((request, response)=>{
JavaProxy.requestCallback(request, response)
})
.listen(8000, ()=>console.log("Graal.js server running at http://127.0.0.1:8000/"))