fix: support for NodeJS 15, fixes #732

This commit is contained in:
Yang Jun
2024-08-16 23:40:48 +08:00
committed by Jun Yang
parent 22ba21d63f
commit 4548c11406
7 changed files with 48 additions and 16 deletions
+3
View File
@@ -15,6 +15,9 @@ jobs:
- os: ubuntu-latest - os: ubuntu-latest
timezone: Etc/GMT timezone: Etc/GMT
node-version: 16 node-version: 16
- os: ubuntu-latest
timezone: Asia/Shanghai
node-version: 15
- os: ubuntu-latest - os: ubuntu-latest
timezone: Asia/Shanghai timezone: Asia/Shanghai
node-version: 14 node-version: 14
-7
View File
@@ -50,10 +50,6 @@ const browserStream = {
delimiters: ['', ''], delimiters: ['', ''],
'./streamed-emitter': '../build/streamed-emitter-browser' './streamed-emitter': '../build/streamed-emitter-browser'
} }
const browserPerf = {
include: ['./src/context/context.ts', './src/render/render.ts'],
'node:perf_hooks': '../build/perf_hooks-browser'
}
const esmRequire = { const esmRequire = {
include: './src/fs/node.ts', include: './src/fs/node.ts',
delimiters: ['', ''], delimiters: ['', ''],
@@ -99,7 +95,6 @@ const browserEsm = {
versionInjection, versionInjection,
replace(browserFS), replace(browserFS),
replace(browserStream), replace(browserStream),
replace(browserPerf),
typescript(tsconfig('es6')) typescript(tsconfig('es6'))
], ],
treeshake, treeshake,
@@ -118,7 +113,6 @@ const browserUmd = {
versionInjection, versionInjection,
replace(browserFS), replace(browserFS),
replace(browserStream), replace(browserStream),
replace(browserPerf),
typescript(tsconfig('es5')) typescript(tsconfig('es5'))
], ],
treeshake, treeshake,
@@ -137,7 +131,6 @@ const browserMin = {
versionInjection, versionInjection,
replace(browserFS), replace(browserFS),
replace(browserStream), replace(browserStream),
replace(browserPerf),
typescript(tsconfig('es5')), typescript(tsconfig('es5')),
uglify() uglify()
], ],
-5
View File
@@ -1,5 +0,0 @@
const polyfill = {
now: () => Date.now()
}
export const performance = typeof window === 'object' && window.performance || polyfill
+2 -2
View File
@@ -1,4 +1,4 @@
import { performance } from 'node:perf_hooks' import { getPerformance } from '../util/performance'
import { Drop } from '../drop/drop' import { Drop } from '../drop/drop'
import { __assign } from 'tslib' import { __assign } from 'tslib'
import { NormalizedFullOptions, defaultOptions, RenderOptions } from '../liquid-options' import { NormalizedFullOptions, defaultOptions, RenderOptions } from '../liquid-options'
@@ -44,7 +44,7 @@ export class Context {
this.strictVariables = renderOptions.strictVariables ?? this.opts.strictVariables this.strictVariables = renderOptions.strictVariables ?? this.opts.strictVariables
this.ownPropertyOnly = renderOptions.ownPropertyOnly ?? opts.ownPropertyOnly this.ownPropertyOnly = renderOptions.ownPropertyOnly ?? opts.ownPropertyOnly
this.memoryLimit = memoryLimit ?? new Limiter('memory alloc', renderOptions.memoryLimit ?? opts.memoryLimit) this.memoryLimit = memoryLimit ?? new Limiter('memory alloc', renderOptions.memoryLimit ?? opts.memoryLimit)
this.renderLimit = renderLimit ?? new Limiter('template render', performance.now() + (renderOptions.renderLimit ?? opts.renderLimit)) this.renderLimit = renderLimit ?? new Limiter('template render', getPerformance().now() + (renderOptions.renderLimit ?? opts.renderLimit))
} }
public getRegister (key: string) { public getRegister (key: string) {
return (this.registers[key] = this.registers[key] || {}) return (this.registers[key] = this.registers[key] || {})
+2 -2
View File
@@ -1,4 +1,4 @@
import { performance } from 'node:perf_hooks' import { getPerformance } from '../util/performance'
import { toPromise, RenderError, LiquidErrors, LiquidError } from '../util' import { toPromise, RenderError, LiquidErrors, LiquidError } from '../util'
import { Context } from '../context' import { Context } from '../context'
import { Template } from '../template' import { Template } from '../template'
@@ -17,7 +17,7 @@ export class Render {
} }
const errors = [] const errors = []
for (const tpl of templates) { for (const tpl of templates) {
ctx.renderLimit.check(performance.now()) ctx.renderLimit.check(getPerformance().now())
try { try {
// if tpl.render supports emitter, it'll return empty `html` // if tpl.render supports emitter, it'll return empty `html`
const html = yield tpl.render(ctx, emitter) const html = yield tpl.render(ctx, emitter)
+28
View File
@@ -0,0 +1,28 @@
import { getPerformance } from './performance'
describe('performance', () => {
let globalPerformance: Performance
beforeEach(() => {
globalPerformance = global.performance
})
afterEach(() => {
global.performance = globalPerformance
delete (global as any).window
})
it('should use global.performance if exist', () => {
const performance = {} as any as Performance
global.performance = performance
expect(getPerformance()).toEqual(performance)
})
it('should use window.performance if exist', () => {
const performance = {} as any as Performance
delete (global as any).performance
global.window = { performance } as any
expect(getPerformance()).toEqual(performance)
})
it('should use polyfill if no window/global.performance', () => {
delete (global as any).performance
const now = getPerformance().now()
expect(Number.isInteger(now)).toBeTruthy()
})
})
+13
View File
@@ -0,0 +1,13 @@
interface LiquidPerformance {
now: () => number
}
const polyfill: LiquidPerformance = {
now: () => Date.now()
}
export function getPerformance (): LiquidPerformance {
return (typeof global === 'object' && global.performance) ||
(typeof window === 'object' && window.performance) ||
polyfill
}