Skip to content

Commit 06df698

Browse files
authored
fix(perf): lazy load glob on normalize.js (#89)
Since npm run didn't call those steps, we can save some `ms` by lazy loading this dependency. Before: ![image](https://github.com/npm/package-json/assets/12551007/b4654691-85c8-4431-ba60-f7188363a125) After: ![image](https://github.com/npm/package-json/assets/12551007/f3b02669-2070-4171-aeda-90954c55f54d) The entire load time didn't change, still `13ms` before & after, but if we call `npm run` inside a project that didn't have workspaces, we will save some `ms`, like this one: ![image](https://github.com/npm/package-json/assets/12551007/0fe98367-4967-40f2-8d71-2a1b8c4eda55) But this will also require the PR npm/cli#7360 to be merged.
1 parent d4814d4 commit 06df698

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

lib/normalize.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
const valid = require('semver/functions/valid')
22
const clean = require('semver/functions/clean')
33
const fs = require('fs/promises')
4-
const { glob } = require('glob')
54
const path = require('path')
65
const log = require('proc-log')
76
const hostedGitInfo = require('hosted-git-info')
87

8+
/**
9+
* @type {import('glob').glob}
10+
*/
11+
let _glob
12+
function lazyLoadGlob () {
13+
if (!_glob) {
14+
_glob = require('glob').glob
15+
}
16+
return _glob
17+
}
18+
919
// used to be npm-normalize-package-bin
1020
function normalizePackageBin (pkg, changes) {
1121
if (pkg.bin) {
@@ -206,7 +216,7 @@ const normalize = async (pkg, { strict, steps, root, changes, allowLegacyCase })
206216
// add "install" attribute if any "*.gyp" files exist
207217
if (steps.includes('gypfile')) {
208218
if (!scripts.install && !scripts.preinstall && data.gypfile !== false) {
209-
const files = await glob('*.gyp', { cwd: pkg.path })
219+
const files = await lazyLoadGlob()('*.gyp', { cwd: pkg.path })
210220
if (files.length) {
211221
scripts.install = 'node-gyp rebuild'
212222
data.scripts = scripts
@@ -273,7 +283,11 @@ const normalize = async (pkg, { strict, steps, root, changes, allowLegacyCase })
273283
// populate "readme" attribute
274284
if (steps.includes('readme') && !data.readme) {
275285
const mdre = /\.m?a?r?k?d?o?w?n?$/i
276-
const files = await glob('{README,README.*}', { cwd: pkg.path, nocase: true, mark: true })
286+
const files = await lazyLoadGlob()('{README,README.*}', {
287+
cwd: pkg.path,
288+
nocase: true,
289+
mark: true,
290+
})
277291
let readmeFile
278292
for (const file of files) {
279293
// don't accept directories.
@@ -304,7 +318,7 @@ const normalize = async (pkg, { strict, steps, root, changes, allowLegacyCase })
304318
if (steps.includes('mans') && !data.man && data.directories?.man) {
305319
const manDir = data.directories.man
306320
const cwd = path.resolve(pkg.path, manDir)
307-
const files = await glob('**/*.[0-9]', { cwd })
321+
const files = await lazyLoadGlob()('**/*.[0-9]', { cwd })
308322
data.man = files.map(man =>
309323
path.relative(pkg.path, path.join(cwd, man)).split(path.sep).join('/')
310324
)
@@ -317,7 +331,7 @@ const normalize = async (pkg, { strict, steps, root, changes, allowLegacyCase })
317331
// expand "directories.bin"
318332
if (steps.includes('binDir') && data.directories?.bin && !data.bin) {
319333
const binsDir = path.resolve(pkg.path, path.join('.', path.join('/', data.directories.bin)))
320-
const bins = await glob('**', { cwd: binsDir })
334+
const bins = await lazyLoadGlob()('**', { cwd: binsDir })
321335
data.bin = bins.reduce((acc, binFile) => {
322336
if (binFile && !binFile.startsWith('.')) {
323337
const binName = path.basename(binFile)

0 commit comments

Comments
 (0)