vite/playground/css-sourcemap/vite.config.js
Hiroshi Ogawa d4e0442f9d
feat(css)!: change default sass api to modern/modern-compiler (#17937)
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
2024-10-25 16:49:27 +09:00

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()
})
},
},
],
})