Skip to content

Commit

Permalink
feat: add tuono.config.ts support (#153)
Browse files Browse the repository at this point in the history
Co-authored-by: Marco Pasqualetti <[email protected]>
  • Loading branch information
Valerioageno and marcalexiei authored Nov 30, 2024
1 parent 8052de8 commit ec4577c
Show file tree
Hide file tree
Showing 19 changed files with 409 additions and 76 deletions.
13 changes: 13 additions & 0 deletions crates/tuono/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ const ROUTES_FOLDER_PATH: &str = "\\src\\routes";
#[cfg(target_os = "windows")]
const BUILD_JS_SCRIPT: &str = ".\\node_modules\\.bin\\tuono-build-prod.cmd";

#[cfg(target_os = "windows")]
const BUILD_TUONO_CONFIG: &str = ".\\node_modules\\.bin\\tuono-build-config.cmd";

#[cfg(not(target_os = "windows"))]
const ROUTES_FOLDER_PATH: &str = "/src/routes";
#[cfg(not(target_os = "windows"))]
const BUILD_JS_SCRIPT: &str = "./node_modules/.bin/tuono-build-prod";

#[cfg(not(target_os = "windows"))]
const BUILD_TUONO_CONFIG: &str = "./node_modules/.bin/tuono-build-config";

#[derive(Debug)]
pub struct App {
pub route_map: HashMap<String, Route>,
Expand Down Expand Up @@ -147,6 +153,13 @@ impl App {
.expect("Failed to run the rust server")
}

pub fn build_tuono_config(&self) -> Result<std::process::Output, std::io::Error> {
Command::new(BUILD_TUONO_CONFIG)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
}
pub fn get_used_http_methods(&self) -> HashSet<Method> {
let mut acc = HashSet::new();

Expand Down
7 changes: 6 additions & 1 deletion crates/tuono/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ pub fn app() -> std::io::Result<()> {
Actions::Dev => {
check_ports(Mode::Dev);

let _ = init_tuono_folder(Mode::Dev)?;
let app = init_tuono_folder(Mode::Dev)?;
app.build_tuono_config()
.expect("Failed to build tuono.config.ts");

watch::watch().unwrap();
}
Expand All @@ -105,6 +107,9 @@ pub fn app() -> std::io::Result<()> {
return Ok(());
}

app.build_tuono_config()
.expect("Failed to build tuono.config.ts");

app.build_react_prod();

if ssg {
Expand Down
2 changes: 1 addition & 1 deletion examples/tuono-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"],
"include": ["src", "tuono.config.ts"],
"references": [{ "path": "./tsconfig.node.json" }]
}
5 changes: 5 additions & 0 deletions examples/tuono-app/tuono.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { TuonoConfig } from 'tuono/config'

const config: TuonoConfig = {}

export default config
2 changes: 1 addition & 1 deletion examples/tuono-tutorial/src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import type { JSX } from 'react'
import { Head, type TuonoProps } from 'tuono'

import PokemonLink from '../components/PokemonLink'
import PokemonLink from '@/components/PokemonLink'

interface Pokemon {
name: string
Expand Down
2 changes: 1 addition & 1 deletion examples/tuono-tutorial/src/routes/pokemons/[pokemon].tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { JSX } from 'react'
import { Head, type TuonoProps } from 'tuono'

import PokemonView from '../../components/PokemonView'
import PokemonView from '@/components/PokemonView'

interface Pokemon {
name: string
Expand Down
7 changes: 5 additions & 2 deletions examples/tuono-tutorial/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
"noFallthroughCasesInSwitch": true,
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["src"],
"include": ["src", "tuono.config.ts"],
"references": [{ "path": "./tsconfig.node.json" }]
}
11 changes: 11 additions & 0 deletions examples/tuono-tutorial/tuono.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { TuonoConfig } from 'tuono/config'

const config: TuonoConfig = {
vite: {
alias: {
'@': 'src',
},
},
}

export default config
2 changes: 1 addition & 1 deletion examples/with-mdx/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"],
"include": ["src", "tuono.config.ts"],
"references": [{ "path": "./tsconfig.node.json" }]
}
5 changes: 5 additions & 0 deletions examples/with-mdx/tuono.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { TuonoConfig } from 'tuono/config'

const config: TuonoConfig = {}

export default config
5 changes: 5 additions & 0 deletions packages/tuono/bin/build-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

import { buildConfig } from '../dist/esm/build/index.js'

buildConfig()
17 changes: 15 additions & 2 deletions packages/tuono/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"lint": "eslint --ext .ts,.tsx ./src -c ../../.eslintrc",
"format": "prettier -u --write --ignore-unknown '**/*'",
"format:check": "prettier --check --ignore-unknown '**/*'",
"types": "tsc --noEmit"
"types": "tsc --noEmit",
"test:watch": "vitest",
"test": "vitest run"
},
"repository": {
"type": "git",
Expand All @@ -31,6 +33,16 @@
"default": "./dist/cjs/build/index.js"
}
},
"./config": {
"import": {
"types": "./dist/esm/config/index.d.ts",
"default": "./dist/esm/config/index.js"
},
"require": {
"types": "./dist/cjs/config/index.d.ts",
"default": "./dist/cjs/config/index.js"
}
},
"./ssr": {
"import": {
"types": "./dist/esm/ssr/index.d.ts",
Expand Down Expand Up @@ -66,7 +78,8 @@
"bin": {
"tuono-dev-ssr": "./bin/dev-ssr.js",
"tuono-dev-watch": "./bin/watch.js",
"tuono-build-prod": "./bin/build-prod.js"
"tuono-build-prod": "./bin/build-prod.js",
"tuono-build-config": "./bin/build-config.js"
},
"files": [
"dist",
Expand Down
3 changes: 3 additions & 0 deletions packages/tuono/src/build/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const DOT_TUONO_FOLDER_NAME = '.tuono'
export const CONFIG_FOLDER_NAME = 'config'
export const CONFIG_FILE_NAME = 'config.mjs'
179 changes: 112 additions & 67 deletions packages/tuono/src/build/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { build, createServer, InlineConfig } from 'vite'
import { build, createServer, InlineConfig, mergeConfig } from 'vite'
import react from '@vitejs/plugin-react-swc'
import ViteFsRouter from 'tuono-fs-router-vite-plugin'
import { LazyLoadingPlugin } from 'tuono-lazy-fn-vite-plugin'
import mdx from '@mdx-js/rollup'
import { loadConfig, blockingAsync } from './utils'

const VITE_PORT = 3001

const BASE_CONFIG: InlineConfig = {
root: '.tuono',
Expand All @@ -22,91 +25,133 @@ const BASE_CONFIG: InlineConfig = {
],
}

const VITE_PORT = 3001

export function developmentSSRBundle() {
;(async () => {
await build({
...BASE_CONFIG,
build: {
ssr: true,
minify: false,
outDir: 'server',
emptyOutDir: true,
rollupOptions: {
input: './.tuono/server-main.tsx',
// Silent all logs
onLog() {},
output: {
entryFileNames: 'dev-server.js',
format: 'iife',
const developmentSSRBundle = () => {
blockingAsync(async () => {
const config = await loadConfig()
await build(
mergeConfig(BASE_CONFIG, {
resolve: {
alias: config.vite?.alias || {},
},
build: {
ssr: true,
minify: false,
outDir: 'server',
emptyOutDir: true,
rollupOptions: {
input: './.tuono/server-main.tsx',
// Silent all logs
onLog() {},
output: {
entryFileNames: 'dev-server.js',
format: 'iife',
},
},
},
},
ssr: {
target: 'webworker',
noExternal: true,
},
})
})()
ssr: {
target: 'webworker',
noExternal: true,
},
}),
)
})
}

export function developmentCSRWatch() {
;(async () => {
const server = await createServer({
...BASE_CONFIG,
// Entry point for the development vite proxy
base: '/vite-server/',
const developmentCSRWatch = () => {
blockingAsync(async () => {
const config = await loadConfig()
const server = await createServer(
mergeConfig(BASE_CONFIG, {
resolve: {
alias: config.vite?.alias || {},
},
// Entry point for the development vite proxy
base: '/vite-server/',

server: {
port: VITE_PORT,
strictPort: true,
},
build: {
manifest: true,
emptyOutDir: true,
rollupOptions: {
input: './.tuono/client-main.tsx',
server: {
port: VITE_PORT,
strictPort: true,
},
},
})
build: {
manifest: true,
emptyOutDir: true,
rollupOptions: {
input: './.tuono/client-main.tsx',
},
},
}),
)
await server.listen()
})()
})
}

export function buildProd() {
;(async () => {
await build({
...BASE_CONFIG,
build: {
manifest: true,
emptyOutDir: true,
outDir: '../out/client',
rollupOptions: {
input: './.tuono/client-main.tsx',
const buildProd = () => {
blockingAsync(async () => {
const config = await loadConfig()

await build(
mergeConfig(BASE_CONFIG, {
resolve: {
alias: config.vite?.alias || {},
},
},
})
build: {
manifest: true,
emptyOutDir: true,
outDir: '../out/client',
rollupOptions: {
input: './.tuono/client-main.tsx',
},
},
}),
)

await build(
mergeConfig(BASE_CONFIG, {
resolve: {
alias: config.vite?.alias || {},
},
build: {
ssr: true,
minify: true,
outDir: '../out/server',
emptyOutDir: true,
rollupOptions: {
input: './.tuono/server-main.tsx',
output: {
entryFileNames: 'prod-server.js',
format: 'iife',
},
},
},
ssr: {
target: 'webworker',
noExternal: true,
},
}),
)
})
}

const buildConfig = () => {
blockingAsync(async () => {
await build({
...BASE_CONFIG,
root: '.tuono',
logLevel: 'silent',
cacheDir: 'cache',
envDir: '../',
build: {
ssr: true,
minify: true,
outDir: '../out/server',
outDir: 'config',
emptyOutDir: true,
rollupOptions: {
input: './.tuono/server-main.tsx',
input: './tuono.config.ts',
output: {
entryFileNames: 'prod-server.js',
format: 'iife',
entryFileNames: 'config.mjs',
},
},
},
ssr: {
target: 'webworker',
noExternal: true,
},
})
})()
})
}

export { buildProd, buildConfig, developmentCSRWatch, developmentSSRBundle }
Loading

0 comments on commit ec4577c

Please sign in to comment.