presetter-preset-rollup/source/rollup.ts

224 lines
6.0 KiB
TypeScript
Raw Normal View History

/*
* *** MIT LICENSE ***
* -------------------------------------------------------------------------
* This code may be modified and distributed under the MIT license.
* See the LICENSE file for details.
* -------------------------------------------------------------------------
*
* @summary Collection of helpers for rollup
*
* @author Alvis HT Tang <alvis@hilbert.space>
* @license MIT
* @copyright Copyright (c) 2021 - All Rights Reserved.
* -------------------------------------------------------------------------
*/
import {
isDirective,
isJSON,
merge,
resolveDirective,
template,
} from 'presetter';
import { assertPluginManifest } from './plugin';
import type {
PluginConfiguration,
PluginList,
PluginManifest,
PluginObject,
} from './plugin';
import type {
ApplyDirective,
ImportDirective,
ResolvedPresetContext,
} from 'presetter';
/** preset configuration for rollup */
export interface RollupConfig {
[index: string]: unknown | RollupConfig;
/** list of plugin and its options */
plugins?: PluginManifest | ApplyDirective | ImportDirective;
}
/** genuine configuration that rollup would take, making sure all plugins are a list */
interface TrueRollupConfig {
[index: string]: unknown | TrueRollupConfig;
/** list of plugin and its options */
plugins?: PluginConfiguration[];
}
/** transformed configuration for rollup, with all plugins represented by an object */
interface IntermediateRollupConfig {
[index: string]: unknown | IntermediateRollupConfig;
/** list of plugin and its options */
plugins?: PluginObject;
}
/**
* get template parameters for rollup
* @param context context about the build environment
* @returns template parameter related to rollup
*/
export async function getRollupParameter(
context: ResolvedPresetContext,
): Promise<Record<'rollupImport' | 'rollupExport', string>> {
const { config, variable } = context.custom;
2022-05-15 15:07:02 +00:00
const normalizedConfig = template(
normalizeConfig(transformConfig({ ...config.rollup })),
variable,
);
2022-05-15 15:07:02 +00:00
return generateRollupParameter(normalizedConfig, context);
}
/**
* generate template parameters for rollup
2022-05-15 15:07:02 +00:00
* @param config normalized rollup config
* @param context context about the build environment
* @returns template parameter related to rollup
*/
function generateRollupParameter(
config: TrueRollupConfig,
context: ResolvedPresetContext,
): Record<'rollupImport' | 'rollupExport', string> {
const { importMap, stringifiedConfig } = resolveDirective(config, context);
// generate import statements
const rollupImport = Object.entries(importMap)
.map(([name, resolved]) => `import * as ${resolved} from '${name}';`)
.join('\n');
// generate export statements
const rollupExport = `export default ${stringifiedConfig}`;
return { rollupImport, rollupExport };
}
/**
2022-05-15 15:07:02 +00:00
* normalize rollup config with all plugins represented as a list
* @param config transformed config
* @returns config that rollup would take
*/
2022-05-15 15:07:02 +00:00
function normalizeConfig(config: IntermediateRollupConfig): TrueRollupConfig {
return Object.fromEntries(
Object.entries(config).map(([key, value]): [string, unknown] => {
return [
key,
2022-05-15 15:07:02 +00:00
isDirective(value) ? value : normalizeConfigValue(key, value),
];
}),
);
}
/**
2022-05-15 15:07:02 +00:00
* try to normalize any nested configuration
* @param key field name
* @param value value of a field
2022-05-15 15:07:02 +00:00
* @returns normalized value
*/
2022-05-15 15:07:02 +00:00
function normalizeConfigValue(key: string, value: unknown): unknown {
switch (key) {
case 'plugins':
return [
...Object.entries(value as PluginObject)
.filter(([_, options]) => options !== null)
.map(([plugin, options]) =>
2022-05-15 15:07:02 +00:00
[plugin, normalizeConfigValue(plugin, options)].filter(
(element) => element !== undefined,
),
),
];
default:
return isJSON(value)
2022-05-15 15:07:02 +00:00
? normalizeConfig(value as IntermediateRollupConfig)
: value;
}
}
/**
* transform rollup config with plugins represented by an object for better merging
* @param config rollup config in .presetterrc
* @returns transformed config
*/
function transformConfig(
config: Record<string, any>,
): IntermediateRollupConfig {
return Object.fromEntries(
Object.entries(config).map(([key, value]): [string, unknown] => {
return [
key,
isDirective(value) ? value : transformConfigValue(key, value),
];
}),
);
}
/**
* try to transform any nested configuration
* @param key field name
* @param value value of a field
* @returns transformed value
*/
function transformConfigValue(key: string, value: unknown): unknown {
switch (key) {
case 'plugins':
assertPluginManifest(value);
return objectifyPlugins(value);
default:
return isJSON(value) ? transformConfig(value) : value;
}
}
/**
* objectify rollup plugins
* @param plugins rollup plugin config
2022-05-15 15:07:02 +00:00
* @returns normalized plugin config
*/
function objectifyPlugins(
plugins: PluginManifest,
): IntermediateRollupConfig['plugins'] {
2022-05-15 15:07:02 +00:00
const normalizedPlugin: PluginObject = {};
const pluginList: PluginConfiguration[] = Array.isArray(plugins)
? arrayToPluginConfiguration(plugins)
: objectToPluginConfiguration(plugins);
for (const [name, options] of pluginList) {
Object.assign(
2022-05-15 15:07:02 +00:00
normalizedPlugin,
merge(normalizedPlugin, { [name]: options }),
);
}
2022-05-15 15:07:02 +00:00
return normalizedPlugin;
}
/**
2022-05-15 15:07:02 +00:00
* normalize rollup plugin config in array form
* @param plugins rollup plugin config in array form
2022-05-15 15:07:02 +00:00
* @returns normalized plugin config
*/
function arrayToPluginConfiguration(
plugins: PluginList,
): PluginConfiguration[] {
return plugins.map((plugin) =>
typeof plugin === 'string' ? [plugin] : plugin,
);
}
/**
2022-05-15 15:07:02 +00:00
* normalize rollup plugin config in object form
* @param plugins rollup plugin config in object form
2022-05-15 15:07:02 +00:00
* @returns normalized plugin config
*/
function objectToPluginConfiguration(
plugins: PluginObject,
): PluginConfiguration[] {
return [...Object.entries(plugins)];
}