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

Inconsistent behavior when logging in plugin hook #15821

Closed
7 tasks done
nojaf opened this issue Feb 6, 2024 · 3 comments
Closed
7 tasks done

Inconsistent behavior when logging in plugin hook #15821

nojaf opened this issue Feb 6, 2024 · 3 comments
Labels
duplicate This issue or pull request already exists

Comments

@nojaf
Copy link

nojaf commented Feb 6, 2024

Describe the bug

Imagine the following plugin:

import { defineConfig } from 'vite'

function loggerPlugin(config = {}) {
  return {
    name: "my-logger-plugin",
    buildStart: async function (options) {
        this.info('XYZ: buildStart')
    }
  };
}

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [ loggerPlugin() ],
})

This log will only show when running npm run build and not when running npm run dev. While in both cases the code in the hook will be executed.

Reproduction

https://stackblitz.com/edit/vitejs-vite-ihlmcj?file=vite.config.js

Steps to reproduce

npm i, npm run dev => No log
npm run build => Will log

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.14.0 - /usr/local/bin/pnpm
  npmPackages:
    vite: ^5.0.12 => 5.0.12

Used Package Manager

npm

Logs

No response

Validations

Copy link

stackblitz bot commented Feb 6, 2024

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@nojaf
Copy link
Author

nojaf commented Feb 6, 2024

One workaround seems to to capture the logger from the configResolved hook and reuse it later:

function loggerPlugin(config = {}) {
  let logger = {};

  return {
    name: "my-logger-plugin",
    configResolved: async function (resolvedConfig) { 
        logger = resolvedConfig.logger;
    }
    buildStart: async function (options) {
        logger.info('XYZ: buildStart')
    }
  };
}

@sapphi-red
Copy link
Member

Duplicate of #13624

@sapphi-red sapphi-red marked this as a duplicate of #13624 Feb 8, 2024
@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Feb 8, 2024
@sapphi-red sapphi-red added duplicate This issue or pull request already exists and removed pending triage labels Feb 8, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Feb 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants