Skip to content

Commit 015ab6b

Browse files
committed
Support sync-module
1 parent f3571d5 commit 015ab6b

File tree

9 files changed

+195
-32
lines changed

9 files changed

+195
-32
lines changed

.config/rollup.base.config.mjs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import { readPackageUpSync } from 'read-package-up'
1010
import rangesIntersect from 'semver/ranges/intersects.js'
1111
import { purgePolyfills } from 'unplugin-purge-polyfills'
1212

13-
import constants from '@socketsecurity/registry/lib/constants'
1413
import { isRelative } from '@socketsecurity/registry/lib/path'
1514
import { escapeRegExp } from '@socketsecurity/registry/lib/regexps'
1615
import {
1716
isValidPackageName,
1817
readPackageJsonSync
1918
} from '@socketsecurity/registry/lib/packages'
2019

20+
import constants from '../scripts/constants.js'
2121
import socketModifyPlugin from '../scripts/rollup/socket-modify-plugin.js'
2222
import {
2323
getPackageName,
@@ -28,17 +28,18 @@ import {
2828
resolveId
2929
} from '../scripts/utils/packages.js'
3030

31-
const { LATEST } = constants
31+
const {
32+
LATEST,
33+
ROLLUP_ENTRY_SUFFIX,
34+
ROLLUP_EXTERNAL_SUFFIX,
35+
SLASH_NODE_MODULES_SLASH
36+
} = constants
3237

3338
const __dirname = fileURLToPath(new URL('.', import.meta.url))
3439
const require = createRequire(import.meta.url)
3540

3641
const ts = require('rollup-plugin-ts')
3742

38-
const ENTRY_SUFFIX = '?commonjs-entry'
39-
const EXTERNAL_SUFFIX = '?commonjs-external'
40-
const SLASH_NODE_MODULES_SLASH = '/node_modules/'
41-
4243
const builtinAliases = builtinModules.reduce((o, n) => {
4344
o[n] = `node:${n}`
4445
return o
@@ -91,7 +92,7 @@ function isAncestorsExternal(id, depStats) {
9192
return true
9293
}
9394

94-
export default (extendConfig = {}) => {
95+
export default function baseConfig(extendConfig = {}) {
9596
const depStats = {
9697
dependencies: { __proto__: null },
9798
devDependencies: { __proto__: null },
@@ -107,15 +108,15 @@ export default (extendConfig = {}) => {
107108
}
108109
},
109110
external(id_, parentId_) {
110-
if (id_.endsWith(EXTERNAL_SUFFIX) || isBuiltin(id_)) {
111+
if (id_.endsWith(ROLLUP_EXTERNAL_SUFFIX) || isBuiltin(id_)) {
111112
return true
112113
}
113114
const id = normalizeId(id_)
114115
if (
115116
isRelative(id) ||
117+
id.startsWith('src/') ||
116118
id.endsWith('.mjs') ||
117-
id.endsWith('.mts') ||
118-
id.endsWith('.ts')
119+
id.endsWith('.mts')
119120
) {
120121
return false
121122
}
@@ -234,13 +235,13 @@ export default (extendConfig = {}) => {
234235
}
235236
}),
236237
commonjs({
238+
defaultIsModuleExports: true,
239+
extensions: ['.cjs', '.js', '.ts', `.ts${ROLLUP_ENTRY_SUFFIX}`],
237240
ignoreDynamicRequires: true,
238241
ignoreGlobal: true,
239242
ignoreTryCatch: true,
240-
defaultIsModuleExports: true,
241243
strictRequires: 'auto',
242-
transformMixedEsModules: true,
243-
extensions: ['.cjs', '.js', '.ts', `.ts${ENTRY_SUFFIX}`]
244+
transformMixedEsModules: true
244245
}),
245246
...(extendConfig.plugins ?? [])
246247
]

.config/rollup.dist.config.mjs

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,34 @@ import { fileURLToPath } from 'node:url'
44

55
import { toSortedObject } from '@socketsecurity/registry/lib/objects'
66
import { readPackageJsonSync } from '@socketsecurity/registry/lib/packages'
7+
import { isRelative } from '@socketsecurity/registry/lib/path'
78

89
import baseConfig from './rollup.base.config.mjs'
10+
import constants from '../scripts/constants.js'
911
import { readJsonSync } from '../scripts/utils/fs.js'
1012
import { formatObject } from '../scripts/utils/objects.js'
13+
import { normalizeId, isBuiltin } from '../scripts/utils/packages.js'
14+
15+
const { ROLLUP_EXTERNAL_SUFFIX } = constants
1116

1217
const __dirname = fileURLToPath(new URL('.', import.meta.url))
1318

1419
const rootPath = path.resolve(__dirname, '..')
1520
const depStatsPath = path.join(rootPath, '.dep-stats.json')
1621
const distPath = path.join(rootPath, 'dist')
22+
const distLegacyPath = path.join(rootPath, 'dist-legacy')
1723
const srcPath = path.join(rootPath, 'src')
1824

25+
const binBasenames = ['cli.js', 'npm-cli.js', 'npx-cli.js']
1926
const editablePkgJson = readPackageJsonSync(rootPath, { editable: true })
2027

28+
function setBinPerm(filepath) {
29+
// Make file chmod +x.
30+
chmodSync(filepath, 0o755)
31+
}
32+
2133
export default () => {
22-
const config = baseConfig({
34+
const legacyConfig = baseConfig({
2335
input: {
2436
cli: `${srcPath}/cli.ts`,
2537
'npm-cli': `${srcPath}/shadow/npm-cli.ts`,
@@ -28,7 +40,7 @@ export default () => {
2840
},
2941
output: [
3042
{
31-
dir: 'dist',
43+
dir: 'dist-legacy',
3244
entryFileNames: '[name].js',
3345
format: 'cjs',
3446
exports: 'auto',
@@ -42,7 +54,7 @@ export default () => {
4254
const { content: pkgJson } = editablePkgJson
4355
const { '@cyclonedx/cdxgen': cdxgenRange, synp: synpRange } =
4456
pkgJson.dependencies
45-
const { depStats } = config.meta
57+
const { depStats } = legacyConfig.meta
4658

4759
// Manually add @cyclonedx/cdxgen and synp as they are not directly
4860
// referenced in the code but used through spawned processes.
@@ -71,10 +83,7 @@ export default () => {
7183

7284
// Write dep stats
7385
writeFileSync(depStatsPath, `${formatObject(depStats)}\n`, 'utf8')
74-
// Make dist files chmod +x
75-
chmodSync(path.join(distPath, 'cli.js'), 0o755)
76-
chmodSync(path.join(distPath, 'npm-cli.js'), 0o755)
77-
chmodSync(path.join(distPath, 'npx-cli.js'), 0o755)
86+
7887
// Update dependencies with additional inlined modules
7988
editablePkgJson
8089
.update({
@@ -84,10 +93,49 @@ export default () => {
8493
}
8594
})
8695
.saveSync()
96+
97+
for (const binBasename of binBasenames) {
98+
setBinPerm(path.join(distLegacyPath, binBasename))
99+
}
100+
}
101+
}
102+
]
103+
})
104+
105+
const syncEsmConfig = baseConfig({
106+
input: {
107+
cli: `${srcPath}/cli.ts`,
108+
'npm-cli': `${srcPath}/shadow/npm-cli.ts`,
109+
'npx-cli': `${srcPath}/shadow/npx-cli.ts`,
110+
'npm-injection': `${srcPath}/shadow/npm-injection.ts`
111+
},
112+
output: [
113+
{
114+
dir: 'dist',
115+
entryFileNames: '[name].js',
116+
format: 'cjs',
117+
exports: 'auto',
118+
externalLiveBindings: false,
119+
freeze: false
120+
}
121+
],
122+
external(id_) {
123+
if (id_.endsWith(ROLLUP_EXTERNAL_SUFFIX) || isBuiltin(id_)) {
124+
return true
125+
}
126+
const id = normalizeId(id_)
127+
return !(isRelative(id) || id.startsWith(srcPath))
128+
},
129+
plugins: [
130+
{
131+
writeBundle() {
132+
for (const binBasename of binBasenames) {
133+
setBinPerm(path.join(distPath, binBasename))
134+
}
87135
}
88136
}
89137
]
90138
})
91139

92-
return config
140+
return [legacyConfig, syncEsmConfig]
93141
}

.config/rollup.test.config.mjs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import path from 'node:path'
22
import { fileURLToPath } from 'node:url'
33

4+
import { isRelative } from '@socketsecurity/registry/lib/path'
5+
46
import baseConfig from './rollup.base.config.mjs'
7+
import constants from '../scripts/constants.js'
8+
import { normalizeId, isBuiltin } from '../scripts/utils/packages.js'
9+
10+
const { ROLLUP_EXTERNAL_SUFFIX, SUPPORTS_SYNC_ESM } = constants
511

612
const __dirname = fileURLToPath(new URL('.', import.meta.url))
713

@@ -23,5 +29,16 @@ export default () =>
2329
externalLiveBindings: false,
2430
freeze: false
2531
}
26-
]
32+
],
33+
...(SUPPORTS_SYNC_ESM
34+
? {
35+
external(id_) {
36+
if (id_.endsWith(ROLLUP_EXTERNAL_SUFFIX) || isBuiltin(id_)) {
37+
return true
38+
}
39+
const id = normalizeId(id_)
40+
return !(isRelative(id) || id.startsWith(srcPath))
41+
}
42+
}
43+
: {})
2744
})

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Thumbs.db
88
/coverage
99
/npm-debug.log
1010
**/dist
11+
**/dist-legacy
1112
**/node_modules
1213
/*.tsbuildinfo
1314
*.d.ts

package.json

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,25 @@
1414
"url": "https://socket.dev"
1515
},
1616
"bin": {
17-
"socket": "dist/cli.js",
18-
"socket-npm": "dist/npm-cli.js",
19-
"socket-npx": "dist/npx-cli.js"
17+
"socket": "./dist/cli.js",
18+
"socket-npm": "./dist/npm-cli.js",
19+
"socket-npx": "./dist/npx-cli.js"
20+
},
21+
"exports": {
22+
"./dist/cli.js": {
23+
"module-sync": "./dist/cli.js",
24+
"require": "./dist-legacy/cli.js"
25+
},
26+
"./dist/npm-cli.js": {
27+
"module-sync": "./dist/npm-cli.js",
28+
"require": "./dist-legacy/npm-cli.js"
29+
},
30+
"./dist/npx-cli.js": {
31+
"module-sync": "./dist/npx-cli.js",
32+
"require": "./dist-legacy/npx-cli.js"
33+
},
34+
"./package.json": "./package.json",
35+
"./translations.json": "./translations.json"
2036
},
2137
"scripts": {
2238
"build": "run-s build:*",
@@ -31,12 +47,12 @@
3147
"lint": "oxlint -c=./.oxlintrc.json --ignore-path=./.prettierignore --tsconfig=./tsconfig.json .",
3248
"lint:fix": "npm run lint -- --fix && npm run lint:fix:fast",
3349
"lint:fix:fast": "prettier --cache --log-level warn --write .",
34-
"prepare": "husky",
50+
"prepare": "husky && custompatch",
3551
"test": "run-s check build:* test:*",
3652
"test:c8": "c8 --reporter=none node --test 'test/socket-npm.test.cjs'",
3753
"test-ci": "run-s build:* test:*",
38-
"test:unit": "tap",
39-
"test:coverage": "cp -r .tap/coverage/*.json coverage/tmp && c8 --reporter=lcov --reporter=text --include 'dist/*.js' --exclude 'dist/vendor.js' report"
54+
"test:unit": "tap-run",
55+
"test:coverage": "cp -r .tap/coverage/*.json coverage/tmp && c8 --reporter=lcov --reporter=text --include 'dist{,-legacy}/*.js' --exclude 'dist-legacy/vendor.js' report"
4056
},
4157
"dependencies": {
4258
"@apideck/better-ajv-errors": "^0.3.6",
@@ -83,6 +99,7 @@
8399
"@rollup/plugin-node-resolve": "^15.3.0",
84100
"@rollup/plugin-replace": "^6.0.1",
85101
"@rollup/pluginutils": "^5.1.3",
102+
"@tapjs/run": "^4.0.1",
86103
"@types/blessed": "^0.1.25",
87104
"@types/micromatch": "^4.0.9",
88105
"@types/mocha": "^10.0.10",
@@ -98,6 +115,7 @@
98115
"@typescript-eslint/eslint-plugin": "^8.17.0",
99116
"@typescript-eslint/parser": "^8.17.0",
100117
"c8": "^10.1.2",
118+
"custompatch": "^1.0.28",
101119
"eslint": "^9.16.0",
102120
"eslint-import-resolver-oxc": "^0.5.0",
103121
"eslint-plugin-depend": "^0.12.0",
@@ -120,7 +138,6 @@
120138
"read-package-up": "^11.0.0",
121139
"rollup": "4.28.0",
122140
"rollup-plugin-ts": "^3.4.5",
123-
"tap": "^21.0.1",
124141
"terminal-link": "^3.0.0",
125142
"tiny-updater": "^3.5.2",
126143
"type-coverage": "^2.29.7",
@@ -180,6 +197,7 @@
180197
"files": [
181198
"bin/**",
182199
"dist/**",
200+
"dist-legacy/**",
183201
"translations.json"
184202
]
185203
}

patches/@tapjs+run#4.0.1.patch

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Index: /@tapjs/run/dist/esm/index.js
2+
===================================================================
3+
--- /@tapjs/run/dist/esm/index.js
4+
+++ /@tapjs/run/dist/esm/index.js
5+
@@ -5,8 +5,9 @@
6+
// same process as the @tapjs/core Base class. node:repl loads
7+
// node:domain, which conflicts with async-hook-domains used by tap
8+
import { args, config, mainCommand } from './main-config.js';
9+
process.title = 'tap';
10+
+void (async () => {
11+
switch (mainCommand) {
12+
case 'help':
13+
;
14+
(await import('./help.js')).help(args, config);
15+
@@ -52,5 +53,6 @@
16+
;
17+
(await import('./list.js')).list(args, config);
18+
break;
19+
}
20+
-//# sourceMappingURL=index.js.map
21+
\ No newline at end of file
22+
+})()
23+
+//# sourceMappingURL=index.js.map

scripts/constants.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict'
2+
3+
const semver = require('semver')
4+
5+
const registryConstants = require('@socketsecurity/registry/lib/constants')
6+
const {
7+
objectEntries,
8+
objectFromEntries
9+
} = require('@socketsecurity/registry/lib/objects')
10+
11+
const ROLLUP_ENTRY_SUFFIX = '?commonjs-entry'
12+
const ROLLUP_EXTERNAL_SUFFIX = '?commonjs-external'
13+
const SLASH_NODE_MODULES_SLASH = '/node_modules/'
14+
const SUPPORTS_SYNC_ESM = semver.satisfies(process.versions.node, '>=22.12')
15+
16+
const constants = Object.freeze(
17+
Object.defineProperties(
18+
{ __proto__: null },
19+
objectFromEntries(
20+
objectEntries(Object.getOwnPropertyDescriptors(registryConstants)).reduce(
21+
(entries, entry) => {
22+
if (entries.findIndex(p => p[0] === entry[0]) === -1) {
23+
entries.push(entry)
24+
}
25+
return entries
26+
},
27+
objectEntries(
28+
Object.getOwnPropertyDescriptors({
29+
ROLLUP_ENTRY_SUFFIX,
30+
ROLLUP_EXTERNAL_SUFFIX,
31+
SLASH_NODE_MODULES_SLASH,
32+
SUPPORTS_SYNC_ESM
33+
})
34+
)
35+
)
36+
)
37+
)
38+
)
39+
module.exports = constants

0 commit comments

Comments
 (0)