mirror of
https://github.com/vitejs/vite.git
synced 2024-11-22 07:09:05 +00:00
242f550eb4
Co-authored-by: Hiroshi Ogawa <hi.ogawa.zz@gmail.com> Co-authored-by: Dario Piotrowicz <dario.piotrowicz@gmail.com> Co-authored-by: Vladimir Sheremet <sleuths.slews0s@icloud.com> Co-authored-by: Arnaud Barré <arnaud.barre@carbometrix.com> Co-authored-by: Anthony Fu <github@antfu.me> Co-authored-by: Dominik G <dominik.goepel@gmx.de> Co-authored-by: Igor Minar <i@igor.dev> Co-authored-by: Viktor Lázár <lazarv1982@gmail.com> Co-authored-by: Joaquín Sánchez <userquin@gmail.com> Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Co-authored-by: bluwy <bjornlu.dev@gmail.com>
95 lines
2.4 KiB
TypeScript
95 lines
2.4 KiB
TypeScript
import fs from 'node:fs/promises'
|
|
import path from 'node:path'
|
|
import { defineConfig } from 'vite'
|
|
import type { Plugin } from 'vite'
|
|
|
|
export default defineConfig({
|
|
experimental: {
|
|
hmrPartialAccept: true,
|
|
},
|
|
plugins: [
|
|
{
|
|
name: 'mock-custom',
|
|
async hotUpdate({ file, read }) {
|
|
if (file.endsWith('customFile.js')) {
|
|
const content = await read()
|
|
const msg = content.match(/export const msg = '(\w+)'/)[1]
|
|
this.environment.hot.send('custom:foo', { msg })
|
|
this.environment.hot.send('custom:remove', { msg })
|
|
}
|
|
},
|
|
configureServer(server) {
|
|
server.environments.client.hot.on(
|
|
'custom:remote-add',
|
|
({ a, b }, client) => {
|
|
client.send('custom:remote-add-result', { result: a + b })
|
|
},
|
|
)
|
|
},
|
|
},
|
|
virtualPlugin(),
|
|
transformCountPlugin(),
|
|
watchCssDepsPlugin(),
|
|
],
|
|
})
|
|
|
|
function virtualPlugin(): Plugin {
|
|
let num = 0
|
|
return {
|
|
name: 'virtual-file',
|
|
resolveId(id) {
|
|
if (id === 'virtual:file') {
|
|
return '\0virtual:file'
|
|
}
|
|
},
|
|
load(id) {
|
|
if (id === '\0virtual:file') {
|
|
return `\
|
|
import { virtual as _virtual } from "/importedVirtual.js";
|
|
export const virtual = _virtual + '${num}';`
|
|
}
|
|
},
|
|
configureServer(server) {
|
|
server.environments.client.hot.on('virtual:increment', async () => {
|
|
const mod =
|
|
await server.environments.client.moduleGraph.getModuleByUrl(
|
|
'\0virtual:file',
|
|
)
|
|
if (mod) {
|
|
num++
|
|
server.environments.client.reloadModule(mod)
|
|
}
|
|
})
|
|
},
|
|
}
|
|
}
|
|
|
|
function transformCountPlugin(): Plugin {
|
|
let num = 0
|
|
return {
|
|
name: 'transform-count',
|
|
transform(code) {
|
|
if (code.includes('__TRANSFORM_COUNT__')) {
|
|
return code.replace('__TRANSFORM_COUNT__', String(++num))
|
|
}
|
|
},
|
|
}
|
|
}
|
|
|
|
function watchCssDepsPlugin(): Plugin {
|
|
return {
|
|
name: 'watch-css-deps',
|
|
async transform(code, id) {
|
|
// replace the `replaced` identifier in the CSS file with the adjacent
|
|
// `dep.js` file's `color` variable.
|
|
if (id.includes('css-deps/main.css')) {
|
|
const depPath = path.resolve(__dirname, './css-deps/dep.js')
|
|
const dep = await fs.readFile(depPath, 'utf-8')
|
|
const color = dep.match(/color = '(.+?)'/)[1]
|
|
this.addWatchFile(depPath)
|
|
return code.replace('replaced', color)
|
|
}
|
|
},
|
|
}
|
|
}
|