Skip to content

Commit fec1b7a

Browse files
committed
Fix circular issues with dist/blessed
1 parent 207e829 commit fec1b7a

File tree

8 files changed

+678
-35
lines changed

8 files changed

+678
-35
lines changed

.config/rollup.base.config.mjs

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import commonjsPlugin from '@rollup/plugin-commonjs'
66
import jsonPlugin from '@rollup/plugin-json'
77
import { nodeResolve } from '@rollup/plugin-node-resolve'
88
import replacePlugin from '@rollup/plugin-replace'
9+
import MagicString from 'magic-string'
910
import { readPackageUpSync } from 'read-package-up'
1011
import rangesIntersect from 'semver/ranges/intersects.js'
1112
import { purgePolyfills } from 'unplugin-purge-polyfills'
@@ -16,6 +17,7 @@ import {
1617
readPackageJsonSync
1718
} from '@socketsecurity/registry/lib/packages'
1819
import { isRelative } from '@socketsecurity/registry/lib/path'
20+
import { escapeRegExp } from '@socketsecurity/registry/lib/regexps'
1921
import { spawnSync } from '@socketsecurity/registry/lib/spawn'
2022

2123
import constants from '../scripts/constants.js'
@@ -53,12 +55,13 @@ const {
5355
VITEST
5456
} = constants
5557

