Browser extensions - switching Parcel to Vite

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
We have already had quite the exposure to browser extensions so far and looked at the following items: A first extension New tab extension Adding Tailwind to browser extensions Adding React to browser extensions This article will look at how we can...
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Not that there was anything wrong with Parcel, but I wanted to try out Vite.
So instead of doing it all from scratch, let's look at how we can migrate from Parcel to Vite.
Note: You can also use this article as a guide on building an extension powered by Vite.
If you want to follow along with the article and try it out, use the following GitHub branch as your starting point.
For those who don't know Vite yet, it's a build tool we can use to run a development server, and output builds. It's very similar to Parcel, and at this point of my research, it comes down to personal preferences to pick.
My first process was to clean up the existing Parcel config and related items.
.parcelrc.package.json and remove the following lines."parcel": "^2.7.0",
"parcel-reporter-static-files-copy": "^1.3.4",
Also, remove all the existing scripts in the package.json file. We'll come back to add new ones.
Now run an npm i to remove the packages.
At this point, our app is no longer powered by Parcel, but nothing powers it now, so we'll have to fix it.
Let's start by adding the packages we need. As I'm running React, I need the react plugin. This might differ if you use other frameworks.
I also added the new scripts that are needed for Vite.
{
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"vite": "^3.0.4"
},
"devDependencies": {
"@vitejs/plugin-react": "^2.0.0"
}
}
Note: These are only the changes. Your
package.jsonshould have some more lines already.
Now run npm i to install the new dependencies.
This won't run by default as Vite uses a slightly different approach than Parcel.
The first thing we want to change is renaming all our .js files to .jsx. (this is not a must, but easier)
App.js => App.jsxCounter.js => Counter.jsxindex.js => index.jsxWhile doing this, move the index.jsx file inside your src directory. (Don't forget to change the import inside the new-tab.html file)
Now rename the static folder to public, as Vite uses the public folder in favor of static.
Then we also need to modify our tailwind.config.js file to use .jsx files rather than .js.
/** @type {import('tailwindcss').Config} */
module.exports = {
- content: ['src/*.js'],
+ content: ['src/*.jsx'],
};
And lastly, we need to define a vite.config.js file at the root of our project.
In here, we need to tell Vite it's a React project.
Since we are not using an index.html file, we can also notify Vite that we have a different entry point: new-tab.html.
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [react()],
build: {
rollupOptions: {
input: {
app: './new-tab.html',
},
},
},
});
And that's it. You can now try a new build by running the following command.
npm run build
To test it out, you must follow the steps described in this article, but pick the dist folder to be loaded.
And you are done. You converted the Parcel build to use Vite instead.
You can also find the complete code sample on this GitHub repo.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter