tiptap/demos/vite.config.ts

173 lines
4.3 KiB
TypeScript
Raw Normal View History

2021-08-25 17:52:20 +08:00
import {
resolve,
basename,
dirname,
join,
} from 'path'
import { v4 as uuid } from 'uuid'
import fs from 'fs'
2021-09-30 17:43:51 +08:00
import fg from 'fast-glob'
2021-08-25 17:52:20 +08:00
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import reactRefresh from '@vitejs/plugin-react-refresh'
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(),
reactRefresh(),
{
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!', '')
2021-09-30 17:43:51 +08:00
const files = fg.sync(`${path}/**/*`, {
2021-09-24 04:25:54 +08:00
ignore: [
'**/index.html',
'**/*.spec.js',
'**/*.spec.ts',
],
})
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`) }
}),
],
},
})