plugins_rollup.js

/**
 * @file plugins/rollup.js
 * @copyright @spmhome @_2025
 * @author Scott Meesseman @spmeesseman
 *//** */

const { join } = require("path");
const WpwTaskPlugin = require("./basetask");
const { apply, asArray } = require("@spmhome/type-utils");


/**
 * @augments WpwTaskPlugin
 */
class WpwRollupPlugin extends WpwTaskPlugin
{
    /**
     * @param {WpwPluginOptions} options
     */
    constructor(options)
    {
        super("rollup", /** @type {WpwTaskPluginOptions<any, any, true>} */(
            apply({}, { injectEntry: false, taskHandler: "executeRollupBuild", taskStage: "ADDITIONS" }, options))
        );
        this.buildOptions = /** @type {WpwBuildOptionsPluginConfig<"rollup">} */(this.buildOptions);
    }


    /**
     * @override
     * @param {WpwBuild} build
     */
    static create = WpwRollupPlugin.wrap.bind(this);


	/**
	 * @param {WebpackCompilationAssets} _assets
     * @returns {Promise<WpwPluginTaskResult | void | SpmhError>}
	 */
    async executeRollupBuild(_assets)
    {
        try
        {   const assetInfo = {}; // this.buildOptions.assets || {};
            const args = [];
            args.push(this.buildOptions.entry, "--file", join(this.build.virtualEntry.dirBuild, "index.js"));
            if (this.buildOptions.format) {
                args.push("--format", this.buildOptions.format);
            }
            if (this.buildOptions.external) {
                args.push("--external", this.buildOptions.external.join(","));
            }
            args.push(...asArray(this.buildOptions.args));

            const defs = /** @type {WpwPluginConfigRunScripts} */({
                enabled: true,
                mode: "inline",
                timeout: 45000,
                assets:
                {   tag: "rollup",
                    immutable: false,
                    glob: "**/*.{c,m,}js",
                    paths: join(this.build.virtualEntry.dirBuild, this.buildOptions.entry)
                },
                items: [{
                    command: "rollup",
                    args: [ args.shift(), ...args ]
                }]
             });

            await this.execScriptsAsync(apply({}, defs, this.buildOptions), "");
            return {
                glob: assetInfo.glob || "*.*",
                immutable: !!assetInfo.immutable,
                tag: assetInfo.tag || this.optionsKey,
                srcpaths: asArray([]),
                paths: asArray([])
            };
        }
        catch (e)
        {   this.addMessage(
            {   exception: e,
                code: this.MsgCode.ERROR_PLUGIN_HOOK_FAILED,
                message: "top level error, see exception info for any detail"
            }, true);
        }
    };
}


module.exports = WpwRollupPlugin.create;