diff --git a/artifacts/stats.json b/artifacts/stats.json new file mode 100644 index 0000000..5f33a8c --- /dev/null +++ b/artifacts/stats.json @@ -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 + } +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index e604cf4..8c8638f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; @@ -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 diff --git a/test/package/package-lock.json b/test/package/package-lock.json new file mode 100644 index 0000000..a0d960c --- /dev/null +++ b/test/package/package-lock.json @@ -0,0 +1,103 @@ +{ + "name": "rollup-plugin-stats-package-test", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "rollup-plugin-stats-package-test", + "version": "1.0.0", + "license": "ISC", + "devDependencies": { + "rollup": "^4.9.1", + "rollup-plugin-stats": "../../" + } + }, + "../..": { + "version": "1.2.2", + "dev": true, + "license": "MIT", + "devDependencies": { + "@eslint/js": "^9.17.0", + "@release-it/conventional-changelog": "10.0.0", + "@rollup/plugin-typescript": "^12.1.2", + "@tsconfig/node18": "^18.2.4", + "@types/lodash": "^4.17.13", + "@types/node": "^22.10.2", + "dotenv": "^16.4.7", + "eslint": "^9.17.0", + "globals": "^15.14.0", + "husky": "^8.0.3", + "memfs": "^4.15.1", + "prettier": "^3.4.2", + "release-it": "18.0.0", + "rollup": "^4.29.1", + "typescript": "^5.7.2", + "typescript-eslint": "^8.18.2", + "vitest": "^2.1.8" + }, + "engines": { + "node": ">=18" + }, + "peerDependencies": { + "rollup": "^3.0.0 || ^4.0.0" + } + }, + "node_modules/@rollup/rollup-darwin-arm64": { + "version": "4.9.1", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ] + }, + "node_modules/fsevents": { + "version": "2.3.3", + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, + "node_modules/rollup": { + "version": "4.9.1", + "dev": true, + "license": "MIT", + "bin": { + "rollup": "dist/bin/rollup" + }, + "engines": { + "node": ">=18.0.0", + "npm": ">=8.0.0" + }, + "optionalDependencies": { + "@rollup/rollup-android-arm-eabi": "4.9.1", + "@rollup/rollup-android-arm64": "4.9.1", + "@rollup/rollup-darwin-arm64": "4.9.1", + "@rollup/rollup-darwin-x64": "4.9.1", + "@rollup/rollup-linux-arm-gnueabihf": "4.9.1", + "@rollup/rollup-linux-arm64-gnu": "4.9.1", + "@rollup/rollup-linux-arm64-musl": "4.9.1", + "@rollup/rollup-linux-riscv64-gnu": "4.9.1", + "@rollup/rollup-linux-x64-gnu": "4.9.1", + "@rollup/rollup-linux-x64-musl": "4.9.1", + "@rollup/rollup-win32-arm64-msvc": "4.9.1", + "@rollup/rollup-win32-ia32-msvc": "4.9.1", + "@rollup/rollup-win32-x64-msvc": "4.9.1", + "fsevents": "~2.3.2" + } + }, + "node_modules/rollup-plugin-stats": { + "resolved": "../..", + "link": true + } + } +} diff --git a/test/package/package.test.js b/test/package/package.test.js index 9b5c17c..2ad6fa2 100644 --- a/test/package/package.test.js +++ b/test/package/package.test.js @@ -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(() => { @@ -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); diff --git a/test/package/rollup.config.js b/test/package/rollup.config.js index f5b3af1..c89c5d1 100644 --- a/test/package/rollup.config.js +++ b/test/package/rollup.config.js @@ -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: {