Skip to content
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
26 changes: 21 additions & 5 deletions src/node/server/serverPluginAssets.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
import { ServerPlugin } from '.'
import { isImportRequest, isStaticAsset } from '../utils'

export const assetPathPlugin: ServerPlugin = ({ app }) => {
const usedAssetsSet = new Set<string>()

export const assetPathPlugin: ServerPlugin = ({ app, resolver, watcher }) => {
app.use(async (ctx, next) => {
if (isStaticAsset(ctx.path) && isImportRequest(ctx)) {
ctx.type = 'js'
ctx.body = `export default ${JSON.stringify(ctx.path)}`
return
if (isStaticAsset(ctx.path)) {
if (isImportRequest(ctx)) {
ctx.type = 'js'
ctx.body = `export default ${JSON.stringify(ctx.path)}`
return
}
usedAssetsSet.add(ctx.path)
}
return next()
})

watcher.on('change', (filePath) => {
if (isStaticAsset(filePath)) {
const publicPath = resolver.fileToRequest(filePath)

// skip unused
if (!usedAssetsSet.has(publicPath)) return

watcher.handleJSReload(filePath)
}
})
}
2 changes: 2 additions & 0 deletions src/node/server/serverPluginHmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { InternalResolver } from '../resolver'
import LRUCache from 'lru-cache'
import slash from 'slash'
import { cssPreprocessLangRE } from '../utils/cssUtils'
import { isStaticAsset } from '../utils'

export const debugHmr = require('debug')('vite:hmr')

Expand Down Expand Up @@ -183,6 +184,7 @@ export const hmrPlugin: ServerPlugin = ({
watcher.on('change', (file) => {
if (
!(
isStaticAsset(file) ||
file.endsWith('.vue') ||
file.endsWith('.css') ||
cssPreprocessLangRE.test(file)
Expand Down