REGARDS Frontend Plugins
Presentation
REGARDS UI plugins are pieces of Javascript files bundled together that can be added dynamically into the user interface. They allow administrators creating dedicated UI parts to match project needs. There are two types of plugins:
criterion plugins, that are used in the search form of search-results module (more details here)
A search form example, with several criteria plugins
service plugins, that adds functionalities to an entity or a selection of entities (more detail here).
Some services example: on lines, services for one entity. On header, services for current selection.
The following sections describe common considerations and methods for all plugins.
Main principles
Each plugin:
- Is defined as an independent NPM package, in plugins folder
- Is built as a small bundle, independent of the main application
- Exposes a component view, with optional styles, messages and reducers
- Exposes a definition, allowing REGARDS interfaces to create automated configuration forms
- Is provided through a server, outside main application source code. When in develop mode, that server may be
webpack-dev-server. In production mode, it could in any public repository, or in default REGARDSplugins repository.
On a development point of view, plugins are very similar to REGARDS user modules, as they expose a view, reducers, styles and messages. However, they do not define any administration view, which is built automatically based on plugin-info.json content.
Create a new plugin
All plugin types can be created using the frontend plugin generator in webapp/yeoman/generator-regards-ui-plugin. To install and use the generator, run the following commands, in rs-frontend folder:
$ cd webapp/yeoman/generator-regards-ui-plugin
$ npm install -g
$ npm install -g yo
$ cd ../../plugins
$ yo regards-ui-plugin
Yeoman will ask you several parameters to generate the new plugin, depending on the chosen type. Once finished, the plugin folder is initialized with a basic example.
Plugin folder structure
The following folder structure is generated by Yeoman. It matches REGARDS standard NPM packages structure:
- node_modules: Installed node modules from package JSON
- reports: Built reports for coverage and tests
- src: Plugin main source folder
- clients: Plugin redux API clients
- components: Plugin React components
- containers: Plugin React containers
- i18n: Plugin internationalization
- styles: Plugin graphic styles
- main.js: Plugin exported index
- plugin-info.js: Plugin definition
- reducer.js: Redux reducers builder
- target: Built sources for plugins
- tests: Plugin tests source folder
- package.json: Npm module file describing plugin module
- README.md: Plugin README
- webpack.<mode>.js: Webpack files for plugin building
Note: node_modules, reports and target folders may not be present initially, as they are build folders. Some other folders or files, like clients or reducer.js, are not initially created. They should be added by developer when required.
Commands
Plugin package.json file holds NPM plugin commands to:
- install command (standard for any NPM package)
- compile plugin
- run plugin test
- compute plugin report coverage
- lint plugin code
In order to use those commands, rs-frontend repository must have been installed (see Frontend setup page), as it provides the core webpack DLLs, used to lower compilation duration and plugin bundle size.
1. Install plugin
Before running any other command, plugin should be installed, using command:
myPlugin:$ npm i
2. Compile plugin
The following commands compile the plugin into a usable plugin bundle:
myPlugin:$ npm run build:dev
myPlugin:$ npm run build:watch
myPlugin:$ npm run build:production
Command modes:
build:dev: Builds the bundle once for development - main application webpack dev server must be running.build:watch: Watches for code changes. Triggersbuild:devon detectionbuild:production: Builds the bundle once for production - build result can be pasted on definitive location.
3. Tests commands
The following commands run any mocha test in files matching patterns *.test.js or *.test.jsx, and stored in /tests folder:
myPlugin:$ npm run test
myPlugin:$ npm run test:watch
myPlugin:$ npm run test:coverage
Command modes:
testortest:mocha: Runs all tests once and shows tests resultstest:watch: Watches for code changes. Triggerstest:mochaon detectiontest:coverage: Runs tests and creates a coverage report
4. Lint source code
The following commands check lint rules expectations in src and tests folders, then display a report:
myPlugin:$ npm run lint
myPlugin:$ npm run lint:fix
Command options:
lint:fix: Attempts to correct issues.
5. Plugins group commands
Additionally, some commands are provided in rs-frontend/webapp/scripts folder.
webapp:$ ./scripts/build-all-plugins dev criterion
webapp:$ ./scripts/test-all-plugins services
webapp:$ ./scripts/lint-all-plugins all
webapp:$ ./scripts/compareDependencies.js ./package.json ./plugins/criterion/string/package.json
- build-all-plugins: Builds all plugins found in
rs-frontend/webapp/plugins(can be restricted with parameters) - test-all-plugins: Tests all plugins found in
rs-frontend/webapp/plugins(can be restricted with parameters) - lint-all-plugins: Lints all plugins found in
rs-frontend/webapp/plugins(can be restricted with parameters) - compareDependencies: Compares dependencies of the first and second package.json files as parameters (helps upgrading plugin dependencies versions when core dependencies changed)
Deploy plugin
To use a plugin, the following requirements must be met:
- The plugin bundle must be available. The method to use depends on development phase (see sections hereafter)
- The plugin must be declared. That declaration can be performed through REGARDS project administration interface, like shown in the picture below (User interface / Plugins)
Declaring the plugin "example-criterion"
For development
The development build commands appends plugin into the running instance of webpack-dev-server, under the path /plugins/{pluginTypeFolder}/{pluginName}/plugin.js. That means main application build:watch command must be running.
Once build command has been performed, the plugin can be declared, like any other plugin (see parent section)