-
Notifications
You must be signed in to change notification settings - Fork 570
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
14 changed files
with
334 additions
and
430 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
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
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,70 @@ | ||
import type { Compiler } from 'webpack'; | ||
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'; | ||
|
||
import { getBundleAnalyzerPort } from './utils'; | ||
|
||
jest.mock('webpack-bundle-analyzer', () => ({ | ||
BundleAnalyzerPlugin: jest.fn(), | ||
})); | ||
|
||
describe('getBundleAnalyzerPort', () => { | ||
it('returns the port of the bundle analyzer server', async () => { | ||
const compiler: Compiler = { | ||
// @ts-expect-error: Mock `Compiler` object. | ||
options: { | ||
plugins: [new BundleAnalyzerPlugin()], | ||
}, | ||
}; | ||
|
||
const plugin = jest.mocked(BundleAnalyzerPlugin); | ||
const instance = plugin.mock.instances[0]; | ||
|
||
// @ts-expect-error: Partial `server` mock. | ||
instance.server = Promise.resolve({ | ||
http: { | ||
address: () => 'http://localhost:8888', | ||
}, | ||
}); | ||
|
||
const port = await getBundleAnalyzerPort(compiler); | ||
expect(port).toBe(8888); | ||
}); | ||
|
||
it('returns the port of the bundle analyzer server that returns an object', async () => { | ||
const compiler: Compiler = { | ||
// @ts-expect-error: Mock `Compiler` object. | ||
options: { | ||
plugins: [new BundleAnalyzerPlugin()], | ||
}, | ||
}; | ||
|
||
const plugin = jest.mocked(BundleAnalyzerPlugin); | ||
const instance = plugin.mock.instances[0]; | ||
|
||
// @ts-expect-error: Partial `server` mock. | ||
instance.server = Promise.resolve({ | ||
http: { | ||
address: () => { | ||
return { | ||
port: 8888, | ||
}; | ||
}, | ||
}, | ||
}); | ||
|
||
const port = await getBundleAnalyzerPort(compiler); | ||
expect(port).toBe(8888); | ||
}); | ||
|
||
it('returns undefined if the bundle analyzer server is not available', async () => { | ||
const compiler: Compiler = { | ||
// @ts-expect-error: Mock `Compiler` object. | ||
options: { | ||
plugins: [new BundleAnalyzerPlugin()], | ||
}, | ||
}; | ||
|
||
const port = await getBundleAnalyzerPort(compiler); | ||
expect(port).toBeUndefined(); | ||
}); | ||
}); |
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,29 @@ | ||
import type { Compiler } from 'webpack'; | ||
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'; | ||
|
||
/** | ||
* Get the port of the bundle analyzer server. | ||
* | ||
* @param compiler - The Webpack compiler. | ||
* @returns The port of the bundle analyzer server. | ||
*/ | ||
export async function getBundleAnalyzerPort(compiler: Compiler) { | ||
const analyzerPlugin = compiler.options.plugins.find( | ||
(plugin): plugin is BundleAnalyzerPlugin => | ||
plugin instanceof BundleAnalyzerPlugin, | ||
); | ||
|
||
if (analyzerPlugin?.server) { | ||
const { http } = await analyzerPlugin.server; | ||
|
||
const address = http.address(); | ||
if (typeof address === 'string') { | ||
const { port } = new URL(address); | ||
return parseInt(port, 10); | ||
} | ||
|
||
return address?.port; | ||
} | ||
|
||
return undefined; | ||
} |
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,22 @@ | ||
declare module 'webpack-bundle-analyzer' { | ||
import type { Server } from 'http'; | ||
import type { Compiler, WebpackPluginInstance } from 'webpack'; | ||
|
||
export type BundleAnalyzerPluginOptions = { | ||
analyzerPort?: number | undefined; | ||
logLevel?: 'info' | 'warn' | 'error' | 'silent' | undefined; | ||
openAnalyzer?: boolean | undefined; | ||
}; | ||
|
||
export class BundleAnalyzerPlugin implements WebpackPluginInstance { | ||
readonly opts: BundleAnalyzerPluginOptions; | ||
|
||
server?: Promise<{ | ||
http: Server; | ||
}>; | ||
|
||
constructor(options?: BundleAnalyzerPluginOptions); | ||
|
||
apply(compiler: Compiler): void; | ||
} | ||
} |
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
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.