Skip to content

Commit

Permalink
Merge pull request #624 from marp-team/enable-compile-cache
Browse files Browse the repository at this point in the history
Enable Node.js compile cache on CLI when `module.enableCompileCache()` is available
  • Loading branch information
yhatt authored Dec 1, 2024
2 parents 6fa5803 + 47e5157 commit 4628dda
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Added

- Make faster launching CLI on Node.js 22.8.0 and later, by enabling Node.js compile cache with `module.enableCompileCache()` ([#624](https://github.com/marp-team/marp-cli/pull/624))

### Changed

- Upgrade dependent packages to the latest versions ([#623](https://github.com/marp-team/marp-cli/pull/623))
Expand Down
11 changes: 11 additions & 0 deletions src/patch.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import module from 'node:module'
import { isStandaloneBinary } from './utils/binary'
import { debug } from './utils/debug'

export const patch = () => {
patchSegmenter()
enableCompileCache()
}

export const enableCompileCache = () => {
try {
// enableCompileCache() is available only in Node.js 22.8.0 and later
;(module as any).enableCompileCache?.()
} catch {
// no ops
}
}

export const patchSegmenter = () => {
Expand Down
40 changes: 40 additions & 0 deletions test/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ afterEach(() => {
})

describe('patch()', () => {
it('calls enableCompileCache()', () => {
jest.spyOn(patch, 'enableCompileCache')

patch.patch()
expect(patch.enableCompileCache).toHaveBeenCalled()
})

it('calls patchSegmenter()', () => {
jest.spyOn(patch, 'patchSegmenter')

Expand All @@ -21,6 +28,39 @@ describe('patch()', () => {
})
})

describe('enableCompileCache()', () => {
beforeEach(() => {
jest.resetModules()
})

const enableCompileCache = async () =>
(await import('../src/patch')).enableCompileCache()

it('calls module.enableCompileCache()', async () => {
const mock = jest.fn()
jest.doMock('node:module', () => ({ enableCompileCache: mock }))

await enableCompileCache()
expect(mock).toHaveBeenCalled()
})

it('ignores error raised from module.enableCompileCache()', async () => {
jest.doMock('node:module', () => ({
enableCompileCache: jest.fn(() => {
throw new Error('test')
}),
}))

expect(enableCompileCache).not.toThrow()
})

it('does nothing if module.enableCompileCache is not defined (for older versions of Node.js)', async () => {
jest.doMock('node:module', () => ({}))

expect(enableCompileCache).not.toThrow()
})
})

describe('patchSegmenter()', () => {
const originalVariables = process.config.variables

Expand Down

0 comments on commit 4628dda

Please sign in to comment.