├── .babelrc ├── .eslintrc ├── .gitignore ├── .prettierrc ├── .vscode └── settings.json ├── LICENSE ├── README.md ├── package-lock.json ├── package.json ├── src ├── assets │ └── img │ │ ├── icon-128.png │ │ ├── icon-34.png │ │ └── logo.svg ├── containers │ └── Greetings │ │ └── Greetings.jsx ├── manifest.json └── pages │ ├── Background │ └── index.js │ ├── Content │ ├── content.styles.css │ ├── index.js │ └── modules │ │ └── print.js │ ├── Devtools │ ├── index.html │ └── index.js │ ├── Newtab │ ├── Newtab.css │ ├── Newtab.jsx │ ├── Newtab.scss │ ├── index.css │ ├── index.html │ └── index.jsx │ ├── Options │ ├── Options.css │ ├── Options.tsx │ ├── index.css │ ├── index.html │ └── index.jsx │ ├── Panel │ ├── Panel.css │ ├── Panel.tsx │ ├── index.css │ ├── index.html │ └── index.jsx │ └── Popup │ ├── Popup.css │ ├── Popup.jsx │ ├── index.css │ ├── index.html │ └── index.jsx ├── tsconfig.json ├── utils ├── build.js ├── env.js └── webserver.js └── webpack.config.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | // "@babel/preset-env" 4 | "@babel/preset-react" 5 | // "react-app" 6 | ], 7 | "plugins": [ 8 | // "@babel/plugin-proposal-class-properties", 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "react-app", 3 | "globals": { 4 | "chrome": "readonly" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | 6 | # testing 7 | /coverage 8 | 9 | # production 10 | /build 11 | 12 | # zip 13 | /zip 14 | 15 | # misc 16 | .DS_Store 17 | .env.local 18 | .env.development.local 19 | .env.test.local 20 | .env.production.local 21 | .history 22 | 23 | # secrets 24 | secrets.*.js 25 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "es5", 4 | "requirePragma": false, 5 | "arrowParens": "always" 6 | } -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "editor.formatOnSave": true 3 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2019 Michael Xieyang Liu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Chrome Extension (MV3) Boilerplate with React 18 and Webpack 5 4 | 5 | [![npm](https://img.shields.io/npm/v/chrome-extension-boilerplate-react)](https://www.npmjs.com/package/chrome-extension-boilerplate-react) 6 | [![npm-download](https://img.shields.io/npm/dw/chrome-extension-boilerplate-react)](https://www.npmjs.com/package/chrome-extension-boilerplate-react) 7 | [![npm](https://img.shields.io/npm/dm/chrome-extension-boilerplate-react)](https://www.npmjs.com/package/chrome-extension-boilerplate-react) 8 | 9 | ## Announcements 10 | 11 | - Recently updated from **[React](https://reactjs.org)** ~~17~~ to **18**! 12 | - **_This boilerplate adopts [Manifest V3](https://developer.chrome.com/docs/extensions/mv3/intro/mv3-overview/)!_** 13 | - For V2 users, please check out the [manifest-v2](https://github.com/lxieyang/chrome-extension-boilerplate-react/tree/manifest-v2) branch, or use version [3.x](https://www.npmjs.com/package/chrome-extension-boilerplate-react/v/3.3.0). 14 | - Check out the [Manifest V3 Migration Guide](https://developer.chrome.com/docs/extensions/mv3/intro/mv3-migration/). 15 | - Recently added [devtools](https://developer.chrome.com/docs/extensions/mv3/devtools/) Support! Thanks [GeekaholicLin](https://github.com/lxieyang/chrome-extension-boilerplate-react/issues/17)! 16 | - Recently updated from **[Webpack Dev Server](https://webpack.js.org/configuration/dev-server/)** ~~3.x~~ to **4.x** and **[Webpack](https://webpack.js.org/)** ~~4~~ to **5**! 17 | - Recently added [TypeScript](https://www.typescriptlang.org/) Support! 18 | 19 | ## Features 20 | 21 | This is a basic Chrome Extensions boilerplate to help you write modular and modern Javascript code, load CSS easily and [automatic reload the browser on code changes](https://webpack.github.io/docs/webpack-dev-server.html#automatic-refresh). 22 | 23 | This boilerplate is updated with: 24 | 25 | - [Chrome Extension Manifest V3](https://developer.chrome.com/docs/extensions/mv3/intro/mv3-overview/) 26 | - [React 18](https://reactjs.org) 27 | - [Webpack 5](https://webpack.js.org/) 28 | - [Webpack Dev Server 4](https://webpack.js.org/configuration/dev-server/) 29 | - [React Refresh](https://www.npmjs.com/package/react-refresh) 30 | - [react-refresh-webpack-plugin](https://github.com/pmmmwh/react-refresh-webpack-plugin) 31 | - [eslint-config-react-app](https://www.npmjs.com/package/eslint-config-react-app) 32 | - [Prettier](https://prettier.io/) 33 | - [TypeScript](https://www.typescriptlang.org/) 34 | 35 | This boilerplate is heavily inspired by and adapted from [https://github.com/samuelsimoes/chrome-extension-webpack-boilerplate](https://github.com/samuelsimoes/chrome-extension-webpack-boilerplate), with additional support for React 18 features, Webpack 5, and Webpack Dev Server 4. 36 | 37 | Please open up an issue to nudge me to keep the npm packages up-to-date. FYI, it takes time to make different packages with different versions work together nicely. 38 | 39 | ## Installing and Running 40 | 41 | ### Procedures: 42 | 43 | 1. Check if your [Node.js](https://nodejs.org/) version is >= **18**. 44 | 2. Clone this repository. 45 | 3. Change the package's `name`, `description`, and `repository` fields in `package.json`. 46 | 4. Change the name of your extension on `src/manifest.json`. 47 | 5. Run `npm install` to install the dependencies. 48 | 6. Run `npm start` 49 | 7. Load your extension on Chrome following: 50 | 1. Access `chrome://extensions/` 51 | 2. Check `Developer mode` 52 | 3. Click on `Load unpacked extension` 53 | 4. Select the `build` folder. 54 | 8. Happy hacking. 55 | 56 | ## Structure 57 | 58 | All your extension's code must be placed in the `src` folder. 59 | 60 | The boilerplate is already prepared to have a popup, an options page, a background page, and a new tab page (which replaces the new tab page of your browser). But feel free to customize these. 61 | 62 | ## TypeScript 63 | 64 | This boilerplate now supports TypeScript! The `Options` Page is implemented using TypeScript. Please refer to `src/pages/Options/` for example usages. 65 | 66 | ## Webpack auto-reload and HRM 67 | 68 | To make your workflow much more efficient this boilerplate uses the [webpack server](https://webpack.github.io/docs/webpack-dev-server.html) to development (started with `npm start`) with auto reload feature that reloads the browser automatically every time that you save some file in your editor. 69 | 70 | You can run the dev mode on other port if you want. Just specify the env var `port` like this: 71 | 72 | ``` 73 | $ PORT=6002 npm run start 74 | ``` 75 | 76 | ## Content Scripts 77 | 78 | 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](https://developer.chrome.com/extensions/content_scripts), but you need to exclude these entry points from hot reloading [(why?)](https://github.com/samuelsimoes/chrome-extension-webpack-boilerplate/issues/4#issuecomment-261788690). 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. 79 | 80 | 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: 81 | 82 | ```js 83 | { 84 | … 85 | entry: { 86 | myContentScript: "./src/js/myContentScript.js" 87 | }, 88 | chromeExtensionBoilerplate: { 89 | notHotReload: ["myContentScript"] 90 | } 91 | … 92 | } 93 | ``` 94 | 95 | and on your `src/manifest.json`: 96 | 97 | ```json 98 | { 99 | "content_scripts": [ 100 | { 101 | "matches": ["https://www.google.com/*"], 102 | "js": ["myContentScript.bundle.js"] 103 | } 104 | ] 105 | } 106 | ``` 107 | 108 | ## Intelligent Code Completion 109 | 110 | Thanks to [@hudidit](https://github.com/lxieyang/chrome-extension-boilerplate-react/issues/4)'s kind suggestions, this boilerplate supports chrome-specific intelligent code completion using [@types/chrome](https://www.npmjs.com/package/@types/chrome). 111 | 112 | ## Packing 113 | 114 | After the development of your extension run the command 115 | 116 | ``` 117 | $ NODE_ENV=production npm run build 118 | ``` 119 | 120 | 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](https://developer.chrome.com/webstore/publish) to more infos about publishing. 121 | 122 | ## Secrets 123 | 124 | 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. 125 | 126 | To this task this boilerplate import the file `./secrets..js` on your modules through the module named as `secrets`, so you can do things like this: 127 | 128 | _./secrets.development.js_ 129 | 130 | ```js 131 | export default { key: '123' }; 132 | ``` 133 | 134 | _./src/popup.js_ 135 | 136 | ```js 137 | import secrets from 'secrets'; 138 | ApiCall({ key: secrets.key }); 139 | ``` 140 | 141 | :point_right: The files with name `secrets.*.js` already are ignored on the repository. 142 | 143 | ## Resources: 144 | 145 | - [Webpack documentation](https://webpack.js.org/concepts/) 146 | - [Chrome Extension documentation](https://developer.chrome.com/extensions/getstarted) 147 | 148 | --- 149 | 150 | Michael Xieyang Liu | [Website](https://lxieyang.github.io) 151 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "chrome-extension-boilerplate-react", 3 | "version": "5.0.4", 4 | "description": "A chrome extension boilerplate built with React 18, Webpack 5, and Webpack Dev Server 4", 5 | "license": "MIT", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/lxieyang/chrome-extension-boilerplate-react.git" 9 | }, 10 | "scripts": { 11 | "build": "node utils/build.js", 12 | "start": "node utils/webserver.js", 13 | "prettier": "prettier --write '**/*.{js,jsx,ts,tsx,json,css,scss,md}'" 14 | }, 15 | "dependencies": { 16 | "react": "^18.2.0", 17 | "react-dom": "^18.2.0" 18 | }, 19 | "devDependencies": { 20 | "@babel/core": "^7.20.12", 21 | "@babel/plugin-proposal-class-properties": "^7.18.6", 22 | "@babel/preset-env": "^7.20.2", 23 | "@babel/preset-react": "^7.18.6", 24 | "@pmmmwh/react-refresh-webpack-plugin": "^0.5.10", 25 | "@types/chrome": "^0.0.202", 26 | "@types/react": "^18.0.26", 27 | "@types/react-dom": "^18.0.10", 28 | "babel-eslint": "^10.1.0", 29 | "babel-loader": "^9.1.2", 30 | "babel-preset-react-app": "^10.0.1", 31 | "clean-webpack-plugin": "^4.0.0", 32 | "copy-webpack-plugin": "^11.0.0", 33 | "css-loader": "^6.7.3", 34 | "eslint": "^8.31.0", 35 | "eslint-config-react-app": "^7.0.1", 36 | "eslint-plugin-flowtype": "^8.0.3", 37 | "eslint-plugin-import": "^2.27.4", 38 | "eslint-plugin-jsx-a11y": "^6.7.1", 39 | "eslint-plugin-react": "^7.32.0", 40 | "eslint-plugin-react-hooks": "^4.6.0", 41 | "file-loader": "^6.2.0", 42 | "fs-extra": "^11.1.0", 43 | "html-loader": "^4.2.0", 44 | "html-webpack-plugin": "^5.5.0", 45 | "prettier": "^2.8.3", 46 | "react-refresh": "^0.14.0", 47 | "react-refresh-typescript": "^2.0.7", 48 | "sass": "^1.57.1", 49 | "sass-loader": "^13.2.0", 50 | "source-map-loader": "^3.0.1", 51 | "style-loader": "^3.3.1", 52 | "terser-webpack-plugin": "^5.3.6", 53 | "ts-loader": "^9.4.2", 54 | "type-fest": "^3.5.2", 55 | "typescript": "^4.9.4", 56 | "webpack": "^5.75.0", 57 | "webpack-cli": "^4.10.0", 58 | "webpack-dev-server": "^4.11.1", 59 | "zip-webpack-plugin": "^4.0.1" 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/assets/img/icon-128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lxieyang/chrome-extension-boilerplate-react/f7b0a5b89059ea943eaccd94aad3a429fb744644/src/assets/img/icon-128.png -------------------------------------------------------------------------------- /src/assets/img/icon-34.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lxieyang/chrome-extension-boilerplate-react/f7b0a5b89059ea943eaccd94aad3a429fb744644/src/assets/img/icon-34.png -------------------------------------------------------------------------------- /src/assets/img/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/containers/Greetings/Greetings.jsx: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import icon from '../../assets/img/icon-128.png'; 3 | 4 | class GreetingComponent extends Component { 5 | state = { 6 | name: 'dev', 7 | }; 8 | 9 | render() { 10 | return ( 11 |
12 |

