Create an app only when needed
This commit is contained in:
parent
1bdf67c08d
commit
3a4cdbef15
12 changed files with 449 additions and 34 deletions
28
build/msw_plugin.js
Normal file
28
build/msw_plugin.js
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import { resolve } from 'node:path'
|
||||
import { readFile } from 'node:fs/promises'
|
||||
|
||||
const target = 'node_modules/msw/lib/mockServiceWorker.js'
|
||||
|
||||
const mswPlugin = () => {
|
||||
let projectRoot
|
||||
return {
|
||||
name: 'msw-plugin',
|
||||
apply: 'serve',
|
||||
configResolved (conf) {
|
||||
projectRoot = conf.root
|
||||
},
|
||||
configureServer (server) {
|
||||
server.middlewares.use(async (req, res, next) => {
|
||||
if (req.path === '/mockServiceWorker.js') {
|
||||
const file = await readFile(resolve(projectRoot, target))
|
||||
res.set('Content-Type', 'text/javascript')
|
||||
res.send(file)
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default mswPlugin
|
||||
Loading…
Add table
Add a link
Reference in a new issue