Skip to content

Commit

Permalink
better size analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
arshaw committed Jan 29, 2024
1 parent fc9f6e3 commit 899784b
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 27 deletions.
2 changes: 1 addition & 1 deletion packages/export-size
Submodule export-size updated 1 files
+0 −2 src/cli.ts
4 changes: 2 additions & 2 deletions packages/temporal-polyfill/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"readme": "cp ../../README.md dist",
"test": "./scripts/test.js",
"clean": "./scripts/clean.js --tsc",
"size": "gzip-size --include-original --raw dist/global.min.js && pnpm export-size ./dist --output --bundler rollup"
"size": "./scripts/size.js"
},
"type": "module",
"buildConfig": {
Expand Down Expand Up @@ -88,7 +88,7 @@
"concurrently": "^8.2.0",
"eslint": "^7.25.0",
"export-size": "workspace:*",
"gzip-size": "^7.0.0",
"gzip-size-cli": "^5.1.0",
"rollup": "^4.9.6",
"rollup-plugin-dts": "^6.1.0",
"rollup-plugin-sourcemaps": "^0.6.3",
Expand Down
26 changes: 26 additions & 0 deletions packages/temporal-polyfill/scripts/lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { spawn } from 'child_process'

export function execLive(cmdParts, options = {}) {
return new Promise((resolve, reject) => {
spawn(cmdParts[0], cmdParts.slice(1), {
shell: false,
stdio: 'inherit',
...options,
}).on('close', (status) => {
if (status === 0) {
resolve()
} else {
reject(new Error(`Command failed with status code ${status}`))
}
})
})
}

export function popFlag(argv, arg) {
const i = argv.indexOf(arg)
if (i !== -1) {
argv.splice(i, 1)
return true
}
return false
}
56 changes: 56 additions & 0 deletions packages/temporal-polyfill/scripts/size.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env node

import { readFile } from 'fs/promises'
import { popFlag, execLive } from './lib/utils.js'

const argv = process.argv.slice(2)
displaySizes(
popFlag(argv, '--output'),
popFlag(argv, '--raw'),
popFlag(argv, '--all'),
argv,
)

/*
Only works when run from PNPM-run context, for bin paths
*/
async function displaySizes(debugOutput, rawSizes, allEntryPoints, entryPoints) {
if (allEntryPoints) {
const pkgJson = JSON.parse(await readFile('./package.json'))
entryPoints = Object.keys(pkgJson.buildConfig.exports)
}

if (!entryPoints.length) {
entryPoints = ['.']
}

// normalize, remove leading ./ (except for root, which leaves '.')
entryPoints = entryPoints
.map((entryPoint) => entryPoint.replace(/^\.\//, ''))
.filter((entryPoint) => Boolean(entryPoint))

if (entryPoints.length > 1 && debugOutput) {
throw RangeError('Cannot debug output with multiple entry points')
}

const globalIifePath = './dist/global.min.js'
console.log(`Size of ${globalIifePath} ...`)
await execLive([
'gzip-size',
'--include-original',
...(rawSizes ? ['--raw'] : []),
globalIifePath,
])
console.log()

for (const entryPoint of entryPoints) {
await execLive([
'export-size',
'--bundler', 'rollup',
'--compression', 'gzip',
...(debugOutput ? ['--output'] : []),
...(rawSizes ? ['--raw'] : []),
'./dist' + (entryPoint === '.' ? '' : `:${entryPoint}`),
])
}
}
23 changes: 2 additions & 21 deletions packages/temporal-polyfill/scripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

import runTest262 from '@js-temporal/temporal-test262-runner'
import { join as joinPaths } from 'path'
import { spawn } from 'child_process'
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import { execLive } from './lib/utils.js'
import { extensions } from './lib/config.js'

const scriptsDir = joinPaths(process.argv[1], '..')
Expand Down Expand Up @@ -62,7 +62,7 @@ yargs(hideBin(process.argv))
options.nodeVersion !== currentNodeVersion &&
!process.env.NODE_VERSION // not already executing a forced-version call
) {
return await liveExec([
return await execLive([
'pnpm', 'exec', 'node', ...process.argv.slice(1),
], {
cwd: process.cwd(),
Expand Down Expand Up @@ -125,25 +125,6 @@ yargs(hideBin(process.argv))
.showHelpOnFail(false)
.parse()

// Utils
// -------------------------------------------------------------------------------------------------

function liveExec(cmdParts, options = {}) {
return new Promise((resolve, reject) => {
spawn(cmdParts[0], cmdParts.slice(1), {
shell: false,
stdio: 'inherit',
...options,
}).on('close', (status) => {
if (status === 0) {
resolve()
} else {
reject(new Error(`Command failed with status code ${status}`))
}
})
})
}

// Filter away Node-related environment variables because prevents
// PNPM's use-node-version from being reset
function filterEnv(oldEnv) {
Expand Down
Loading

0 comments on commit 899784b

Please sign in to comment.