diff --git a/docs/config/index.md b/docs/config/index.md index e333c277635e9a..ab0b831e05bd7c 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -524,6 +524,20 @@ createServer() } ``` +### server.publicPath + +- **Type:** `string` + + This option allows the user to customize the URL prefix. Example: + + ```js + export default { + server: { + publicPath: 'http://127.0.0.1:8080/' + } + } + ``` + ## Build Options ### build.target diff --git a/packages/vite/src/node/plugins/asset.ts b/packages/vite/src/node/plugins/asset.ts index a44622c256ad44..50ecdaa98786f1 100644 --- a/packages/vite/src/node/plugins/asset.ts +++ b/packages/vite/src/node/plugins/asset.ts @@ -175,7 +175,8 @@ function fileToDevUrl(id: string, config: ResolvedConfig) { // (this is special handled by the serve static middleware rtn = path.posix.join(FS_PREFIX + id) } - return config.base + rtn.replace(/^\//, '') + const publicPath = config.server?.publicPath || '' + return publicPath + config.base + rtn.replace(/^\//, '') } export function getAssetFilename( diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 72bad509077675..614a8d1bfc778f 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -127,6 +127,7 @@ export interface ServerOptions { * Options for files served via '/\@fs/'. */ fs?: FileSystemServeOptions + publicPath?: string } export interface ResolvedServerOptions extends ServerOptions {