mirror of
https://github.com/stablyai/orca.git
synced 2026-09-22 00:02:31 +00:00
refactor(packaging): prune declaration and source-map artifacts in one walk (#17659)
* refactor(packaging): prune declaration and source-map artifacts in one walk prunePackagedRuntimeTypeDeclarations and prunePackagedRuntimeSourceMaps were byte-identical apart from their regex, and each did its own full recursive walk of packaged Resources/node_modules (~1.7s per walk). Collapse them into prunePackagedRuntimeTypeAndSourceMapArtifacts, which runs a single walk with the OR of both predicates. The two regexes are disjoint (.d.ts.map never ends in .js.map), so one pass deletes exactly the union the two passes deleted. Neither old function had a production caller outside prunePackagedRuntimeNodeModules, so both exports are replaced by the combined one rather than kept as wrappers, which would have reintroduced the duplicate walk. Also moves prunePackagedZodSources ahead of the filename walk: zod/src is removed wholesale, so traversing it first was pure wasted work. The prunes are independent, so the reorder does not change the result. * fix: correct the one-walk rationale and close the .d.mts coverage gap The comment credited predicate disjointness for making the merge safe. That is not the reason and is misleading: it implies a future overlapping predicate would break the collapse. Passes commute because pruneMatchingFiles only deletes files and never removes directories, so the tree it walks is identical each time — verified by running the old two-walk code with the passes reversed and diffing survivors. Also narrow isPrunablePackagedRuntimeArtifact to isPrunableTypeOrSourceMapArtifact (node-pty prebuilds and duplicate sherpa dylibs are prunable runtime artifacts too, but this predicate returns false for them), and add the missing .d.mts fixture so every branch of the (?:c|m)? alternation is exercised against the exact-survivor assertion.
This commit is contained in:
@@ -439,24 +439,22 @@ function prunePackagedParcelWatcher(resourcesDir, electronPlatformName, electron
|
||||
}
|
||||
}
|
||||
|
||||
function prunePackagedRuntimeTypeDeclarations(resourcesDir) {
|
||||
const nodeModulesDir = join(resourcesDir, 'node_modules')
|
||||
if (!existsSync(nodeModulesDir)) {
|
||||
return
|
||||
}
|
||||
pruneMatchingFiles(nodeModulesDir, (filename) => TYPE_DECLARATION_ARTIFACT_RE.test(filename))
|
||||
// Why type declarations: they are compile-time only; the packaged app never resolves them.
|
||||
// Why source maps: they embed the original sources (megabytes for @linear/sdk alone) and
|
||||
// nothing in the packaged app turns on Node's source-map support, so they are never read.
|
||||
// Orca's own main-process maps live outside node_modules and ship as a separate release artifact.
|
||||
function isPrunableTypeOrSourceMapArtifact(filename) {
|
||||
return TYPE_DECLARATION_ARTIFACT_RE.test(filename) || JS_SOURCE_MAP_ARTIFACT_RE.test(filename)
|
||||
}
|
||||
|
||||
function prunePackagedRuntimeSourceMaps(resourcesDir) {
|
||||
// Why: dependency source maps embed the original sources (megabytes for
|
||||
// @linear/sdk alone), and nothing in the packaged app turns on Node's
|
||||
// source-map support, so they are never read. Orca's own main-process maps
|
||||
// live outside node_modules and ship as a separate release artifact.
|
||||
// Why one walk: pruneMatchingFiles only ever deletes files, so passes over the same tree
|
||||
// commute — a second recursive traversal costs seconds for no extra deletions.
|
||||
function prunePackagedRuntimeTypeAndSourceMapArtifacts(resourcesDir) {
|
||||
const nodeModulesDir = join(resourcesDir, 'node_modules')
|
||||
if (!existsSync(nodeModulesDir)) {
|
||||
return
|
||||
}
|
||||
pruneMatchingFiles(nodeModulesDir, (filename) => JS_SOURCE_MAP_ARTIFACT_RE.test(filename))
|
||||
pruneMatchingFiles(nodeModulesDir, isPrunableTypeOrSourceMapArtifact)
|
||||
}
|
||||
|
||||
function prunePackagedSherpaOnnx(resourcesDir, electronPlatformName) {
|
||||
@@ -494,10 +492,10 @@ function prunePackagedRuntimeNodeModules(resourcesDir, electronPlatformName, ele
|
||||
const architecture = normalizeElectronArchitecture(electronArch)
|
||||
prunePackagedNodePty(resourcesDir, electronPlatformName, architecture)
|
||||
prunePackagedParcelWatcher(resourcesDir, electronPlatformName, architecture)
|
||||
prunePackagedRuntimeTypeDeclarations(resourcesDir)
|
||||
prunePackagedRuntimeSourceMaps(resourcesDir)
|
||||
prunePackagedSherpaOnnx(resourcesDir, electronPlatformName)
|
||||
// Why before the filename walk: zod/src is deleted wholesale, so walking it first is wasted work.
|
||||
prunePackagedZodSources(resourcesDir)
|
||||
prunePackagedRuntimeTypeAndSourceMapArtifacts(resourcesDir)
|
||||
prunePackagedSherpaOnnx(resourcesDir, electronPlatformName)
|
||||
}
|
||||
|
||||
function pruneMatchingFiles(directory, shouldPrune) {
|
||||
@@ -520,9 +518,8 @@ module.exports = {
|
||||
prunePackagedNodePty,
|
||||
prunePackagedParcelWatcher,
|
||||
prunePackagedRuntimeNodeModules,
|
||||
prunePackagedRuntimeSourceMaps,
|
||||
prunePackagedRuntimeTypeAndSourceMapArtifacts,
|
||||
prunePackagedSherpaOnnx,
|
||||
prunePackagedRuntimeTypeDeclarations,
|
||||
prunePackagedZodSources,
|
||||
verifyPackagedMainRuntimeDeps
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ const {
|
||||
prunePackagedNodePty,
|
||||
prunePackagedParcelWatcher,
|
||||
prunePackagedSherpaOnnx,
|
||||
prunePackagedRuntimeTypeDeclarations,
|
||||
prunePackagedRuntimeTypeAndSourceMapArtifacts,
|
||||
prunePackagedZodSources,
|
||||
verifyPackagedMainRuntimeDeps
|
||||
} = require('../packaged-runtime-node-modules.cjs')
|
||||
@@ -534,9 +534,11 @@ describe('electron-builder config', () => {
|
||||
await writeFile(join(packageDir, 'dist', 'index.cjs'), 'module.exports = {}', 'utf8')
|
||||
await writeFile(join(packageDir, 'dist', 'index.d.ts'), 'export type Value = string', 'utf8')
|
||||
await writeFile(join(packageDir, 'dist', 'index.d.cts'), 'export type Value = string', 'utf8')
|
||||
await writeFile(join(packageDir, 'dist', 'index.d.mts'), 'export type Value = string', 'utf8')
|
||||
await writeFile(join(packageDir, 'dist', 'index.d.cts.map'), '{}', 'utf8')
|
||||
await writeFile(join(packageDir, 'dist', 'index.d.mts.map'), '{}', 'utf8')
|
||||
|
||||
prunePackagedRuntimeTypeDeclarations(resourcesDir)
|
||||
prunePackagedRuntimeTypeAndSourceMapArtifacts(resourcesDir)
|
||||
|
||||
await expect(readdir(join(packageDir, 'dist'))).resolves.toEqual(['index.cjs'])
|
||||
} finally {
|
||||
|
||||
@@ -6,7 +6,7 @@ import { describe, expect, it } from 'vitest'
|
||||
|
||||
const require = createRequire(import.meta.url)
|
||||
const {
|
||||
prunePackagedRuntimeSourceMaps,
|
||||
prunePackagedRuntimeTypeAndSourceMapArtifacts,
|
||||
prunePackagedRuntimeNodeModules
|
||||
} = require('../packaged-runtime-node-modules.cjs')
|
||||
|
||||
@@ -54,14 +54,14 @@ async function createPackagedNodeModulesFixture(resourcesDir) {
|
||||
return { packageDir, distDir, webhooksDir, updaterDir, jsYamlDir, nodePtyDir }
|
||||
}
|
||||
|
||||
describe('packaged runtime source-map pruning', () => {
|
||||
describe('packaged runtime type-declaration and source-map pruning', () => {
|
||||
it('removes @linear/sdk source maps while preserving runtime files and non-JS maps', async () => {
|
||||
const resourcesDir = await mkdtemp(join(tmpdir(), 'orca-source-map-prune-'))
|
||||
try {
|
||||
const { packageDir, distDir, webhooksDir } =
|
||||
await createPackagedNodeModulesFixture(resourcesDir)
|
||||
|
||||
prunePackagedRuntimeSourceMaps(resourcesDir)
|
||||
prunePackagedRuntimeTypeAndSourceMapArtifacts(resourcesDir)
|
||||
|
||||
await expect(readdir(distDir).then((entries) => entries.sort())).resolves.toEqual([
|
||||
'index.cjs',
|
||||
@@ -91,7 +91,7 @@ describe('packaged runtime source-map pruning', () => {
|
||||
try {
|
||||
const { jsYamlDir, nodePtyDir } = await createPackagedNodeModulesFixture(resourcesDir)
|
||||
|
||||
prunePackagedRuntimeSourceMaps(resourcesDir)
|
||||
prunePackagedRuntimeTypeAndSourceMapArtifacts(resourcesDir)
|
||||
|
||||
await expect(readdir(jsYamlDir)).resolves.toEqual(['js-yaml.min.js'])
|
||||
await expect(readdir(nodePtyDir)).resolves.toEqual(['index.js'])
|
||||
@@ -100,25 +100,20 @@ describe('packaged runtime source-map pruning', () => {
|
||||
}
|
||||
})
|
||||
|
||||
it('leaves declaration-map cleanup to the type-declaration prune', async () => {
|
||||
it('removes type declarations and declaration maps in the same walk', async () => {
|
||||
const resourcesDir = await mkdtemp(join(tmpdir(), 'orca-source-map-prune-dts-'))
|
||||
try {
|
||||
const { updaterDir } = await createPackagedNodeModulesFixture(resourcesDir)
|
||||
|
||||
prunePackagedRuntimeSourceMaps(resourcesDir)
|
||||
prunePackagedRuntimeTypeAndSourceMapArtifacts(resourcesDir)
|
||||
|
||||
// Why: the two predicates are disjoint -- `.d.ts.map` never ends in `.js.map`.
|
||||
await expect(readdir(updaterDir).then((entries) => entries.sort())).resolves.toEqual([
|
||||
'main.d.ts',
|
||||
'main.d.ts.map',
|
||||
'main.js'
|
||||
])
|
||||
await expect(readdir(updaterDir)).resolves.toEqual(['main.js'])
|
||||
} finally {
|
||||
await rm(resourcesDir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
it('runs the source-map prune through aggregate runtime cleanup', async () => {
|
||||
it('runs the artifact prune through aggregate runtime cleanup', async () => {
|
||||
const resourcesDir = await mkdtemp(join(tmpdir(), 'orca-source-map-aggregate-prune-'))
|
||||
try {
|
||||
const { distDir, updaterDir, packageDir } =
|
||||
|
||||
Reference in New Issue
Block a user