Skip to content

Commit 07ae62f

Browse files
committed
Simplify build and shadow process
1 parent 2089b9a commit 07ae62f

28 files changed

+173
-255
lines changed

.config/babel.config.js

Lines changed: 0 additions & 21 deletions
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

Lines changed: 14 additions & 3 deletions
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

Lines changed: 19 additions & 54 deletions
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,8 @@ 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+
constants: `${rootSrcPath}/constants.ts`,
93+
'shadow-bin': `${rootSrcPath}/shadow/shadow-bin.ts`,
13494
'npm-injection': `${rootSrcPath}/shadow/npm-injection.ts`
13595
},
13696
output: [
@@ -163,11 +123,13 @@ export default () => {
163123
},
164124
plugins: [
165125
{
166-
writeBundle() {
167-
setBinPermsSync(distModuleSyncPath)
168-
copyConstantsModuleSync(distModuleSyncPath, rootDistPath)
169-
modifyConstantsModuleExportsSync(rootDistPath)
170-
rewriteConstantsModuleSync(distModuleSyncPath)
126+
generateBundle(_options, bundle) {
127+
const constantsBundle = bundle[CONSTANTS_JS]
128+
if (constantsBundle) {
129+
mkdirSync(rootDistPath, { recursive: true })
130+
writeFileSync(distConstantsPath, constantsBundle.code, 'utf8')
131+
bundle[CONSTANTS_JS].code = CONSTANTS_STUB_CODE
132+
}
171133
}
172134
}
173135
]
@@ -176,8 +138,8 @@ export default () => {
176138
const requireConfig = baseConfig({
177139
input: {
178140
cli: `${rootSrcPath}/cli.ts`,
179-
'npm-cli': `${rootSrcPath}/shadow/npm-cli.ts`,
180-
'npx-cli': `${rootSrcPath}/shadow/npx-cli.ts`,
141+
constants: `${rootSrcPath}/constants.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

Lines changed: 2 additions & 0 deletions
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/npm-cli.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
'use strict'
33

44
const constants = require('../dist/constants')
5-
require(`../dist/${constants.DIST_TYPE}/npm-cli.js`)
5+
const shadowBin = require(`../dist/${constants.DIST_TYPE}/shadow-bin.js`)
6+
shadowBin('npm')

bin/npx-cli.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
'use strict'
33

44
const constants = require('../dist/constants')
5-
require(`../dist/${constants.DIST_TYPE}/npx-cli.js`)
5+
const shadowBin = require(`../dist/${constants.DIST_TYPE}/shadow-bin.js`)
6+
shadowBin('npx')

bin/shadow/module-sync/npm

Lines changed: 0 additions & 3 deletions
This file was deleted.

bin/shadow/module-sync/npx

Lines changed: 0 additions & 3 deletions
This file was deleted.

bin/shadow/require/npm

Lines changed: 0 additions & 3 deletions
This file was deleted.

bin/shadow/require/npx

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)