Web Dev/Other/ChromeExtension
Build a Chrome Extension with Vue/Vite
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
- 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.
How is this guide?