Skip to content

Commit 64d7c3b

Browse files
authored
feat(build): support custom assetsDir (#2497)
1 parent 2c25855 commit 64d7c3b

File tree

9 files changed

+33
-8
lines changed

9 files changed

+33
-8
lines changed

Diff for: docs/reference/site-config.md

+13
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,19 @@ export default {
290290
}
291291
```
292292

293+
### assetsDir
294+
295+
- Type: `string`
296+
- Default: `assets`
297+
298+
The directory for assets files. See also: [assetsDir](https://vitejs.dev/config/build-options.html#build-assetsdir).
299+
300+
```ts
301+
export default {
302+
assetsDir: 'static'
303+
}
304+
```
305+
293306
### cacheDir
294307

295308
- Type: `string`

Diff for: src/client/app/utils.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ export function pathToFile(path: string) {
5858
pageHash = __VP_HASH_MAP__[pagePath.toLowerCase()]
5959
}
6060
if (!pageHash) return null
61-
pagePath = `${base}assets/${pagePath}.${pageHash}.js`
61+
pagePath = `${base}${__ASSETS_DIR__.replace(
62+
/"(.+)"/,
63+
'$1'
64+
)}/${pagePath}.${pageHash}.js`
6265
} else {
6366
// ssr build uses much simpler name mapping
6467
pagePath = `./${sanitizeFileName(

Diff for: src/client/shim.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ declare const __VP_LOCAL_SEARCH__: boolean
33
declare const __ALGOLIA__: boolean
44
declare const __CARBON__: boolean
55
declare const __VUE_PROD_DEVTOOLS__: boolean
6+
declare const __ASSETS_DIR__: string
67

78
declare module '*.vue' {
89
import type { DefineComponent } from 'vue'

Diff for: src/node/build/bundle.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,19 @@ export async function bundle(
9494
output: {
9595
sanitizeFileName,
9696
...rollupOptions?.output,
97-
assetFileNames: 'assets/[name].[hash].[ext]',
97+
assetFileNames: `${config.assetsDir}/[name].[hash].[ext]`,
9898
...(ssr
9999
? {
100100
entryFileNames: '[name].js',
101101
chunkFileNames: '[name].[hash].js'
102102
}
103103
: {
104-
entryFileNames: 'assets/[name].[hash].js',
104+
entryFileNames: `${config.assetsDir}/[name].[hash].js`,
105105
chunkFileNames(chunk) {
106106
// avoid ads chunk being intercepted by adblock
107107
return /(?:Carbon|BuySell)Ads/.test(chunk.name)
108-
? 'assets/chunks/ui-custom.[hash].js'
109-
: 'assets/chunks/[name].[hash].js'
108+
? `${config.assetsDir}/chunks/ui-custom.[hash].js`
109+
: `${config.assetsDir}/chunks/[name].[hash].js`
110110
},
111111
manualChunks(id, ctx) {
112112
if (lazyDefaultThemeComponentsRE.test(id)) {

Diff for: src/node/build/render.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export async function renderPage(
4646
// for any initial page load, we only need the lean version of the page js
4747
// since the static content is already on the page!
4848
const pageHash = pageToHashMap[pageName.toLowerCase()]
49-
const pageClientJsFileName = `assets/${pageName}.${pageHash}.lean.js`
49+
const pageClientJsFileName = `${config.assetsDir}/${pageName}.${pageHash}.lean.js`
5050

5151
let pageData: PageData
5252
let hasCustom404 = true

Diff for: src/node/config.ts

+4
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ export async function resolveConfig(
7373
})
7474
const site = await resolveSiteData(root, userConfig)
7575
const srcDir = normalizePath(path.resolve(root, userConfig.srcDir || '.'))
76+
const assetsDir = userConfig.assetsDir
77+
? userConfig.assetsDir.replace(/\//g, '')
78+
: 'assets'
7679
const outDir = userConfig.outDir
7780
? normalizePath(path.resolve(root, userConfig.outDir))
7881
: resolve(root, 'dist')
@@ -94,6 +97,7 @@ export async function resolveConfig(
9497
const config: SiteConfig = {
9598
root,
9699
srcDir,
100+
assetsDir,
97101
site,
98102
themeDir,
99103
pages,

Diff for: src/node/plugin.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ export async function createVitePressPlugin(
129129
__ALGOLIA__:
130130
site.themeConfig?.search?.provider === 'algolia' ||
131131
!!site.themeConfig?.algolia, // legacy
132-
__CARBON__: !!site.themeConfig?.carbonAds
132+
__CARBON__: !!site.themeConfig?.carbonAds,
133+
__ASSETS_DIR__: JSON.stringify(siteConfig.assetsDir)
133134
},
134135
optimizeDeps: {
135136
// force include vue to avoid duplicated copies when linked + optimized

Diff for: src/node/serve/serve.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ export async function serve(options: ServeOptions = {}) {
2828
const config = await resolveConfig(options.root, 'serve', 'production')
2929
const base = trimChar(options?.base ?? config?.site?.base ?? '', '/')
3030

31-
const notAnAsset = (pathname: string) => !pathname.includes('/assets/')
31+
const notAnAsset = (pathname: string) =>
32+
!pathname.includes(`/${config.assetsDir}/`)
3233
const notFound = fs.readFileSync(path.resolve(config.outDir, './404.html'))
3334
const onNoMatch: IOptions['onNoMatch'] = (req, res) => {
3435
res.statusCode = 404

Diff for: src/node/siteConfig.ts

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export interface UserConfig<ThemeConfig = any>
5959
srcDir?: string
6060
srcExclude?: string[]
6161
outDir?: string
62+
assetsDir?: string
6263
cacheDir?: string
6364

6465
shouldPreload?: (link: string, page: string) => boolean
@@ -192,6 +193,7 @@ export interface SiteConfig<ThemeConfig = any>
192193
configDeps: string[]
193194
themeDir: string
194195
outDir: string
196+
assetsDir: string
195197
cacheDir: string
196198
tempDir: string
197199
pages: string[]

0 commit comments

Comments
 (0)