Skip to content

Commit 7e7fbae

Browse files
authored
perf(cli): load @babel/parser on first use in contribution validation (#11256)
[perf] Contribution validation now loads `@babel/parser` only when it parses a contribution's source, so a run where nothing else loads the parser, such as `component <Name> --json` or `hook --list`, no longer loads it. Every command prints the same output.
1 parent 59fced7 commit 7e7fbae

3 files changed

Lines changed: 33 additions & 13 deletions

File tree

‎.changeset/lazy-babel-parser.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/cli': patch
3+
---
4+
5+
[perf] Contribution validation now loads `@babel/parser` only when it parses a contribution's source, so a run where nothing else loads the parser, such as `component <Name> --json` or `hook --list`, no longer loads it. Every command prints the same output.
6+
7+
@josephfarina

‎packages/cli/foundation/doc-compiler/doc-loads.test.mjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,8 @@ const RUNNERS = {
449449
sites: ['importUserModule'],
450450
},
451451
'foundation/integrations/validate-contributions.mjs': {
452-
runs: "`npm pack --dry-run --json --ignore-scripts`, to read which files an integration's package holds when a template loads an asset (no lifecycle script runs, nothing is written)",
453-
sites: ['child_process.spawnSync'],
452+
runs: "`npm pack --dry-run --json --ignore-scripts`, to read which files an integration's package holds when a template loads an asset (no lifecycle script runs, nothing is written); and @babel/parser, loaded on first use, to read a contribution's source",
453+
sites: ['child_process.spawnSync', 'require(@babel/parser)'],
454454
},
455455
'foundation/release/cache.mjs': {
456456
runs: 'npm (under npm-guard.mjs), to install and look up CLI releases in the per-user cache; each installed release once, to check it starts and reports its version; and ps (tasklist on Windows), to confirm the process a stale lock records is node or npm before ending it',

‎packages/cli/foundation/integrations/validate-contributions.mjs‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
import * as fs from 'node:fs';
2525
import * as path from 'node:path';
2626
import {spawnSync} from 'node:child_process';
27-
import {builtinModules} from 'node:module';
28-
import {parse as parseModule} from '@babel/parser';
27+
import {builtinModules, createRequire} from 'node:module';
2928
import {isValidSemver} from '../env/semver.mjs';
3029
import {findProjectCoreDir} from '../fs/paths.mjs';
3130
import {KNOWN_INTEGRATION_KEYS} from '../../authoring/integration/schema.mjs';
@@ -64,6 +63,18 @@ import {
6463
import {discoverIntegrationDocs} from '../discovery/docs-discovery.mjs';
6564
import {discoverIntegrationThemes} from '../discovery/theme-discovery.mjs';
6665

66+
const requireFromHere = createRequire(import.meta.url);
67+
68+
/**
69+
* `@babel/parser`'s parse, loaded on first use: most commands that load the
70+
* project's integrations never validate a contribution's source.
71+
* @param {string} source
72+
* @param {import('@babel/parser').ParserOptions} [options]
73+
*/
74+
function parseModule(source, options) {
75+
return requireFromHere('@babel/parser').parse(source, options);
76+
}
77+
6778
/**
6879
* @typedef {import('./issue').AstryxIntegrationIssue} Issue
6980
* @typedef {import('./integrations.mjs').LoadedIntegration} LoadedIntegration
@@ -1647,15 +1658,17 @@ export async function componentExamplesCheck(integration) {
16471658
: negated != null && !outsideFiles(assetRel, packedFiles)
16481659
? `${rel(template.filePath)}: loads ${assetRel}, which the \`files\` entry "${negated}" leaves out of the package, so the template breaks once installed. Fix: drop or narrow "${negated}" so it no longer matches ${assetRel}, and confirm with \`npm pack --dry-run\`.`
16491660
: Array.isArray(packedFiles) &&
1650-
(outsideFiles(assetRel, packedFiles) ||
1651-
!ignoreFilesFor(base, assetRel).some(file => file.includes('/')))
1652-
? `${rel(template.filePath)}: loads ${assetRel}, which npm leaves out of the package by its \`files\` field, so the template breaks once installed. Fix: add "${folder}" to \`files\`, drop any \`!\` entry that excludes it, and confirm with \`npm pack --dry-run\`.`
1653-
: ignoreFix(
1654-
base,
1655-
rel(template.filePath),
1656-
assetRel,
1657-
Array.isArray(packedFiles),
1658-
),
1661+
(outsideFiles(assetRel, packedFiles) ||
1662+
!ignoreFilesFor(base, assetRel).some(file =>
1663+
file.includes('/'),
1664+
))
1665+
? `${rel(template.filePath)}: loads ${assetRel}, which npm leaves out of the package by its \`files\` field, so the template breaks once installed. Fix: add "${folder}" to \`files\`, drop any \`!\` entry that excludes it, and confirm with \`npm pack --dry-run\`.`
1666+
: ignoreFix(
1667+
base,
1668+
rel(template.filePath),
1669+
assetRel,
1670+
Array.isArray(packedFiles),
1671+
),
16591672
});
16601673
}
16611674
}

0 commit comments

Comments
 (0)