feat: create dev scripts

This commit is contained in:
Valerio Ageno
2024-05-23 22:02:50 +02:00
parent ec2fb17f7f
commit 322908145e
9 changed files with 132 additions and 42 deletions
+34 -19
View File
@@ -7,10 +7,33 @@ use watchexec::Watchexec;
use watchexec_signals::Signal;
use watchexec_supervisor::job::start_job;
// What is the development pipeline?
//
// 1. Any file gets updates (rs/js/ts)
// 2. Client side vite works separately
// 3. Stop the dev server
// 4. Build the ssr client bundle
// 5. Restart the server
//
//
// The current solution is not optimized
// - We should avoid to bundle static lib (i.e. react) in the main bundle
#[tokio::main]
pub async fn watch() -> Result<()> {
bundle_axum_source();
let (watch_client, _) = start_job(Arc::new(Command {
program: Program::Exec {
prog: "node_modules/.bin/tuono-dev-watch".into(),
args: vec![],
}
.into(),
options: Default::default(),
}));
watch_client.start().await;
let (run_server, _) = start_job(Arc::new(Command {
program: Program::Exec {
prog: "cargo".into(),
@@ -20,35 +43,27 @@ pub async fn watch() -> Result<()> {
options: Default::default(),
}));
let (build_server_js, _) = start_job(Arc::new(Command {
let (build_ssr_bundle, _) = start_job(Arc::new(Command {
program: Program::Exec {
prog: "node_modules/.bin/vite".into(),
// TODO: update output directory: Use .tuono
args: vec![
"build".to_string(),
"--ssr".to_string(),
"--logLevel".to_string(),
"silent".to_string(),
],
prog: "node_modules/.bin/tuono-dev-ssr".into(),
args: vec![],
}
.into(),
options: Default::default(),
}));
build_server_js.start().await;
build_ssr_bundle.start().await;
run_server.start().await;
let wx = Watchexec::new(move |mut action| {
for event in action.events.iter() {
for path in event.paths() {
if path.0.to_string_lossy().ends_with(".rs") {
run_server.stop();
println!("Reloading server...");
build_server_js.stop();
build_server_js.start();
bundle_axum_source();
run_server.start();
}
for _ in event.paths() {
run_server.stop();
println!("Reloading server...");
build_ssr_bundle.stop();
build_ssr_bundle.start();
bundle_axum_source();
run_server.start();
}
}
+1 -19
View File
@@ -1,22 +1,4 @@
use bundler::bundler;
use std::path::Path;
use std::process::Command;
use std::thread;
pub fn run() {
let current_dir = std::env::current_dir().unwrap();
let vite_path = Path::new("node_modules/.bin/vite");
let vite = current_dir.join(vite_path);
let client_handler = thread::spawn(move || {
let _ = Command::new(vite).arg("dev").output();
});
let server_handler = thread::spawn(move || {
bundler::watch().unwrap();
});
let _ = client_handler.join();
let _ = server_handler.join();
bundler::watch().unwrap();
}
+1 -1
View File
@@ -7,7 +7,7 @@ impl Js {
thread_local! {
pub static SSR: RefCell<Ssr<'static, 'static>> = RefCell::new(
Ssr::from(
read_to_string("./out/server/server-main.js").unwrap(),
read_to_string("./.tuono/server/dev-server.js").unwrap(),
""
).unwrap()
)
+5
View File
@@ -0,0 +1,5 @@
#!/usr/bin/env node
import { buildProd } from '../dist/esm/build/index.js'
buildProd()
+5
View File
@@ -0,0 +1,5 @@
#!/usr/bin/env node
import { developmentSSRBundle } from '../dist/esm/build/index.js'
developmentSSRBundle()
+5
View File
@@ -0,0 +1,5 @@
#!/usr/bin/env node
import { developmentCSRWatch } from '../dist/esm/build/index.js'
developmentCSRWatch()
+20 -2
View File
@@ -15,6 +15,16 @@
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.js",
"exports": {
"./build": {
"import": {
"types": "./dist/esm/build/index.d.ts",
"default": "./dist/esm/build/index.js"
},
"require": {
"types": "./dist/cjs/build/index.d.ts",
"default": "./dist/cjs/build/index.js"
}
},
".": {
"import": {
"types": "./dist/esm/index.d.ts",
@@ -27,13 +37,21 @@
},
"./package.json": "./package.json"
},
"bin": {
"tuono-dev-ssr": "./bin/dev-ssr.js",
"tuono-dev-watch": "./bin/watch.js"
},
"files": [
"dist",
"src",
"README.md"
"README.md",
"bin/**"
],
"dependencies": {
"tuono-router": "workspace:*"
"@vitejs/plugin-react-swc": "^3.7.0",
"tuono-router": "workspace:*",
"tuono-vite": "workspace:*",
"vite": "^5.2.11"
},
"sideEffects": false,
"keywords": [],
+60
View File
@@ -0,0 +1,60 @@
import { build, createServer } from 'vite'
import react from '@vitejs/plugin-react-swc'
import { ViteFsRouter } from 'tuono-vite'
const BASE_CONFIG = {
silent: true,
root: '.tuono',
publicDir: '../public',
cacheDir: 'cache',
envDir: '../',
plugins: [react(), ViteFsRouter()],
}
export function developmentSSRBundle() {
;(async () => {
console.log('Build SSR')
await build({
...BASE_CONFIG,
build: {
ssr: true,
minify: false,
outDir: 'server',
emptyOutDir: true,
rollupOptions: {
input: './.tuono/server-main.tsx',
output: {
entryFileNames: 'dev-server.js',
format: 'iife',
},
},
},
ssr: {
target: 'webworker',
noExternal: true,
},
})
})()
}
export function developmentCSRWatch() {
;(async () => {
console.log('Watch files')
const server = await createServer({
...BASE_CONFIG,
server: {
port: 3001,
strictPort: true,
},
build: {
manifest: true,
outDir: '../out',
emptyOutDir: true,
rollupOptions: {
input: './.tuono/client-main.tsx',
},
},
})
await server.listen()
})()
}
+1 -1
View File
@@ -14,7 +14,7 @@ const config = defineConfig({
export default mergeConfig(
config,
tanstackBuildConfig({
entry: './src/index.ts',
entry: ['./src/index.ts', './src/build/index.ts'],
srcDir: './src',
}),
)