|
| 1 | +# Configuration |
| 2 | + |
| 3 | +Learn about all configuration options available in `rsbuild-plugin-web-extension`. |
| 4 | + |
| 5 | +## Plugin Options |
| 6 | + |
| 7 | +The plugin accepts a configuration object with the following options: |
| 8 | + |
| 9 | +```typescript |
| 10 | +interface Options { |
| 11 | + manifest: chrome.runtime.ManifestV3; |
| 12 | +} |
| 13 | +``` |
| 14 | + |
| 15 | +### manifest |
| 16 | + |
| 17 | +- **Type**: `chrome.runtime.ManifestV3` |
| 18 | +- **Required**: `true` |
| 19 | + |
| 20 | +The Chrome Extension Manifest V3 configuration object. This will be processed and written to `dist/manifest.json` during build. |
| 21 | + |
| 22 | +```typescript |
| 23 | +import { pluginWebExtension } from "rsbuild-plugin-web-extension"; |
| 24 | + |
| 25 | +export default defineConfig({ |
| 26 | + plugins: [ |
| 27 | + pluginWebExtension({ |
| 28 | + manifest: { |
| 29 | + manifest_version: 3, |
| 30 | + name: "My Extension", |
| 31 | + version: "1.0.0", |
| 32 | + // ... other manifest properties |
| 33 | + }, |
| 34 | + }), |
| 35 | + ], |
| 36 | +}); |
| 37 | +``` |
| 38 | + |
| 39 | +## Automatic Configurations |
| 40 | + |
| 41 | +The plugin automatically configures several Rsbuild settings to optimize web extension development: |
| 42 | + |
| 43 | +### Entry Points |
| 44 | + |
| 45 | +Entry points are automatically derived from your manifest configuration: |
| 46 | + |
| 47 | +- **popup**: From `manifest.action.default_popup` |
| 48 | +- **devtools**: From `manifest.devtools_page` |
| 49 | +- **newtab**: From `manifest.chrome_url_overrides.newtab` |
| 50 | +- **options**: From `manifest.options_ui.page` |
| 51 | +- **background**: From `manifest.background.service_worker` |
| 52 | + |
| 53 | +### Output Configuration |
| 54 | + |
| 55 | +```typescript |
| 56 | +{ |
| 57 | + output: { |
| 58 | + filenameHash: false, // Disable hashing for extension compatibility |
| 59 | + filename: { |
| 60 | + js: "index.js", // Consistent filename |
| 61 | + }, |
| 62 | + distPath: { |
| 63 | + js: "src/[name]", // Organize by entry name |
| 64 | + }, |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +### Development Server |
| 70 | + |
| 71 | +```typescript |
| 72 | +{ |
| 73 | + server: { |
| 74 | + port: 3130, // Default development port |
| 75 | + }, |
| 76 | + dev: { |
| 77 | + client: { |
| 78 | + port: 3130, |
| 79 | + host: "localhost", |
| 80 | + }, |
| 81 | + writeToDisk: true, // Required for extension loading |
| 82 | + }, |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +### Hot Module Replacement |
| 87 | + |
| 88 | +```typescript |
| 89 | +{ |
| 90 | + output: { |
| 91 | + // HMR files location |
| 92 | + hotUpdateChunkFilename: "hot/[id].[fullhash].hot-update.js", |
| 93 | + hotUpdateMainFilename: "hot/[runtime].[fullhash].hot-update.json", |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +## Environment Variables |
| 99 | + |
| 100 | +### PORT |
| 101 | + |
| 102 | +Change the development server port: |
| 103 | + |
| 104 | +```bash |
| 105 | +PORT=8080 npm run dev |
| 106 | +``` |
| 107 | + |
| 108 | +### **FIREFOX** |
| 109 | + |
| 110 | +Enable Firefox-specific manifest transformations: |
| 111 | + |
| 112 | +```bash |
| 113 | +__FIREFOX__=true npm run build |
| 114 | +``` |
| 115 | + |
| 116 | +This will: |
| 117 | + |
| 118 | +- Convert `background.service_worker` to `background.scripts` |
| 119 | +- Add Firefox-compatible `content_security_policy` |
| 120 | +- Handle `options_page` vs `options_ui` differences |
| 121 | + |
| 122 | +## File Processing |
| 123 | + |
| 124 | +### Automatic Extension Transformation |
| 125 | + |
| 126 | +The plugin automatically transforms file extensions in the manifest: |
| 127 | + |
| 128 | +- `.tsx`, `.jsx`, `.ts` → `.js` |
| 129 | +- `.scss`, `.sass`, `.stylus` → `.css` |
| 130 | + |
| 131 | +```typescript |
| 132 | +// In manifest.ts |
| 133 | +const manifest = { |
| 134 | + action: { |
| 135 | + default_popup: "./src/popup/index.html", // References popup.tsx |
| 136 | + }, |
| 137 | + background: { |
| 138 | + service_worker: "./src/background/index.ts", // Will become index.js |
| 139 | + }, |
| 140 | +}; |
| 141 | +``` |
| 142 | + |
| 143 | +### HTML Entry Processing |
| 144 | + |
| 145 | +For each HTML file in your manifest, the plugin: |
| 146 | + |
| 147 | +1. Creates an Rsbuild entry point |
| 148 | +2. Configures HTMLPlugin to process the template |
| 149 | +3. Converts `.html` references to their corresponding `.tsx`/`.jsx` files |
| 150 | + |
| 151 | +## Integration with Other Plugins |
| 152 | + |
| 153 | +### React |
| 154 | + |
| 155 | +```typescript |
| 156 | +import { pluginReact } from "@rsbuild/plugin-react"; |
| 157 | +import { pluginWebExtension } from "rsbuild-plugin-web-extension"; |
| 158 | + |
| 159 | +export default defineConfig({ |
| 160 | + plugins: [pluginReact(), pluginWebExtension({ manifest })], |
| 161 | +}); |
| 162 | +``` |
| 163 | + |
| 164 | +### Vue |
| 165 | + |
| 166 | +```typescript |
| 167 | +import { pluginVue } from "@rsbuild/plugin-vue"; |
| 168 | +import { pluginWebExtension } from "rsbuild-plugin-web-extension"; |
| 169 | + |
| 170 | +export default defineConfig({ |
| 171 | + plugins: [pluginVue(), pluginWebExtension({ manifest })], |
| 172 | +}); |
| 173 | +``` |
| 174 | + |
| 175 | +### TypeScript |
| 176 | + |
| 177 | +TypeScript support is enabled by default. Make sure to install Chrome types: |
| 178 | + |
| 179 | +```bash |
| 180 | +npm install -D @types/chrome |
| 181 | +``` |
| 182 | + |
| 183 | +## Custom Rsbuild Configuration |
| 184 | + |
| 185 | +You can still customize Rsbuild configuration alongside the plugin: |
| 186 | + |
| 187 | +```typescript |
| 188 | +export default defineConfig({ |
| 189 | + plugins: [pluginWebExtension({ manifest })], |
| 190 | + // Additional Rsbuild configuration |
| 191 | + source: { |
| 192 | + alias: { |
| 193 | + "@": "./src", |
| 194 | + }, |
| 195 | + }, |
| 196 | + tools: { |
| 197 | + postcss: { |
| 198 | + plugins: [ |
| 199 | + // Your PostCSS plugins |
| 200 | + ], |
| 201 | + }, |
| 202 | + }, |
| 203 | +}); |
| 204 | +``` |
| 205 | + |
| 206 | +## Troubleshooting |
| 207 | + |
| 208 | +### Common Issues |
| 209 | + |
| 210 | +**HMR not working in extension pages** |
| 211 | + |
| 212 | +Make sure `dev.writeToDisk` is `true` (this is set automatically by the plugin). |
| 213 | + |
| 214 | +**Extension not loading after changes** |
| 215 | + |
| 216 | +1. Check the browser's extension management page for errors |
| 217 | +2. Reload the extension manually |
| 218 | +3. Verify the `dist/manifest.json` is being generated correctly |
| 219 | + |
| 220 | +**TypeScript errors with Chrome APIs** |
| 221 | + |
| 222 | +Install Chrome types: |
| 223 | + |
| 224 | +```bash |
| 225 | +npm install -D @types/chrome |
| 226 | +``` |
| 227 | + |
| 228 | +Add to your `tsconfig.json`: |
| 229 | + |
| 230 | +```json |
| 231 | +{ |
| 232 | + "compilerOptions": { |
| 233 | + "types": ["chrome"] |
| 234 | + } |
| 235 | +} |
| 236 | +``` |
0 commit comments