-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
282 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`), | ||
]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.