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

[proposal] add a plugin helper package #52

Closed
pimlie opened this issue Jul 2, 2017 · 3 comments
Closed

[proposal] add a plugin helper package #52

pimlie opened this issue Jul 2, 2017 · 3 comments

Comments

@pimlie
Copy link
Member

pimlie commented Jul 2, 2017

Would you accept a PR for a @nuxtjs/plugin-helper package which will at least contain debug/info functions? At the moment most logging by the plugins are done by calling console.log, but it would be nice if we use the same debug package as nuxtjs uses so all output during build is nicely formatted.

My idea would be to export functions that always add a 'plugin:' text to the logging. And maybe we choose a different colour then nuxt's blue? This way we have an uniform and recognizable way of logging.

const isProd = process.env.NODE_ENV === 'production'

if (isProd) {
  const log = () => () => null
  const error = () => () => null

  module.exports = { log, error }
} else {
  const Debug = require('debug')

  let instances = {}

  const log = (plugin_name) => {
    if (!instances['log_' + plugin_name]) {
      const log = Debug('plugin:' + plugin_name)
      log.enabled = true
      log.color = 5
      instances['log_' + plugin_name] = log
    }
    return instances['log_' + plugin_name]
  }

  const error = (plugin_name) => {
    if (!instances['err_' + plugin_name]) {
      const error = Debug('plugin:' + plugin_name)
      error.enabled = true
      error.color = 1
      instances['err_' + plugin_name] = error
    }
    return instances['err_' + plugin_name]
  }

  module.exports = { log, error }
}

and usage would be:

module.exports.meta = require('./package.json')
const debug = require('@nuxtjs/plugin-utils').log(module.exports.meta.name)
const error = require('@nuxtjs/plugin-utils').error(module.exports.meta.name)

log('information about what the plugin is doing during build') // printed in purple
error('oops, something went wrong') // printed in red

I am using currently something similar for rfg-icon, but think we could benefit if we centralize the logging :)

This question is available on Nuxt.js community (#c44)
@pi0
Copy link
Member

pi0 commented Jul 2, 2017

Hi. That's a nice idea indeed i was also thinking of a helper package for plugins! I suggest some improvements to basic idea:

  • Having a more general name like plugin-utils
  • Create debug instances out of arrow functions once, for performance.
    • Using some template condition to remove dependency of Debug package from production builds. This is important as can add unwanted package size. (We can create a noop handler like () => () => null to remove debug)

@pimlie
Copy link
Member Author

pimlie commented Jul 2, 2017

I updated both example codes, would something like that be ok? I used a simple if (process.env.NODE_ENV === 'production') { so code is removed through tree shaking. Not sure how you could get template conditions to work here while keeping a package that works both in prod as in dev.

@anteriovieira
Copy link
Member

I'm closing this issue by understanding that the module was added as discussed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants