Skip to content

Commit

Permalink
fix: support aliases in html references
Browse files Browse the repository at this point in the history
fix #1363
  • Loading branch information
yyx990803 committed Jan 5, 2021
1 parent 99522d0 commit 68eac64
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 6 deletions.
4 changes: 4 additions & 0 deletions packages/playground/alias/__tests__/alias.spec.ts
Expand Up @@ -17,3 +17,7 @@ test('regex', async () => {
test('dependency', async () => {
expect(await page.textContent('.dep')).toMatch('[success] out of root')
})

test('from html', async () => {
expect(await page.textContent('.from-html')).toMatch('[success] from html')
})
1 change: 1 addition & 0 deletions packages/playground/alias/dir/from-html.js
@@ -0,0 +1 @@
document.querySelector('.from-html').textContent = '[success] from html'
5 changes: 4 additions & 1 deletion packages/playground/alias/index.html
Expand Up @@ -4,6 +4,7 @@ <h1>Alias</h1>
<p class="fs-dir"></p>
<p class="regex"></p>
<p class="dep"></p>
<p class="from-html"></p>

<script type="module">
import { msg as fsMsg } from 'fs'
Expand All @@ -19,4 +20,6 @@ <h1>Alias</h1>
text('.fs-dir', fsDirMsg)
text('.regex', regexMsg + ' via regex')
text('.dep', depMsg)
</script>
</script>

<script type="module" src="/@/from-html.js"></script>
3 changes: 2 additions & 1 deletion packages/playground/alias/vite.config.js
Expand Up @@ -11,6 +11,7 @@ module.exports = {
{
find: /^regex\/(.*)/,
replacement: `${path.resolve(__dirname, 'dir')}/$1`
}
},
{ find: '/@', replacement: path.resolve(__dirname, 'dir') }
]
}
3 changes: 2 additions & 1 deletion packages/vite/src/node/config.ts
Expand Up @@ -96,7 +96,7 @@ export interface UserConfig {
}

export type ResolvedConfig = Readonly<
Omit<UserConfig, 'plugins' | 'assetsInclude'> & {
Omit<UserConfig, 'plugins' | 'alias' | 'assetsInclude'> & {
configPath: string | undefined
inlineConfig: UserConfig
root: string
Expand All @@ -105,6 +105,7 @@ export type ResolvedConfig = Readonly<
isProduction: boolean
optimizeCacheDir: string | undefined
env: Record<string, any>
alias: Alias[]
plugins: readonly Plugin[]
server: ServerOptions
build: Required<BuildOptions>
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/index.ts
Expand Up @@ -303,7 +303,7 @@ export async function createServer(

// serve static files
app.use(rawFsStaticMiddleware())
app.use(serveStaticMiddleware(root))
app.use(serveStaticMiddleware(root, resolvedConfig))

// spa fallback
app.use(
Expand Down
28 changes: 26 additions & 2 deletions packages/vite/src/node/server/middlewares/static.ts
Expand Up @@ -2,13 +2,17 @@ import os from 'os'
import path from 'path'
import sirv from 'sirv'
import { Connect } from 'types/connect'
import { ResolvedConfig } from '../..'
import { FS_PREFIX } from '../../constants'
import { cleanUrl, isImportRequest } from '../../utils'

const sirvOptions = { dev: true, etag: true }

export function serveStaticMiddleware(dir: string): Connect.NextHandleFunction {
export function serveStaticMiddleware(
dir: string,
config?: ResolvedConfig
): Connect.NextHandleFunction {
const serve = sirv(dir, sirvOptions)

return (req, res, next) => {
const url = req.url!

Expand All @@ -25,6 +29,26 @@ export function serveStaticMiddleware(dir: string): Connect.NextHandleFunction {
return next()
}

// apply aliases to static requests as well
if (config) {
let redirected: string | undefined
for (const { find, replacement } of config.alias) {
const matches =
typeof find === 'string' ? url.startsWith(find) : find.test(url)
if (matches) {
redirected = url.replace(find, replacement)
break
}
}
if (redirected) {
// dir is pre-normalized to posix style
if (redirected.startsWith(dir)) {
redirected = redirected.slice(dir.length)
}
req.url = redirected
}
}

serve(req, res, next)
}
}
Expand Down

0 comments on commit 68eac64

Please sign in to comment.