In the Kopytko packager config file (.kopytkorc) plugins need to be defined first to be able to use it later, unless it's a kopytko-packager pre-built plugin.
Define a plugin in a pluginDefinitions
config property, an object with a plugin name as a key and plugin function file path as a value.
The path will be added to the root directory path due to resolve this dependency (<project_root_directory><plugin_path>
).
{
...
"pluginDefinitions": {
"my-plugin": "/my-plugins/my-plugin/index.js"
},
...
}
If a plugin should be executed each time the build process run, add its name to the plugins
array property.
Custom plugin names have to be defined in the pluginDefinitions
property.
{
...
"plugins": ["my-global-plugin"],
...
}
If the plugin should be executed only on a specific environment, define it in the environment's plugins
config property.
Custom plugin names have to be defined in the pluginDefinitions
property.
{
...
"environments": {
"production": {
"plugins": ["my-custom-plugin", "my-production-plugin"]
},
"staging": {
"plugins": ["my-custom-plugin"]
},
"test": {
"plugins": ["my-test-plugin"]
}
}
...
}
The plugins execute in the following order:
- All plugins defined in the global
plugins
array, that are objects containing preEnvironmentPlugin with valuetrue
, according to the order of the array. - All plugins defined in the environment
plugins
array, that are strings or objects containing postGlobalPlugin with valuefalse
, according to the order of the array. - All plugins defined in the global
plugins
array, that are strings or objects containing preEnvironmentPlugin with valuefalse
, according to the order of the array. - All plugins defined in the environment
plugins
array, that are objects containing postGlobalPlugin with valuetrue
, according to the order of the array. - All kopytko-packager built-in plugins. For now, it is only a plugin generating the manifest file.
Example:
{
...
"plugins": [
{ "name": "plugin-1", "preEnvironmentPlugin": true },
{ "name": "plugin-4", "preEnvironmentPlugin": false },
"plugin-5"
],
...
"environments": {
"production": {
"plugins": [
"plugin-2",
{ "name": "plugin-3", "postGlobalPlugin": false },
{ "name": "plugin-6", "postGlobalPlugin": true }
]
}
}
...
}
Plugins from the example will execute in following order:
- "plugin-1".
- "plugin-2".
- "plugin-3".
- "plugin-4".
- "plugin-5".
- "plugin-6".
- Plugin has to be a function.
- Plugin function arguments:
- The first argument is a temporary directory path. That directory contains all app files as they are in the source directory.
- The second argument is an environment string passed as a build/start/test script argument or ENV value from the .env file. If no environment was passed, then it is an empty string.
example plugin code:
const glob = require('glob-promise');
const path = require('path');
module.exports = async function myPlugin(tempDir, env) {
if (env === 'production') {
const xmlFilePaths = await glob(path.join(tempDir, '/components/**/*.xml'));
// do something with all XML files in your project during the package build process only for the production environment
}
}