Skip to content

Commit

Permalink
fix: warnOnce() lru
Browse files Browse the repository at this point in the history
  • Loading branch information
styfle committed Dec 10, 2024
1 parent 21eb54a commit de3cac7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
5 changes: 2 additions & 3 deletions packages/next/src/build/output/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,11 @@ export function trace(...message: any[]) {
prefixedLog('trace', ...message)
}

const warnOnceCache = new LRUCache<string>(10_000, (message) => message.length)

const warnOnceCache = new LRUCache<string>(10_000, (value) => value.length)
export function warnOnce(...message: any[]) {
const key = message.join(' ')
if (!warnOnceCache.has(key)) {
warnOnceCache.set(key)
warnOnceCache.set(key, key)
warn(...message)
}
}
27 changes: 27 additions & 0 deletions test/unit/build-output-log.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { warnOnce } from 'next/dist/build/output/log'

describe('build/output/log', () => {
it('warnOnce', () => {
const original = console.warn
try {
const messages = []
console.warn = (m: any) => messages.push(m)
warnOnce('test')
expect(messages.length).toEqual(1)
warnOnce('test again')
expect(messages.length).toEqual(2)
warnOnce('test', 'more')
expect(messages.length).toEqual(3)
warnOnce('test')
expect(messages.length).toEqual(3)
warnOnce('test again')
expect(messages.length).toEqual(3)
warnOnce('test', 'more')
expect(messages.length).toEqual(3)
warnOnce('test', 'should', 'add', 'another')
expect(messages.length).toEqual(4)
} finally {
console.warn = original
}
})
})

0 comments on commit de3cac7

Please sign in to comment.