mirror of
https://github.com/vitejs/vite.git
synced 2024-11-21 22:59:10 +00:00
d4e0442f9d
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com> Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
60 lines
1.4 KiB
JavaScript
60 lines
1.4 KiB
JavaScript
import { defineConfig } from 'vite'
|
|
import MagicString from 'magic-string'
|
|
|
|
export default defineConfig({
|
|
resolve: {
|
|
alias: {
|
|
'@': __dirname,
|
|
},
|
|
},
|
|
css: {
|
|
devSourcemap: true,
|
|
preprocessorOptions: {
|
|
less: {
|
|
additionalData: '@color: red;',
|
|
},
|
|
styl: {
|
|
additionalData: (content, filename) => {
|
|
const ms = new MagicString(content, { filename })
|
|
|
|
const willBeReplaced = 'blue-red-mixed'
|
|
const start = content.indexOf(willBeReplaced)
|
|
ms.overwrite(start, start + willBeReplaced.length, 'purple')
|
|
|
|
const map = ms.generateMap({ hires: 'boundary' })
|
|
map.file = filename
|
|
map.sources = [filename]
|
|
|
|
return {
|
|
content: ms.toString(),
|
|
map,
|
|
}
|
|
},
|
|
},
|
|
},
|
|
},
|
|
build: {
|
|
sourcemap: true,
|
|
},
|
|
plugins: [
|
|
{
|
|
name: 'virtual-html',
|
|
configureServer(server) {
|
|
server.middlewares.use(async (req, res, next) => {
|
|
if (req.url === '/virtual.html') {
|
|
const t = await server.transformIndexHtml(
|
|
'/virtual.html',
|
|
'<style> .foo { color: red; } </style> <p class="foo">virtual html</p>',
|
|
)
|
|
res.setHeader('Content-Type', 'text/html')
|
|
res.statusCode = 200
|
|
res.end(t)
|
|
return
|
|
}
|
|
next()
|
|
})
|
|
},
|
|
},
|
|
],
|
|
})
|