Hello, {this.state.name}!

13 | extension icon 14 |
15 | ); 16 | } 17 | } 18 | 19 | export default GreetingComponent; 20 | -------------------------------------------------------------------------------- /src/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "manifest_version": 3, 3 | "name": "Chrome Extension with React & Webpack", 4 | "description": "A chrome extension boilerplate built with React 17, Webpack 5, and Webpack Dev Server 4", 5 | "options_page": "options.html", 6 | "background": { "service_worker": "background.bundle.js" }, 7 | "action": { 8 | "default_popup": "popup.html", 9 | "default_icon": "icon-34.png" 10 | }, 11 | "chrome_url_overrides": { 12 | "newtab": "newtab.html" 13 | }, 14 | "icons": { 15 | "128": "icon-128.png" 16 | }, 17 | "content_scripts": [ 18 | { 19 | "matches": ["http://*/*", "https://*/*", ""], 20 | "js": ["contentScript.bundle.js"], 21 | "css": ["content.styles.css"] 22 | } 23 | ], 24 | "devtools_page": "devtools.html", 25 | "web_accessible_resources": [ 26 | { 27 | "resources": ["content.styles.css", "icon-128.png", "icon-34.png"], 28 | "matches": [] 29 | } 30 | ] 31 | } 32 | -------------------------------------------------------------------------------- /src/pages/Background/index.js: -------------------------------------------------------------------------------- 1 | console.log('This is the background page.'); 2 | console.log('Put the background scripts here.'); 3 | -------------------------------------------------------------------------------- /src/pages/Content/content.styles.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lxieyang/chrome-extension-boilerplate-react/f7b0a5b89059ea943eaccd94aad3a429fb744644/src/pages/Content/content.styles.css -------------------------------------------------------------------------------- /src/pages/Content/index.js: -------------------------------------------------------------------------------- 1 | import { printLine } from './modules/print'; 2 | 3 | console.log('Content script works!'); 4 | console.log('Must reload extension for modifications to take effect.'); 5 | 6 | printLine("Using the 'printLine' function from the Print Module"); 7 | -------------------------------------------------------------------------------- /src/pages/Content/modules/print.js: -------------------------------------------------------------------------------- 1 | export const printLine = (line) => { 2 | console.log('===> FROM THE PRINT MODULE:', line); 3 | }; 4 | -------------------------------------------------------------------------------- /src/pages/Devtools/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /src/pages/Devtools/index.js: -------------------------------------------------------------------------------- 1 | chrome.devtools.panels.create( 2 | 'Dev Tools from chrome-extension-boilerplate-react', 3 | 'icon-34.png', 4 | 'panel.html' 5 | ); 6 | -------------------------------------------------------------------------------- /src/pages/Newtab/Newtab.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .App-logo { 6 | height: 40vmin; 7 | pointer-events: none; 8 | } 9 | 10 | @media (prefers-reduced-motion: no-preference) { 11 | .App-logo { 12 | animation: App-logo-spin infinite 20s linear; 13 | } 14 | } 15 | 16 | .App-header { 17 | background-color: #282c34; 18 | min-height: 100vh; 19 | display: flex; 20 | flex-direction: column; 21 | align-items: center; 22 | justify-content: center; 23 | font-size: calc(10px + 2vmin); 24 | color: white; 25 | } 26 | 27 | .App-link { 28 | color: #61dafb; 29 | } 30 | 31 | @keyframes App-logo-spin { 32 | from { 33 | transform: rotate(0deg); 34 | } 35 | to { 36 | transform: rotate(360deg); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/pages/Newtab/Newtab.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import logo from '../../assets/img/logo.svg'; 3 | import './Newtab.css'; 4 | import './Newtab.scss'; 5 | 6 | const Newtab = () => { 7 | return ( 8 |
9 |
10 | logo 11 |

12 | Edit src/pages/Newtab/Newtab.js and save to reload. 13 |

14 | 20 | Learn React! 21 | 22 |
The color of this paragraph is defined using SASS.
23 |
24 |
25 | ); 26 | }; 27 | 28 | export default Newtab; 29 | -------------------------------------------------------------------------------- /src/pages/Newtab/Newtab.scss: -------------------------------------------------------------------------------- 1 | $myColor: orange; 2 | 3 | h1, 4 | h2, 5 | h3, 6 | h4, 7 | h5, 8 | h6 { 9 | color: $myColor; 10 | } 11 | -------------------------------------------------------------------------------- /src/pages/Newtab/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /src/pages/Newtab/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Chrome Extension Boilerplate (with React 16.6+ & Webpack 4+) 7 | 8 | 9 | 10 |
11 | 12 | 13 | -------------------------------------------------------------------------------- /src/pages/Newtab/index.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { createRoot } from 'react-dom/client'; 3 | 4 | import Newtab from './Newtab'; 5 | import './index.css'; 6 | 7 | const container = document.getElementById('app-container'); 8 | const root = createRoot(container); // createRoot(container!) if you use TypeScript 9 | root.render(); 10 | -------------------------------------------------------------------------------- /src/pages/Options/Options.css: -------------------------------------------------------------------------------- 1 | .OptionsContainer { 2 | width: 100%; 3 | height: 50vh; 4 | font-size: 2rem; 5 | display: flex; 6 | align-items: center; 7 | justify-content: center; 8 | } 9 | -------------------------------------------------------------------------------- /src/pages/Options/Options.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import './Options.css'; 3 | 4 | interface Props { 5 | title: string; 6 | } 7 | 8 | const Options: React.FC = ({ title }: Props) => { 9 | return
{title} Page
; 10 | }; 11 | 12 | export default Options; 13 | -------------------------------------------------------------------------------- /src/pages/Options/index.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lxieyang/chrome-extension-boilerplate-react/f7b0a5b89059ea943eaccd94aad3a429fb744644/src/pages/Options/index.css -------------------------------------------------------------------------------- /src/pages/Options/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Settings 7 | 8 | 9 | 10 |
11 | 12 | 13 | -------------------------------------------------------------------------------- /src/pages/Options/index.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { createRoot } from 'react-dom/client'; 3 | 4 | import Options from './Options'; 5 | import './index.css'; 6 | 7 | const container = document.getElementById('app-container'); 8 | const root = createRoot(container); // createRoot(container!) if you use TypeScript 9 | root.render(); 10 | -------------------------------------------------------------------------------- /src/pages/Panel/Panel.css: -------------------------------------------------------------------------------- 1 | body { 2 | background-color: #242424; 3 | } 4 | 5 | .container { 6 | color: #ffffff; 7 | } -------------------------------------------------------------------------------- /src/pages/Panel/Panel.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import './Panel.css'; 3 | 4 | const Panel: React.FC = () => { 5 | return ( 6 |
7 |

