Skip to content

Commit c05c910

Browse files
committed
Move dist variants
1 parent 3f605ed commit c05c910

30 files changed

+1687
-380
lines changed

.config/rollup.base.config.mjs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { builtinModules, createRequire } from 'node:module'
22
import path from 'node:path'
3-
import { fileURLToPath } from 'node:url'
43

54
import commonjs from '@rollup/plugin-commonjs'
65
import json from '@rollup/plugin-json'
@@ -22,36 +21,37 @@ import socketModifyPlugin from '../scripts/rollup/socket-modify-plugin.js'
2221
import {
2322
getPackageName,
2423
getPackageNameEnd,
24+
isBuiltin,
2525
isEsmId,
2626
normalizeId,
27-
isBuiltin,
2827
resolveId
2928
} from '../scripts/utils/packages.js'
3029

3130
const {
3231
LATEST,
3332
ROLLUP_ENTRY_SUFFIX,
3433
ROLLUP_EXTERNAL_SUFFIX,
35-
SLASH_NODE_MODULES_SLASH
34+
SLASH_NODE_MODULES_SLASH,
35+
babelConfigPath,
36+
rootPackageJsonPath,
37+
rootPath,
38+
rootSrcPath,
39+
tsconfigPath
3640
} = constants
3741

38-
const __dirname = fileURLToPath(new URL('.', import.meta.url))
3942
const require = createRequire(import.meta.url)
4043

4144
const ts = require('rollup-plugin-ts')
4245

46+
const rootPackageJson = require(rootPackageJsonPath)
47+
const { dependencies: pkgDeps, devDependencies: pkgDevDeps } = rootPackageJson
48+
4349
const builtinAliases = builtinModules.reduce((o, n) => {
4450
o[n] = `node:${n}`
4551
return o
4652
}, {})
4753

48-
const rootPath = path.resolve(__dirname, '..')
49-
const babelConfigPath = path.join(__dirname, 'babel.config.js')
50-
const tsconfigPath = path.join(__dirname, 'tsconfig.rollup.json')
51-
5254
const babelConfig = require(babelConfigPath)
53-
const { dependencies: pkgDeps, devDependencies: pkgDevDeps } =
54-
readPackageJsonSync(rootPath)
5555

