Skip to content

A base chrome extension app template that uses webpack

License

Notifications You must be signed in to change notification settings

narralabs/brackets

Repository files navigation

Brackets

Brackets is a template for creating chrome extensions. It uses webpack to support frontend frameworks and other libraries. It also uses Tailwind CSS 3 (through webpack/postcss) for its CSS framework and works out of the box.

brackets_logo

Requirements

  • Node.js (Check .node-version for version)

Getting Started

All the work will be done in the src folder. The src folder will then be compiled to the extension folder.

  1. Install packages: npm install
  2. Run the build: npm run-script dev. This will watch the src folder for changes and compile into extension
  3. In chrome://extensions, click "Load unpacked" and select the extension folder

Adding React

If you want to use react in the chrome extension popup do the following after cloning:

  1. Add the react and react-dom packages: npm install --save-dev react react-dom
  2. Add the webpack plugins to support JSX compilation:
npm install -D babel-loader @babel/preset-env @babel/preset-react @babel/runtime @babel/plugin-transform-runtime @babel/plugin-syntax-dynamic-import @babel/plugin-proposal-class-properties babel-plugin-transform-react-remove-prop-types @babel/plugin-transform-react-inline-elements @babel/plugin-transform-react-constant-elements
  1. Modify webpack.config.js and add line to support JSX
...
  module: {
    rules: [
      {
        test: /\.css$/i,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          'postcss-loader',
        ],
      },
+     {
+       test: /\.jsx?$/,
+       exclude: /node_modules/,
+       use: {
+         loader: 'babel-loader',
+         options: {
+           presets: ['@babel/preset-react'],
+         },
+       },
+     },
  }
...
  1. Create a file src/app.jsx
import React from 'react';
import { useState } from 'react';
import { createRoot } from 'react-dom/client';

function App() {
  const [count, setCount] = useState(0);
  return (
    <>
      <h1>Brackets + React</h1>
      <h2>{count}</h2>
      <button onClick={() => setCount(count + 1)}>
        Increment
      </button>
    </>
  );
}

const root = createRoot(document.getElementById('root'));
root.render(<App />);
  1. Modify src/popup.js with
import './popup.css';
+ import './app.jsx';

console.log('[CONTENTSCRIPT] Popup opened');
  1. Modify src/popup.html with
  ...
  <body>
    + <div id="root">
    + </div>

    <script src="popup.js"></script>
  </body>
  ...
  1. Run the build: npm run-script dev. This will watch the src folder for changes and compile into extension
  2. In chrome://extensions, click "Load unpacked" and select the extension folder

Releasing Your Extension

  1. Make sure to uncomment purge: true in tailwind.config.js
  2. Zip the extensions folder
  3. Upload chrome extension store

About

A base chrome extension app template that uses webpack

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published