Skip to content

Commit 24e048c

Browse files
refactor(vitrify): refactor and cleanup
1 parent 35774d4 commit 24e048c

File tree

3 files changed

+63
-113
lines changed

3 files changed

+63
-113
lines changed

.changeset/tender-hotels-arrive.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'vitrify': minor
3+
---
4+
5+
refactor(vitrify): refactor and cleanup

packages/vitrify/src/node/index.ts

Lines changed: 57 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import vuePlugin from '@vitejs/plugin-vue'
2-
import type { Alias, InlineConfig, UserConfig as ViteUserConfig } from 'vite'
2+
import type {
3+
Alias,
4+
InlineConfig,
5+
Plugin,
6+
UserConfig as ViteUserConfig
7+
} from 'vite'
38
import { findDepPkgJsonPath } from 'vitefu'
49
import { mergeConfig } from 'vite'
510
import { build } from 'esbuild'
@@ -29,7 +34,6 @@ import type { VitrifyPlugin } from './plugins/index.js'
2934
import { resolve } from './app-urls.js'
3035
import type { ManualChunksOption, RollupOptions } from 'rollup'
3136
import { addOrReplaceTitle, appendToBody } from './helpers/utils.js'
32-
import type { ComponentResolver } from 'unplugin-vue-components'
3337
import Components from 'unplugin-vue-components/vite'
3438
import { VitePWA } from 'vite-plugin-pwa'
3539
import UnoCSS from 'unocss/vite'
@@ -50,21 +54,6 @@ const internalServerModules = [
5054
'abort-controller'
5155
]
5256

53-
const configPluginMap: Record<string, () => Promise<VitrifyPlugin>> = {
54-
quasar: () =>
55-
import('./plugins/quasar.js').then((module) => module.QuasarPlugin)
56-
}
57-
58-
const configResolverMap: Record<
59-
string,
60-
() => Promise<ComponentResolver | ComponentResolver[]>
61-
> = {
62-
quasar: () =>
63-
import('unplugin-vue-components/resolvers').then((module) =>
64-
module.QuasarResolver()
65-
)
66-
}
67-
6857
const manualChunkNames = [
6958
'prerender',
7059
'fastify-ssr-plugin',
@@ -103,31 +92,6 @@ const manualChunksFn = (manualChunkList?: string[]): ManualChunksOption => {
10392
}
10493
}
10594

