We have set up a Chrome extension using vueJs using hmr. We are facing an issue on the development server to run the extension on windows 11.
Webpage Render:
Vite server did not start
Configuration:
Windows 11 12th gen i7 processor
vite: ^3.1.6
vue: ^3.2.40
Node: v20
vite.config.ts:
/// <reference types="vitest" />
import { dirname, relative } from 'path'
import type { UserConfig } from 'vite'
import { defineConfig } from 'vite'
import Vue from '@vitejs/plugin-vue'
import replace from '@rollup/plugin-replace'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import Components from 'unplugin-vue-components/vite'
import AutoImport from 'unplugin-auto-import/vite'
import UnoCSS from 'unocss/vite'
import { isDev, port, r } from './scripts/utils'
import { MV3Hmr } from './vite-mv3-hmr'
export const sharedConfig: UserConfig = {
root: r('src'),
resolve: {
alias: {
'~/': `${r('src')}/`,
},
},
plugins: [
[Vue()],
AutoImport({
imports: [
'vue',
{
'webextension-polyfill': [
['*', 'browser'],
],
},
],
dts: r('src/auto-imports.d.ts'),
}),
Components({
dirs: [r('src/components')],
// generate `components.d.ts` for ts support with Volar
dts: r('src/components.d.ts'),
resolvers: [
// auto import icons
IconsResolver({
componentPrefix: '',
}),
],
}),
Icons(),
UnoCSS(),
replace({
'__DEV__': JSON.stringify(isDev),
'process.env.NODE_ENV': JSON.stringify(isDev ? 'development' : 'production'),
'__VUE_OPTIONS_API__': JSON.stringify(true),
'__VUE_PROD_DEVTOOLS__': JSON.stringify(false),
}),
// rewrite assets to use relative path
{
name: 'assets-rewrite',
enforce: 'post',
apply: 'build',
transformIndexHtml(html, { path }) {
return html.replace(/"/assets//g, `"${relative(dirname(path), '/assets')}/`)
},
},
],
optimizeDeps: {
include: [
'vue',
'@vueuse/core',
'webextension-polyfill',
],
exclude: [
'vue-demi',
],
},
}
export default defineConfig(({ command }) => ({
...sharedConfig,
base: command === 'serve' ? `http://localhost:${port}/` : './extension/dist',
server: {
watch: {
usePolling: true,
},
port,
hmr: {
host: `http://localhost:${port}/`,
},
},
build: {
outDir: r('extension/dist'),
emptyOutDir: false,
sourcemap: isDev ? 'inline' : false,
terserOptions: {
mangle: false,
},
rollupOptions: {
input: {
options: r('src/options/index.html'),
popup: r('src/popup/index.html'),
},
},
},
plugins: [
...sharedConfig.plugins!,
MV3Hmr(),
],
test: {
globals: true,
environment: 'jsdom',
},
}))
script to run dev server:
"scripts": {
"dev": "npm run clear && cross-env NODE_ENV=development run-p dev:*",
"dev:prepare": "esno scripts/prepare.ts",
"dev:web": "vite dev",
"dev:js": "npm run build:js -- --mode development",
"dev:bg": "tsup --watch ./src",
}
Your help can save someone’s career 😀
8