Skip to content

Commit 7925dec

Browse files
committed
perf: optimize hot paths
relate #687
1 parent 6db5f21 commit 7925dec

File tree

3 files changed

+30
-40
lines changed

3 files changed

+30
-40
lines changed

src/config/options.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,7 @@ function filterEnv(
318318
) {
319319
const env: Record<string, string> = {}
320320
for (const [key, value] of Object.entries(envDict)) {
321-
if (
322-
envPrefixes.some((prefix) => key.startsWith(prefix)) &&
323-
value !== undefined
324-
) {
321+
if (value != null && envPrefixes.some((prefix) => key.startsWith(prefix))) {
325322
env[key] = value
326323
}
327324
}

src/features/external.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ Imported by ${underline(importer)}`,
9797
}
9898
}
9999

100-
if (deps && deps.some((dep) => id === dep || id.startsWith(`${dep}/`))) {
100+
if (
101+
deps &&
102+
(deps.includes(id) || deps.some((dep) => id.startsWith(`${dep}/`)))
103+
) {
101104
return true
102105
}
103106

src/features/node-protocol.ts

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,42 @@
11
import { builtinModules } from 'node:module'
22
import type { Plugin } from 'rolldown'
33

4-
const modulesWithoutProtocol = builtinModules.filter(
5-
(mod) => !mod.startsWith('node:'),
6-
)
7-
84
/**
95
* The `node:` protocol was added in Node.js v14.18.0.
106
* @see https://nodejs.org/api/esm.html#node-imports
117
*/
128
export function NodeProtocolPlugin(nodeProtocolOption: 'strip' | true): Plugin {
13-
if (nodeProtocolOption === 'strip') {
14-
const regex = new RegExp(`^node:(${modulesWithoutProtocol.join('|')})$`)
15-
16-
return {
17-
name: 'tsdown:node-protocol:strip',
18-
resolveId: {
19-
order: 'pre',
20-
filter: { id: regex },
21-
handler(id) {
22-
return {
23-
id: id.slice(5), // strip the `node:` prefix
24-
external: true,
25-
moduleSideEffects: false,
26-
}
27-
},
28-
},
29-
}
30-
}
31-
32-
// create regex from builtin modules
33-
// filter without `node:` prefix
34-
const builtinModulesRegex = new RegExp(
35-
`^(${modulesWithoutProtocol.join('|')})$`,
9+
const modulesWithoutProtocol = builtinModules.filter(
10+
(mod) => !mod.startsWith('node:'),
3611
)
3712

3813
return {
39-
name: 'tsdown:node-protocol:add',
14+
name: `tsdown:node-protocol`,
4015
resolveId: {
4116
order: 'pre',
42-
filter: { id: builtinModulesRegex },
43-
handler(id) {
44-
return {
45-
id: `node:${id}`,
46-
external: true,
47-
moduleSideEffects: false,
48-
}
17+
filter: {
18+
id:
19+
nodeProtocolOption === 'strip'
20+
? new RegExp(`^node:(${modulesWithoutProtocol.join('|')})$`)
21+
: new RegExp(`^(${modulesWithoutProtocol.join('|')})$`),
4922
},
23+
handler:
24+
nodeProtocolOption === 'strip'
25+
? (id) => {
26+
return {
27+
// strip the `node:` prefix
28+
id: id.slice(5),
29+
external: true,
30+
moduleSideEffects: false,
31+
}
32+
}
33+
: (id) => {
34+
return {
35+
id: `node:${id}`,
36+
external: true,
37+
moduleSideEffects: false,
38+
}
39+
},
5040
},
5141
}
5242
}

0 commit comments

Comments
 (0)