Dev Tools Panel

8 |
9 | ); 10 | }; 11 | 12 | export default Panel; 13 | -------------------------------------------------------------------------------- /src/pages/Panel/index.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lxieyang/chrome-extension-boilerplate-react/f7b0a5b89059ea943eaccd94aad3a429fb744644/src/pages/Panel/index.css -------------------------------------------------------------------------------- /src/pages/Panel/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Dev Tools Panel 7 | 8 | 9 | 10 |
11 | 12 | 13 | -------------------------------------------------------------------------------- /src/pages/Panel/index.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { createRoot } from 'react-dom/client'; 3 | 4 | import Panel from './Panel'; 5 | import './index.css'; 6 | 7 | const container = document.getElementById('app-container'); 8 | const root = createRoot(container); // createRoot(container!) if you use TypeScript 9 | root.render(); 10 | -------------------------------------------------------------------------------- /src/pages/Popup/Popup.css: -------------------------------------------------------------------------------- 1 | .App { 2 | position: absolute; 3 | top: 0px; 4 | bottom: 0px; 5 | left: 0px; 6 | right: 0px; 7 | text-align: center; 8 | height: 100%; 9 | padding: 10px; 10 | background-color: #282c34; 11 | } 12 | 13 | .App-logo { 14 | height: 30vmin; 15 | pointer-events: none; 16 | } 17 | 18 | @media (prefers-reduced-motion: no-preference) { 19 | .App-logo { 20 | animation: App-logo-spin infinite 20s linear; 21 | } 22 | } 23 | 24 | .App-header { 25 | height: 100%; 26 | display: flex; 27 | flex-direction: column; 28 | align-items: center; 29 | justify-content: center; 30 | font-size: calc(10px + 2vmin); 31 | color: white; 32 | } 33 | 34 | .App-link { 35 | color: #61dafb; 36 | } 37 | 38 | @keyframes App-logo-spin { 39 | from { 40 | transform: rotate(0deg); 41 | } 42 | to { 43 | transform: rotate(360deg); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/pages/Popup/Popup.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import logo from '../../assets/img/logo.svg'; 3 | import Greetings from '../../containers/Greetings/Greetings'; 4 | import './Popup.css'; 5 | 6 | const Popup = () => { 7 | return ( 8 |
9 |
10 | logo 11 |

