Skip to content

Commit 4548c11

Browse files
committed
fix: support for NodeJS 15, fixes #732
1 parent 22ba21d commit 4548c11

File tree

7 files changed

+48
-16
lines changed

7 files changed

+48
-16
lines changed

.github/workflows/test.yml

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ jobs:
1515
- os: ubuntu-latest
1616
timezone: Etc/GMT
1717
node-version: 16
18+
- os: ubuntu-latest
19+
timezone: Asia/Shanghai
20+
node-version: 15
1821
- os: ubuntu-latest
1922
timezone: Asia/Shanghai
2023
node-version: 14

rollup.config.mjs

-7
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ const browserStream = {
5050
delimiters: ['', ''],
5151
'./streamed-emitter': '../build/streamed-emitter-browser'
5252
}
53-
const browserPerf = {
54-
include: ['./src/context/context.ts', './src/render/render.ts'],
55-
'node:perf_hooks': '../build/perf_hooks-browser'
56-
}
5753
const esmRequire = {
5854
include: './src/fs/node.ts',
5955
delimiters: ['', ''],
@@ -99,7 +95,6 @@ const browserEsm = {
9995
versionInjection,
10096
replace(browserFS),
10197
replace(browserStream),
102-
replace(browserPerf),
10398
typescript(tsconfig('es6'))
10499
],
105100
treeshake,
@@ -118,7 +113,6 @@ const browserUmd = {
118113
versionInjection,
119114
replace(browserFS),
120115
replace(browserStream),
121-
replace(browserPerf),
122116
typescript(tsconfig('es5'))
123117
],
124118
treeshake,
@@ -137,7 +131,6 @@ const browserMin = {
137131
versionInjection,
138132
replace(browserFS),
139133
replace(browserStream),
140-
replace(browserPerf),
141134
typescript(tsconfig('es5')),
142135
uglify()
143136
],

src/build/perf_hooks-browser.ts

-5
This file was deleted.

src/context/context.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { performance } from 'node:perf_hooks'
1+
import { getPerformance } from '../util/performance'
22
import { Drop } from '../drop/drop'
33
import { __assign } from 'tslib'
44
import { NormalizedFullOptions, defaultOptions, RenderOptions } from '../liquid-options'
@@ -44,7 +44,7 @@ export class Context {
4444
this.strictVariables = renderOptions.strictVariables ?? this.opts.strictVariables
4545
this.ownPropertyOnly = renderOptions.ownPropertyOnly ?? opts.ownPropertyOnly
4646
this.memoryLimit = memoryLimit ?? new Limiter('memory alloc', renderOptions.memoryLimit ?? opts.memoryLimit)
47-
this.renderLimit = renderLimit ?? new Limiter('template render', performance.now() + (renderOptions.renderLimit ?? opts.renderLimit))
47+
this.renderLimit = renderLimit ?? new Limiter('template render', getPerformance().now() + (renderOptions.renderLimit ?? opts.renderLimit))
4848
}
4949
public getRegister (key: string) {
5050
return (this.registers[key] = this.registers[key] || {})

src/render/render.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { performance } from 'node:perf_hooks'
1+
import { getPerformance } from '../util/performance'
22
import { toPromise, RenderError, LiquidErrors, LiquidError } from '../util'
33
import { Context } from '../context'
44
import { Template } from '../template'
@@ -17,7 +17,7 @@ export class Render {
1717
}
1818
const errors = []
1919
for (const tpl of templates) {
20-
ctx.renderLimit.check(performance.now())
20+
ctx.renderLimit.check(getPerformance().now())
2121
try {
2222
// if tpl.render supports emitter, it'll return empty `html`
2323
const html = yield tpl.render(ctx, emitter)

src/util/performance.spec.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { getPerformance } from './performance'
2+
3+
describe('performance', () => {
4+
let globalPerformance: Performance
5+
beforeEach(() => {
6+
globalPerformance = global.performance
7+
})
8+
afterEach(() => {
9+
global.performance = globalPerformance
10+
delete (global as any).window
11+
})
12+
it('should use global.performance if exist', () => {
13+
const performance = {} as any as Performance
14+
global.performance = performance
15+
expect(getPerformance()).toEqual(performance)
16+
})
17+
it('should use window.performance if exist', () => {
18+
const performance = {} as any as Performance
19+
delete (global as any).performance
20+
global.window = { performance } as any
21+
expect(getPerformance()).toEqual(performance)
22+
})
23+
it('should use polyfill if no window/global.performance', () => {
24+
delete (global as any).performance
25+
const now = getPerformance().now()
26+
expect(Number.isInteger(now)).toBeTruthy()
27+
})
28+
})

src/util/performance.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
interface LiquidPerformance {
2+
now: () => number
3+
}
4+
5+
const polyfill: LiquidPerformance = {
6+
now: () => Date.now()
7+
}
8+
9+
export function getPerformance (): LiquidPerformance {
10+
return (typeof global === 'object' && global.performance) ||
11+
(typeof window === 'object' && window.performance) ||
12+
polyfill
13+
}

0 commit comments

Comments
 (0)