Skip to content

Instantly share code, notes, and snippets.

@frank-dspeed
Last active March 11, 2022 09:06
Show Gist options
  • Select an option

  • Save frank-dspeed/e2293d2f98afc2c73b25455ec23a6784 to your computer and use it in GitHub Desktop.

Select an option

Save frank-dspeed/e2293d2f98afc2c73b25455ec23a6784 to your computer and use it in GitHub Desktop.
I got the case that i needed to run something after rollup -c without package.json see: https://github.com/rollup/rollup/issues/4441
import { rollup } from 'rollup';
/** @typedef {rollup.RollupOptions} RollupOptions */
const parseOptions = async (/** @type {RollupOptions[]} */ rollupOptions, commandOptions = { external: [], globals: {} }) => {
const loadConfigFile_js = await import('rollup/dist/shared/loadConfigFile.js');
const mergeOptions = await import('rollup/dist/shared/mergeOptions.js');
const warnings = loadConfigFile_js.batchWarnings();
try {
/** @type {RollupOptions[]} */
const normalizedOptions = [];
for (const config of rollupOptions) {
const options = mergeOptions.mergeOptions(config, commandOptions, warnings.add);
// command line plugins are ignored
normalizedOptions.push(options);
return { normalizedOptions, warnings };
}
} catch (err) {
warnings.flush();
throw err;
}
}
/**
* load the config file next to the current script;
* the commandOptions = { format: es } object has the same effect as passing "--format es" command line plugins are ignored
* on the command line and will override the format of all outputs
*/
const getNormalizedOptions = async (options, commandOptions = { external: [], globals: {} }) => {
const rollupOptions = /** @type {RollupOptions[]} */ (await options) || (await import('./rollup.config.js')).default;
// const { options, warnings } = await loadConfigFile_js.loadAndParseConfigFile(rollupConfigPath, { format: 'es' })
// const normalizedOptions = /** @type {RollupOptions[]} */ (options)
const { normalizedOptions, warnings } = await parseOptions(rollupOptions, commandOptions)
// "warnings" wraps the default `onwarn` handler passed by the CLI.
// This prints all warnings up to this point:
console.log(`We currently have ${warnings.count} warnings`);
// This prints all deferred warnings
warnings.flush();
return normalizedOptions;
}
const rollupC = async (options) => {
const rollupOptions = await getNormalizedOptions(options); // getConfigs();
// options is an array of "inputOptions" objects with an additional "output"
// property that contains an array of "outputOptions".
// The following will generate all outputs for all inputs, and write them to disk the same
// way the CLI does it:
for (const optionsObj of rollupOptions) {
const bundle = await rollup(optionsObj);
await Promise.all(optionsObj.output.map(bundle.write));
bundle.close();
}
// You can also pass this directly to "rollup.watch"
// rollup.watch(rollupOptions);
}
export const buildConfigs = (rollupConfigs, then) =>
({ input: "", plugins: [{ async options() {
await rollupC(rollupConfigs).then(then);
process.exit(0)
}}]})
export default buildConfigs;
import {buildConfigs} from './rollup.build.js' // replicates rollup -c programatical
import { builtinModules } from 'node:module'
import { writeFileSync } from 'node:fs';
/** @typedef {rollup.RollupOptions} RollupOptions */
/** @type {rollupOptions[]} */
const rollupConfigs = [{
input: ['modules/auto-install'],
external: (id) => id.indexOf('node:') > -1 || builtinModules.includes(id), // ['path', 'fs', 'child_process', 'module', 'util'],
output: [{
sourcemap: true,
format: 'cjs',
entryFileNames: '[name].cjs',
dir: './dist',
interop: 'esModule', // see: https://github.com/rollup/rollup/pull/3710
exports: 'named'
}]
}]
const addPackageJson = () => {
const pkgJson = {
"name": "rollup-plugin-auto-install-cjs",
"main": "auto-install.cjs",
"types": "auto-install.d.cts",
"version": "2.2.0",
"publishConfig": {
"access": "public"
},
"description": "Automatically install dependencies that are imported by a bundle",
}
writeFileSync('dist/package.json',JSON.stringify(pkgJson,null,2));
}
export default buildConfigs(rollupConfigs, addPackageJson)
/**
* real solution to run something after all is done
* you do not need rollup.build.js to track the end all builds as the rollup -c command will exit :)
*/
// export default rollupConfigs;
// process.on('exit', () => addPackageJson());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment