# webextension-manifest-loader [![npm version](https://img.shields.io/npm/v/webextension-manifest-loader.svg)](https://www.npmjs.com/package/webextension-manifest-loader) [![node version](https://img.shields.io/node/v/webextension-manifest-loader.svg)](https://www.npmjs.com/package/webextension-manifest-loader) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier) Webpack loader that loads browser tailored manifest.json. It also imports all importable properties, allowing you to have 'manifest.json' as your only webpack entry point. **Use it together with [inert-entry-webpack-plugin][inert], [spawn-loader][spawn], and maybe [html-loader][html], more info below.** ## Table of Contents - [Getting Started](#getting-started) - [webpack.config.js](#webpackconfigjs) - [Options](#options) - [Example](#example) - [Configuration](#configuration) - [webpack.config.js (configuration)](#webpackconfigjs-configuration) - [manifest.json (configuration)](#manifestjson-configuration) - [Output if targetVendor == 'chrome'](#output-if-targetvendor--chrome) - [manifest.json (output)](#manifestjson-output) - [FAQ](#faq) - [Which properties does it currently support?](#which-properties-does-it-currently-support) - [Are there any known limitation or problems?](#are-there-any-known-limitation-or-problems) - [Related and thanks](#related-and-thanks) ## Getting Started To begin, you'll need to install webextension-manifest-loader: ``` npm install --save-dev webextension-manifest-loader ``` Then add the loader to your webpack config. For example: ### webpack.config.js ```javascript module.exports = { module: { rules: [ { // Prevent json-loader from loading the file type: 'javascript/auto', test: /manifest\.json$/, use: [ // webextension-manifest-loader returns a function for importing in a .js file, // convert it to a plain string and resolve imports using extract-loader 'extract-loader', { loader: 'webextension-manifest-loader', options: { // Use the environment variable TARGET_VENDOR as the target vendor targetVendor: process.env.TARGET_VENDOR, // Merge some properties into the finished manifest.json merge: { // This could easily come from an import of your package.json version: '1.3.0', }, }, }, ], }, ], }, }; ``` ## Options | Name | Type | Default | Description | | :----------------: | :------: | :---------: | :---------------------------------------------------------------------------- | | **`targetVendor`** | `string` | `undefined` | Specify the target vendor, should be one of `firefox`,`chrome`,`edge`,`opera` | | **`merge`** | `Object` | `{}` | Allows merging properties into the finished manifest.json | ## Example See [jsmnbom/ao3-enhancements](https://github.com/jsmnbom/ao3-enhancements) ([manifest.json](https://github.com/jsmnbom/ao3-enhancements/blob/main/src/manifest.json), [webpack.config.ts](https://github.com/jsmnbom/ao3-enhancements/blob/main/webpack.config.ts)) for a fully featured example of using this loader together with typescript, vue (+vuetify) and pug. ### Configuration **Make sure you're using [inert-entry-webpack-plugin][inert], otherwise webpack will try to output manifest.json as a javascript file, and that will obviously fail!** #### webpack.config.js (configuration) ```javascript module.exports = { entry: { manifest: './manifest.json', }, module: { rules: [ { // Prevent json-loader from loading the file type: 'javascript/auto', test: /manifest\.json$/, use: [ // webextension-manifest-loader returns a function for importing in a .js file, // convert it to a plain string and resolve imports using extract-loader 'extract-loader', { loader: 'webextension-manifest-loader', options: { // Use the environment variable TARGET_VENDOR as the target vendor targetVendor: process.env.TARGET_VENDOR, // Merge some properties into the finished manifest.json merge: { // This could easily come from an import of your package.json version: '1.3.0', }, }, }, ], }, { test: /\.html$/, use: [ 'file-loader?name=[path][name].html', 'extract-loader', 'html-loader', ], }, ], }, plugins: [new InertEntryPlugin()], }; ``` #### manifest.json (configuration) Now you'll be able to specify vendors in your manifest.json keys and to import files. ```jsonc { "manifest_version": 2, "version": "", "__firefox_icons__": { "48": "./icon.svg", "96": "./icon.svg" }, "__chrome_icons__": { "48": "./icon-48.png", "96": "./icon-96.png" }, "options_ui": { "page": "~spawn-loader?name=options_ui/index.html!./options_ui/index.html", "__firefox_browser_style__": false, "__chrome_chrome_style__": false }, "content_scripts": [ { "matches": [...], "__firefox_js__": [ "~spawn-loader?name=content_script/index.js!./content_script/index.js" ], "__chrome_js__": [ "~file-loader?name=[name].[ext]!webextension-polyfill/dist/browser-polyfill.min.js", "~spawn-loader?name=content_script/index.js!./content_script/index.js" ], "css": ["./content_script/style.css"] } ] } ``` Here we import `.svg` if compiling for firefox, but `.png`s if compiling for chrome since chrome doesn't support svgs in the icons property. We also add the [webextension-polyfill](https://github.com/mozilla/webextension-polyfill) on chrome but not on firefox as it's not needed there. Use a loader like [spawn-loader][spawn] to add a new entry point. Remember to add a tilde in front of the loader or it will be resolved as a relative path. ### Output if targetVendor == 'chrome' Here's the output of the above configuration when run through webpack. And because we are using [html-loader][html] which can also resolve imports in `