/
getPublicPath.ts
47 lines (44 loc) · 1.08 KB
/
getPublicPath.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import type { DevServerOptions } from '../../types';
/** {@link getPublicPath} options. */
export interface GetPublicPathOptions {
/** Target application platform. */
platform: string;
/** Development server configuration options. */
devServer: Pick<DevServerOptions, 'port' | 'host' | 'https'>;
}
/**
* Get Webpack's public path.
*
* @param options Options object.
* @returns Value for Webpack's `output.publicPath` option.
*
* @category Webpack util
*
* @example Usage in Webpack config:
* ```ts
* import * as Repack from '@callstack/repack';
*
* export default (env) => {
* const {
* platform,
* devServer = undefined,
* } = env;
*
* return {
* output: {
* publicPath: Repack.getPublicPath({ platform, devServer }),
* },
* };
* };
* ```
*/
export function getPublicPath(options?: GetPublicPathOptions) {
if (options?.devServer) {
const { port, host, https } = options.devServer;
return `${https ? 'https' : 'http'}://${host || 'localhost'}:${port}/${
options.platform
}/`;
} else {
return `noop:///`;
}
}