2024-01-16 19:46:32 +00:00
|
|
|
import fs from 'node:fs/promises'
|
|
|
|
import path from 'node:path'
|
2022-03-26 17:08:36 +00:00
|
|
|
import { defineConfig } from 'vite'
|
2022-10-14 09:02:34 +00:00
|
|
|
import type { Plugin } from 'vite'
|
2022-03-26 17:08:36 +00:00
|
|
|
|
|
|
|
export default defineConfig({
|
2022-06-20 12:57:51 +00:00
|
|
|
experimental: {
|
2022-12-04 07:19:06 +00:00
|
|
|
hmrPartialAccept: true,
|
2022-06-20 12:57:51 +00:00
|
|
|
},
|
2024-11-06 09:28:44 +00:00
|
|
|
build: {
|
|
|
|
assetsInlineLimit(filePath) {
|
|
|
|
if (filePath.endsWith('logo-no-inline.svg')) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2020-12-29 21:29:51 +00:00
|
|
|
plugins: [
|
|
|
|
{
|
|
|
|
name: 'mock-custom',
|
2024-09-04 09:27:09 +00:00
|
|
|
async hotUpdate({ file, read }) {
|
2020-12-29 21:29:51 +00:00
|
|
|
if (file.endsWith('customFile.js')) {
|
|
|
|
const content = await read()
|
|
|
|
const msg = content.match(/export const msg = '(\w+)'/)[1]
|
2024-09-04 09:27:09 +00:00
|
|
|
this.environment.hot.send('custom:foo', { msg })
|
|
|
|
this.environment.hot.send('custom:remove', { msg })
|
2020-12-29 21:29:51 +00:00
|
|
|
}
|
2022-03-26 14:42:07 +00:00
|
|
|
},
|
|
|
|
configureServer(server) {
|
2024-09-04 09:27:09 +00:00
|
|
|
server.environments.client.hot.on(
|
|
|
|
'custom:remote-add',
|
|
|
|
({ a, b }, client) => {
|
|
|
|
client.send('custom:remote-add-result', { result: a + b })
|
|
|
|
},
|
|
|
|
)
|
2022-12-04 07:19:06 +00:00
|
|
|
},
|
2022-09-19 12:34:50 +00:00
|
|
|
},
|
2022-12-04 07:19:06 +00:00
|
|
|
virtualPlugin(),
|
2023-10-25 14:58:01 +00:00
|
|
|
transformCountPlugin(),
|
2024-01-16 19:46:32 +00:00
|
|
|
watchCssDepsPlugin(),
|
2022-12-04 07:19:06 +00:00
|
|
|
],
|
2022-03-26 17:08:36 +00:00
|
|
|
})
|
2022-10-14 09:02:34 +00:00
|
|
|
|
|
|
|
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) {
|
2024-09-04 09:27:09 +00:00
|
|
|
server.environments.client.hot.on('virtual:increment', async () => {
|
|
|
|
const mod =
|
|
|
|
await server.environments.client.moduleGraph.getModuleByUrl(
|
|
|
|
'\0virtual:file',
|
|
|
|
)
|
2022-10-14 09:02:34 +00:00
|
|
|
if (mod) {
|
|
|
|
num++
|
2024-09-04 09:27:09 +00:00
|
|
|
server.environments.client.reloadModule(mod)
|
2022-10-14 09:02:34 +00:00
|
|
|
}
|
|
|
|
})
|
2022-12-04 07:19:06 +00:00
|
|
|
},
|
2022-10-14 09:02:34 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-25 14:58:01 +00:00
|
|
|
|
|
|
|
function transformCountPlugin(): Plugin {
|
|
|
|
let num = 0
|
|
|
|
return {
|
|
|
|
name: 'transform-count',
|
|
|
|
transform(code) {
|
|
|
|
if (code.includes('__TRANSFORM_COUNT__')) {
|
|
|
|
return code.replace('__TRANSFORM_COUNT__', String(++num))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2024-01-16 19:46:32 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|