+++ title = "Legacy code style guide" aliases = ["/docs/grafana/latest/plugins/developing/code-styleguide/"] +++ # Legacy code style guide This guide has two parts. The first part describes the metadata and the second part is a styleguide for HTML/CSS and JavaScript in Grafana plugins and applies if you are using ES6 in your plugin. If using TypeScript then the [Angular TypeScript styleguide](https://angular.io/styleguide) is recommended. ## Metadata The plugin metadata consists of a plugin.json file and the README.md file. These two files are used by Grafana and Grafana.com. ### Plugin.json (mandatory) The plugin.json file is the same concept as the package.json file for an npm package. When Grafana starts it will scan the plugin folders and mount every folder that contains a plugin.json file unless the folder contains a subfolder named `dist`. In that case grafana will mount the `dist` folder instead. The most important fields are the first three, especially the id. The convention for the plugin id is **[github username/org]-[plugin name]-[datasource|app|panel]** and it has to be unique. Examples: ```bash raintank-worldping-app grafana-simple-json-datasource grafana-piechart-panel mtanda-histogram-panel ``` For more information about the file format for `plugin.json` file, refer to [metadata]({{< relref "../metadata.md" >}}). Minimal plugin.json: ```javascript { "type": "panel", "name": "Clock", "id": "yourorg-clock-panel", "info": { "description": "Clock panel for grafana", "author": { "name": "Grafana Labs", "url": "https://grafana.com" }, "keywords": ["clock", "panel"], "version": "1.0.0", "updated": "2015-03-24" }, "dependencies": { "grafanaVersion": "3.x.x", "plugins": [ ] } } ``` ### README.md The README.md file is rendered both in the grafana.com plugins page, and within the Grafana application. The only difference from how GitHub renders markdown is that html is not allowed. ## File and Directory Structure Conventions Here is a typical directory structure for a plugin. ```bash johnnyb-awesome-datasource |-- dist |-- spec | |-- datasource_spec.js | |-- query_ctrl_spec.js | |-- test-main.js |-- src | |-- img | | |-- logo.svg | |-- partials | | |-- annotations.editor.html | | |-- config.html | | |-- query.editor.html | |-- datasource.js | |-- module.js | |-- plugin.json | |-- query_ctrl.js |-- Gruntfile.js |-- LICENSE |-- package.json |-- README.md ``` Most JavaScript projects have a build step and most Grafana plugins are built using Babel and ES6. The generated JavaScript should be placed in the `dist` directory and the source code in the `src` directory. We recommend that the plugin.json file be placed in the src directory and then copied over to the dist directory when building. The `README.md` can be placed in the root or in the dist directory. Directories: - `src/` contains plugin source files. - `src/partials` contains html templates. - `src/img` contains plugin logos and other images. - `spec/` contains tests (optional). - `dist/` contains built content. ## HTML and CSS For the HTML on editor tabs, we recommend using the inbuilt Grafana styles rather than defining your own. This makes plugins feel like a more natural part of Grafana. If done correctly, the html will also be responsive and adapt to smaller screens. The `gf-form` css classes should be used for labels and inputs. Below is a minimal example of an editor row with one form group and two fields, a dropdown and a text input: ```html