From 47af1df9df9e99d557fd3e685a0734ef399f19e9 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 9 Oct 2018 17:14:40 +0330 Subject: [PATCH] feat: improve packaging for browser support --- demo.js => demo/index.js | 2 +- index.js | 7 ------- main/consola.browser.js | 12 +++++++++++ main/consola.js | 22 ++++++++++++++++++++ package.json | 9 ++++++--- src/cjs.js | 1 - src/consola.js | 6 +++--- src/index.js | 43 ++++++++++------------------------------ src/reporters/basic.js | 19 ++---------------- src/reporters/browser.js | 10 ++++++++++ src/reporters/index.js | 1 + src/types.js | 2 +- src/utils.js | 15 ++++++++++++++ 13 files changed, 83 insertions(+), 66 deletions(-) rename demo.js => demo/index.js (95%) delete mode 100644 index.js create mode 100644 main/consola.browser.js create mode 100644 main/consola.js delete mode 100644 src/cjs.js create mode 100644 src/reporters/browser.js diff --git a/demo.js b/demo/index.js similarity index 95% rename from demo.js rename to demo/index.js index 5dcc3493..00c06978 100755 --- a/demo.js +++ b/demo/index.js @@ -1,6 +1,6 @@ #!/usr/bin/env node -const Consola = require('./src/cjs') +const Consola = require('../lib/esm') const reporters = [ 'FancyReporter', diff --git a/index.js b/index.js deleted file mode 100644 index c648d345..00000000 --- a/index.js +++ /dev/null @@ -1,7 +0,0 @@ -if (global.consola) { - module.exports = global.consola -} else { - const consola = require('./dist/consola.cjs.js') - module.exports = consola - global.consola = consola -} diff --git a/main/consola.browser.js b/main/consola.browser.js new file mode 100644 index 00000000..3f5c9623 --- /dev/null +++ b/main/consola.browser.js @@ -0,0 +1,12 @@ +import { Consola, BrowserReporter } from '../src' + +if (window.consola) { + module.exports = window.consola +} else { + // Create new consola instance + module.exports = window.consola = new Consola({ + reporters: [ + new BrowserReporter() + ] + }) +} diff --git a/main/consola.js b/main/consola.js new file mode 100644 index 00000000..c39df320 --- /dev/null +++ b/main/consola.js @@ -0,0 +1,22 @@ +if (global.consola) { + module.exports = global.consola +} else { + const env = require('std-env') + const { Consola, BasicReporter, FancyReporter } = require('../dist/consola.cjs.js') + + // Log level + let level = env.debug ? 4 : 3 + if (process.env['CONSOLA_LEVEL']) { + level = parseInt(process.env['CONSOLA_LEVEL']) || level + } + + // Create new consola instance + module.exports = global.consola = new Consola({ + level, + reporters: [ + env.minimalCLI + ? new BasicReporter() + : new FancyReporter() + ] + }) +} diff --git a/package.json b/package.json index b28c506f..3e3bee6d 100644 --- a/package.json +++ b/package.json @@ -4,17 +4,20 @@ "description": "Elegant Console Logger", "license": "MIT", "repository": "nuxt/consola", - "main": "index.js", + "main": "main/node.js", + "browser": "dist/consola.js", "scripts": { - "build": "bili", + "build": "yarn build:cjs && yarn build:browser", + "build:cjs": "bili -t node --format cjs src/index.js", + "build:browser": "bili --format umd main/consola.browser.js", "lint": "eslint .", "test": "yarn lint && yarn build && jest test", "prepublish": "yarn build", "release": "standard-version && yarn build && git push --follow-tags && npm publish" }, "files": [ - "index.js", "dist", + "main", "src" ], "keywords": [ diff --git a/src/cjs.js b/src/cjs.js deleted file mode 100644 index 85e02904..00000000 --- a/src/cjs.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('esm')(module, { mode: 'all' })('./index.js').default diff --git a/src/consola.js b/src/consola.js index 9bd0c711..466cb19c 100644 --- a/src/consola.js +++ b/src/consola.js @@ -1,12 +1,12 @@ -import defaultTypes from './types' +import { Types } from './types' import { isLogObj } from './utils' import { version } from '../package.json' -export default class Consola { +export class Consola { constructor (options = {}) { this.reporters = options.reporters || [] this.level = options.level != null ? options.level : 3 - this.types = options.types || defaultTypes + this.types = options.types || Types this.defaults = options.defaults || {} // Create logger functions for current instance diff --git a/src/index.js b/src/index.js index 6ee74d0d..3d5ff1ce 100644 --- a/src/index.js +++ b/src/index.js @@ -1,36 +1,13 @@ -import env from 'std-env' -import Consola from './consola' -import { - BasicReporter, - FancyReporter, - JSONReporter, - WinstonReporter -} from './reporters' +export { + Consola +} from './consola' -// Log level -let level = env.debug ? 4 : 3 -if (typeof process !== 'undefined' && process.env['CONSOLA_LEVEL']) { - level = parseInt(process.env['CONSOLA_LEVEL']) -} +export { + Types +} from './types' -// Create new consola instance -const consola = new Consola({ - level -}) +export { + isLogObj +} from './utils' -// Add default reporter based on env -if (env.minimalCLI) { - consola.add(new BasicReporter()) -} else { - consola.add(new FancyReporter()) -} - -Object.assign(consola, { - Consola, - BasicReporter, - FancyReporter, - JSONReporter, - WinstonReporter -}) - -export default consola +export * from './reporters' diff --git a/src/reporters/basic.js b/src/reporters/basic.js index 9a32710a..2ff20d2f 100644 --- a/src/reporters/basic.js +++ b/src/reporters/basic.js @@ -1,5 +1,5 @@ import util from 'util' -import { isPlainObject } from '../utils' +import { isPlainObject, parseStack } from '../utils' export default class BasicReporter { constructor (options) { @@ -16,23 +16,8 @@ export default class BasicReporter { this.options.stream.write(data) } - parseStack (stack) { - let lines = stack - .split('\n') - .map(l => l - .trim() - .replace(/^at /, '') - ) - - if (lines[0].indexOf('Error: ') === 0) { - lines = lines.splice(1) - } - - return lines - } - formatStack (stack) { - return '> ' + this.parseStack(stack).join('\n> ') + return '> ' + parseStack(stack).join('\n> ') } format (arg) { diff --git a/src/reporters/browser.js b/src/reporters/browser.js new file mode 100644 index 00000000..e9c0ccf4 --- /dev/null +++ b/src/reporters/browser.js @@ -0,0 +1,10 @@ +export default class BrowserReporter { + constructor (options) { + this.options = Object.assign({}, options) + } + + log (logObj) { + // TODO: Improve me + console.log(logObj) // eslint-disable-line no-console + } +} diff --git a/src/reporters/index.js b/src/reporters/index.js index c011df69..5aae10cf 100644 --- a/src/reporters/index.js +++ b/src/reporters/index.js @@ -1,4 +1,5 @@ export { default as BasicReporter } from './basic' +export { default as BrowserReporter } from './browser' export { default as FancyReporter } from './fancy' export { default as JSONReporter } from './json' export { default as WinstonReporter } from './winston' diff --git a/src/types.js b/src/types.js index 45821974..63267277 100644 --- a/src/types.js +++ b/src/types.js @@ -1,4 +1,4 @@ -export default { +export const Types = { fatal: { level: 0, color: 'red' diff --git a/src/utils.js b/src/utils.js index 73267e68..36384365 100644 --- a/src/utils.js +++ b/src/utils.js @@ -7,3 +7,18 @@ export function isLogObj (arg) { // Also contains either message or args field return isPlainObject(arg) && (Boolean(arg.message || arg.args)) } + +export function parseStack (stack) { + let lines = stack + .split('\n') + .map(l => l + .trim() + .replace(/^at /, '') + ) + + if (lines[0].indexOf('Error: ') === 0) { + lines = lines.splice(1) + } + + return lines +}