Skip to content

Commit

Permalink
@size-limit/esbuild-why: Automatically open esbuild reports (#328)
Browse files Browse the repository at this point in the history
* feat: automatically open esbuild report on complete

* docs: update readme with auto open note
  • Loading branch information
slavik-chapelskyi committed Jun 24, 2023
1 parent 5618368 commit 777df54
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ You will need to install `@size-limit/esbuild-why` or `@size-limit/webpack-why`
depends on which bundler you are using (default is `esbuild`).

For `@size-limit/esbuild-why`,
it will generate a `esbuild-why.html` at the current directory.
it will generate a `esbuild-why.html` at the current directory & open it in the browser.

If you also specify `--save-bundle <DIR>`,
the report will be generated inside `<DIR>`.
Expand Down
9 changes: 9 additions & 0 deletions packages/esbuild-why/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
let { visualizer } = require('esbuild-visualizer')
let { join } = require('path')
let { writeFileSync } = require('fs')
let open = require('open')

let { getReportName } = require('./report')

Expand All @@ -11,8 +12,16 @@ let self = {
if (config.why && check.esbuildMetafile) {
let result = await visualizer(check.esbuildMetafile)
let file = join(config.saveBundle ?? '', getReportName(config, check))
check.esbuildVisualizerFile = file;
writeFileSync(file, result)
}
},
async finally(config, check) {
let {esbuildVisualizerFile} = check

if (esbuildVisualizerFile) {
await open(esbuildVisualizerFile)
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/esbuild-why/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"size-limit": "8.2.4"
},
"dependencies": {
"esbuild-visualizer": "^0.4.0"
"esbuild-visualizer": "^0.4.0",
"open": "^8.4.2"
},
"devDependencies": {
"@size-limit/esbuild": "workspace:^",
Expand Down
30 changes: 27 additions & 3 deletions packages/esbuild-why/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
let { readFile } = require('fs').promises
let {readFile} = require('fs').promises
let [esbuild] = require('@size-limit/esbuild')
let { join } = require('path')
let {join} = require('path')
let rm = require('size-limit/rm')
let open = require('open')

jest.mock('open');

let [esbuildWhy] = require('..')

Expand All @@ -22,7 +25,7 @@ it('supports --why', async () => {
project: 'superProject',
why: true,
saveBundle: DIST,
checks: [{ files: [fixture('big.js')] }]
checks: [{files: [fixture('big.js')]}]
}
try {
await esbuild.before(config)
Expand All @@ -37,3 +40,24 @@ it('supports --why', async () => {
await esbuild.finally(config, config.checks[0])
}
})

it('supports open esbuild visualizer on complete', async () => {
let config = {
project: 'superProject',
why: true,
saveBundle: DIST,
checks: [{files: [fixture('big.js')]}]
}
try {
await esbuild.before(config)
await esbuild.step20(config, config.checks[0])
await esbuild.step40(config, config.checks[0])
await esbuildWhy.step81(config, config.checks[0])
} finally {
await esbuild.finally(config, config.checks[0])
await esbuildWhy.finally(config, config.checks[0])
}

expect(open).toHaveBeenCalledTimes(1);
expect(open).toHaveBeenCalledWith(expect.stringMatching( /.*\/out\/esbuild-why.html$/));
})
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 777df54

Please sign in to comment.