feat(build): support custom `assetsDir` (#2497)

pull/2626/head
烽宁 1 year ago committed by GitHub
parent 2c25855986
commit 64d7c3ba54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -290,6 +290,19 @@ export default {
} }
``` ```
### assetsDir
- Type: `string`
- Default: `assets`
The directory for assets files. See also: [assetsDir](https://vitejs.dev/config/build-options.html#build-assetsdir).
```ts
export default {
assetsDir: 'static'
}
```
### cacheDir ### cacheDir
- Type: `string` - Type: `string`

@ -58,7 +58,10 @@ export function pathToFile(path: string) {
pageHash = __VP_HASH_MAP__[pagePath.toLowerCase()] pageHash = __VP_HASH_MAP__[pagePath.toLowerCase()]
} }
if (!pageHash) return null if (!pageHash) return null
pagePath = `${base}assets/${pagePath}.${pageHash}.js` pagePath = `${base}${__ASSETS_DIR__.replace(
/"(.+)"/,
'$1'
)}/${pagePath}.${pageHash}.js`
} else { } else {
// ssr build uses much simpler name mapping // ssr build uses much simpler name mapping
pagePath = `./${sanitizeFileName( pagePath = `./${sanitizeFileName(

@ -3,6 +3,7 @@ declare const __VP_LOCAL_SEARCH__: boolean
declare const __ALGOLIA__: boolean declare const __ALGOLIA__: boolean
declare const __CARBON__: boolean declare const __CARBON__: boolean
declare const __VUE_PROD_DEVTOOLS__: boolean declare const __VUE_PROD_DEVTOOLS__: boolean
declare const __ASSETS_DIR__: string
declare module '*.vue' { declare module '*.vue' {
import type { DefineComponent } from 'vue' import type { DefineComponent } from 'vue'

@ -94,19 +94,19 @@ export async function bundle(
output: { output: {
sanitizeFileName, sanitizeFileName,
...rollupOptions?.output, ...rollupOptions?.output,
assetFileNames: 'assets/[name].[hash].[ext]', assetFileNames: `${config.assetsDir}/[name].[hash].[ext]`,
...(ssr ...(ssr
? { ? {
entryFileNames: '[name].js', entryFileNames: '[name].js',
chunkFileNames: '[name].[hash].js' chunkFileNames: '[name].[hash].js'
} }
: { : {
entryFileNames: 'assets/[name].[hash].js', entryFileNames: `${config.assetsDir}/[name].[hash].js`,
chunkFileNames(chunk) { chunkFileNames(chunk) {
// avoid ads chunk being intercepted by adblock // avoid ads chunk being intercepted by adblock
return /(?:Carbon|BuySell)Ads/.test(chunk.name) return /(?:Carbon|BuySell)Ads/.test(chunk.name)
? 'assets/chunks/ui-custom.[hash].js' ? `${config.assetsDir}/chunks/ui-custom.[hash].js`
: 'assets/chunks/[name].[hash].js' : `${config.assetsDir}/chunks/[name].[hash].js`
}, },
manualChunks(id, ctx) { manualChunks(id, ctx) {
if (lazyDefaultThemeComponentsRE.test(id)) { if (lazyDefaultThemeComponentsRE.test(id)) {

@ -46,7 +46,7 @@ export async function renderPage(
// for any initial page load, we only need the lean version of the page js // for any initial page load, we only need the lean version of the page js
// since the static content is already on the page! // since the static content is already on the page!
const pageHash = pageToHashMap[pageName.toLowerCase()] const pageHash = pageToHashMap[pageName.toLowerCase()]
const pageClientJsFileName = `assets/${pageName}.${pageHash}.lean.js` const pageClientJsFileName = `${config.assetsDir}/${pageName}.${pageHash}.lean.js`
let pageData: PageData let pageData: PageData
let hasCustom404 = true let hasCustom404 = true

@ -73,6 +73,9 @@ export async function resolveConfig(
}) })
const site = await resolveSiteData(root, userConfig) const site = await resolveSiteData(root, userConfig)
const srcDir = normalizePath(path.resolve(root, userConfig.srcDir || '.')) const srcDir = normalizePath(path.resolve(root, userConfig.srcDir || '.'))
const assetsDir = userConfig.assetsDir
? userConfig.assetsDir.replace(/\//g, '')
: 'assets'
const outDir = userConfig.outDir const outDir = userConfig.outDir
? normalizePath(path.resolve(root, userConfig.outDir)) ? normalizePath(path.resolve(root, userConfig.outDir))
: resolve(root, 'dist') : resolve(root, 'dist')
@ -94,6 +97,7 @@ export async function resolveConfig(
const config: SiteConfig = { const config: SiteConfig = {
root, root,
srcDir, srcDir,
assetsDir,
site, site,
themeDir, themeDir,
pages, pages,

@ -129,7 +129,8 @@ export async function createVitePressPlugin(
__ALGOLIA__: __ALGOLIA__:
site.themeConfig?.search?.provider === 'algolia' || site.themeConfig?.search?.provider === 'algolia' ||
!!site.themeConfig?.algolia, // legacy !!site.themeConfig?.algolia, // legacy
__CARBON__: !!site.themeConfig?.carbonAds __CARBON__: !!site.themeConfig?.carbonAds,
__ASSETS_DIR__: JSON.stringify(siteConfig.assetsDir)
}, },
optimizeDeps: { optimizeDeps: {
// force include vue to avoid duplicated copies when linked + optimized // force include vue to avoid duplicated copies when linked + optimized

@ -28,7 +28,8 @@ export async function serve(options: ServeOptions = {}) {
const config = await resolveConfig(options.root, 'serve', 'production') const config = await resolveConfig(options.root, 'serve', 'production')
const base = trimChar(options?.base ?? config?.site?.base ?? '', '/') const base = trimChar(options?.base ?? config?.site?.base ?? '', '/')
const notAnAsset = (pathname: string) => !pathname.includes('/assets/') const notAnAsset = (pathname: string) =>
!pathname.includes(`/${config.assetsDir}/`)
const notFound = fs.readFileSync(path.resolve(config.outDir, './404.html')) const notFound = fs.readFileSync(path.resolve(config.outDir, './404.html'))
const onNoMatch: IOptions['onNoMatch'] = (req, res) => { const onNoMatch: IOptions['onNoMatch'] = (req, res) => {
res.statusCode = 404 res.statusCode = 404

@ -59,6 +59,7 @@ export interface UserConfig<ThemeConfig = any>
srcDir?: string srcDir?: string
srcExclude?: string[] srcExclude?: string[]
outDir?: string outDir?: string
assetsDir?: string
cacheDir?: string cacheDir?: string
shouldPreload?: (link: string, page: string) => boolean shouldPreload?: (link: string, page: string) => boolean
@ -192,6 +193,7 @@ export interface SiteConfig<ThemeConfig = any>
configDeps: string[] configDeps: string[]
themeDir: string themeDir: string
outDir: string outDir: string
assetsDir: string
cacheDir: string cacheDir: string
tempDir: string tempDir: string
pages: string[] pages: string[]

Loading…
Cancel
Save