Skip to content

Commit

Permalink
feat: improve packaging for browser support
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 9, 2018
1 parent 45e2f99 commit 47af1df
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 66 deletions.
2 changes: 1 addition & 1 deletion demo.js → demo/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

const Consola = require('./src/cjs')
const Consola = require('../lib/esm')

const reporters = [
'FancyReporter',
Expand Down
7 changes: 0 additions & 7 deletions index.js

This file was deleted.

12 changes: 12 additions & 0 deletions main/consola.browser.js
Original file line number Diff line number Diff line change
@@ -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()
]
})
}
22 changes: 22 additions & 0 deletions main/consola.js
Original file line number Diff line number Diff line change
@@ -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()
]
})
}
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
1 change: 0 additions & 1 deletion src/cjs.js

This file was deleted.

6 changes: 3 additions & 3 deletions src/consola.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
43 changes: 10 additions & 33 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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'
19 changes: 2 additions & 17 deletions src/reporters/basic.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import util from 'util'
import { isPlainObject } from '../utils'
import { isPlainObject, parseStack } from '../utils'

export default class BasicReporter {
constructor (options) {
Expand All @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions src/reporters/browser.js
Original file line number Diff line number Diff line change
@@ -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
}
}
1 change: 1 addition & 0 deletions src/reporters/index.js
Original file line number Diff line number Diff line change
@@ -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'
2 changes: 1 addition & 1 deletion src/types.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default {
export const Types = {
fatal: {
level: 0,
color: 'red'
Expand Down
15 changes: 15 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 47af1df

Please sign in to comment.