Skip to content

Commit 34a67f3

Browse files
authored
hotfix: lazy-load node:module (#685)
1 parent 0f69d7b commit 34a67f3

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@node-core/doc-kit",
33
"type": "module",
4-
"version": "1.0.1",
4+
"version": "1.0.2",
55
"repository": {
66
"type": "git",
77
"url": "git+https://github.com/nodejs/doc-kit.git"

src/generators/web/constants.mjs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
1-
import { findPackageJSON } from 'node:module';
21
import { resolve, dirname } from 'node:path';
32
import { fileURLToPath } from 'node:url';
43

54
export const ROOT = dirname(fileURLToPath(import.meta.url));
65

7-
// Resolve node_modules relative to this package (doc-kit), not cwd.
8-
// We do this by finding where one of our dependencies (preact) is stored,
9-
// and using it's NODE_MODULES
10-
export const NODE_MODULES = resolve(
11-
findPackageJSON(new URL(import.meta.resolve('preact'))),
12-
'../..'
13-
);
14-
156
/**
167
* @typedef {Object} JSXImportConfig
178
* @property {string} name - The name of the component to be imported.

src/generators/web/utils/bundle.mjs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
1+
import { resolve } from 'node:path';
2+
13
import virtual from '@rollup/plugin-virtual';
24
import { build } from 'rolldown';
35

46
import cssLoader from './css.mjs';
57
import getStaticData from './data.mjs';
68
import getConfig from '../../../utils/configuration/index.mjs';
7-
import { NODE_MODULES } from '../constants.mjs';
9+
import { lazy } from '../../../utils/misc.mjs';
10+
11+
// Resolve node_modules relative to this package (doc-kit), not cwd.
12+
// We do this by finding where one of our dependencies (preact) is stored,
13+
// and using it's NODE_MODULES
14+
//
15+
// FIXME(@avivkeller): When our CI (in Node.js Core) supports v22.x,
16+
// this lazy-loading solution can be replaced by a simple import. For that
17+
// matter, glob can also be replaced in other files throughout this repo.
18+
const getNodeModules = lazy(async () => {
19+
const { findPackageJSON } = await import('node:module');
20+
21+
return resolve(
22+
findPackageJSON(new URL(import.meta.resolve('preact'))),
23+
'../..'
24+
);
25+
});
826

927
/**
1028
* Asynchronously bundles JavaScript source code (and its CSS imports),
@@ -92,7 +110,7 @@ export default async function bundleCode(codeMap, { server = false } = {}) {
92110

93111
// Tell the bundler where to find node_modules.
94112
// We use our custom `NODE_MODULES`, and then the cwd's `node_modules`.
95-
modules: [NODE_MODULES, 'node_modules'],
113+
modules: [await getNodeModules(), 'node_modules'],
96114
},
97115

98116
// Array of plugins to apply during the build.

0 commit comments

Comments
 (0)