src/services/configurations/nodeProductionConfiguration.js
const resolve = require('@rollup/plugin-node-resolve');
const babel = require('rollup-plugin-babel');
const commonjs = require('@rollup/plugin-commonjs');
const sass = require('rollup-plugin-sass');
const html = require('rollup-plugin-html');
const json = require('@rollup/plugin-json');
const visualizer = require('rollup-plugin-visualizer');
const { provider } = require('jimple');
const ConfigurationFile = require('../../abstracts/configurationFile');
const {
copy,
css,
extraWatch,
moduleReplace,
stats,
stylesheetAssets,
urls,
} = require('../../plugins');
/**
* Creates the specifics of a Rollup configuration for a Node target production build.
* @extends {ConfigurationFile}
*/
class RollupNodeProductionConfiguration extends ConfigurationFile {
/**
* @param {Events} events
* To reduce the configuration.
* @param {PathUtils} pathUtils
* Required by `ConfigurationFile` in order to build the path to the overwrite file.
* @param {RollupPluginSettingsConfiguration} rollupPluginSettingsConfiguration
* To get the plugin settings.
*/
constructor(
events,
pathUtils,
rollupPluginSettingsConfiguration
) {
super(pathUtils, [
'config/rollup/node.production.config.js',
'config/rollup/node.config.js',
]);
/**
* A local reference for the `events` service.
* @type {Events}
*/
this.events = events;
/**
* A local reference for the `rollupPluginSettingsConfiguration` service.
* @type {RollupPluginSettingsConfiguration}
*/
this.rollupPluginSettingsConfiguration = rollupPluginSettingsConfiguration;
}
/**
* Creates the complete configuration for a Node target production build.
* This method uses the reducer events `rollup-node-production-configuration` and
* `rollup-node-configuration`. It sends the configuration, the received `params` and
* expects a configuration on return.
* @param {RollupConfigurationParams} params A dictionary generated by the top service building
* the configuration and that includes things like the
* target information, its entry settings, output
* paths, etc.
* @return {Object}
*/
createConfig(params) {
const {
input,
output,
target,
analyze,
} = params;
// Create the `stats` plugin instance.
const statsPlugin = stats({
path: `${target.paths.build}/`,
});
// Get the plugins settings.
const pluginSettings = this.rollupPluginSettingsConfiguration.getConfig(
params,
statsPlugin.add
);
// Define the plugins list.
const plugins = [
statsPlugin.reset(),
resolve(pluginSettings.resolve),
commonjs(pluginSettings.commonjs),
babel(pluginSettings.babel),
...(
pluginSettings.moduleReplace.instructions.length ?
[moduleReplace(pluginSettings.moduleReplace)] :
[]
),
extraWatch(pluginSettings.extraWatch),
sass(pluginSettings.sass),
css(pluginSettings.css),
stylesheetAssets.helper(pluginSettings.stylesheetAssetsHelper),
stylesheetAssets(pluginSettings.stylesheetAssets),
html(pluginSettings.html),
json(pluginSettings.json),
urls(pluginSettings.urls),
copy(pluginSettings.copy),
statsPlugin.log(pluginSettings.statsLog),
...(
analyze ?
[visualizer(pluginSettings.visualizer)] :
[]
),
];
// Get the list of external dependencies.
const { external } = pluginSettings.external;
/**
* Merge the globals settings with any globals that may have been defined on the `output`
* object by the parent configuration.
*/
const globals = output.globals ?
Object.assign({}, output.globals, pluginSettings.globals) :
pluginSettings.globals;
// Define the configuration object.
const config = {
input,
output: Object.assign({}, output, { globals }),
plugins,
external,
};
// If the watch mode is enabled, add the watch settings.
if (target.watch.production) {
config.watch = pluginSettings.watch;
}
// Return the reduced configuration.
return this.events.reduce(
[
'rollup-node-production-configuration',
'rollup-node-configuration',
],
config,
params
);
}
}
/**
* The service provider that once registered on the app container will set an instance of
* `RollupNodeProductionConfiguration` as the `rollupNodeProductionConfiguration` service.
* @example
* // Register it on the container
* container.register(rollupNodeProductionConfiguration);
* // Getting access to the service instance
* const rollupNodeProdConfig = container.get('rollupNodeProductionConfiguration');
* @type {Provider}
*/
const rollupNodeProductionConfiguration = provider((app) => {
app.set('rollupNodeProductionConfiguration', () => new RollupNodeProductionConfiguration(
app.get('events'),
app.get('pathUtils'),
app.get('rollupPluginSettingsConfiguration')
));
});
module.exports = {
RollupNodeProductionConfiguration,
rollupNodeProductionConfiguration,
};