Skip to content

Commit 4da784d

Browse files
committed
feat: add global.consola
1 parent 6180985 commit 4da784d

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist
2+
node_modules

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
if (global.consola) {
2+
module.exports = global.consola
3+
} else {
4+
const consola = require('./dist/consola.cjs.js')
5+
module.exports = consola
6+
global.consola = consola
7+
}

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
"description": "Elegant Console Logger",
55
"license": "MIT",
66
"repository": "nuxt/consola",
7-
"main": "dist/consola.cjs.js",
7+
"main": "index.js",
88
"scripts": {
99
"build": "bili",
10-
"lint": "eslint src test examples",
11-
"test": "jest test",
10+
"lint": "eslint .",
11+
"test": "yarn lint && yarn build && jest test",
1212
"prepublish": "yarn build",
1313
"release": "standard-version && yarn build && git push --follow-tags && npm publish"
1414
},
1515
"files": [
16+
"index.js",
1617
"dist",
1718
"src"
1819
],

test/custom-consola.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
describe('custom consola', () => {
3+
afterEach(() => {
4+
delete global.consola
5+
jest.resetModules()
6+
})
7+
8+
test('require twice has same consola', () => {
9+
const consola1 = require('consola')
10+
jest.resetModules()
11+
const consola2 = require('consola')
12+
13+
expect(consola1 === consola2).toBe(true)
14+
})
15+
16+
test('custom consola fails without delete cache', async () => {
17+
require('consola')
18+
const consola1 = 'my-consola'
19+
global.consola = consola1
20+
21+
const consola2 = require('consola')
22+
23+
expect(consola1 === consola2).toBe(false)
24+
expect(global.consola === consola2).toBe(false)
25+
})
26+
27+
test('require consola used global.consola by default', () => {
28+
const consola1 = 'my-consola'
29+
global.consola = consola1
30+
31+
const consola2 = require('consola')
32+
expect(consola1 === consola2).toBe(true)
33+
expect(global.consola === consola2).toBe(true)
34+
})
35+
36+
test('custom consola works when imported but deleted', () => {
37+
const consola0 = require('consola')
38+
expect(consola0).not.toBe(undefined)
39+
40+
const consola1 = 'my-consola2'
41+
global.consola = consola1
42+
43+
jest.resetModules() // jest equivalent to delete require.cache
44+
45+
const consola2 = require('consola')
46+
expect(consola1 === consola2).toBe(true)
47+
expect(global.consola === consola2).toBe(true)
48+
})
49+
})

0 commit comments

Comments
 (0)