Skip to content

Commit a5a9812

Browse files
committed
Cleanup socket bundle modifications
1 parent 4c0032e commit a5a9812

File tree

1 file changed

+44
-33
lines changed

1 file changed

+44
-33
lines changed

.config/rollup.base.config.mjs

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ const {
5151
overrides: pkgOverrides
5252
} = rootPackageJson
5353

54+
const SOCKET_INTEROP = '_socketInterop'
55+
5456
const builtinAliases = builtinModules.reduce((o, n) => {
5557
o[n] = `node:${n}`
5658
return o
@@ -63,6 +65,20 @@ const customResolver = nodeResolve({
6365
preferBuiltins: true
6466
})
6567

68+
const requireAssignmentsRegExp =
69+
/(?<=\s*=\s*)require\(["'].+?["']\)(?=;?\r?\n)/g
70+
const checkRequireAssignmentRegExp = new RegExp(
71+
requireAssignmentsRegExp.source,
72+
''
73+
)
74+
const checkSocketInteropUseRegExp = new RegExp(`\\b${SOCKET_INTEROP}\\b`)
75+
const danglingRequiresRegExp = /^\s*require\(["'].+?["']\);?\r?\n/gm
76+
const firstUseStrictRegExp = /'use strict';?/
77+
const oraSpinnersAssignmentsRegExp = /(?<=ora[^.]+\.spinners\s*=\s*)[$\w]+/g
78+
const requireUrlAssignmentRegExp =
79+
/(?<=var +)[$\w]+(?= *= *require\('node:url'\))/
80+
const splitUrlRequiresRegExp = /require\('u' \+ 'rl'\)/g
81+
6682
function isAncestorsExternal(id, depStats) {
6783
let currNmIndex = id.indexOf(SLASH_NODE_MODULES_SLASH)
6884
while (currNmIndex !== -1) {
@@ -154,12 +170,9 @@ export default function baseConfig(extendConfig = {}) {
154170
LATEST
155171
return false
156172
}
157-
const parentNodeModulesIndex = parentId.lastIndexOf(
158-
SLASH_NODE_MODULES_SLASH
159-
)
160-
if (parentNodeModulesIndex !== -1) {
161-
const parentNameStart =
162-
parentNodeModulesIndex + SLASH_NODE_MODULES_SLASH.length
173+
const parentNmIndex = parentId.lastIndexOf(SLASH_NODE_MODULES_SLASH)
174+
if (parentNmIndex !== -1) {
175+
const parentNameStart = parentNmIndex + SLASH_NODE_MODULES_SLASH.length
163176
const parentNameEnd = getPackageNameEnd(parentId, parentNameStart)
164177
const {
165178
version,
@@ -216,30 +229,26 @@ export default function baseConfig(extendConfig = {}) {
216229
preventAssignment: false,
217230
values: builtinAliases
218231
}),
219-
// Convert `require('u' + 'rl')` into something like `require$$2$3`.
232+
// Try to convert `require('u' + 'rl')` into something like `require$$2$3`.
220233
socketModifyPlugin({
221-
find: /require\('u' \+ 'rl'\)/g,
234+
find: splitUrlRequiresRegExp,
222235
replace(match) {
223-
return (
224-
/(?<=var +)[$\w]+(?= *= *require\('node:url'\))/.exec(
225-
this.input
226-
)?.[0] ?? match
227-
)
236+
return requireUrlAssignmentRegExp.exec(this.input)?.[0] ?? match
228237
}
229238
}),
230-
// Remove bare require calls, e.g. require calls not associated with an
231-
// import binding:
239+
// Remove dangling require calls, e.g. require calls not associated with
240+
// an import binding:
232241
// require('node:util')
233242
// require('graceful-fs')
234243
socketModifyPlugin({
235-
find: /^\s*require\(["'].+?["']\);?\r?\n/gm,
244+
find: danglingRequiresRegExp,
236245
replace: ''
237246
}),
238247
// Fix incorrectly set "spinners" binding caused by a transpilation bug
239248
// https://github.com/sindresorhus/ora/blob/v8.1.1/index.js#L424
240249
// export {default as spinners} from 'cli-spinners'
241250
socketModifyPlugin({
242-
find: /(?<=ora[^.]+\.spinners\s*=\s*)[$\w]+/g,
251+
find: oraSpinnersAssignmentsRegExp,
243252
replace(match) {
244253
return (
245254
new RegExp(`(?<=${escapeRegExp(match)}\\s*=\\s*)[$\\w]+`).exec(
@@ -248,26 +257,28 @@ export default function baseConfig(extendConfig = {}) {
248257
)
249258
}
250259
}),
260+
// Wrap require calls with SOCKET_INTEROP helper.
261+
socketModifyPlugin({
262+
find: requireAssignmentsRegExp,
263+
replace: match => `${SOCKET_INTEROP}(${match})`
264+
}),
251265
// Add CJS interop helper for "default" only exports.
252266
socketModifyPlugin({
253-
find: /'use strict';?/,
254-
replace: match => `${match}\n
255-
function _interop(e) {
256-
let d
257-
if (e) {
258-
let c = 0
259-
for (const k in e) {
260-
d = c++ === 0 && k === 'default' ? e[k] : void 0
261-
if (!d) break
262-
}
267+
find: firstUseStrictRegExp,
268+
replace(match) {
269+
return checkRequireAssignmentRegExp.test(this.input) ||
270+
checkSocketInteropUseRegExp.test(this.input)
271+
? `${match}\n
272+
function ${SOCKET_INTEROP}(e) {
273+
let c = 0
274+
for (const k in e ?? {}) {
275+
c = c === 0 && k === 'default' ? 1 : 0
276+
if (!c) break
263277
}
264-
return d ?? e
278+
return c ? e.default : e
265279
}`
266-
}),
267-
// Wrap require calls with "_interop" helper.
268-
socketModifyPlugin({
269-
find: /(?<=\s*=\s*)require\(["'].+?["']\)(?=;?\r?\n)/g,
270-
replace: match => `_interop(${match})`
280+
: match
281+
}
271282
}),
272283
commonjs({
273284
extensions: ['.cjs', '.js', '.ts', `.ts${ROLLUP_ENTRY_SUFFIX}`],

0 commit comments

Comments
 (0)