Skip to content

Commit 23acf7e

Browse files
committed
Simplify build and shadow process
1 parent 2089b9a commit 23acf7e

29 files changed

+210
-258
lines changed

.config/babel.config.js

-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict'
22

3-
const { isEsmId } = require('../scripts/utils/packages')
4-
53
module.exports = {
64
plugins: [
75
'@babel/plugin-proposal-export-default-from',
@@ -15,25 +13,6 @@ module.exports = {
1513
regenerator: false,
1614
version: '^7.25.7'
1715
}
18-
],
19-
[
20-
'@babel/plugin-transform-modules-commonjs',
21-
{
22-
allowTopLevelThis: true,
23-
importInterop: (specifier, requestingFilename) => {
24-
if (requestingFilename) {
25-
const specIsEsm = isEsmId(specifier, requestingFilename)
26-
const parentIsEsm = isEsmId(requestingFilename)
27-
if (specIsEsm && parentIsEsm) {
28-
return 'none'
29-
}
30-
if (specIsEsm) {
31-
return 'babel'
32-
}
33-
}
34-
return 'node'
35-
}
36-
}
3716
]
3817
]
3918
}

.config/rollup.base.config.mjs

+14-3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ const {
5353

5454
const SOCKET_INTEROP = '_socketInterop'
5555

56+
const constantsSrcPath = path.join(rootSrcPath, 'constants.ts')
57+
5658
const builtinAliases = builtinModules.reduce((o, n) => {
5759
o[n] = `node:${n}`
5860
return o
@@ -251,6 +253,7 @@ export default function baseConfig(extendConfig = {}) {
251253
}
252254
}),
253255
commonjs({
256+
defaultIsModuleExports: true,
254257
extensions: ['.cjs', '.js', '.ts', `.ts${ROLLUP_ENTRY_SUFFIX}`],
255258
ignoreDynamicRequires: true,
256259
ignoreGlobal: true,
@@ -273,7 +276,7 @@ function ${SOCKET_INTEROP}(e) {
273276
let c = 0
274277
for (const k in e ?? {}) {
275278
c = c === 0 && k === 'default' ? 1 : 0
276-
if (!c) break
279+
if (!c && k !== '__esModule') break
277280
}
278281
return c ? e.default : e
279282
}`
@@ -293,8 +296,16 @@ function ${SOCKET_INTEROP}(e) {
293296
).map(o => ({
294297
...o,
295298
chunkFileNames: '[name].js',
296-
manualChunks: id_ =>
297-
normalizeId(id_).includes(SLASH_NODE_MODULES_SLASH) ? 'vendor' : null
299+
manualChunks: id_ => {
300+
const id = normalizeId(id_)
301+
if (id === constantsSrcPath) {
302+
return 'constants'
303+
}
304+
if (id.includes(SLASH_NODE_MODULES_SLASH)) {
305+
return 'vendor'
306+
}
307+
return null
308+
}
298309
}))
299310

300311
// Replace hard-coded absolute paths in source with hard-coded relative paths.

.config/rollup.dist.config.mjs

+19-54
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import {
2-
chmodSync,
3-
copyFileSync,
4-
existsSync,
5-
readFileSync,
6-
rmSync,
7-
writeFileSync
8-
} from 'node:fs'
1+
import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'
92
import path from 'node:path'
103

114
import { globSync as tinyGlobSync } from 'tinyglobby'
@@ -36,32 +29,14 @@ const {
3629
} = constants
3730

3831
const CONSTANTS_JS = 'constants.js'
32+
const CONSTANTS_STUB_CODE = `'use strict'\n\nmodule.exports = require('../${CONSTANTS_JS}')\n`
3933

34+
const distConstantsPath = path.join(rootDistPath, CONSTANTS_JS)
4035
const distModuleSyncPath = path.join(rootDistPath, 'module-sync')
4136
const distRequirePath = path.join(rootDistPath, 'require')
4237

43-
const binBasenames = ['cli.js', 'npm-cli.js', 'npx-cli.js']
4438
const editablePkgJson = readPackageJsonSync(rootPath, { editable: true })
4539

46-
function copyConstantsModuleSync(srcPath, destPath) {
47-
copyFileSync(
48-
path.join(srcPath, CONSTANTS_JS),
49-
path.join(destPath, CONSTANTS_JS)
50-
)
51-
}
52-
53-
function modifyConstantsModuleExportsSync(distPath) {
54-
const filepath = path.join(distPath, CONSTANTS_JS)
55-
let code = readFileSync(filepath, 'utf8')
56-
code = code
57-
// Remove @rollup/commonjs interop from code.
58-
.replace(/var constants\$\d+ = {};?\n+/, '')
59-
.replace(/Object\.defineProperty\(constants\$\d+[\s\S]+?\}\);?\n/, '')
60-
.replace(/^(?:exports.[$\w]+|[$\w]+\.default)\s*=.*(?:\n|$)/gm, '')
61-
code = code + 'module.exports = constants\n'
62-
writeFileSync(filepath, code, 'utf8')
63-
}
64-
6540
function removeDtsFilesSync(distPath) {
6641
for (const filepath of tinyGlobSync(['**/*.d.ts'], {
6742
absolute: true,
@@ -71,21 +46,6 @@ function removeDtsFilesSync(distPath) {
7146
}
7247
}
7348

74-
function rewriteConstantsModuleSync(distPath) {
75-
writeFileSync(
76-
path.join(distPath, CONSTANTS_JS),
77-
`'use strict'\n\nmodule.exports = require('../constants.js')\n`,
78-
'utf8'
79-
)
80-
}
81-
82-
function setBinPermsSync(distPath) {
83-
for (const binBasename of binBasenames) {
84-
// Make file chmod +x.
85-
chmodSync(path.join(distPath, binBasename), 0o755)
86-
}
87-
}
88-
8949
function updateDepStatsSync(depStats) {
9050
const { content: pkgJson } = editablePkgJson
9151
const oldDepStats = existsSync(depStatsPath)
@@ -129,8 +89,7 @@ export default () => {
12989
const moduleSyncConfig = baseConfig({
13090
input: {
13191
cli: `${rootSrcPath}/cli.ts`,
132-
'npm-cli': `${rootSrcPath}/shadow/npm-cli.ts`,
133-
'npx-cli': `${rootSrcPath}/shadow/npx-cli.ts`,
92+
'shadow-bin': `${rootSrcPath}/shadow/shadow-bin.ts`,
13493
'npm-injection': `${rootSrcPath}/shadow/npm-injection.ts`
13594
},
13695
output: [
@@ -163,11 +122,15 @@ export default () => {
163122
},
164123
plugins: [
165124
{
166-
writeBundle() {
167-
setBinPermsSync(distModuleSyncPath)
168-
copyConstantsModuleSync(distModuleSyncPath, rootDistPath)
169-
modifyConstantsModuleExportsSync(rootDistPath)
170-
rewriteConstantsModuleSync(distModuleSyncPath)
125+
generateBundle(_options, bundle) {
126+
if (bundle[CONSTANTS_JS]) {
127+
let { code } = bundle[CONSTANTS_JS]
128+
// Remove @rollup/commonjs interop from code.
129+
code = code.replace(/^exports.constants = constants;\n/m, '')
130+
mkdirSync(rootDistPath, { recursive: true })
131+
writeFileSync(distConstantsPath, code, 'utf8')
132+
bundle[CONSTANTS_JS].code = CONSTANTS_STUB_CODE
133+
}
171134
}
172135
}
173136
]
@@ -176,8 +139,7 @@ export default () => {
176139
const requireConfig = baseConfig({
177140
input: {
178141
cli: `${rootSrcPath}/cli.ts`,
179-
'npm-cli': `${rootSrcPath}/shadow/npm-cli.ts`,
180-
'npx-cli': `${rootSrcPath}/shadow/npx-cli.ts`,
142+
'shadow-bin': `${rootSrcPath}/shadow/shadow-bin.ts`,
181143
'npm-injection': `${rootSrcPath}/shadow/npm-injection.ts`
182144
},
183145
output: [
@@ -192,10 +154,13 @@ export default () => {
192154
],
193155
plugins: [
194156
{
157+
generateBundle(_options, bundle) {
158+
if (bundle[CONSTANTS_JS]) {
159+
bundle[CONSTANTS_JS].code = CONSTANTS_STUB_CODE
160+
}
161+
},
195162
writeBundle() {
196-
setBinPermsSync(distRequirePath)
197163
removeDtsFilesSync(distRequirePath)
198-
rewriteConstantsModuleSync(distRequirePath)
199164
updateDepStatsSync(requireConfig.meta.depStats)
200165
}
201166
}

.dep-stats.json

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"blessed-contrib": "^4.11.0",
1313
"browserslist": "4.24.2",
1414
"chalk-table": "^1.0.2",
15+
"cmd-shim": "^7.0.0",
1516
"hpagent": "^1.2.0",
1617
"ignore": "^6.0.2",
1718
"micromatch": "^4.0.8",
@@ -51,6 +52,7 @@
5152
"blessed-contrib": "^4.11.0",
5253
"browserslist": "4.24.2",
5354
"chalk-table": "^1.0.2",
55+
"cmd-shim": "^7.0.0",
5456
"has-flag": "^4.0.0",
5557
"hpagent": "^1.2.0",
5658
"ignore": "^6.0.2",

bin/cli.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
#!/usr/bin/env node
22
'use strict'
33

4-
const constants = require('../dist/constants')
5-
require(`../dist/${constants.DIST_TYPE}/cli.js`)
4+
const { readFileSync } = require('node:fs')
5+
const path = require('node:path')
6+
7+
const rootBinPath = readFileSync(__dirname)
8+
const rootPath = path.resolve(rootBinPath, '..')
9+
const rootDistPath = path.join(rootPath, 'dist')
10+
11+
const constants = require(path.join(rootDistPath, 'constants.js'))
12+
13+
require(path.join(rootDistPath, constants.DIST_TYPE, 'cli.js'))

bin/npm-cli.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
#!/usr/bin/env node
22
'use strict'
33

4-
const constants = require('../dist/constants')
5-
require(`../dist/${constants.DIST_TYPE}/npm-cli.js`)
4+
const { readFileSync } = require('node:fs')
5+
const path = require('node:path')
6+
7+
const rootBinPath = readFileSync(__dirname)
8+
const rootPath = path.resolve(rootBinPath, '..')
9+
const rootDistPath = path.join(rootPath, 'dist')
10+
11+
const constants = require(path.join(rootDistPath, 'constants.js'))
12+
const shadowBin = require(path.join(rootDistPath, constants.DIST_TYPE, 'shadow-bin.js'))
13+
14+
shadowBin('npm')

bin/npx-cli.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
#!/usr/bin/env node
22
'use strict'
33

4-
const constants = require('../dist/constants')
5-
require(`../dist/${constants.DIST_TYPE}/npx-cli.js`)
4+
const { readFileSync } = require('node:fs')
5+
const path = require('node:path')
6+
7+
const rootBinPath = readFileSync(__dirname)
8+
const rootPath = path.resolve(rootBinPath, '..')
9+
const rootDistPath = path.join(rootPath, 'dist')
10+
11+
const constants = require(path.join(rootDistPath, 'constants.js'))
12+
const shadowBin = require(path.join(rootDistPath, constants.DIST_TYPE, 'shadow-bin.js'))
13+
14+
shadowBin('npx')

bin/shadow/module-sync/npm

-3
This file was deleted.

bin/shadow/module-sync/npx

-3
This file was deleted.

bin/shadow/require/npm

-3
This file was deleted.

bin/shadow/require/npx

-3
This file was deleted.

eslint.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ module.exports = [
176176
rules: {
177177
...nodePlugin.configs['flat/recommended-script'].rules,
178178
'n/exports-style': ['error', 'module.exports'],
179+
'n/no-missing-require': ['off'],
179180
// The n/no-unpublished-bin rule does does not support non-trivial glob
180181
// patterns used in package.json "files" fields. In those cases we simplify
181182
// the glob patterns used.

package-lock.json

+12-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)