From 48d58c5859eea93f02da34ea28341b3803821931 Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Sun, 10 Nov 2024 04:57:50 +0900 Subject: [PATCH 1/3] Enable compile cache in Node.js v22.8+ --- src/patch.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/patch.ts b/src/patch.ts index d4187b2f..88addcb6 100644 --- a/src/patch.ts +++ b/src/patch.ts @@ -1,10 +1,21 @@ +import module from 'node:module' import { isStandaloneBinary } from './utils/binary' import { debug } from './utils/debug' export const patch = () => { + enableCompileCache() patchSegmenter() } +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 = () => { // Avoid SEGFAULT in the standalone binary. pkg is using a Node.js build with small ICU. // https://github.com/nodejs/node/issues/51752 From 88969dc5ddc8f10882df93cb77e7d7dd3e8689b4 Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Sun, 1 Dec 2024 23:57:35 +0900 Subject: [PATCH 2/3] Add test for `patch.enableCompileCache()` --- src/patch.ts | 2 +- test/patch.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/patch.ts b/src/patch.ts index 88addcb6..d30759ad 100644 --- a/src/patch.ts +++ b/src/patch.ts @@ -3,8 +3,8 @@ import { isStandaloneBinary } from './utils/binary' import { debug } from './utils/debug' export const patch = () => { - enableCompileCache() patchSegmenter() + enableCompileCache() } export const enableCompileCache = () => { diff --git a/test/patch.ts b/test/patch.ts index a8fd3604..5fc31f9d 100644 --- a/test/patch.ts +++ b/test/patch.ts @@ -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') @@ -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 From 47e5157b7b965e794593db675f1a90817a6bcbf9 Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Mon, 2 Dec 2024 00:28:58 +0900 Subject: [PATCH 3/3] [ci skip] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ba28be7..90ec5e90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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))