Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(assets): assetsFlatten option #17626

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/config/shared-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,14 @@ export default defineConfig({
})
```

## assetsFlatten

- **Type:** `boolean`
- **Default:** `true`
- **Related:** [Static Asset Handling](/guide/assets)

Whether to flatten assets into a single directory, or to preserve the assets directory structure.

## logLevel

- **Type:** `'info' | 'warn' | 'error' | 'silent'`
Expand Down
8 changes: 8 additions & 0 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ export interface UserConfig {
* Specify additional picomatch patterns to be treated as static assets.
*/
assetsInclude?: string | RegExp | (string | RegExp)[]
/**
* Whether to flatten assets into a single directory, or to preserve the assets directory structure.
* @default true
*/
assetsFlatten?: boolean
/**
* Server specific options, e.g. host, port, https...
*/
Expand Down Expand Up @@ -390,6 +395,7 @@ export type ResolvedConfig = Readonly<
preview: ResolvedPreviewOptions
ssr: ResolvedSSROptions
assetsInclude: (file: string) => boolean
assetsFlatten: boolean
logger: Logger
createResolver: (options?: Partial<InternalResolveOptions>) => ResolveFn
optimizeDeps: DepOptimizationOptions
Expand Down Expand Up @@ -805,6 +811,8 @@ export async function resolveConfig(
assetsInclude(file: string) {
return DEFAULT_ASSETS_RE.test(file) || assetsFilter(file)
},
assetsFlatten:
typeof config.assetsFlatten !== 'undefined' ? config.assetsFlatten : true,
logger,
packageCache,
createResolver,
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,14 +381,15 @@ async function fileToBuiltUrl(
const { search, hash } = parseUrl(id)
const postfix = (search || '') + (hash || '')

const originalName = normalizePath(path.relative(config.root, file))

const referenceId = pluginContext.emitFile({
// Ignore directory structure for asset file names
name: path.basename(file),
name: config.assetsFlatten ? path.basename(file) : originalName,
type: 'asset',
source: content,
})

const originalName = normalizePath(path.relative(config.root, file))
generatedAssets.get(config)!.set(referenceId, { originalName })

url = `__VITE_ASSET__${referenceId}__${postfix ? `$_${postfix}__` : ``}` // TODO_BASE
Expand Down
Loading
Loading