Build a Chrome Extension with Vue/Vite
At the time of writing this note, official Vue has switched to Vite, which is a much faster alternative of original Vue CLI.
See Vite Note for more details.
See Sample Code
Vite
Let's suppose you want to build 2 diffrent pages, one for popup and one for options.
Procedures
npm init vue@latest
- Select the components you need
npm install
- Copy
index.html
and name the new filepopup.html
, rename./src/main.ts
topopup.ts
.- Within
popup.html
, replace thesrc
of<script type="module" src="/src/main.ts"></script>
with/src/popup.ts
- Within
- Make a copy of
popup.html
and./src/popup.ts
, call themoptions.html
and./src/options.ts
- Within
options.html
, replace thesrc
with./src/options.ts
- Within
- Rename
./src/App.vue
to./src/Popup.vue
. Make a copy of it and name it./src/Options.vue
./src/options.ts
and./src/popup.ts
will be the entrypoint when building the 2 pages./src/Popup.vue
and./src/Options.vue
will be the 2 pages themselves
- In
vite.config.ts
, add the following todefineConfig
import { fileURLToPath, URL } from 'url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
const root = resolve(__dirname, 'src');
const outDir = resolve(__dirname, 'dist');
const publicDir = resolve(__dirname, 'public')
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
publicDir,
root,
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
},
build: {
outDir,
emptyOutDir: true,
rollupOptions: {
input: {
popup: resolve(root, 'index.html'),
options: resolve(root, 'pages/options/index.html')
}
}
}
})
- Now, if you run
npm run build
, the dist folder will containoptions.html
andpopup.html
- Note that the
index.html
is not deleted. It's used for development server.- Toggle the
src
attribute between/src/popup.ts
and/src/options.ts
to decide which page you want to develop. - Another option is to use a config file to specify which entrypoint html file to use.
- Toggle the
Sample Code
See chrome-ext-vue3-ts for sample code.
Check different branches. Both original Vue and Vite examples are supplied.