Skip to content

Commit

Permalink
chore: server plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarosabu committed Jun 24, 2023
1 parent ab01e7e commit 140caa2
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 27 deletions.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions plugins/vite-plugin-tres/src/client/.tres-inspect/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + Vue + TS</title>
<script type="module" crossorigin src="/assets/index-3372999a.js"></script>
<link rel="stylesheet" href="/assets/index-6f33960a.css">
</head>
<body>
<div id="app"></div>

</body>
</html>
1 change: 1 addition & 0 deletions plugins/vite-plugin-tres/src/client/.tres-inspect/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions plugins/vite-plugin-tres/src/client/test/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script setup lang="ts">
</script>

<template>
Lo hemos logrado paco
</template>

<style scoped>
.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.vue:hover {
filter: drop-shadow(0 0 2em #42b883aa);
}
</style>
3 changes: 2 additions & 1 deletion plugins/vite-plugin-tres/src/client/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import TresPlugin from '../node'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
plugins: [vue(), TresPlugin()],
})
3 changes: 3 additions & 0 deletions plugins/vite-plugin-tres/src/node/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface Options {

}
61 changes: 35 additions & 26 deletions plugins/vite-plugin-tres/src/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { type Connect, type Plugin, type ResolvedConfig, type ViteDevServer } from 'vite'
import { UserConfig, type Connect, type Plugin, type ResolvedConfig, type ViteDevServer } from 'vite'
import sirv from 'sirv'
import { createServer } from 'node:http'
import type { AddressInfo } from 'node:net'
import { isAbsolute, join, resolve } from 'node:path'
import fs from 'fs-extra'

import { Options } from './config'
import { lightGreen, yellow, gray, bold } from 'kolorist'

import { DIR_CLIENT } from '../dir'

export const plugin = (options: Options = {}) => {
export const plugin = (useSirv = false, options: Options = {}) => {
const outputDir = options.outputDir ?? '.tres-inspect'
/* let config: ResolvedConfig */

Expand All @@ -34,17 +34,21 @@ export const plugin = (options: Options = {}) => {
return targetDir
}

/* function configureServer(server: ViteDevServer) {
const base = (options.base ?? server.config.base) || '/'
server.middlewares.use((req, res, next) => {
// custom handle request...
server.middlewares.use(`${base}__inspect`, sirv(DIR_CLIENT, {
single: true,
dev: true,
}))
})
} */
function configureServer(server: ViteDevServer) {
const base = (options.base ?? server.config.base) || '/'

server.middlewares.use((req, res, next) => {
console.log(req.url)
next()
})

if (useSirv) {
server.middlewares.use(`${base}__tres`, sirv('.', {
single: true,
dev: true,
}))
}
}

function createPreviewServer(staticPath: string) {
const server = createServer()
Expand Down Expand Up @@ -75,19 +79,24 @@ export const plugin = (options: Options = {}) => {
return {
name: 'vite-plugin-tres',
enforce: 'pre',
/* apply(_, { command }) {
if (command === 'serve' && dev)
return true
if (command === 'build' && build)
return true
return false
}, */
apply: 'serve',
config(config) {
const thisConfig: UserConfig = {}
if (typeof config.server?.open === 'undefined') {
thisConfig.server = {
open: true,
}
}
console.log(thisConfig)
return thisConfig
},
configureServer,
/* configureServer, */
async buildStart() {
const dir = await generateBuild()
console.log(` ${lightGreen('➜')} ${bold('Tres Inspect Build Completed')}: ${dir}`)
createPreviewServer(dir)
}
/* async buildStart() {
const dir = await generateBuild()
console.log(` ${lightGreen('➜')} ${bold('Tres Inspect Build Completed')}: ${dir}`)
createPreviewServer(dir)
} */
}

}
Expand Down

0 comments on commit 140caa2

Please sign in to comment.