58+
export const EXTERNAL_PACKAGES = ['blessed']
59+
5660
export const INLINED_PACKAGES = [
5761
'@babel/runtime',
58-
// 'blessed' package dependencies.
62+
// 'blessed-contrib' package dependencies.
5963
'ansi-escapes',
6064
'ansi-regex',
61-
'blessed',
6265
'blessed-contrib',
6366
'bresenham',
6467
'buffers',
@@ -98,6 +101,7 @@ const checkRequireAssignmentRegExp = new RegExp(
98101
requireAssignmentsRegExp.source,
99102
''
100103
)
104+
101105
const checkSocketInteropUseRegExp = new RegExp(`\\b${SOCKET_INTEROP}\\b`)
102106

103107
const danglingRequiresRegExp = /^\s*require\(["'].+?["']\);?\r?\n/gm
@@ -106,8 +110,11 @@ const firstUseStrictRegExp = /'use strict';?/
106110

107111
const requireTinyColorsRegExp = /require\(["']tiny-colors["']\)/g
108112

113+
const requireBlessedAssignmentRegExp =
114+
/(?<=var +)[$\w]+(?= *= *require\(["']blessed["']\))/
115+
109116
const requireUrlAssignmentRegExp =
110-
/(?<=var +)[$\w]+(?= *= *require\('node:url'\))/
117+
/(?<=var +)[$\w]+(?= *= *require\(["']node:url["']\))/
111118

112119
const splitUrlRequiresRegExp = /require\(["']u["']\s*\+\s*["']rl["']\)/g
113120

@@ -217,7 +224,11 @@ export default function baseConfig(extendConfig = {}) {
217224
}
218225
const id = normalizeId(id_)
219226
const name = getPackageName(id)
220-
if (pkgOverrides[name] || isBlessedPackageName(name)) {
227+
if (
228+
pkgOverrides[name] ||
229+
isBlessedPackageName(name) ||
230+
EXTERNAL_PACKAGES.includes(name)
231+
) {
221232
return true
222233
}
223234
if (
@@ -404,13 +415,47 @@ export default function baseConfig(extendConfig = {}) {
404415
find: danglingRequiresRegExp,
405416
replace: ''
406417
}),
418+
// Replace hoisted requires of ./dist/blessed modules with lazily loaded
419+
// variants.
420+
{
421+
name: 'modify-blessed-requires',
422+
renderChunk(code) {
423+
const s = new MagicString(code)
424+
const varName = requireBlessedAssignmentRegExp.exec(code)?.[0]
425+
if (varName) {
426+
const varNameAssignmentRegExp = new RegExp(
427+
`var\\s+${escapeRegExp(varName)}\\s*=.+`,
428+
'g'
429+
)
430+
let match
431+
while ((match = varNameAssignmentRegExp.exec(code)) !== null) {
432+
s.overwrite(match.index, match.index + match[0].length, '')
433+
}
434+
const varNameRegExp = new RegExp(
435+
`(?<!var\\s+)\\b${escapeRegExp(varName)}\\b`,
436+
'g'
437+
)
438+
while ((match = varNameRegExp.exec(code)) !== null) {
439+
s.overwrite(
440+
match.index,
441+
match.index + match[0].length,
442+
"require('../blessed/lib/blessed')"
443+
)
444+
}
445+
}
446+
return {
447+
code: s.toString(),
448+
map: s.generateMap()
449+
}
450+
}
451+
},
407452
commonjsPlugin({
408453
defaultIsModuleExports: true,
409454
extensions: ['.cjs', '.js', '.ts', `.ts${ROLLUP_ENTRY_SUFFIX}`],
410455
ignoreDynamicRequires: true,
411456
ignoreGlobal: true,
412457
ignoreTryCatch: true,
413-
strictRequires: 'auto'
458+
strictRequires: true
414459
}),
415460
// Wrap require calls with SOCKET_INTEROP helper.
416461
socketModifyPlugin({

.config/rollup.dist.config.mjs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ import {
1616
import { isRelative } from '@socketsecurity/registry/lib/path'
1717
import { naturalCompare } from '@socketsecurity/registry/lib/sorts'
1818

19-
import baseConfig, { INLINED_PACKAGES } from './rollup.base.config.mjs'
19+
import baseConfig, {
20+
EXTERNAL_PACKAGES,
21+
INLINED_PACKAGES
22+
} from './rollup.base.config.mjs'
2023
import constants from '../scripts/constants.js'
21-
import socketModifyPlugin from '../scripts/rollup/socket-modify-plugin.js'
2224
import {
2325
getPackageName,
2426
getPackageNameEnd,
@@ -66,9 +68,6 @@ const VENDOR_JS = `${VENDOR}.js`
6668
const distModuleSyncPath = path.join(rootDistPath, MODULE_SYNC)
6769
const distRequirePath = path.join(rootDistPath, REQUIRE)
6870

69-
const injectBlessedTermInfoRegExp = /__dirname\s*\+\s*["']\/..\/usr\//g
70-
const requireBlessedWidgetsRegExp = /require\s*\(\s*["'].\/widgets\//g
71-
7271
const sharedInputs = {
7372
cli: `${rootSrcPath}/cli.ts`,
7473
[CONSTANTS]: `${rootSrcPath}/constants.ts`,
@@ -100,16 +99,6 @@ const sharedPlugins = [
10099
},
101100
{}
102101
)
103-
}),
104-
// Replace 'blessed' this.injectTerminfo calls to point to ./dist/blessed/usr.
105-
socketModifyPlugin({
106-
find: injectBlessedTermInfoRegExp,
107-
replace: "__dirname + '/../blessed/usr/"
108-
}),
109-
// Replace require calls for 'blessed' widgets to point to ./dist/blessed/lib/widgets.
110-
socketModifyPlugin({
111-
find: requireBlessedWidgetsRegExp,
112-
replace: "require('../blessed/lib/widgets/"
113102
})
114103
]
115104

@@ -402,6 +391,9 @@ export default () => {
402391
}
403392
const id = normalizeId(id_)
404393
const name = getPackageName(id)
394+
if (EXTERNAL_PACKAGES.includes(name)) {
395+
return true
396+
}
405397
if (
406398
INLINED_PACKAGES.includes(name) ||
407399
// Inline local src/ modules.

.config/rollup.test.config.mjs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,39 @@ import replacePlugin from '@rollup/plugin-replace'
33
import { isValidPackageName } from '@socketsecurity/registry/lib/packages'
44
import { isRelative } from '@socketsecurity/registry/lib/path'
55

6-
import baseConfig, { INLINED_PACKAGES } from './rollup.base.config.mjs'
6+
import baseConfig, {
7+
EXTERNAL_PACKAGES,
8+
INLINED_PACKAGES
9+
} from './rollup.base.config.mjs'
710
import constants from '../scripts/constants.js'
811
import {
912
getPackageName,
13+
getPackageNameEnd,
1014
isBuiltin,
11-
normalizeId
15+
normalizeId,
16+
resolveId
1217
} from '../scripts/utils/packages.js'
1318

14-
const { INLINED_SOCKET_CLI_TEST_DIST_BUILD, ROLLUP_EXTERNAL_SUFFIX } = constants
19+
const {
20+
INLINED_SOCKET_CLI_TEST_DIST_BUILD,
21+
ROLLUP_EXTERNAL_SUFFIX,
22+
SLASH_NODE_MODULES_SLASH
23+
} = constants
24+
25+
function isAncestorsExternal(id) {
26+
let currNmIndex = id.indexOf(SLASH_NODE_MODULES_SLASH)
27+
while (currNmIndex !== -1) {
28+
const nextNmIndex = id.indexOf(SLASH_NODE_MODULES_SLASH, currNmIndex + 1)
29+
const nameStart = currNmIndex + SLASH_NODE_MODULES_SLASH.length
30+
const nameEnd = getPackageNameEnd(id, nameStart)
31+
const name = id.slice(nameStart, nameEnd)
32+
if (INLINED_PACKAGES.includes(name)) {
33+
return false
34+
}
35+
currNmIndex = nextNmIndex
36+
}
37+
return true
38+
}
1539

1640
export default () => {
1741
// Lazily access constants.rootSrcPath
@@ -36,12 +60,15 @@ export default () => {
3660
// Lazily access constants.SUPPORTS_SYNC_ESM
3761
...(constants.SUPPORTS_SYNC_ESM
3862
? {
39-
external(id_) {
63+
external(id_, parentId_) {
4064
if (id_.endsWith(ROLLUP_EXTERNAL_SUFFIX) || isBuiltin(id_)) {
4165
return true
4266
}
4367
const id = normalizeId(id_)
4468
const name = getPackageName(id)
69+
if (EXTERNAL_PACKAGES.includes(name)) {
70+
return true
71+
}
4572
if (
4673
INLINED_PACKAGES.includes(name) ||
4774
id.startsWith(rootSrcPath) ||
@@ -52,6 +79,14 @@ export default () => {
5279
) {
5380
return false
5481
}
82+
const parentId = parentId_ ? resolveId(parentId_) : undefined
83+
if (parentId && !isAncestorsExternal(parentId)) {
84+
return false
85+
}
86+
const resolvedId = resolveId(id, parentId)
87+
if (!isAncestorsExternal(resolvedId)) {
88+
return false
89+
}
5590
return true
5691
}
5792
}

0 commit comments

Comments
 (0)