(body: string)
| 2263 | // where there are enough to pay; without annotations, as a bundler keeps the |
| 2264 | // call anyway, and oxc's minifier keeps their bytes |
| 2265 | const inGzipOrder = (body: string): string => { |
| 2266 | const { program, comments } = parseUnits(body) |
| 2267 | const annotations = comments.filter(comment => |
| 2268 | pureAnnotation.test(comment.value) |
| 2269 | ) |
| 2270 | let next = 0 |
| 2271 | // read in order, each span after the last |
| 2272 | const textOf = (start: number, end: number) => { |
| 2273 | while (next < annotations.length && annotations[next].start < start) next++ |
| 2274 | let text = "" |
| 2275 | for (; next < annotations.length && annotations[next].end <= end; next++) { |
| 2276 | text += body.slice(start, annotations[next].start) |
| 2277 | start = annotations[next].end |
| 2278 | } |
| 2279 | return text + body.slice(start, end) |
| 2280 | } |
| 2281 | const functions: [key: string, text: string][] = [] |
| 2282 | const rest: string[] = [] |
| 2283 | for (const statement of program.body as Node[]) { |
| 2284 | if (statement.type !== "VariableDeclaration") { |
| 2285 | rest.push(textOf(statement.start, statement.end)) |
| 2286 | continue |
| 2287 | } |
| 2288 | for (const declarator of statement.declarations) { |
| 2289 | const text = `${statement.kind} ${textOf(declarator.start, declarator.end)}` |
| 2290 | const { init } = declarator |
| 2291 | if (init && isFunctionExpression(init)) { |
| 2292 | functions.push([ |
| 2293 | body.slice(init.start, init.end).replace(unitName, "_"), |
| 2294 | text |
| 2295 | ]) |
| 2296 | } else rest.push(text) |
| 2297 | } |
| 2298 | } |
| 2299 | if (functions.length < minSortedFunctions) { |
| 2300 | next = 0 |
| 2301 | return textOf(0, body.length) |
| 2302 | } |
| 2303 | functions.sort(([l], [r]) => |
| 2304 | l < r ? -1 |
| 2305 | : l > r ? 1 |
| 2306 | : 0 |
| 2307 | ) |
| 2308 | return [...functions.map(([, text]) => text), ...rest].join("\n") |
| 2309 | } |
| 2310 | |
| 2311 | const minSortedFunctions = 64 |
| 2312 |
no test coverage detected