Skip to content

Commit de71a67

Browse files
committed
fix: publicPath in dev doesnt use overriden port (#1103)
* fix: publicpath in dev when changing port * chore: changeset
1 parent 3ce83dd commit de71a67

4 files changed

Lines changed: 43 additions & 3 deletions

File tree

.changeset/weak-hairs-serve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@callstack/repack": patch
3+
---
4+
5+
Fix publicPath not using port overriden through CLI in development

packages/repack/src/commands/common/config/__tests__/normalizeConfig.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,34 @@ describe('normalizeConfig', () => {
7070
expect(normalized.output?.publicPath).toBe('noop:///');
7171
});
7272

73+
it('should set publicPath to devServer public path if it uses DEV_SERVER_PUBLIC_PATH', () => {
74+
const config = {
75+
devServer: { host: 'example.com', port: 3000 },
76+
output: { publicPath: 'DEV_SERVER_PUBLIC_PATH' },
77+
} as ConfigurationObject;
78+
const normalized = normalizeConfig(config, 'ios');
79+
expect(normalized.output?.publicPath).toBe(
80+
'http://example.com:3000/ios/'
81+
);
82+
});
83+
7384
it('should keep custom publicPath unchanged', () => {
7485
const config = {
7586
output: { publicPath: 'http://localhost:8081' },
7687
} as ConfigurationObject;
7788
const normalized = normalizeConfig(config, 'ios');
7889
expect(normalized.output?.publicPath).toBe('http://localhost:8081');
7990
});
91+
92+
it('should replace [platform] placeholders', () => {
93+
const config = {
94+
output: { publicPath: 'http://example.com:3000/[platform]/' },
95+
} as ConfigurationObject;
96+
const normalized = normalizeConfig(config, 'ios');
97+
expect(normalized.output?.publicPath).toBe(
98+
'http://example.com:3000/ios/'
99+
);
100+
});
80101
});
81102

82103
describe('resolve.extensions normalization', () => {

packages/repack/src/commands/common/config/getCommandConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function getStartCommandDefaults() {
1010
server: 'http',
1111
},
1212
output: {
13-
publicPath: `http://${DEFAULT_HOSTNAME}:${DEFAULT_PORT}/[platform]/`,
13+
publicPath: 'DEV_SERVER_PUBLIC_PATH',
1414
},
1515
};
1616
}

packages/repack/src/commands/common/config/normalizeConfig.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,21 @@ function normalizeOutputPath(
2424
.replaceAll('[platform]', platform);
2525
}
2626

27-
function normalizePublicPath(publicPath: string, platform: string): string {
27+
function normalizePublicPath(
28+
publicPath: string,
29+
platform: string,
30+
host?: string,
31+
port?: number
32+
): string {
2833
/* set public path to noop if it's using the deprecated `getPublicPath` function */
2934
if (publicPath === 'DEPRECATED_GET_PUBLIC_PATH') {
3035
return 'noop:///';
3136
}
3237

38+
if (publicPath === 'DEV_SERVER_PUBLIC_PATH') {
39+
return `http://${host}:${port}/${platform}/`;
40+
}
41+
3342
return publicPath.replaceAll('[platform]', platform);
3443
}
3544

@@ -73,7 +82,12 @@ export function normalizeConfig<C extends ConfigurationObject>(
7382
if (config.output?.publicPath) {
7483
normalizedConfig.output = {
7584
...normalizedConfig.output,
76-
publicPath: normalizePublicPath(config.output.publicPath, platform),
85+
publicPath: normalizePublicPath(
86+
config.output.publicPath,
87+
platform,
88+
normalizedConfig.devServer?.host ?? config.devServer?.host,
89+
normalizedConfig.devServer?.port ?? config.devServer?.port
90+
),
7791
};
7892
}
7993

0 commit comments

Comments
 (0)