tiptap/demos/vite.config.ts

283 lines
7.7 KiB
TypeScript
Raw Normal View History

import { svelte } from '@sveltejs/vite-plugin-svelte'
import react from '@vitejs/plugin-react'
import vue from '@vitejs/plugin-vue'
import fg from 'fast-glob'
import fs from 'fs'
2021-08-25 17:52:20 +08:00
import {
basename,
dirname,
join,
resolve,
2021-08-25 17:52:20 +08:00
} from 'path'
import { v4 as uuid } from 'uuid'
import { defineConfig } from 'vite'
2021-09-18 05:44:01 +08:00
// import checker from 'vite-plugin-checker'
2021-08-25 17:52:20 +08:00
const includeDependencies = fs.readFileSync('./includeDependencies.txt')
.toString()
.replace(/\r\n/g, '\n')
.split('\n')
.filter(value => value)
2021-08-25 17:52:20 +08:00
export default defineConfig({
2021-08-26 22:23:19 +08:00
optimizeDeps: {
include: includeDependencies,
2021-08-26 22:23:19 +08:00
},
2021-08-25 17:52:20 +08:00
build: {
rollupOptions: {
2021-09-30 17:43:51 +08:00
input: fg.sync('./**/index.html', {
2021-08-25 17:52:20 +08:00
ignore: ['dist'],
}),
},
},
plugins: [
2021-09-18 05:44:01 +08:00
// checker({ typescript: { tsconfigPath: './tsconfig.base.json' } }),
// checker({ typescript: { tsconfigPath: './tsconfig.react.json' } }),
// checker({ typescript: { tsconfigPath: './tsconfig.vue-2.json' } }),
// checker({ typescript: { tsconfigPath: './tsconfig.vue-3.json' } }),
2021-08-25 17:52:20 +08:00
vue(),
react(),
svelte(),
2021-08-25 17:52:20 +08:00
{
name: 'html-transform',
transformIndexHtml: {
enforce: 'pre',
transform(html: string, context) {
const dir = dirname(context.path)
const data = dir.split('/')
const demoCategory = data[2]
const demoName = data[3]
if (dir.endsWith('/JS')) {
return {
html: `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
${html}
<script type="module">
import setup from '../../../../setup/js.ts'
import source from '@source'
setup('${demoCategory}/${demoName}', source)
</script>
</body>
</html>
`,
tags: [],
}
}
if (dir.endsWith('/Vue')) {
return {
html: `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
<div id="app"></div>
<script type="module">
import setup from '../../../../setup/vue.ts'
import source from '@source'
setup('${demoCategory}/${demoName}', source)
</script>
</body>
</html>
`,
tags: [],
}
}
if (dir.endsWith('/Svelte')) {
return {
html: `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
<div id="app"></div>
<script type="module">
import setup from '../../../../setup/svelte.ts'
import source from '@source'
setup('${demoCategory}/${demoName}', source)
</script>
</body>
</html>
`,
tags: [],
}
}
if (dir.endsWith('/React')) {
return {
html: `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
<div id="app"></div>
<script type="module">
import setup from '../../../../setup/react.ts'
import source from '@source'
setup('${demoCategory}/${demoName}', source)
</script>
</body>
</html>
`,
tags: [],
}
}
},
},
},
2021-08-25 17:52:20 +08:00
{
name: 'raw',
resolveId(id, importer) {
2021-08-27 05:40:31 +08:00
if (id.startsWith('raw!') && importer) {
2021-08-25 17:52:20 +08:00
const [, relativePath] = id.split('raw!')
const fullPath = join(dirname(importer), relativePath)
return `virtual!${fullPath}!!${uuid()}`
}
},
load(id) {
if (id.startsWith('virtual!')) {
const path = id.split('!!')[0].replace('virtual!', '')
const data = fs.readFileSync(path, 'utf8')
return `export default ${JSON.stringify(data)}`
}
},
},
{
name: 'demos',
resolveId(id) {
if (id === '@demos') {
return '@demos'
}
},
load(id) {
if (id === '@demos') {
2021-09-30 17:43:51 +08:00
const demos = fg.sync('./src/*/*', { onlyDirectories: true })
2021-08-25 17:52:20 +08:00
.map(demoPath => {
const name = demoPath.replace('./src/', '')
2021-09-30 17:43:51 +08:00
const tabs = fg.sync(`./src/${name}/*`, { onlyDirectories: true })
2021-08-25 17:52:20 +08:00
.map(tabPath => ({
name: basename(tabPath),
}))
return {
name,
tabs,
}
})
return `export const demos = ${JSON.stringify(demos)}`
}
},
},
{
name: 'source',
resolveId(id, importer) {
2021-08-27 05:40:31 +08:00
if (id === '@source' && importer) {
2021-08-25 17:52:20 +08:00
return `source!${dirname(importer)}!!${uuid()}`
}
},
load(id) {
if (id.startsWith('source!')) {
const path = id.split('!!')[0].replace('source!', '')
const ignore = [
'**/*.spec.js',
'**/*.spec.ts',
]
if (!path.endsWith('/JS')) {
ignore.push('**/index.html')
}
const files = fg.sync(`${path}/**/*`, { ignore })
2021-08-25 17:52:20 +08:00
.map(filePath => {
const name = filePath.replace(`${path}/`, '')
return {
name,
content: fs.readFileSync(`${path}/${name}`, 'utf8'),
}
})
2021-08-27 05:28:52 +08:00
.sort((a, b) => {
const depthA = a.name.split('/').length
const depthB = b.name.split('/').length
2021-08-25 17:52:20 +08:00
2021-08-27 05:28:52 +08:00
if (depthA > depthB) {
return 1
}
2021-08-27 05:03:31 +08:00
2021-08-27 05:28:52 +08:00
if (depthA < depthB) {
return -1
}
const aIsIndex = basename(a.name).includes('index.')
const bIsIndex = basename(b.name).includes('index.')
if (aIsIndex) {
return -1
}
if (bIsIndex) {
return 1
}
return 0
})
2021-08-27 05:03:31 +08:00
2021-08-27 05:28:52 +08:00
return `export default ${JSON.stringify(files)}`
2021-08-25 17:52:20 +08:00
}
},
},
{
name: 'middleware',
apply: 'serve',
configureServer(viteDevServer) {
return () => {
viteDevServer.middlewares.use(async (req, res, next) => {
2021-08-27 05:40:31 +08:00
if (req?.originalUrl?.startsWith('/preview')) {
2021-08-25 17:52:20 +08:00
req.url = '/preview/index.html'
}
next()
})
}
},
},
],
resolve: {
alias: [
2021-09-30 17:43:51 +08:00
...fg.sync('../packages/*', { onlyDirectories: true })
2021-08-25 17:52:20 +08:00
.map(name => name.replace('../packages/', ''))
.map(name => {
return { find: `@tiptap/${name}`, replacement: resolve(`../packages/${name}/src/index.ts`) }
}),
],
},
})