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 fdfd5a5
Show file tree
Hide file tree
Showing 28 changed files with 177 additions and 253 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
73 changes: 18 additions & 55 deletions .config/rollup.dist.config.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
chmodSync,
copyFileSync,
existsSync,
readFileSync,
rmSync,
writeFileSync
} from 'node:fs'
import { existsSync, rmSync, writeFileSync } from 'node:fs'
import path from 'node:path'

import { globSync as tinyGlobSync } from 'tinyglobby'
Expand Down Expand Up @@ -35,33 +28,14 @@ const {
rootSrcPath
} = constants

const CONSTANTS_JS = 'constants.js'
const CONSTANTS_STUB_CODE = `'use strict'\n\nmodule.exports = require('../constants.js')\n`

const distConstantsPath = path.join(rootDistPath, '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) {
copyFileSync(
path.join(srcPath, CONSTANTS_JS),
path.join(destPath, CONSTANTS_JS)
)
}

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'
writeFileSync(filepath, code, 'utf8')
}

function removeDtsFilesSync(distPath) {
for (const filepath of tinyGlobSync(['**/*.d.ts'], {
absolute: true,
Expand All @@ -71,21 +45,6 @@ function removeDtsFilesSync(distPath) {
}
}

function rewriteConstantsModuleSync(distPath) {
writeFileSync(
path.join(distPath, CONSTANTS_JS),
`'use strict'\n\nmodule.exports = require('../constants.js')\n`,
'utf8'
)
}

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 +88,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 @@ -163,11 +121,14 @@ export default () => {
},
plugins: [
{
writeBundle() {
setBinPermsSync(distModuleSyncPath)
copyConstantsModuleSync(distModuleSyncPath, rootDistPath)
modifyConstantsModuleExportsSync(rootDistPath)
rewriteConstantsModuleSync(distModuleSyncPath)
generateBundle(_options, bundle) {
if (bundle['constants.js']) {
let { code } = bundle['constants.js']
// Remove @rollup/commonjs interop from code.
code = code.replace(/^exports.constants = constants;\n/m, '')
writeFileSync(distConstantsPath, code, 'utf8')
bundle['constants.js'].code = CONSTANTS_STUB_CODE
}
}
}
]
Expand All @@ -176,8 +137,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 @@ -192,10 +152,13 @@ export default () => {
],
plugins: [
{
generateBundle(_options, bundle) {
if (bundle['constants.js']) {
bundle['constants.js'].code = CONSTANTS_STUB_CODE
}
},
writeBundle() {
setBinPermsSync(distRequirePath)
removeDtsFilesSync(distRequirePath)
rewriteConstantsModuleSync(distRequirePath)
updateDepStatsSync(requireConfig.meta.depStats)
}
}
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"
]
}
Loading

0 comments on commit fdfd5a5

Please sign in to comment.