12 | Edit src/pages/Popup/Popup.jsx and save to reload. 13 |

14 | 20 | Learn React! 21 | 22 |
23 |
24 | ); 25 | }; 26 | 27 | export default Popup; 28 | -------------------------------------------------------------------------------- /src/pages/Popup/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | width: 300px; 3 | height: 260px; 4 | margin: 0; 5 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 6 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 7 | sans-serif; 8 | -webkit-font-smoothing: antialiased; 9 | -moz-osx-font-smoothing: grayscale; 10 | 11 | position: relative; 12 | } 13 | 14 | code { 15 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 16 | monospace; 17 | } 18 | -------------------------------------------------------------------------------- /src/pages/Popup/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Popup 7 | 8 | 9 | 10 |
11 | 12 | 13 | -------------------------------------------------------------------------------- /src/pages/Popup/index.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { createRoot } from 'react-dom/client'; 3 | 4 | import Popup from './Popup'; 5 | import './index.css'; 6 | 7 | const container = document.getElementById('app-container'); 8 | const root = createRoot(container); // createRoot(container!) if you use TypeScript 9 | root.render(); 10 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": ["dom", "dom.iterable", "esnext"], 5 | "allowJs": false, 6 | "skipLibCheck": true, 7 | "esModuleInterop": true, 8 | "allowSyntheticDefaultImports": true, 9 | "strict": true, 10 | "forceConsistentCasingInFileNames": true, 11 | "noFallthroughCasesInSwitch": true, 12 | "module": "esnext", 13 | "moduleResolution": "node", 14 | "resolveJsonModule": true, 15 | "noEmit": false, 16 | "jsx": "react" 17 | }, 18 | "include": ["src"], 19 | "exclude": ["build", "node_modules"] 20 | } 21 | -------------------------------------------------------------------------------- /utils/build.js: -------------------------------------------------------------------------------- 1 | // Do this as the first thing so that any code reading it knows the right env. 2 | process.env.BABEL_ENV = 'production'; 3 | process.env.NODE_ENV = 'production'; 4 | process.env.ASSET_PATH = '/'; 5 | 6 | var webpack = require('webpack'), 7 | path = require('path'), 8 | fs = require('fs'), 9 | config = require('../webpack.config'), 10 | ZipPlugin = require('zip-webpack-plugin'); 11 | 12 | delete config.chromeExtensionBoilerplate; 13 | 14 | config.mode = 'production'; 15 | 16 | var packageInfo = JSON.parse(fs.readFileSync('package.json', 'utf-8')); 17 | 18 | config.plugins = (config.plugins || []).concat( 19 | new ZipPlugin({ 20 | filename: `${packageInfo.name}-${packageInfo.version}.zip`, 21 | path: path.join(__dirname, '../', 'zip'), 22 | }) 23 | ); 24 | 25 | webpack(config, function (err) { 26 | if (err) throw err; 27 | }); 28 | -------------------------------------------------------------------------------- /utils/env.js: -------------------------------------------------------------------------------- 1 | // tiny wrapper with default env vars 2 | module.exports = { 3 | NODE_ENV: process.env.NODE_ENV || 'development', 4 | PORT: process.env.PORT || 3000, 5 | }; 6 | -------------------------------------------------------------------------------- /utils/webserver.js: -------------------------------------------------------------------------------- 1 | // Do this as the first thing so that any code reading it knows the right env. 2 | process.env.BABEL_ENV = 'development'; 3 | process.env.NODE_ENV = 'development'; 4 | process.env.ASSET_PATH = '/'; 5 | 6 | var WebpackDevServer = require('webpack-dev-server'), 7 | webpack = require('webpack'), 8 | config = require('../webpack.config'), 9 | env = require('./env'), 10 | path = require('path'); 11 | 12 | var options = config.chromeExtensionBoilerplate || {}; 13 | var excludeEntriesToHotReload = options.notHotReload || []; 14 | 15 | for (var entryName in config.entry) { 16 | if (excludeEntriesToHotReload.indexOf(entryName) === -1) { 17 | config.entry[entryName] = [ 18 | 'webpack/hot/dev-server', 19 | `webpack-dev-server/client?hot=true&hostname=localhost&port=${env.PORT}`, 20 | ].concat(config.entry[entryName]); 21 | } 22 | } 23 | 24 | delete config.chromeExtensionBoilerplate; 25 | 26 | var compiler = webpack(config); 27 | 28 | var server = new WebpackDevServer( 29 | { 30 | https: false, 31 | hot: true, 32 | liveReload: false, 33 | client: { 34 | webSocketTransport: 'sockjs', 35 | }, 36 | webSocketServer: 'sockjs', 37 | host: 'localhost', 38 | port: env.PORT, 39 | static: { 40 | directory: path.join(__dirname, '../build'), 41 | }, 42 | devMiddleware: { 43 | publicPath: `http://localhost:${env.PORT}/`, 44 | writeToDisk: true, 45 | }, 46 | headers: { 47 | 'Access-Control-Allow-Origin': '*', 48 | }, 49 | allowedHosts: 'all', 50 | }, 51 | compiler 52 | ); 53 | 54 | (async () => { 55 | await server.start(); 56 | })(); 57 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | var webpack = require('webpack'), 2 | path = require('path'), 3 | fileSystem = require('fs-extra'), 4 | env = require('./utils/env'), 5 | CopyWebpackPlugin = require('copy-webpack-plugin'), 6 | HtmlWebpackPlugin = require('html-webpack-plugin'), 7 | TerserPlugin = require('terser-webpack-plugin'); 8 | var { CleanWebpackPlugin } = require('clean-webpack-plugin'); 9 | var ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin'); 10 | var ReactRefreshTypeScript = require('react-refresh-typescript'); 11 | 12 | const ASSET_PATH = process.env.ASSET_PATH || '/'; 13 | 14 | var alias = {}; 15 | 16 | // load the secrets 17 | var secretsPath = path.join(__dirname, 'secrets.' + env.NODE_ENV + '.js'); 18 | 19 | var fileExtensions = [ 20 | 'jpg', 21 | 'jpeg', 22 | 'png', 23 | 'gif', 24 | 'eot', 25 | 'otf', 26 | 'svg', 27 | 'ttf', 28 | 'woff', 29 | 'woff2', 30 | ]; 31 | 32 | if (fileSystem.existsSync(secretsPath)) { 33 | alias['secrets'] = secretsPath; 34 | } 35 | 36 | const isDevelopment = process.env.NODE_ENV !== 'production'; 37 | 38 | var options = { 39 | mode: process.env.NODE_ENV || 'development', 40 | entry: { 41 | newtab: path.join(__dirname, 'src', 'pages', 'Newtab', 'index.jsx'), 42 | options: path.join(__dirname, 'src', 'pages', 'Options', 'index.jsx'), 43 | popup: path.join(__dirname, 'src', 'pages', 'Popup', 'index.jsx'), 44 | background: path.join(__dirname, 'src', 'pages', 'Background', 'index.js'), 45 | contentScript: path.join(__dirname, 'src', 'pages', 'Content', 'index.js'), 46 | devtools: path.join(__dirname, 'src', 'pages', 'Devtools', 'index.js'), 47 | panel: path.join(__dirname, 'src', 'pages', 'Panel', 'index.jsx'), 48 | }, 49 | chromeExtensionBoilerplate: { 50 | notHotReload: ['background', 'contentScript', 'devtools'], 51 | }, 52 | output: { 53 | filename: '[name].bundle.js', 54 | path: path.resolve(__dirname, 'build'), 55 | clean: true, 56 | publicPath: ASSET_PATH, 57 | }, 58 | module: { 59 | rules: [ 60 | { 61 | // look for .css or .scss files 62 | test: /\.(css|scss)$/, 63 | // in the `src` directory 64 | use: [ 65 | { 66 | loader: 'style-loader', 67 | }, 68 | { 69 | loader: 'css-loader', 70 | }, 71 | { 72 | loader: 'sass-loader', 73 | options: { 74 | sourceMap: true, 75 | }, 76 | }, 77 | ], 78 | }, 79 | { 80 | test: new RegExp('.(' + fileExtensions.join('|') + ')$'), 81 | type: 'asset/resource', 82 | exclude: /node_modules/, 83 | // loader: 'file-loader', 84 | // options: { 85 | // name: '[name].[ext]', 86 | // }, 87 | }, 88 | { 89 | test: /\.html$/, 90 | loader: 'html-loader', 91 | exclude: /node_modules/, 92 | }, 93 | { 94 | test: /\.(ts|tsx)$/, 95 | exclude: /node_modules/, 96 | use: [ 97 | { 98 | loader: require.resolve('ts-loader'), 99 | options: { 100 | getCustomTransformers: () => ({ 101 | before: [isDevelopment && ReactRefreshTypeScript()].filter( 102 | Boolean 103 | ), 104 | }), 105 | transpileOnly: isDevelopment, 106 | }, 107 | }, 108 | ], 109 | }, 110 | { 111 | test: /\.(js|jsx)$/, 112 | use: [ 113 | { 114 | loader: 'source-map-loader', 115 | }, 116 | { 117 | loader: require.resolve('babel-loader'), 118 | options: { 119 | plugins: [ 120 | isDevelopment && require.resolve('react-refresh/babel'), 121 | ].filter(Boolean), 122 | }, 123 | }, 124 | ], 125 | exclude: /node_modules/, 126 | }, 127 | ], 128 | }, 129 | resolve: { 130 | alias: alias, 131 | extensions: fileExtensions 132 | .map((extension) => '.' + extension) 133 | .concat(['.js', '.jsx', '.ts', '.tsx', '.css']), 134 | }, 135 | plugins: [ 136 | isDevelopment && new ReactRefreshWebpackPlugin(), 137 | new CleanWebpackPlugin({ verbose: false }), 138 | new webpack.ProgressPlugin(), 139 | // expose and write the allowed env vars on the compiled bundle 140 | new webpack.EnvironmentPlugin(['NODE_ENV']), 141 | new CopyWebpackPlugin({ 142 | patterns: [ 143 | { 144 | from: 'src/manifest.json', 145 | to: path.join(__dirname, 'build'), 146 | force: true, 147 | transform: function (content, path) { 148 | // generates the manifest file using the package.json informations 149 | return Buffer.from( 150 | JSON.stringify({ 151 | description: process.env.npm_package_description, 152 | version: process.env.npm_package_version, 153 | ...JSON.parse(content.toString()), 154 | }) 155 | ); 156 | }, 157 | }, 158 | ], 159 | }), 160 | new CopyWebpackPlugin({ 161 | patterns: [ 162 | { 163 | from: 'src/pages/Content/content.styles.css', 164 | to: path.join(__dirname, 'build'), 165 | force: true, 166 | }, 167 | ], 168 | }), 169 | new CopyWebpackPlugin({ 170 | patterns: [ 171 | { 172 | from: 'src/assets/img/icon-128.png', 173 | to: path.join(__dirname, 'build'), 174 | force: true, 175 | }, 176 | ], 177 | }), 178 | new CopyWebpackPlugin({ 179 | patterns: [ 180 | { 181 | from: 'src/assets/img/icon-34.png', 182 | to: path.join(__dirname, 'build'), 183 | force: true, 184 | }, 185 | ], 186 | }), 187 | new HtmlWebpackPlugin({ 188 | template: path.join(__dirname, 'src', 'pages', 'Newtab', 'index.html'), 189 | filename: 'newtab.html', 190 | chunks: ['newtab'], 191 | cache: false, 192 | }), 193 | new HtmlWebpackPlugin({ 194 | template: path.join(__dirname, 'src', 'pages', 'Options', 'index.html'), 195 | filename: 'options.html', 196 | chunks: ['options'], 197 | cache: false, 198 | }), 199 | new HtmlWebpackPlugin({ 200 | template: path.join(__dirname, 'src', 'pages', 'Popup', 'index.html'), 201 | filename: 'popup.html', 202 | chunks: ['popup'], 203 | cache: false, 204 | }), 205 | new HtmlWebpackPlugin({ 206 | template: path.join(__dirname, 'src', 'pages', 'Devtools', 'index.html'), 207 | filename: 'devtools.html', 208 | chunks: ['devtools'], 209 | cache: false, 210 | }), 211 | new HtmlWebpackPlugin({ 212 | template: path.join(__dirname, 'src', 'pages', 'Panel', 'index.html'), 213 | filename: 'panel.html', 214 | chunks: ['panel'], 215 | cache: false, 216 | }), 217 | ].filter(Boolean), 218 | infrastructureLogging: { 219 | level: 'info', 220 | }, 221 | }; 222 | 223 | if (env.NODE_ENV === 'development') { 224 | options.devtool = 'cheap-module-source-map'; 225 | } else { 226 | options.optimization = { 227 | minimize: true, 228 | minimizer: [ 229 | new TerserPlugin({ 230 | extractComments: false, 231 | }), 232 | ], 233 | }; 234 | } 235 | 236 | module.exports = options; 237 | --------------------------------------------------------------------------------