mirror of
https://github.com/tuono-labs/tuono
synced 2026-07-25 12:52:47 -07:00
feat: created base vite-fs-router plugin
This commit is contained in:
@@ -3,3 +3,6 @@ dist/
|
||||
.turbo/
|
||||
|
||||
pnpm-lock.yaml
|
||||
|
||||
examples/playground/
|
||||
|
||||
|
||||
+67
-66
@@ -4,16 +4,16 @@
|
||||
const config = {
|
||||
root: true,
|
||||
reportUnusedDisableDirectives: true,
|
||||
ignorePatterns: ["**/build", "**/dist"],
|
||||
parser: "@typescript-eslint/parser",
|
||||
plugins: ["@typescript-eslint", "import"],
|
||||
ignorePatterns: ['**/build', '**/dist'],
|
||||
parser: '@typescript-eslint/parser',
|
||||
plugins: ['@typescript-eslint', 'import'],
|
||||
extends: [
|
||||
"eslint:recommended",
|
||||
"plugin:@typescript-eslint/recommended",
|
||||
"plugin:@typescript-eslint/stylistic",
|
||||
"plugin:import/recommended",
|
||||
"plugin:import/typescript",
|
||||
"prettier",
|
||||
'eslint:recommended',
|
||||
'plugin:@typescript-eslint/recommended',
|
||||
'plugin:@typescript-eslint/stylistic',
|
||||
'plugin:import/recommended',
|
||||
'plugin:import/typescript',
|
||||
'prettier',
|
||||
],
|
||||
env: {
|
||||
browser: true,
|
||||
@@ -22,86 +22,87 @@ const config = {
|
||||
parserOptions: {
|
||||
tsconfigRootDir: __dirname,
|
||||
project: true,
|
||||
sourceType: "module",
|
||||
sourceType: 'module',
|
||||
ecmaVersion: 2020,
|
||||
},
|
||||
settings: {
|
||||
"import/parsers": {
|
||||
"@typescript-eslint/parser": [".ts", ".tsx"],
|
||||
'import/parsers': {
|
||||
'@typescript-eslint/parser': ['.ts', '.tsx'],
|
||||
},
|
||||
"import/resolver": {
|
||||
'import/resolver': {
|
||||
typescript: true,
|
||||
},
|
||||
react: {
|
||||
version: "detect",
|
||||
version: 'detect',
|
||||
},
|
||||
},
|
||||
rules: {
|
||||
"@typescript-eslint/array-type": "error",
|
||||
"@typescript-eslint/ban-types": "error",
|
||||
"@typescript-eslint/ban-ts-comment": "error",
|
||||
"@typescript-eslint/consistent-type-definitions": "error",
|
||||
"@typescript-eslint/consistent-type-imports": [
|
||||
"error",
|
||||
{ prefer: "type-imports" },
|
||||
'@typescript-eslint/array-type': 'error',
|
||||
'@typescript-eslint/ban-types': 'error',
|
||||
'@typescript-eslint/ban-ts-comment': 'error',
|
||||
'@typescript-eslint/consistent-type-definitions': 'error',
|
||||
'@typescript-eslint/consistent-type-imports': [
|
||||
'error',
|
||||
{ prefer: 'type-imports' },
|
||||
],
|
||||
"@typescript-eslint/explicit-module-boundary-types": "error",
|
||||
"@typescript-eslint/method-signature-style": ["error", "property"],
|
||||
"@typescript-eslint/naming-convention": [
|
||||
"error",
|
||||
'@typescript-eslint/explicit-module-boundary-types': 'error',
|
||||
'@typescript-eslint/method-signature-style': ['error', 'property'],
|
||||
'@typescript-eslint/naming-convention': [
|
||||
'error',
|
||||
{
|
||||
selector: "typeParameter",
|
||||
format: ["PascalCase"],
|
||||
leadingUnderscore: "forbid",
|
||||
trailingUnderscore: "forbid",
|
||||
selector: 'typeParameter',
|
||||
format: ['PascalCase'],
|
||||
leadingUnderscore: 'forbid',
|
||||
trailingUnderscore: 'forbid',
|
||||
custom: {
|
||||
regex: "^(T|T[A-Z][A-Za-z]+)$",
|
||||
regex: '^(T|T[A-Z][A-Za-z]+)$',
|
||||
match: true,
|
||||
},
|
||||
},
|
||||
],
|
||||
"@typescript-eslint/no-empty-function": "error",
|
||||
"@typescript-eslint/no-empty-interface": "error",
|
||||
"@typescript-eslint/no-explicit-any": "error",
|
||||
"@typescript-eslint/no-non-null-assertion": "error",
|
||||
"@typescript-eslint/no-unnecessary-condition": "error",
|
||||
"@typescript-eslint/no-unnecessary-type-assertion": "error",
|
||||
"@typescript-eslint/no-unused-vars": "error",
|
||||
"@typescript-eslint/no-inferrable-types": [
|
||||
"error",
|
||||
'@typescript-eslint/no-empty-function': 'error',
|
||||
'@typescript-eslint/no-empty-interface': 'error',
|
||||
'@typescript-eslint/no-explicit-any': 'error',
|
||||
'@typescript-eslint/no-non-null-assertion': 'error',
|
||||
'@typescript-eslint/no-unnecessary-condition': 'error',
|
||||
'@typescript-eslint/no-unnecessary-type-assertion': 'error',
|
||||
'@typescript-eslint/no-unused-vars': 'error',
|
||||
'@typescript-eslint/explicit-function-return-type': 'error',
|
||||
'@typescript-eslint/no-inferrable-types': [
|
||||
'error',
|
||||
{ ignoreParameters: true },
|
||||
],
|
||||
"import/default": "error",
|
||||
"import/export": "error",
|
||||
"import/namespace": "error",
|
||||
"import/newline-after-import": "error",
|
||||
"import/no-cycle": "error",
|
||||
"import/no-duplicates": "off",
|
||||
"import/no-named-as-default-member": "error",
|
||||
"import/no-unused-modules": "error",
|
||||
"import/order": [
|
||||
"error",
|
||||
'import/default': 'error',
|
||||
'import/export': 'error',
|
||||
'import/namespace': 'error',
|
||||
'import/newline-after-import': 'error',
|
||||
'import/no-cycle': 'error',
|
||||
'import/no-duplicates': 'off',
|
||||
'import/no-named-as-default-member': 'error',
|
||||
'import/no-unused-modules': 'error',
|
||||
'import/order': [
|
||||
'error',
|
||||
{
|
||||
groups: [
|
||||
"builtin",
|
||||
"external",
|
||||
"internal",
|
||||
"parent",
|
||||
"sibling",
|
||||
"index",
|
||||
"object",
|
||||
"type",
|
||||
'builtin',
|
||||
'external',
|
||||
'internal',
|
||||
'parent',
|
||||
'sibling',
|
||||
'index',
|
||||
'object',
|
||||
'type',
|
||||
],
|
||||
},
|
||||
],
|
||||
"no-case-declarations": "error",
|
||||
"no-empty": "error",
|
||||
"no-prototype-builtins": "error",
|
||||
"no-redeclare": "error",
|
||||
"no-shadow": "error",
|
||||
"no-undef": "error",
|
||||
"sort-imports": "error",
|
||||
'no-case-declarations': 'error',
|
||||
'no-empty': 'error',
|
||||
'no-prototype-builtins': 'error',
|
||||
'no-redeclare': 'error',
|
||||
'no-shadow': 'error',
|
||||
'no-undef': 'error',
|
||||
'sort-imports': 'error',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = config;
|
||||
module.exports = config
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
# TODO: Basic Tuono app
|
||||
+3
-2
@@ -5,13 +5,14 @@
|
||||
"main": "index.js",
|
||||
"packageManager": "pnpm@8.12.1",
|
||||
"scripts": {
|
||||
"lint": "turbo lint",
|
||||
"format": "turbo format"
|
||||
"lint": "turbo lint",
|
||||
"format": "turbo format"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.12.7",
|
||||
"@typescript-eslint/eslint-plugin": "^6.20.0",
|
||||
"@typescript-eslint/parser": "^6.20.0",
|
||||
"eslint": "^8.56.0",
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
# Tuono packages
|
||||
|
||||
- `vite-fs-router`: Handles the bundling
|
||||
@@ -1,13 +1,41 @@
|
||||
{
|
||||
"name": "vite-fs-router",
|
||||
"name": "vite-plugin-fs-router",
|
||||
"version": "0.1.0",
|
||||
"description": "",
|
||||
"main": "src/index.js",
|
||||
"main": "src/index.ts",
|
||||
"scripts": {
|
||||
"lint": "eslint --ext .ts,.tsx ./src -c ../../eslintrc.cjs",
|
||||
"format": "prettier --ignore-unknown '**/*'"
|
||||
"dev": "vite dev",
|
||||
"build": "vite build",
|
||||
"lint": "eslint --ext .ts,.tsx ./src -c ../../eslintrc.cjs",
|
||||
"format": "prettier --ignore-unknown '**/*'",
|
||||
"types": "tsc --noEmit"
|
||||
},
|
||||
"exports": {
|
||||
".": {
|
||||
"import": {
|
||||
"types": "./dist/esm/index.d.ts",
|
||||
"default": "./dist/esm/index.js"
|
||||
},
|
||||
"require": {
|
||||
"types": "./dist/cjs/index.d.cts",
|
||||
"default": "./dist/cjs/index.cjs"
|
||||
}
|
||||
},
|
||||
"./package.json": "./package.json"
|
||||
},
|
||||
"files": [
|
||||
"dist",
|
||||
"src/**"
|
||||
],
|
||||
"sideEffects": false,
|
||||
"keywords": [],
|
||||
"type": "module",
|
||||
"author": "Valerio Ageno",
|
||||
"license": "MIT"
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"rollup-plugin-preserve-directives": "^0.4.0",
|
||||
"vite": "^5.2.10",
|
||||
"vite-plugin-dts": "^3.9.0",
|
||||
"vite-plugin-externalize-deps": "^0.8.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
export const makeCompile = () => {
|
||||
console.log('compiling [compile fn]')
|
||||
}
|
||||
|
||||
export const splitFile = async ({}) => {
|
||||
console.log('Split file [splitFile fn]')
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
import { normalize } from 'path'
|
||||
import { fileURLToPath, pathToFileURL } from 'url'
|
||||
import type { Plugin } from 'vite'
|
||||
import { makeCompile, splitFile } from './compiler'
|
||||
|
||||
const SPLIT_PREFIX = 'sp-'
|
||||
const ROUTES_DIRECTORY_PATH = 'src/routes'
|
||||
const DEBUG = true
|
||||
|
||||
let lock = false
|
||||
|
||||
export function RouterGenerator(): Plugin {
|
||||
const _generate = async () => {
|
||||
if (lock) return
|
||||
lock = true
|
||||
|
||||
try {
|
||||
// TODO: generator function
|
||||
console.log('Generating [generate fn]')
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
} finally {
|
||||
lock = false
|
||||
}
|
||||
}
|
||||
|
||||
const handleFile = async (file: string) => {
|
||||
const filePath = normalize(file)
|
||||
|
||||
if (filePath.startsWith(ROUTES_DIRECTORY_PATH)) {
|
||||
// TODO: generator function
|
||||
console.log('Generating [handleFile fn]')
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
name: 'vite-plugin-fs-router-generator',
|
||||
watchChange: async (file: string, context: { event: string }) => {
|
||||
if (['create', 'update', 'delete'].includes(context.event)) {
|
||||
await handleFile(file)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export function RouterCodeSplitter(): Plugin {
|
||||
return {
|
||||
name: 'vite-plugin-fs-router-code-splitter',
|
||||
enforce: 'pre',
|
||||
resolveId(source) {
|
||||
if (source.startsWith(SPLIT_PREFIX + ':')) {
|
||||
return source.replace(SPLIT_PREFIX + ':', '')
|
||||
}
|
||||
return
|
||||
},
|
||||
async transform(code, id) {
|
||||
const url = pathToFileURL(id)
|
||||
url.searchParams.delete('v')
|
||||
id = fileURLToPath(url).replace(/\\/g, '/')
|
||||
|
||||
const compile = makeCompile()
|
||||
if (DEBUG) console.info('Route: ', id)
|
||||
|
||||
if (id.includes(SPLIT_PREFIX)) {
|
||||
if (DEBUG) console.info('Splitting route: ', id)
|
||||
|
||||
const compiled = await splitFile({
|
||||
code,
|
||||
compile,
|
||||
filename: id,
|
||||
})
|
||||
|
||||
if (DEBUG) {
|
||||
console.info('')
|
||||
console.info('Split Output')
|
||||
console.info('')
|
||||
//console.info(compiled.code)
|
||||
console.info('')
|
||||
console.info('')
|
||||
console.info('')
|
||||
console.info('')
|
||||
console.info('')
|
||||
console.info('')
|
||||
console.info('')
|
||||
console.info('')
|
||||
}
|
||||
|
||||
return compiled
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export function ViteFsRouter(): Array<Plugin> {
|
||||
return [RouterGenerator(), RouterCodeSplitter()]
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
import { defineConfig } from 'vite'
|
||||
import { preserveDirectives } from 'rollup-plugin-preserve-directives'
|
||||
import { externalizeDeps } from 'vite-plugin-externalize-deps'
|
||||
import dts from 'vite-plugin-dts'
|
||||
|
||||
const config = ({ entryRoot, include, exclude }) =>
|
||||
defineConfig({
|
||||
plugins: [
|
||||
externalizeDeps(),
|
||||
preserveDirectives(),
|
||||
dts({
|
||||
outDir: 'dist/esm',
|
||||
entryRoot,
|
||||
include,
|
||||
exclude,
|
||||
compilerOptions: {
|
||||
// @ts-expect-error
|
||||
module: 'esnext',
|
||||
declarationMap: false,
|
||||
},
|
||||
beforeWriteFile: (filePath, content) => {
|
||||
content = content.replace(
|
||||
/^(im|ex)port\s[\w{}*\s,]+from\s['"]\.\/[^.'"]+(?=['"];?$)/gm,
|
||||
'$&.js',
|
||||
)
|
||||
|
||||
return { filePath, content }
|
||||
},
|
||||
}),
|
||||
dts({
|
||||
outDir: 'dist/cjs',
|
||||
entryRoot,
|
||||
include,
|
||||
exclude,
|
||||
compilerOptions: {
|
||||
// @ts-expect-error
|
||||
module: 'commonjs',
|
||||
declarationMap: false,
|
||||
},
|
||||
beforeWriteFile: (filePath, content) => {
|
||||
content = content.replace(
|
||||
/^(im|ex)port\s[\w{}*\s,]+from\s['"]\.\/[^.'"]+(?=['"];?$)/gm,
|
||||
'$&.cjs',
|
||||
)
|
||||
|
||||
filePath = filePath.replace('.d.ts', '.d.cts')
|
||||
|
||||
return { filePath, content }
|
||||
},
|
||||
}),
|
||||
],
|
||||
build: {
|
||||
outDir: 'dist',
|
||||
minify: false,
|
||||
sourcemap: true,
|
||||
lib: {
|
||||
entry: entryRoot,
|
||||
formats: ['es', 'cjs'],
|
||||
fileName: (format) => {
|
||||
if (format === 'cjs') return 'cjs/[name].cjs'
|
||||
return 'esm/[name].js'
|
||||
},
|
||||
},
|
||||
rollupOptions: {
|
||||
output: {
|
||||
preserveModules: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
export default config({
|
||||
entryRoot: './src/index.ts',
|
||||
include: './src',
|
||||
})
|
||||
@@ -1,2 +1,3 @@
|
||||
packages:
|
||||
- "packages/*"
|
||||
- "!**/examples/**"
|
||||
|
||||
Reference in New Issue
Block a user