chrome-extension-webpack-boilerplate
A basic foundation boilerplate for rich Chrome Extensions using Webpack to help you write modular and modern Javascript code, load CSS easily and automatic reload the browser on code changes.
Top Related Projects
Scaffold out a Chrome extension
A Chrome Extensions boilerplate using React 18 and Webpack 5.
🖥🔋Web Extension starter to build "Write Once Run on Any Browser" extension
⚡️ A template for building cross browser extensions for Chrome, Opera & Firefox.
Quick Overview
The samuelsimoes/chrome-extension-webpack-boilerplate
is a boilerplate project that provides a starting point for building Chrome extensions using Webpack. It simplifies the setup process and includes a set of best practices and configurations to help developers get started quickly.
Pros
- Webpack Integration: The boilerplate comes with Webpack pre-configured, making it easy to bundle and optimize the extension's assets.
- Hot Reloading: The boilerplate supports hot reloading, allowing developers to see changes in the extension immediately without having to manually reload the browser.
- Modern Tooling: The project uses modern web development tools and practices, such as Babel, ESLint, and Sass, ensuring a smooth and efficient development experience.
- Extensible Structure: The boilerplate provides a well-organized and extensible project structure, making it easy to add new features and customize the extension as needed.
Cons
- Limited Documentation: The project's documentation could be more comprehensive, especially for developers new to Chrome extension development or Webpack.
- Opinionated Approach: The boilerplate takes an opinionated approach to the project structure and tooling, which may not align with the preferences of all developers.
- Potential Complexity: The inclusion of Webpack and other modern tools may add some complexity to the setup process, especially for developers who are new to these technologies.
- Specific to Chrome: The boilerplate is designed specifically for Chrome extensions, which may limit its usefulness for developers targeting other browser platforms.
Getting Started
To get started with the samuelsimoes/chrome-extension-webpack-boilerplate
, follow these steps:
-
Clone the repository:
git clone https://github.com/samuelsimoes/chrome-extension-webpack-boilerplate.git
-
Install the dependencies:
cd chrome-extension-webpack-boilerplate npm install
-
Start the development server:
npm start
This will start the Webpack development server and open a new Chrome window with the extension loaded.
-
Start developing your extension. The boilerplate includes a basic example extension that you can use as a starting point.
-
When you're ready to build the extension for production, run:
npm run build
This will create a
dist
folder with the optimized and bundled extension files. -
Load the extension in Chrome:
- Open the Chrome Extensions page (
chrome://extensions/
) - Enable "Developer mode"
- Click "Load unpacked" and select the
dist
folder
- Open the Chrome Extensions page (
That's it! You're now ready to start building your Chrome extension using the samuelsimoes/chrome-extension-webpack-boilerplate
.
Competitor Comparisons
Scaffold out a Chrome extension
Pros of generator-chrome-extension
- Utilizes Yeoman, a popular scaffolding tool, providing a more standardized and familiar setup process
- Offers more customization options during project initialization
- Includes built-in support for unit testing with Mocha and Chai
Cons of generator-chrome-extension
- Less focused on modern JavaScript development practices (e.g., no built-in webpack configuration)
- May require more manual setup for advanced features like hot reloading
- Has fewer recent updates compared to chrome-extension-webpack-boilerplate
Code Comparison
generator-chrome-extension:
// Gruntfile.js
grunt.initConfig({
// Task configurations
});
grunt.registerTask('default', ['jshint']);
chrome-extension-webpack-boilerplate:
// webpack.config.js
module.exports = {
entry: {
popup: path.join(__dirname, "src", "js", "popup.js"),
options: path.join(__dirname, "src", "js", "options.js"),
background: path.join(__dirname, "src", "js", "background.js")
},
// ... other webpack configurations
};
The code comparison highlights the different build tools used: generator-chrome-extension uses Grunt, while chrome-extension-webpack-boilerplate uses webpack, which is more modern and flexible for JavaScript projects.
A Chrome Extensions boilerplate using React 18 and Webpack 5.
Pros of chrome-extension-boilerplate-react
- Built with React, offering a more modern and component-based development approach
- Includes hot-reloading for faster development and debugging
- Provides a more comprehensive project structure with separate directories for components, pages, and hooks
Cons of chrome-extension-boilerplate-react
- More complex setup and potentially steeper learning curve for developers new to React
- Larger bundle size due to React dependencies, which may impact extension performance
- Less frequently updated compared to chrome-extension-webpack-boilerplate
Code Comparison
chrome-extension-boilerplate-react:
import React from 'react';
import { render } from 'react-dom';
import Popup from './Popup';
import './index.css';
render(<Popup />, document.getElementById('root'));
chrome-extension-webpack-boilerplate:
import '../css/popup.css';
console.log('This is the background page.');
console.log('Put the background scripts here.');
The code comparison shows that chrome-extension-boilerplate-react uses React components and JSX syntax, while chrome-extension-webpack-boilerplate uses plain JavaScript. This highlights the different approaches to building the extension's UI and logic.
🖥🔋Web Extension starter to build "Write Once Run on Any Browser" extension
Pros of web-extension-starter
- Supports multiple browsers (Chrome, Firefox, Opera, Edge) out of the box
- Uses TypeScript for improved type safety and developer experience
- Includes ESLint and Prettier for code quality and formatting
Cons of web-extension-starter
- More complex setup and configuration due to multi-browser support
- Larger project structure, which may be overwhelming for simple extensions
- Steeper learning curve for developers unfamiliar with TypeScript
Code Comparison
web-extension-starter:
import Browser from 'webextension-polyfill';
export async function getCurrentTab() {
const [tab] = await Browser.tabs.query({ active: true, currentWindow: true });
return tab;
}
chrome-extension-webpack-boilerplate:
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
var activeTab = tabs[0];
chrome.tabs.sendMessage(activeTab.id, { "message": "clicked_browser_action" });
});
The web-extension-starter uses the webextension-polyfill
library for cross-browser compatibility and async/await syntax, while chrome-extension-webpack-boilerplate uses the Chrome-specific API with callbacks.
web-extension-starter offers a more modern and flexible approach to extension development, supporting multiple browsers and using TypeScript. However, it may be more complex for simple projects. chrome-extension-webpack-boilerplate provides a simpler setup focused on Chrome, which could be preferable for developers targeting only that browser or those who prefer plain JavaScript.
⚡️ A template for building cross browser extensions for Chrome, Opera & Firefox.
Pros of extension-boilerplate
- Includes support for multiple browsers (Chrome, Firefox, Opera)
- Provides a more comprehensive project structure with separate directories for styles, icons, and scripts
- Includes a basic options page template
Cons of extension-boilerplate
- Less focus on modern JavaScript tooling (no Webpack integration)
- May require more manual configuration for advanced build processes
- Last updated in 2017, potentially outdated compared to chrome-extension-webpack-boilerplate
Code Comparison
extension-boilerplate (manifest.json):
{
"name": "Extension Boilerplate",
"version": "0.0.1",
"manifest_version": 2,
"description": "This is a sample extension description",
"icons": {
"16": "icons/icon16.png",
"48": "icons/icon48.png",
"128": "icons/icon128.png"
}
}
chrome-extension-webpack-boilerplate (webpack.config.js):
const webpack = require("webpack"),
path = require("path"),
fileSystem = require("fs"),
env = require("./utils/env"),
CleanWebpackPlugin = require("clean-webpack-plugin").CleanWebpackPlugin,
CopyWebpackPlugin = require("copy-webpack-plugin"),
HtmlWebpackPlugin = require("html-webpack-plugin"),
WriteFilePlugin = require("write-file-webpack-plugin");
The extension-boilerplate provides a more traditional extension structure, while chrome-extension-webpack-boilerplate focuses on integrating modern build tools like Webpack for improved development workflow.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Chrome Extension Webpack Boilerplate
A basic foundation boilerplate for rich Chrome Extensions using Webpack to help you write modular and modern Javascript code, load CSS easily and automatic reload the browser on code changes.
Developing a new extension
I'll assume that you already read the Webpack docs and the Chrome Extension docs.
- Check if your Node.js version is >= 6.
- Clone the repository.
- Install yarn.
- Run
yarn
. - Change the package's name and description on
package.json
. - Change the name of your extension on
src/manifest.json
. - Run
yarn run start
- Load your extension on Chrome following:
- Access
chrome://extensions/
- Check
Developer mode
- Click on
Load unpacked extension
- Select the
build
folder.
- Access
- Have fun.
Structure
All your extension's development code must be placed in src
folder, including the extension manifest.
The boilerplate is already prepared to have a popup, a options page and a background page. You can easily customize this.
Each page has its own assets package defined. So, to code on popup you must start your code on src/js/popup.js
, for example.
You must use the ES6 modules to a better code organization. The boilerplate is already prepared to that and here you have a little example.
Webpack auto-reload and HRM
To make your workflow much more efficient this boilerplate uses the webpack server to development (started with yarn run server
) with auto reload feature that reloads the browser automatically every time that you save some file o your editor.
You can run the dev mode on other port if you want. Just specify the env var port
like this:
$ PORT=6002 yarn run start
Content Scripts
Although this boilerplate uses the webpack dev server, it's also prepared to write all your bundles files on the disk at every code change, so you can point, on your extension manifest, to your bundles that you want to use as content scripts, but you need to exclude these entry points from hot reloading (why?). To do so you need to expose which entry points are content scripts on the webpack.config.js
using the chromeExtensionBoilerplate -> notHotReload
config. Look the example below.
Let's say that you want use the myContentScript
entry point as content script, so on your webpack.config.js
you will configure the entry point and exclude it from hot reloading, like this:
{
â¦
entry: {
myContentScript: "./src/js/myContentScript.js"
},
chromeExtensionBoilerplate: {
notHotReload: ["myContentScript"]
}
â¦
}
and on your src/manifest.json
:
{
"content_scripts": [
{
"matches": ["https://www.google.com/*"],
"js": ["myContentScript.bundle.js"]
}
]
}
Packing
After the development of your extension run the command
$ NODE_ENV=production yarn run build
Now, the content of build
folder will be the extension ready to be submitted to the Chrome Web Store. Just take a look at the official guide to more infos about publishing.
Secrets
If you are developing an extension that talks with some API you probably are using different keys for testing and production. Is a good practice you not commit your secret keys and expose to anyone that have access to the repository.
To this task this boilerplate import the file ./secrets.<THE-NODE_ENV>.js
on your modules through the module named as secrets
, so you can do things like this:
./secrets.development.js
export default { key: "123" };
./src/popup.js
import secrets from "secrets";
ApiCall({ key: secrets.key });
:point_right: The files with name secrets.*.js
already are ignored on the repository.
With React.js
:bulb: If you want use React.js with this boilerplate, check the react branch.
Contributing
- Please!! Do not create a pull request without an issue before discussing the problem.
- On your PR make sure that you are following the current codebase style.
- Your PR must be single purpose. Resolve just one problem on your PR.
- Make sure to commit in the same style that we are committing until now on the project.
Samuel Simões ~ @samuelsimoes ~ Blog
Top Related Projects
Scaffold out a Chrome extension
A Chrome Extensions boilerplate using React 18 and Webpack 5.
🖥🔋Web Extension starter to build "Write Once Run on Any Browser" extension
⚡️ A template for building cross browser extensions for Chrome, Opera & Firefox.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot