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 26255b0
Show file tree
Hide file tree
Showing 29 changed files with 210 additions and 258 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: 19 additions & 54 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, mkdirSync, rmSync, writeFileSync } from 'node:fs'
import path from 'node:path'

import { globSync as tinyGlobSync } from 'tinyglobby'
Expand Down Expand Up @@ -36,32 +29,14 @@ const {
} = 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 +46,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 +89,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 +122,15 @@ 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, '')
mkdirSync(rootDistPath, { recursive: true })
writeFileSync(distConstantsPath, code, 'utf8')
bundle[CONSTANTS_JS].code = CONSTANTS_STUB_CODE
}
}
}
]
Expand All @@ -176,8 +139,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 +154,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
12 changes: 10 additions & 2 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
#!/usr/bin/env node
'use strict'

const constants = require('../dist/constants')
require(`../dist/${constants.DIST_TYPE}/cli.js`)
const { realpathSync } = require('node:fs')
const path = require('node:path')

const rootBinPath = realpathSync(__dirname)
const rootPath = path.resolve(rootBinPath, '..')
const rootDistPath = path.join(rootPath, 'dist')

const constants = require(path.join(rootDistPath, 'constants.js'))

require(path.join(rootDistPath, constants.DIST_TYPE, 'cli.js'))
13 changes: 11 additions & 2 deletions bin/npm-cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
#!/usr/bin/env node
'use strict'

const constants = require('../dist/constants')
require(`../dist/${constants.DIST_TYPE}/npm-cli.js`)
const { realpathSync } = require('node:fs')
const path = require('node:path')

const rootBinPath = realpathSync(__dirname)
const rootPath = path.resolve(rootBinPath, '..')
const rootDistPath = path.join(rootPath, 'dist')

const constants = require(path.join(rootDistPath, 'constants.js'))
const shadowBin = require(path.join(rootDistPath, constants.DIST_TYPE, 'shadow-bin.js'))

shadowBin('npm')
13 changes: 11 additions & 2 deletions bin/npx-cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
#!/usr/bin/env node
'use strict'

const constants = require('../dist/constants')
require(`../dist/${constants.DIST_TYPE}/npx-cli.js`)
const { realpathSync } = require('node:fs')
const path = require('node:path')

const rootBinPath = realpathSync(__dirname)
const rootPath = path.resolve(rootBinPath, '..')
const rootDistPath = path.join(rootPath, 'dist')

const constants = require(path.join(rootDistPath, 'constants.js'))
const shadowBin = require(path.join(rootDistPath, 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.

Loading

0 comments on commit 26255b0

Please sign in to comment.