106-
// const manualChunks: ManualChunksOption = (
107-
// id: string,
108-
// manualChunkList?: string[]
109-
// ) => {
110-
// const matchedModule = Object.entries(moduleChunks).find(
111-
// ([chunkName, moduleNames]) =>
112-
// moduleNames.some((moduleName) => id.includes(moduleName + '/'))
113-
// )
114-
// if (id.includes('vitrify/src/')) {
115-
// const name = id.split('/').at(-1)?.split('.').at(0)
116-
// if (name && manualChunkNames.includes(name)) return name
117-
// } else if (
118-
// VIRTUAL_MODULES.some((virtualModule) => id.includes(virtualModule))
119-
// ) {
120-
// return VIRTUAL_MODULES.find((name) => id.includes(name))
121-
// } else if (manualChunkList?.some((file) => id.includes(file))) {
122-
// return manualChunkList.find((file) => id.includes(file))
123-
// } else if (id.includes('node_modules')) {
124-
// if (matchedModule) {
125-
// return matchedModule[0]
126-
// }
127-
// return 'vendor'
128-
// }
129-
// }
130-
13195
export const VIRTUAL_MODULES = [
13296
'virtual:vitrify-hooks',
13397
'virtual:static-imports',
@@ -241,7 +205,6 @@ export const baseConfig = async ({
241205
fs.writeFileSync(configPath + '.js', bundledConfig.code)
242206

243207
rawVitrifyConfig = (await import('file://' + configPath + '.js')).default
244-
// vitrifyConfig = (await import(configPath + '.js')).default
245208
fs.unlinkSync(configPath + '.js')
246209
} else {
247210
rawVitrifyConfig = (
@@ -258,9 +221,11 @@ export const baseConfig = async ({
258221
throw e
259222
}
260223

261-
const localPackages = ['vue', 'vue-router', '@vue/server-renderer']
262-
// const localPackages: string[] = []
263-
const cliPackages = []
224+
const localPackages = []
225+
if (framework === 'vue')
226+
localPackages.push('vue', 'vue-router', '@vue/server-renderer')
227+
228+
const cliPackages: string[] = []
264229
const packageUrls: Record<string, URL> =
265230
vitrifyConfig.vitrify?.urls?.packages || {}
266231
await (async () => {
@@ -269,14 +234,6 @@ export const baseConfig = async ({
269234
if (pkgDir) packageUrls![val] = new URL(`file://${pkgDir}`)
270235
}
271236
})()
272-
273-
// await (async () => {
274-
// for (const val of cliPackages)
275-
// packageUrls[val] = getPkgJsonDir(
276-
// new URL(await resolve(val, cliDir!.href))
277-
// )
278-
// })()
279-
280237
if (!productName) {
281238
try {
282239
;({ productName } = JSON.parse(
@@ -294,6 +251,11 @@ export const baseConfig = async ({
294251

295252
const isPwa = !!vitrifyConfig.vitrify?.pwa || false
296253

254+
const frameworkPlugins: Plugin[] = []
255+
if (framework === 'vue') {
256+
frameworkPlugins.push(vuePlugin())
257+
}
258+
297259
const vitrifyPlugins: Plugin[] = []
298260
if (vitrifyConfig.vitrify?.plugins) {
299261
for (const vitrifyPluginConfig of vitrifyConfig.vitrify.plugins) {
@@ -381,13 +343,12 @@ export const baseConfig = async ({
381343
/<style lang="sass">(.*?)<\/style>/,
382344
'<style lang="sass">' + sass + '</style>'
383345
)
384-
// code = code.replace(/<\/style>/, sass + '</style>')
385346
}
386347

387348
return code
388349
}
389350
},
390-
vuePlugin(),
351+
...frameworkPlugins,
391352
...vitrifyPlugins,
392353
{
393354
name: 'vitrify-setup',
@@ -448,10 +409,6 @@ export const baseConfig = async ({
448409
return `import ${varName} from '${
449410
new URL(url, appDir).pathname
450411
}'; onSetup.push(${varName})`
451-
452-
// return `import ${varName} from '${fileURLToPath(
453-
// url
454-
// )}'; onSetup.push(${varName})`
455412
})
456413
.join('\n')}`
457414
} else if (id === 'virtual:static-imports') {
@@ -468,11 +425,7 @@ export const baseConfig = async ({
468425
),
469426
...globalSass.map((sass) => `@import '${sass}'`)
470427
].join('\n')
471-
}
472-
// else if (id === 'vitrify.css') {
473-
// return `${globalCss.map((css) => `@import '${css}'`).join('\n')}`
474-
// }
475-
else if (id === 'virtual:vitrify-config') {
428+
} else if (id === 'virtual:vitrify-config') {
476429
return `export default ${JSON.stringify(vitrifyConfig)}`
477430
}
478431
return null
@@ -502,14 +455,14 @@ export const baseConfig = async ({
502455
}
503456
},
504457
Components({
458+
...vitrifyConfig.vitrify?.unpluginVueComponents,
505459
exclude: [
506460
new RegExp(
507461
`[\\/]node_modules[\\/].*[\\/]!(${serverModules.join('|')})`
508462
),
509463
/[\\/]\.git[\\/]/,
510464
/[\\/]\.nuxt[\\/]/
511-
],
512-
resolvers
465+
]
513466
}),
514467
UnoCSS({
515468
...vitrifyConfig.vitrify?.unocss,
@@ -562,32 +515,13 @@ export const baseConfig = async ({
562515
} else {
563516
entryScript = `<script type="module" src="${entry}"></script>`
564517
}
565-
// html = html.replace('<!--entry-script-->', entryScript)
566518
html = appendToBody(entryScript, html)
567519
if (productName) html = addOrReplaceTitle(productName, html)
568-
// html = html.replace('<!--product-name-->', productName)
569520
return html
570521
}
571522
}
572523
})
573524

574-
// plugins.unshift({
575-
// name: 'product-name',
576-
// enforce: 'post',
577-
// config: (config: VitrifyConfig, env) => {
578-
// if (config.vitrify?.productName)
579-
// productName = config.vitrify?.productName
580-
// return
581-
// },
582-
// transformIndexHtml: {
583-
// enforce: 'post',
584-
// transform: (html) => {
585-
// html = html.replace('<!--product-name-->', productName)
586-
// return html
587-
// }
588-
// }
589-
// })
590-
591525
if (debug) plugins.push(visualizer())
592526
}
593527

@@ -602,41 +536,52 @@ export const baseConfig = async ({
602536
]
603537

604538
const vuePkgAliases: Alias[] = []
605-
for (const pkg of vueInternalPkgs) {
606-
const specifier = pkg.split('/').at(-1)
607-
const pkgJsonPath = await findDepPkgJsonPath(pkg, fileURLToPath(appDir!))
608-
if (pkgJsonPath)
609-
vuePkgAliases.push({
610-
find: pkg,
611-
replacement: fileURLToPath(
612-
new URL(
613-
`./dist/${specifier}.esm-bundler.js`,
614-
`file://${pkgJsonPath}` || ''
539+
if (packageUrls['vue']) {
540+
for (const pkg of vueInternalPkgs) {
541+
const specifier = pkg.split('/').at(-1)
542+
const pkgJsonPath = await findDepPkgJsonPath(pkg, fileURLToPath(appDir!))
543+
if (pkgJsonPath)
544+
vuePkgAliases.push({
545+
find: pkg,
546+
replacement: fileURLToPath(
547+
new URL(
548+
`./dist/${specifier}.esm-bundler.js`,
549+
`file://${pkgJsonPath}` || ''
550+
)
615551
)
616-
)
617-
})
552+
})
553+
554+
vuePkgAliases.push(
555+
{
556+
find: new RegExp('^vue$'),
557+
replacement: fileURLToPath(
558+
new URL('./dist/vue.runtime.esm-bundler.js', packageUrls['vue'])
559+
)
560+
},
561+
{
562+
find: new RegExp('^vue-router$'),
563+
replacement: fileURLToPath(
564+
new URL(
565+
'./dist/vue-router.esm-bundler.js',
566+
packageUrls['vue-router']
567+
)
568+
)
569+
},
570+
...vuePkgAliases
571+
)
572+
}
618573
}
619574

620575
const alias: Alias[] = [
621576
{ find: 'src', replacement: fileURLToPath(srcDir) },
622577
{ find: 'app', replacement: fileURLToPath(appDir) },
623578
{ find: 'cwd', replacement: fileURLToPath(cwd) },
624579
{ find: 'boot', replacement: fileURLToPath(new URL('boot/', srcDir)) },
625-
{ find: 'assets', replacement: fileURLToPath(new URL('assets/', srcDir)) },
626-
{
627-
find: new RegExp('^vue$'),
628-
replacement: fileURLToPath(
629-
new URL('./dist/vue.runtime.esm-bundler.js', packageUrls['vue'])
630-
)
631-
},
632-
{
633-
find: new RegExp('^vue-router$'),
634-
replacement: fileURLToPath(
635-
new URL('./dist/vue-router.esm-bundler.js', packageUrls['vue-router'])
636-
)
637-
},
638-
...vuePkgAliases
580+
{ find: 'assets', replacement: fileURLToPath(new URL('assets/', srcDir)) }
639581
]
582+
583+
if (framework === 'vue') alias.push(...vuePkgAliases)
584+
640585
if (mode === 'development' && vitrifyConfig.vitrify?.dev?.alias)
641586
alias.push(...vitrifyConfig.vitrify.dev.alias)
642587

packages/vitrify/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"declarationDir": "dist/types",
1717
"paths": {
1818
"vitrify": ["./src/node/index.ts"],
19-
"vitrify/*": ["./src/node/bin/*"]
19+
"vitrify/dev": ["./src/node/bin/dev.js"]
2020
}
2121
// "typeRoots": ["./"]
2222
},

0 commit comments

Comments
 (0)