Skip to content

Commit

Permalink
Simplify build and shadow process
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Dec 16, 2024
1 parent 2089b9a commit 7b0a744
Show file tree
Hide file tree
Showing 28 changed files with 175 additions and 225 deletions.
21 changes: 0 additions & 21 deletions .config/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

const { isEsmId } = require('../scripts/utils/packages')

module.exports = {
plugins: [
'@babel/plugin-proposal-export-default-from',
Expand All @@ -15,25 +13,6 @@ module.exports = {
regenerator: false,
version: '^7.25.7'
}
],
[
'@babel/plugin-transform-modules-commonjs',
{
allowTopLevelThis: true,
importInterop: (specifier, requestingFilename) => {
if (requestingFilename) {
const specIsEsm = isEsmId(specifier, requestingFilename)
const parentIsEsm = isEsmId(requestingFilename)
if (specIsEsm && parentIsEsm) {
return 'none'
}
if (specIsEsm) {
return 'babel'
}
}
return 'node'
}
}
]
]
}
17 changes: 14 additions & 3 deletions .config/rollup.base.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ const {

const SOCKET_INTEROP = '_socketInterop'

const constantsSrcPath = path.join(rootSrcPath, 'constants.ts')

const builtinAliases = builtinModules.reduce((o, n) => {
o[n] = `node:${n}`
return o
Expand Down Expand Up @@ -251,6 +253,7 @@ export default function baseConfig(extendConfig = {}) {
}
}),
commonjs({
defaultIsModuleExports: true,
extensions: ['.cjs', '.js', '.ts', `.ts${ROLLUP_ENTRY_SUFFIX}`],
ignoreDynamicRequires: true,
ignoreGlobal: true,
Expand All @@ -273,7 +276,7 @@ function ${SOCKET_INTEROP}(e) {
let c = 0
for (const k in e ?? {}) {
c = c === 0 && k === 'default' ? 1 : 0
if (!c) break
if (!c && k !== '__esModule') break
}
return c ? e.default : e
}`
Expand All @@ -293,8 +296,16 @@ function ${SOCKET_INTEROP}(e) {
).map(o => ({
...o,
chunkFileNames: '[name].js',
manualChunks: id_ =>
normalizeId(id_).includes(SLASH_NODE_MODULES_SLASH) ? 'vendor' : null
manualChunks: id_ => {
const id = normalizeId(id_)
if (id === constantsSrcPath) {
return 'constants'
}
if (id.includes(SLASH_NODE_MODULES_SLASH)) {
return 'vendor'
}
return null
}
}))

// Replace hard-coded absolute paths in source with hard-coded relative paths.
Expand Down
43 changes: 16 additions & 27 deletions .config/rollup.dist.config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
chmodSync,
copyFileSync,
existsSync,
readFileSync,
Expand Down Expand Up @@ -40,7 +39,6 @@ const CONSTANTS_JS = 'constants.js'
const distModuleSyncPath = path.join(rootDistPath, 'module-sync')
const distRequirePath = path.join(rootDistPath, 'require')

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

function copyConstantsModuleSync(srcPath, destPath) {
Expand All @@ -53,12 +51,8 @@ function copyConstantsModuleSync(srcPath, destPath) {
function modifyConstantsModuleExportsSync(distPath) {
const filepath = path.join(distPath, CONSTANTS_JS)
let code = readFileSync(filepath, 'utf8')
code = code
// Remove @rollup/commonjs interop from code.
.replace(/var constants\$\d+ = {};?\n+/, '')
.replace(/Object\.defineProperty\(constants\$\d+[\s\S]+?\}\);?\n/, '')
.replace(/^(?:exports.[$\w]+|[$\w]+\.default)\s*=.*(?:\n|$)/gm, '')
code = code + 'module.exports = constants\n'
// Remove @rollup/commonjs interop from code.
code = code.replace(/^exports.constants = constants;\n/m, '')
writeFileSync(filepath, code, 'utf8')
}

Expand All @@ -79,13 +73,6 @@ function rewriteConstantsModuleSync(distPath) {
)
}

function setBinPermsSync(distPath) {
for (const binBasename of binBasenames) {
// Make file chmod +x.
chmodSync(path.join(distPath, binBasename), 0o755)
}
}

function updateDepStatsSync(depStats) {
const { content: pkgJson } = editablePkgJson
const oldDepStats = existsSync(depStatsPath)
Expand Down Expand Up @@ -129,8 +116,7 @@ export default () => {
const moduleSyncConfig = baseConfig({
input: {
cli: `${rootSrcPath}/cli.ts`,
'npm-cli': `${rootSrcPath}/shadow/npm-cli.ts`,
'npx-cli': `${rootSrcPath}/shadow/npx-cli.ts`,
'shadow-bin': `${rootSrcPath}/shadow/shadow-bin.ts`,
'npm-injection': `${rootSrcPath}/shadow/npm-injection.ts`
},
output: [
Expand Down Expand Up @@ -164,10 +150,12 @@ export default () => {
plugins: [
{
writeBundle() {
setBinPermsSync(distModuleSyncPath)
copyConstantsModuleSync(distModuleSyncPath, rootDistPath)
modifyConstantsModuleExportsSync(rootDistPath)
rewriteConstantsModuleSync(distModuleSyncPath)
// Give some time for Windows to settle.
setTimeout(() => {
copyConstantsModuleSync(distModuleSyncPath, rootDistPath)
modifyConstantsModuleExportsSync(rootDistPath)
rewriteConstantsModuleSync(distModuleSyncPath)
}, 200)
}
}
]
Expand All @@ -176,8 +164,7 @@ export default () => {
const requireConfig = baseConfig({
input: {
cli: `${rootSrcPath}/cli.ts`,
'npm-cli': `${rootSrcPath}/shadow/npm-cli.ts`,
'npx-cli': `${rootSrcPath}/shadow/npx-cli.ts`,
'shadow-bin': `${rootSrcPath}/shadow/shadow-bin.ts`,
'npm-injection': `${rootSrcPath}/shadow/npm-injection.ts`
},
output: [
Expand All @@ -193,10 +180,12 @@ export default () => {
plugins: [
{
writeBundle() {
setBinPermsSync(distRequirePath)
removeDtsFilesSync(distRequirePath)
rewriteConstantsModuleSync(distRequirePath)
updateDepStatsSync(requireConfig.meta.depStats)
// Give some time for Windows to settle.
setTimeout(() => {
removeDtsFilesSync(distRequirePath)
rewriteConstantsModuleSync(distRequirePath)
updateDepStatsSync(requireConfig.meta.depStats)
}, 200)
}
}
]
Expand Down
2 changes: 2 additions & 0 deletions .dep-stats.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"blessed-contrib": "^4.11.0",
"browserslist": "4.24.2",
"chalk-table": "^1.0.2",
"cmd-shim": "^7.0.0",
"hpagent": "^1.2.0",
"ignore": "^6.0.2",
"micromatch": "^4.0.8",
Expand Down Expand Up @@ -51,6 +52,7 @@
"blessed-contrib": "^4.11.0",
"browserslist": "4.24.2",
"chalk-table": "^1.0.2",
"cmd-shim": "^7.0.0",
"has-flag": "^4.0.0",
"hpagent": "^1.2.0",
"ignore": "^6.0.2",
Expand Down
5 changes: 3 additions & 2 deletions bin/npm-cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node
'use strict'

const constants = require('../dist/constants')
require(`../dist/${constants.DIST_TYPE}/npm-cli.js`)
const constants = require('../dist/constants.js')
const shadowBin = require(`../dist/${constants.DIST_TYPE}/shadow-bin.js`)
shadowBin('npm')
5 changes: 3 additions & 2 deletions bin/npx-cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node
'use strict'

const constants = require('../dist/constants')
require(`../dist/${constants.DIST_TYPE}/npx-cli.js`)
const constants = require('../dist/constants.js')
const shadowBin = require(`../dist/${constants.DIST_TYPE}/shadow-bin.js`)
shadowBin('npx')
3 changes: 0 additions & 3 deletions bin/shadow/module-sync/npm

This file was deleted.

3 changes: 0 additions & 3 deletions bin/shadow/module-sync/npx

This file was deleted.

3 changes: 0 additions & 3 deletions bin/shadow/require/npm

This file was deleted.

3 changes: 0 additions & 3 deletions bin/shadow/require/npx

This file was deleted.

1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ module.exports = [
rules: {
...nodePlugin.configs['flat/recommended-script'].rules,
'n/exports-style': ['error', 'module.exports'],
'n/no-missing-require': ['off'],
// The n/no-unpublished-bin rule does does not support non-trivial glob
// patterns used in package.json "files" fields. In those cases we simplify
// the glob patterns used.
Expand Down
16 changes: 12 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,15 @@
"exports": {
"./bin/cli.js": {
"types": "./dist/module-sync/cli.d.ts",
"module-sync": "./dist/module-sync/cli.js",
"require": "./dist/require/cli.js"
"default": "./dist/cli.js"
},
"./bin/npm-cli.js": {
"types": "./dist/module-sync/npm-cli.d.ts",
"module-sync": "./dist/module-sync/npm-cli.js",
"require": "./dist/require/npm-cli.js"
"default": "./dist/npm-cli.js"
},
"./bin/npx-cli.js": {
"types": "./dist/module-sync/npx-cli.d.ts",
"module-sync": "./dist/module-sync/npx-cli.js",
"require": "./dist/require/npx-cli.js"
"default": "./dist/npx-cli.js"
},
"./package.json": "./package.json",
"./translations.json": "./translations.json"
Expand Down Expand Up @@ -70,6 +67,7 @@
"blessed-contrib": "^4.11.0",
"browserslist": "4.24.2",
"chalk-table": "^1.0.2",
"cmd-shim": "^7.0.0",
"has-flag": "^4.0.0",
"hpagent": "^1.2.0",
"ignore": "^6.0.2",
Expand All @@ -89,7 +87,6 @@
"@babel/plugin-proposal-export-default-from": "^7.25.9",
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
"@babel/plugin-transform-export-namespace-from": "^7.25.9",
"@babel/plugin-transform-modules-commonjs": "^7.26.3",
"@babel/plugin-transform-runtime": "^7.25.9",
"@babel/preset-env": "^7.26.0",
"@babel/preset-typescript": "^7.26.0",
Expand All @@ -103,6 +100,7 @@
"@rollup/pluginutils": "^5.1.3",
"@tapjs/run": "^4.0.1",
"@types/blessed": "^0.1.25",
"@types/cmd-shim": "^5.0.2",
"@types/micromatch": "^4.0.9",
"@types/mocha": "^10.0.10",
"@types/mock-fs": "^4.13.4",
Expand Down Expand Up @@ -203,6 +201,7 @@
"files": [
"bin/**",
"dist/**",
"shadow-bin/**",
"translations.json"
]
}
12 changes: 0 additions & 12 deletions scripts/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,13 @@ const babelConfigPath = path.join(rootConfigPath, 'babel.config.js')
const depStatsPath = path.join(rootPath, '.dep-stats.json')
const tsconfigPath = path.join(rootConfigPath, 'tsconfig.rollup.json')

const LAZY_DIST_TYPE = () =>
registryConstants.SUPPORTS_NODE_REQUIRE_MODULE ? 'module-sync' : 'require'

const lazyDistPath = () => path.join(rootDistPath, constants.DIST_TYPE)

const constants = createConstantsObject(
{
// Lazily defined values are initialized as `undefined` to keep their key order.
DIST_TYPE: undefined,
ROLLUP_ENTRY_SUFFIX,
ROLLUP_EXTERNAL_SUFFIX,
SLASH_NODE_MODULES_SLASH,
babelConfigPath,
depStatsPath,
distPath: undefined,
rootConfigPath,
rootDistPath,
rootPackageJsonPath,
Expand All @@ -46,10 +38,6 @@ const constants = createConstantsObject(
tsconfigPath
},
{
getters: {
DIST_TYPE: LAZY_DIST_TYPE,
distPath: lazyDistPath
},
mixin: registryConstants
}
)
Expand Down
4 changes: 4 additions & 0 deletions shadow-bin/npm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env node
'use strict'

require(`../dist/npm-cli.js`)
4 changes: 4 additions & 0 deletions shadow-bin/npx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env node
'use strict'

require(`../dist/npx-cli.js`)
Loading

0 comments on commit 7b0a744

Please sign in to comment.