5656
const customResolver = nodeResolve({
5757
exportConditions: ['node'],
@@ -112,11 +112,20 @@ export default function baseConfig(extendConfig = {}) {
112112
return true
113113
}
114114
const id = normalizeId(id_)
115+
const name = getPackageName(id)
115116
if (
116-
isRelative(id) ||
117-
id.startsWith('src/') ||
117+
name.startsWith('@socketregistry/') ||
118+
name.startsWith('@socketsecurity/')
119+
) {
120+
return true
121+
}
122+
if (
123+
name === '@babel/runtime' ||
124+
id.startsWith(rootSrcPath) ||
118125
id.endsWith('.mjs') ||
119-
id.endsWith('.mts')
126+
id.endsWith('.mts') ||
127+
isRelative(id) ||
128+
!isValidPackageName(name)
120129
) {
121130
return false
122131
}
@@ -128,10 +137,6 @@ export default function baseConfig(extendConfig = {}) {
128137
if (!isAncestorsExternal(resolvedId, depStats)) {
129138
return false
130139
}
131-
const name = getPackageName(id)
132-
if (name === '@babel/runtime' || !isValidPackageName(name)) {
133-
return false
134-
}
135140
if (isEsmId(resolvedId, parentId)) {
136141
const parentPkg = parentId
137142
? readPackageUpSync({ cwd: path.dirname(parentId) })?.packageJson
@@ -196,6 +201,13 @@ export default function baseConfig(extendConfig = {}) {
196201
purgePolyfills.rollup({
197202
replacements: {}
198203
}),
204+
// Convert SOCKET_PACKAGE_NAME to the Socket package name.
205+
replace({
206+
preventAssignment: false,
207+
values: {
208+
SOCKET_PACKAGE_NAME: rootPackageJson.name
209+
}
210+
}),
199211
// Convert un-prefixed built-in imports into "node:"" prefixed forms.
200212
replace({
201213
delimiters: ['(?<=(?:require\\(|from\\s*)["\'])', '(?=["\'])'],
@@ -235,7 +247,7 @@ export default function baseConfig(extendConfig = {}) {
235247
}
236248
}),
237249
commonjs({
238-
defaultIsModuleExports: true,
250+
defaultIsModuleExports: 'auto',
239251
extensions: ['.cjs', '.js', '.ts', `.ts${ROLLUP_ENTRY_SUFFIX}`],
240252
ignoreDynamicRequires: true,
241253
ignoreGlobal: true,
@@ -268,15 +280,13 @@ export default function baseConfig(extendConfig = {}) {
268280
delimiters: ['(?<=["\'])', '/'],
269281
preventAssignment: false,
270282
values: {
271-
[rootPath]: '../'
283+
[rootPath]: '../../'
272284
}
273285
})
274-
275286
const replaceOutputPlugin = {
276287
name: replacePlugin.name,
277288
renderChunk: replacePlugin.renderChunk
278289
}
279-
280290
for (const o of output) {
281291
o.plugins = [
282292
...(Array.isArray(o.plugins) ? o.plugins : []),

.config/rollup.dist.config.mjs

Lines changed: 68 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { chmodSync, writeFileSync } from 'node:fs'
1+
import { chmodSync, existsSync, writeFileSync } from 'node:fs'
22
import path from 'node:path'
3-
import { fileURLToPath } from 'node:url'
43

54
import { toSortedObject } from '@socketsecurity/registry/lib/objects'
65
import { readPackageJsonSync } from '@socketsecurity/registry/lib/packages'
@@ -12,15 +11,16 @@ import { readJsonSync } from '../scripts/utils/fs.js'
1211
import { formatObject } from '../scripts/utils/objects.js'
1312
import { normalizeId, isBuiltin } from '../scripts/utils/packages.js'
1413

15-
const { ROLLUP_EXTERNAL_SUFFIX } = constants
14+
const {
15+
ROLLUP_EXTERNAL_SUFFIX,
16+
depStatsPath,
17+
rootDistPath,
18+
rootPath,
19+
rootSrcPath
20+
} = constants
1621

17-
const __dirname = fileURLToPath(new URL('.', import.meta.url))
18-
19-
const rootPath = path.resolve(__dirname, '..')
20-
const depStatsPath = path.join(rootPath, '.dep-stats.json')
21-
const distPath = path.join(rootPath, 'dist')
22-
const distLegacyPath = path.join(rootPath, 'dist-legacy')
23-
const srcPath = path.join(rootPath, 'src')
22+
const distModuleSyncPath = path.join(rootDistPath, 'module-sync')
23+
const distRequirePath = path.join(rootDistPath, 'require')
2424

2525
const binBasenames = ['cli.js', 'npm-cli.js', 'npx-cli.js']
2626
const editablePkgJson = readPackageJsonSync(rootPath, { editable: true })
@@ -31,111 +31,104 @@ function setBinPerm(filepath) {
3131
}
3232

3333
export default () => {
34-
const legacyConfig = baseConfig({
34+
const moduleSyncConfig = baseConfig({
3535
input: {
36-
cli: `${srcPath}/cli.ts`,
37-
'npm-cli': `${srcPath}/shadow/npm-cli.ts`,
38-
'npx-cli': `${srcPath}/shadow/npx-cli.ts`,
39-
'npm-injection': `${srcPath}/shadow/npm-injection.ts`
36+
cli: `${rootSrcPath}/cli.ts`,
37+
'npm-cli': `${rootSrcPath}/shadow/npm-cli.ts`,
38+
'npx-cli': `${rootSrcPath}/shadow/npx-cli.ts`,
39+
'npm-injection': `${rootSrcPath}/shadow/npm-injection.ts`
4040
},
4141
output: [
4242
{
43-
dir: 'dist-legacy',
43+
dir: path.relative(rootPath, distModuleSyncPath),
4444
entryFileNames: '[name].js',
4545
format: 'cjs',
4646
exports: 'auto',
4747
externalLiveBindings: false,
4848
freeze: false
4949
}
5050
],
51+
external(id_) {
52+
if (id_.endsWith(ROLLUP_EXTERNAL_SUFFIX) || isBuiltin(id_)) {
53+
return true
54+
}
55+
const id = normalizeId(id_)
56+
return !(isRelative(id) || id.startsWith(rootSrcPath))
57+
},
5158
plugins: [
5259
{
5360
writeBundle() {
54-
const { content: pkgJson } = editablePkgJson
55-
const { '@cyclonedx/cdxgen': cdxgenRange, synp: synpRange } =
56-
pkgJson.dependencies
57-
const { depStats } = legacyConfig.meta
58-
59-
// Manually add @cyclonedx/cdxgen and synp as they are not directly
60-
// referenced in the code but used through spawned processes.
61-
depStats.dependencies['@cyclonedx/cdxgen'] = cdxgenRange
62-
depStats.dependencies.synp = synpRange
63-
depStats.external['@cyclonedx/cdxgen'] = cdxgenRange
64-
depStats.external.synp = synpRange
65-
66-
try {
67-
// Remove transitives from dependencies
68-
const oldDepStats = readJsonSync(depStatsPath)
69-
for (const key of Object.keys(oldDepStats.transitives)) {
70-
if (pkgJson.dependencies[key]) {
71-
depStats.transitives[key] = pkgJson.dependencies[key]
72-
depStats.external[key] = pkgJson.dependencies[key]
73-
delete depStats.dependencies[key]
74-
}
75-
}
76-
} catch {}
77-
78-
depStats.dependencies = toSortedObject(depStats.dependencies)
79-
depStats.devDependencies = toSortedObject(depStats.devDependencies)
80-
depStats.esm = toSortedObject(depStats.esm)
81-
depStats.external = toSortedObject(depStats.external)
82-
depStats.transitives = toSortedObject(depStats.transitives)
83-
84-
// Write dep stats
85-
writeFileSync(depStatsPath, `${formatObject(depStats)}\n`, 'utf8')
86-
87-
// Update dependencies with additional inlined modules
88-
editablePkgJson
89-
.update({
90-
dependencies: {
91-
...depStats.dependencies,
92-
...depStats.transitives
93-
}
94-
})
95-
.saveSync()
96-
9761
for (const binBasename of binBasenames) {
98-
setBinPerm(path.join(distLegacyPath, binBasename))
62+
setBinPerm(path.join(distModuleSyncPath, binBasename))
9963
}
10064
}
10165
}
10266
]
10367
})
10468

105-
const syncEsmConfig = baseConfig({
69+
const requireConfig = baseConfig({
10670
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`
71+
cli: `${rootSrcPath}/cli.ts`,
72+
'npm-cli': `${rootSrcPath}/shadow/npm-cli.ts`,
73+
'npx-cli': `${rootSrcPath}/shadow/npx-cli.ts`,
74+
'npm-injection': `${rootSrcPath}/shadow/npm-injection.ts`
11175
},
11276
output: [
11377
{
114-
dir: 'dist',
78+
dir: path.relative(rootPath, distRequirePath),
11579
entryFileNames: '[name].js',
11680
format: 'cjs',
11781
exports: 'auto',
11882
externalLiveBindings: false,
11983
freeze: false
12084
}
12185
],
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-
},
12986
plugins: [
13087
{
13188
writeBundle() {
89+
const { content: pkgJson } = editablePkgJson
90+
const oldDepStats = existsSync(depStatsPath)
91+
? readJsonSync(depStatsPath)
92+
: undefined
93+
const { depStats } = requireConfig.meta
94+
Object.assign(depStats.dependencies, {
95+
// Manually add @cyclonedx/cdxgen and synp as they are not directly
96+
// referenced in the code but used through spawned processes.
97+
'@cyclonedx/cdxgen': pkgJson.dependencies['@cyclonedx/cdxgen'],
98+
synp: pkgJson.dependencies.synp,
99+
...oldDepStats?.dependencies
100+
})
101+
// Remove transitives from dependencies
102+
for (const key of Object.keys(oldDepStats?.transitives ?? {})) {
103+
if (pkgJson.dependencies[key]) {
104+
depStats.transitives[key] = pkgJson.dependencies[key]
105+
depStats.external[key] = pkgJson.dependencies[key]
106+
delete depStats.dependencies[key]
107+
}
108+
}
109+
depStats.dependencies = toSortedObject(depStats.dependencies)
110+
depStats.devDependencies = toSortedObject(depStats.devDependencies)
111+
depStats.esm = toSortedObject(depStats.esm)
112+
depStats.external = toSortedObject(depStats.external)
113+
depStats.transitives = toSortedObject(depStats.transitives)
114+
// Write dep stats
115+
writeFileSync(depStatsPath, `${formatObject(depStats)}\n`, 'utf8')
116+
// Update dependencies with additional inlined modules
117+
editablePkgJson
118+
.update({
119+
dependencies: {
120+
...depStats.dependencies,
121+
...depStats.transitives
122+
}
123+
})
124+
.saveSync()
132125
for (const binBasename of binBasenames) {
133-
setBinPerm(path.join(distPath, binBasename))
126+
setBinPerm(path.join(distRequirePath, binBasename))
134127
}
135128
}
136129
}
137130
]
138131
})
139132

140-
return [legacyConfig, syncEsmConfig]
133+
return [moduleSyncConfig, requireConfig]
141134
}

.config/rollup.test.config.mjs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
1-
import path from 'node:path'
2-
import { fileURLToPath } from 'node:url'
3-
41
import { isRelative } from '@socketsecurity/registry/lib/path'
52

63
import baseConfig from './rollup.base.config.mjs'
74
import constants from '../scripts/constants.js'
85
import { normalizeId, isBuiltin } from '../scripts/utils/packages.js'
96

10-
const { ROLLUP_EXTERNAL_SUFFIX, SUPPORTS_SYNC_ESM } = constants
11-
12-
const __dirname = fileURLToPath(new URL('.', import.meta.url))
13-
14-
const rootPath = path.resolve(__dirname, '..')
15-
const srcPath = path.join(rootPath, 'src')
7+
const { ROLLUP_EXTERNAL_SUFFIX, SUPPORTS_SYNC_ESM, rootSrcPath } = constants
168

179
export default () =>
1810
baseConfig({
1911
input: {
20-
misc: `${srcPath}/utils/misc.ts`,
21-
'path-resolve': `${srcPath}/utils/path-resolve.ts`
12+
constants: `${rootSrcPath}/constants.ts`,
13+
misc: `${rootSrcPath}/utils/misc.ts`,
14+
'path-resolve': `${rootSrcPath}/utils/path-resolve.ts`
2215
},
2316
output: [
2417
{
@@ -37,7 +30,7 @@ export default () =>
3730
return true
3831
}
3932
const id = normalizeId(id_)
40-
return !(isRelative(id) || id.startsWith(srcPath))
33+
return !(isRelative(id) || id.startsWith(rootSrcPath))
4134
}
4235
}
4336
: {})

0 commit comments

Comments
 (0)