Skip to content

Commit

Permalink
feat: Allow dynamic options
Browse files Browse the repository at this point in the history
  • Loading branch information
vio committed Jan 13, 2025
1 parent 2d5e02b commit 93f455f
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 4 deletions.
31 changes: 31 additions & 0 deletions artifacts/stats.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"index.js": {
"exports": [],
"facadeModuleId": "/Users/vio/work/relative-ci/rollup-plugin-stats/test/package/src/index.js",
"isDynamicEntry": false,
"isEntry": true,
"isImplicitEntry": false,
"moduleIds": [
"/Users/vio/work/relative-ci/rollup-plugin-stats/test/package/src/index.js"
],
"name": "index",
"type": "chunk",
"dynamicImports": [],
"fileName": "index.js",
"implicitlyLoadedBefore": [],
"importedBindings": {},
"imports": [],
"modules": {
"/Users/vio/work/relative-ci/rollup-plugin-stats/test/package/src/index.js": {
"originalLength": 28,
"removedExports": [],
"renderedExports": [],
"renderedLength": 27
}
},
"referencedFiles": [],
"map": null,
"preliminaryFileName": "index.js",
"sourcemapFileName": null
}
}
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path';
import process from 'node:process';
import type { Plugin } from 'rollup';
import type { Plugin, OutputOptions } from 'rollup';

import extractRollupStats, { type StatsOptions } from './extract';
import { type RollupStatsWrite, rollupStatsWrite } from './write';
Expand All @@ -26,12 +26,18 @@ export type RollupStatsOptions = {
write?: RollupStatsWrite;
};

function rollupStats(options: RollupStatsOptions = {}): Plugin {
const { fileName, stats: statsOptions, write = rollupStatsWrite } = options;
type RollupStatsOptionsOrOutputOptions =
| RollupStatsOptions
| ((outputOptions: OutputOptions) => RollupStatsOptions);


function rollupStats(options: RollupStatsOptionsOrOutputOptions): Plugin {
return {
name: PLUGIN_NAME,
async generateBundle(context, bundle) {
const resolvedOptions = typeof options === 'function' ? options(context) : options;
const { fileName, stats: statsOptions, write = rollupStatsWrite } = resolvedOptions || {};

const resolvedFileName = fileName || DEFAULT_FILE_NAME;
const filepath = path.isAbsolute(resolvedFileName)
? resolvedFileName
Expand Down
103 changes: 103 additions & 0 deletions test/package/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion test/package/package.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { describe, test, expect, beforeEach } from 'vitest';
import { rollup } from 'rollup';
import { vol } from 'memfs';

import config, { relativeFileNameConfig, absoluteFileNameConfig } from './rollup.config';
import config, { dynamicOptions, relativeFileNameConfig, absoluteFileNameConfig } from './rollup.config';

describe('package test', () => {
beforeEach(() => {
Expand All @@ -22,6 +22,17 @@ describe('package test', () => {
});
});

test('should output stats JSON file with explicit compilation file name', async () => {
const bundle = await rollup(dynamicOptions);
await bundle.generate(config.output);

const actual = await fs.readFile(path.join(config.output.dir, 'stats.es.json'), 'utf8');
const stats = JSON.parse(actual);
expect(stats['index.js']).toMatchObject({
fileName: 'index.js',
});
});

test('should output stats JSON file with custom relative filename', async () => {
const bundle = await rollup(relativeFileNameConfig);
await bundle.generate(relativeFileNameConfig.output);
Expand Down
11 changes: 11 additions & 0 deletions test/package/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ export default defineConfig({
plugins: [stats()],
});

export const dynamicOptions = defineConfig({
input: path.join(__dirname, 'src/index.js'),
output: {
dir: 'dist',
format: 'commonjs',
},
plugins: [stats((options) => ({
fileName: `stats.${options.format}.json`,
}))],
});

export const relativeFileNameConfig = defineConfig({
input: path.join(__dirname, 'src/index.js'),
output: {
Expand Down

0 comments on commit 93f455f

Please sign in to comment.