vaderjs 1.4.2 → 1.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/main.js ADDED
@@ -0,0 +1,314 @@
1
+ #!/usr/bin/env bun
2
+
3
+ import { Glob } from 'bun'
4
+ const args = Bun.argv.slice(2)
5
+ import fs from 'fs'
6
+ import path from 'path'
7
+
8
+ if (!fs.existsSync(process.cwd() + '/app')) {
9
+ console.error(`App directory not found in ${process.cwd()}/app`)
10
+ process.exit(1)
11
+ }
12
+ if (!fs.existsSync(process.cwd() + '/public')) {
13
+ fs.mkdirSync(process.cwd() + '/public')
14
+ }
15
+ const mode = args.includes('dev') ? 'development' : args.includes('prod') ? 'production' : null
16
+ if(!mode){
17
+ console.log(`
18
+ Usage:
19
+ bun vaderjs dev - Start development server output in dist/
20
+ bun vaderjs prod - Build for production output in dist/
21
+ `)
22
+ process.exit(1)
23
+ }
24
+
25
+ let start = Date.now()
26
+ console.log(`Starting build at ${new Date().toLocaleTimeString()}`)
27
+ let { port, host, } = require(process.cwd() + '/config').default
28
+
29
+ if (!fs.existsSync(process.cwd() + '/jsconfig.json')) {
30
+ let json = {
31
+ "compilerOptions": {
32
+ "jsx": "react",
33
+ "jsxFactory": "e",
34
+ }
35
+ }
36
+ await Bun.write(process.cwd() + '/jsconfig.json', JSON.stringify(json, null, 4))
37
+ }
38
+
39
+ const bindes = []
40
+
41
+ const handleReplacements = (code, file) => {
42
+ let lines = code.split('\n')
43
+ let newLines = []
44
+ for (let line of lines) {
45
+ let hasImport = line.includes('import')
46
+ if(hasImport && line.includes('.jsx')){
47
+ line = line.replace('.jsx', '.js')
48
+ }
49
+ if(hasImport && line.includes('.css')){
50
+ try {
51
+ let url = path.join('/' + line.split("'")[1])
52
+ let css = fs.readFileSync(process.cwd() + url, 'utf-8')
53
+ line = '';
54
+ if(!bindes.includes(`<link rel="stylesheet" href="${url}">`)) {
55
+ bindes.push(`<link rel="stylesheet" href="${url}">`)
56
+ }
57
+ fs.mkdirSync(process.cwd() + '/dist' + path.dirname(url), { recursive: true })
58
+ fs.writeFileSync(process.cwd() + '/dist' + url, css)
59
+ } catch (error) {
60
+ console.error(error)
61
+ }
62
+ }
63
+ if (line.toLowerCase().includes('genkey()')) {
64
+ line = line.toLowerCase().replace('genkey()', `this.key = "${crypto.randomUUID()}"`)
65
+ }
66
+ if(!hasImport && line.includes('useFetch')){
67
+ line = line.replace('useFetch', 'this.useFetch')
68
+ }
69
+ if (!hasImport && line.includes('useState')) {
70
+ let key = line.split(',')[0].split('[')[1].replace(' ', '')
71
+ let b4 = line
72
+ b4 = line.replace('useState(', `this.useState('${key}',`)
73
+ line = b4
74
+ }
75
+ if(!hasImport && line.includes('useAsyncState')){
76
+ let key = line.split(',')[0].split('[')[1].replace(' ', '')
77
+ let b4 = line
78
+ b4 = line.replace('useAsyncState(', `this.useAsyncState('${key}',`)
79
+ line = b4
80
+ }
81
+ if (!hasImport && line.includes('useEffect')) {
82
+ let b4 = line
83
+ b4 = line.replace('useEffect(', `this.useEffect(`)
84
+ line = b4
85
+ }
86
+ if (!hasImport && line.includes('useRef')) {
87
+ let b4 = line
88
+ let key = line.split(' ')[1].split('=')[0]
89
+ b4 = line.replace('useRef(', `this.useRef('${key}',`)
90
+ line = b4
91
+ }
92
+ newLines.push(line)
93
+ }
94
+ let c = newLines.join('\n')
95
+ return c
96
+ }
97
+
98
+
99
+ async function generateApp() {
100
+ return new Promise(async (resolve, reject) => {
101
+ let routes = new Bun.FileSystemRouter({
102
+ dir: process.cwd() + '/app',
103
+ style: 'nextjs'
104
+ })
105
+ globalThis.routes = routes.routes
106
+ Object.keys(routes.routes).forEach(async (route) => {
107
+ let r = routes.routes[route]
108
+ let code = await Bun.file(r).text()
109
+ code = handleReplacements(code)
110
+ r = r.replace(process.cwd().replace(/\\/g, '/') + '/app', '')
111
+ r = r.replace('.jsx', '.js')
112
+ fs.mkdirSync(path.dirname(process.cwd() + '/dist/' + r), { recursive: true })
113
+ fs.writeFileSync(process.cwd() + '/dist/' + r, code)
114
+ fs.mkdirSync(process.cwd() + '/dev', { recursive: true })
115
+ fs.writeFileSync(path.join(process.cwd() + '/dev/bundler.js'), `
116
+ import { Component, e, useState, useEffect, useFetch, useAsyncState } from 'vaderjs'
117
+ import { document } from 'vaderjs/document'
118
+ import fs from 'fs'
119
+ let path2 = require('path')
120
+ globalThis.window = {
121
+ location: {
122
+ hash: '',
123
+ host: '',
124
+ }
125
+ }
126
+ globalThis.Component = Component
127
+ globalThis.e = e
128
+ globalThis.useState = useState
129
+ globalThis.genKey = () => {
130
+ return crypto.randomUUID()
131
+ };
132
+ globalThis.document = {
133
+ createElement: (tag) => {},
134
+ getElementById: (id) => {},
135
+ }
136
+ await Bun.build({
137
+ entrypoints: [process.cwd() + '/dist/' + process.env.ENTRYPOINT],
138
+ minify:true,
139
+ root: process.cwd() + '/dist',
140
+ outdir: process.cwd() + '/dist',
141
+ })
142
+ let isClass = function(element) {
143
+ return element.toString().startsWith('class');
144
+ };
145
+ const generatePage = async (data = {path: process.env.INPUT, route: process.env.OUT}) => {
146
+ const { path, route } = data
147
+ if(path.includes('root.js')) return
148
+ let html = await import(path).then(m => m.default)
149
+ let { head } = await import(path).then(m => m)
150
+ let isFunction = false
151
+ globalThis.isServer = true
152
+ if(isClass(html)){
153
+ html = new html()
154
+ html.Mounted = true
155
+ html = html.render()
156
+ }else{
157
+ isFunction = true
158
+ let instance = new Component()
159
+ html = html.bind(instance)
160
+ instance.render = html
161
+ html = instance.render()
162
+ }
163
+
164
+ let h = document(html)
165
+ if(!fs.existsSync(process.cwd() + '/dist' + path2.dirname(route))){
166
+ fs.mkdirSync(process.cwd() + '/dist' + path2.dirname(route), { recursive: true })
167
+ }
168
+ let headHtml = ''
169
+ if(head){
170
+ headHtml = document(head())
171
+ }
172
+ Bun.write(process.cwd() + '/dist/' + route + '/index.html', \`<!DOCTYPE html><head>\${headHtml}</head>\${h}
173
+ <script type="module">
174
+ import c from '\${route === '/' ? './index.js' : route + '/index.js'}'
175
+ import {render} from '/src/vader/index.js'
176
+ render(c, document.body.firstChild)
177
+ </script>
178
+ \`)
179
+ }
180
+ generatePage({path: process.env.INPUT, route: process.env.OUT})
181
+ `)
182
+
183
+ fs.mkdirSync(process.cwd() + '/dist/src/vader', { recursive: true })
184
+ fs.writeFileSync(process.cwd() + '/dist/src/vader/index.js', await new Bun.Transpiler({
185
+ loader: 'ts',
186
+ }).transformSync(await Bun.file(require.resolve('vaderjs')).text()))
187
+ const spawn = Bun.spawn({
188
+ cmd: ['bun', 'run', './dev/bundler.js'],
189
+ cwd: process.cwd(),
190
+ stdout: 'inherit',
191
+ env: {
192
+ ENTRYPOINT: r,
193
+ ROOT: process.cwd() + '/app/',
194
+ OUT: route,
195
+ INPUT: `../app/${r.replace('.js', '.jsx')}`,
196
+ },
197
+ onExit({exitCode: code}) {
198
+ if (code === 0) {
199
+ console.log('Build complete')
200
+ resolve()
201
+ } else {
202
+ reject()
203
+ }
204
+ }
205
+ })
206
+
207
+ })
208
+
209
+ })
210
+
211
+ }
212
+
213
+ generateApp()
214
+ function handleFiles(){
215
+ return new Promise(async (resolve, reject) => {
216
+ try {
217
+ let glob = new Glob('public/**/*')
218
+ for await (var i of glob.scan()){
219
+ let file = i
220
+ fs.mkdirSync(path.join(process.cwd() + '/dist', path.dirname(file)), { recursive: true })
221
+ fs.copyFileSync(file, path.join(process.cwd() + '/dist', file))
222
+ }
223
+ resolve()
224
+ } catch (error) {
225
+ reject(error)
226
+ }
227
+ })
228
+ }
229
+ handleFiles()
230
+ let isBuilding = false;
231
+ let timeout = null
232
+ if(mode === 'development'){
233
+ const watcher = fs.watch(path.join(process.cwd() + '/app'), { recursive: true })
234
+ const publicWatcher = fs.watch(path.join(process.cwd() + '/public'), { recursive: true })
235
+ publicWatcher.on('change', async (event, filename) => {
236
+ try {
237
+ await handleFiles()
238
+ clients.forEach(c => {
239
+ c.send('reload')
240
+ })
241
+ } catch (error) {
242
+ console.error(error)
243
+ }
244
+ })
245
+ watcher.on('change', async (event, filename) => {
246
+ try {
247
+ await generateApp()
248
+ clients.forEach(c => {
249
+ c.send('reload')
250
+ })
251
+ } catch (error) {
252
+ console.error(error)
253
+ }
254
+
255
+ })
256
+ watcher.on('error', (err) => {
257
+ console.error(err)
258
+ })
259
+ }
260
+ globalThis.clients = []
261
+
262
+ if(mode === 'development'){
263
+ let server = Bun.serve({
264
+ port: port || 8080,
265
+ websocket: {
266
+ open(ws) {
267
+ globalThis.clients.push(ws)
268
+ ws.send('Connected')
269
+ },
270
+ message(ws, message) {
271
+ globalThis.clients.forEach(c => {
272
+ c.send(message)
273
+ })
274
+ },
275
+
276
+ },
277
+ async fetch(req, res) {
278
+ if(res.upgrade(req)){
279
+ return new Response('Upgraded', { status: 101 })
280
+ }
281
+
282
+ let url = new URL(req.url)
283
+ if(url.pathname.includes('.')){
284
+ let file = await Bun.file(path.join(process.cwd() + '/dist' + url.pathname))
285
+ if(!await file.exists()) return new Response('Not found', { status: 404 })
286
+ return new Response(await file.text(), {
287
+ headers: {
288
+ 'Content-Type': file.type
289
+ }
290
+ })
291
+ }
292
+ return new Response(fs.readFileSync(process.cwd() + '/dist/' + url.pathname + '/index.html') + `
293
+ <script>
294
+ let ws = new WebSocket('ws://localhost:${server.port}')
295
+ ws.onmessage = (e) => {
296
+ if(e.data === 'reload'){
297
+ window.location.reload()
298
+ }
299
+ console.log(e.data)
300
+ }
301
+ </script>
302
+ `, {
303
+ headers: {
304
+ 'Content-Type': 'text/html'
305
+ }
306
+ })
307
+ }
308
+ })
309
+
310
+ console.log(`Server running on http://localhost:${server.port}`)
311
+ }else{
312
+ console.log(`Build complete in ${Date.now() - start}ms at ${new Date().toLocaleTimeString()}`)
313
+ process.exit(0)
314
+ }
package/package.json CHANGED
@@ -1,25 +1,9 @@
1
- {
2
- "name": "vaderjs",
3
- "description": "A reactive framework for building fast and scalable web applications",
4
- "version": "1.4.2",
5
- "main": "vader.js",
6
- "author": {
7
- "name": "Malikwhitten67",
8
- "email": "malikwhitterb@gmail.com"
9
- },
10
- "license": "MIT",
11
- "repository": {
12
- "type": "git",
13
- "url": "https://github.com/Postr-Inc/Vader.js"
14
- },
15
- "type": "module",
16
- "bin":{
17
- "vader":"./vader.js"
18
- },
19
- "dependencies": {
20
- "ws": "latest"
21
- },
22
- "devDependencies": {
23
- "ws": "latest"
24
- }
1
+ {
2
+ "name": "vaderjs",
3
+ "version": "1.4.4",
4
+ "description": "A simple and powerful JavaScript library for building modern web applications.",
5
+ "main":"./index.js",
6
+ "bin": {
7
+ "vaderjs": "./main.js"
8
+ }
25
9
  }
package/.editorconfig DELETED
@@ -1,11 +0,0 @@
1
- root = true
2
-
3
- [*]
4
- charset = utf-8
5
- indent_style = space
6
- indent_size = 4
7
- insert_final_newline = true
8
- end_of_line = lf
9
-
10
- [*.{js,ts}]
11
- trim_trailing_whitespace = true
@@ -1,21 +0,0 @@
1
- {
2
- "configurations": [
3
- {
4
- "name": "Win32",
5
- "includePath": [
6
- "${workspaceFolder}/**"
7
- ],
8
- "defines": [
9
- "_DEBUG",
10
- "UNICODE",
11
- "_UNICODE"
12
- ],
13
- "windowsSdkVersion": "10.0.22621.0",
14
- "compilerPath": "cl.exe",
15
- "cStandard": "c17",
16
- "cppStandard": "c++17",
17
- "intelliSenseMode": "windows-msvc-x64"
18
- }
19
- ],
20
- "version": 4
21
- }
@@ -1,12 +0,0 @@
1
- {
2
- "files.associations": {
3
- "stdlib.h": "c",
4
- "stdio.h": "c",
5
- "fstream": "cpp",
6
- "istream": "cpp",
7
- "xstring": "cpp",
8
- "ostream": "cpp",
9
- "system_error": "cpp",
10
- "xlocale": "cpp"
11
- }
12
- }