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

Support Rollup logging functions in dev #13624

Open
4 tasks done
sapphi-red opened this issue Jun 25, 2023 · 3 comments · May be fixed by #18922
Open
4 tasks done

Support Rollup logging functions in dev #13624

sapphi-red opened this issue Jun 25, 2023 · 3 comments · May be fixed by #18922
Labels
enhancement New feature or request p3-significant High priority enhancement (priority) rollup plugin compat

Comments

@sapphi-red
Copy link
Member

Description

Rollup added logging functions (this.info, this.debug) in 3.25.0.
Currently we added them as a no-op in #13608.
But we should output logs.

Suggested solution

Implement them.
I guess we would call config.logger.info for this.info().
I don't know what we should do for this.debug().

Also I guess we should pass logLevel to Rollup.

Alternative

No response

Additional context

No response

Validations

@sun0day
Copy link
Member

sun0day commented Jun 28, 2023

Seems that this.info wouldn't be called by rollup itself until the plugins call it

@mato533
Copy link

mato533 commented Jul 6, 2023

Hello!

When I use some rollup plugins, only warning log at the plugin for rollup is treated by the vite.

I propose the following three modifications.

  1. Use onLog instead of onwarn. (Rollup Configuration guide - onLog)

    const rollupOptions: RollupOptions = {
    context: 'globalThis',
    preserveEntrySignatures: ssr
    ? 'allow-extension'
    : libOptions
    ? 'strict'
    : false,
    cache: config.build.watch ? undefined : false,
    ...options.rollupOptions,
    input,
    plugins,
    external,
    onwarn(warning, warn) {
    onRollupWarning(warning, warn, config)
    },
    }

  2. Following codes would be modified to accept all log levels(info,warn,error,debug).

    export function onRollupWarning(
    warning: RollupWarning,
    warn: LoggingFunction,
    config: ResolvedConfig,
    ): void {
    const viteWarn: LoggingFunction = (warnLog) => {
    let warning: string | RollupLog
    if (typeof warnLog === 'function') {
    warning = warnLog()
    } else {
    warning = warnLog
    }
    if (typeof warning === 'object') {
    if (warning.code === 'UNRESOLVED_IMPORT') {
    const id = warning.id
    const exporter = warning.exporter
    // throw unless it's commonjs external...
    if (!id || !/\?commonjs-external$/.test(id)) {
    throw new Error(
    `[vite]: Rollup failed to resolve import "${exporter}" from "${id}".\n` +
    `This is most likely unintended because it can break your application at runtime.\n` +
    `If you do want to externalize this module explicitly add it to\n` +
    `\`build.rollupOptions.external\``,
    )
    }
    }
    if (
    warning.plugin === 'rollup-plugin-dynamic-import-variables' &&
    dynamicImportWarningIgnoreList.some((msg) =>
    // @ts-expect-error warning is RollupLog
    warning.message.includes(msg),
    )
    ) {
    return
    }
    if (warningIgnoreList.includes(warning.code!)) {
    return
    }
    if (warning.code === 'PLUGIN_WARNING') {
    config.logger.warn(
    `${colors.bold(
    colors.yellow(`[plugin:${warning.plugin}]`),
    )} ${colors.yellow(warning.message)}`,
    )
    return
    }
    }
    warn(warnLog)
    }
    const tty = process.stdout.isTTY && !process.env.CI
    if (tty) {
    process.stdout.clearLine(0)
    process.stdout.cursorTo(0)
    }
    const userOnWarn = config.build.rollupOptions?.onwarn
    if (userOnWarn) {
    userOnWarn(warning, viteWarn)
    } else {
    viteWarn(warning)
    }
    }

  3. Also change the options where Rollup is called in different code.

    const bundle = await rollup({
    ...rollupOptions,
    input: cleanUrl(id),
    plugins,
    onwarn(warning, warn) {
    onRollupWarning(warning, warn, config)
    },
    preserveEntrySignatures: false,
    })

Thank you!

@crystalfp
Copy link

+1
Seems onwarn and onLog defined in vite.config.mts are not called when running npx vite build

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request p3-significant High priority enhancement (priority) rollup plugin compat
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants