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
1 change: 1 addition & 0 deletions src/node/server/serverPluginHmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export const hmrPlugin: ServerPlugin = ({
watcher.on('change', (file) => {
if (
!(
file.endsWith('.json') ||
file.endsWith('.vue') ||
file.endsWith('.css') ||
cssPreprocessLangRE.test(file)
Expand Down
24 changes: 20 additions & 4 deletions src/node/server/serverPluginJson.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import { ServerPlugin } from '.'
import { readBody, isImportRequest } from '../utils'

export const jsonPlugin: ServerPlugin = ({ app }) => {
const usedJsonSet = new Set<string>()

export const jsonPlugin: ServerPlugin = ({ app, resolver, watcher }) => {
app.use(async (ctx, next) => {
await next()
// handle .json imports
// note ctx.body could be null if upstream set status to 304
if (ctx.path.endsWith('.json') && isImportRequest(ctx) && ctx.body) {
ctx.type = 'js'
ctx.body = `export default ${await readBody(ctx.body)}`
if (ctx.path.endsWith('.json')) {
if (isImportRequest(ctx) && ctx.body) {
ctx.type = 'js'
ctx.body = `export default ${await readBody(ctx.body)}`
}
usedJsonSet.add(ctx.path)
}
})

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

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

watcher.handleJSReload(filePath)
}
})
}