Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to filter logs via CLI option or environment variable #5035

Merged
merged 10 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 16 additions & 21 deletions build-plugins/copy-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ import { readFile } from 'node:fs/promises';
import { resolve } from 'node:path';
import type { Plugin } from 'rollup';

function copyRollupType(fileName: string): Plugin {
function copyRollupType(
fileName: string,
inputFile = 'src/rollup/types.d.ts',
rollupImportPath?: string
): Plugin {
return {
async generateBundle(_options, _bundle, isWrite) {
if (isWrite) {
this.emitFile({
fileName,
source: await readFile(resolve('src/rollup/types.d.ts'), 'utf8'),
type: 'asset'
});
let source = await readFile(resolve(inputFile), 'utf8');
if (rollupImportPath) {
source = source.replace(rollupImportPath, './rollup');
}
this.emitFile({ fileName, source, type: 'asset' });
}
},
name: 'copy-rollup-type'
Expand All @@ -24,20 +28,11 @@ export function copyBrowserTypes(): Plugin {
export function copyNodeTypes(): Plugin[] {
return [
copyRollupType('rollup.d.ts'),
{
async generateBundle(_options, _bundle, isWrite) {
if (isWrite) {
this.emitFile({
fileName: 'loadConfigFile.d.ts',
source: (await readFile(resolve('cli/run/loadConfigFileType.d.ts'), 'utf8')).replace(
'../../src/rollup/types',
'./rollup'
),
type: 'asset'
});
}
},
name: 'copy-loadConfigFile-type'
}
copyRollupType(
'loadConfigFile.d.ts',
'cli/run/loadConfigFileType.d.ts',
'../../src/rollup/types'
),
copyRollupType('getLogFilter.d.ts', 'src/utils/getLogFilterType.d.ts', '../rollup/types')
];
}
1 change: 1 addition & 0 deletions cli/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Basic options:
--no-externalImportAssertions Omit import assertions in "es" output
--no-externalLiveBindings Do not generate code to support live bindings
--failAfterWarnings Exit with an error if the build produced warnings
--filterLogs <filter> Filter log messages
--footer <text> Code to insert at end of bundle (outside wrapper)
--no-freeze Do not freeze namespace objects
--generatedCode <preset> Which code features to use (es5/es2015)
Expand Down
15 changes: 14 additions & 1 deletion cli/run/batchWarnings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { blue, cyan } from 'colorette';
import type { RollupLog } from '../../src/rollup/types';
import { bold, gray, yellow } from '../../src/utils/colors';
import { ensureArray } from '../../src/utils/ensureArray';
import { getLogFilter } from '../../src/utils/getLogFilter';
import { getNewArray, getOrCreate } from '../../src/utils/getOrCreate';
import { LOGLEVEL_DEBUG, LOGLEVEL_WARN } from '../../src/utils/logging';
import { printQuotedStringList } from '../../src/utils/printStringList';
Expand All @@ -18,7 +20,9 @@ import {
import { stderr } from '../logging';
import type { BatchWarnings } from './loadConfigFileType';

export default function batchWarnings(silent: boolean): BatchWarnings {
export default function batchWarnings(command: Record<string, any>): BatchWarnings {
const silent = !!command.silent;
const logFilter = generateLogFilter(command);
let count = 0;
const deferredWarnings = new Map<keyof typeof deferredHandlers, RollupLog[]>();
let warningOccurred = false;
Expand Down Expand Up @@ -61,6 +65,7 @@ export default function batchWarnings(silent: boolean): BatchWarnings {
},

log(level, log) {
if (!logFilter(log)) return;
switch (level) {
case LOGLEVEL_WARN: {
return add(log);
Expand Down Expand Up @@ -327,3 +332,11 @@ function showTruncatedWarnings(warnings: readonly RollupLog[]): void {
stderr(`\n...and ${nestedByModule.length - displayedByModule.length} other files`);
}
}

function generateLogFilter(command: Record<string, any>) {
const filters = ensureArray(command.filterLogs).flatMap(filter => String(filter).split(','));
if (process.env.ROLLUP_FILTER_LOGS) {
filters.push(...process.env.ROLLUP_FILTER_LOGS.split(','));
}
return getLogFilter(filters);
}
7 changes: 4 additions & 3 deletions cli/run/loadConfigFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const loadConfigFile: LoadConfigFile = async (
getDefaultFromCjs(await getConfigFileExport(fileName, commandOptions, watchMode)),
commandOptions
);
const warnings = batchWarnings(commandOptions.silent);
const warnings = batchWarnings(commandOptions);
try {
const normalizedConfigs: MergedRollupOptions[] = [];
for (const config of configs) {
Expand Down Expand Up @@ -92,9 +92,10 @@ function getDefaultFromCjs(namespace: GenericConfigObject): unknown {

async function loadTranspiledConfigFile(
fileName: string,
{ bundleConfigAsCjs, configPlugin, silent }: Record<string, unknown>
commandOptions: Record<string, unknown>
): Promise<unknown> {
const warnings = batchWarnings(!!silent);
const { bundleConfigAsCjs, configPlugin, silent } = commandOptions;
const warnings = batchWarnings(commandOptions);
const inputOptions = {
external: (id: string) =>
(id[0] !== '.' && !isAbsolute(id)) || id.slice(-5, id.length) === '.json',
Expand Down
12 changes: 6 additions & 6 deletions cli/run/loadConfigFromCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import type { BatchWarnings } from './loadConfigFileType';
import { stdinName } from './stdin';

export default async function loadConfigFromCommand(
command: Record<string, unknown>,
commandOptions: Record<string, unknown>,
watchMode: boolean
): Promise<{
options: MergedRollupOptions[];
warnings: BatchWarnings;
}> {
const warnings = batchWarnings(!!command.silent);
if (!command.input && (command.stdin || !process.stdin.isTTY)) {
command.input = stdinName;
const warnings = batchWarnings(commandOptions);
if (!commandOptions.input && (commandOptions.stdin || !process.stdin.isTTY)) {
commandOptions.input = stdinName;
}
const options = await mergeOptions({ input: [] }, watchMode, command, warnings.log);
await addCommandPluginsToInputOptions(options, command);
const options = await mergeOptions({ input: [] }, watchMode, commandOptions, warnings.log);
await addCommandPluginsToInputOptions(options, commandOptions);
return { options: [options], warnings };
}