Skip to content

Commit 5ef5217

Browse files
authored
fix: bundle options query params for unspecified/default params (#740)
1 parent 1b204c9 commit 5ef5217

7 files changed

Lines changed: 87 additions & 10 deletions

File tree

e2e/react_native_0_60x_ts/__tests__/bundleOptions.android.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,32 @@ describe('packager server', () => {
9494

9595
stopServer(instance);
9696
});
97+
98+
it('platform: android requesting bundle is working correctly with unspecified query bundle options', async () => {
99+
const defaultMinifyOption = false;
100+
let instance = await startServerAsync(PORT, TEST_PROJECT_DIR);
101+
let results = await fetchBundle(PORT, 'android', {
102+
dev: true,
103+
});
104+
validateBaseBundle(results.bundle, { platform: 'android' });
105+
await fetchBundle(PORT, 'android', {
106+
dev: true,
107+
});
108+
109+
await fetchBundle(PORT, 'android', {
110+
dev: true,
111+
minify: defaultMinifyOption,
112+
});
113+
114+
results = await fetchBundle(PORT, 'android', {
115+
dev: false,
116+
});
117+
118+
expect(results.response.status).toBe(501);
119+
expect(results.bundle.toString()).toEqual(
120+
'Changing query params after the bundle has been created is not supported. To see the changes you need to restart the Haul server.'
121+
);
122+
123+
stopServer(instance);
124+
});
97125
});

e2e/react_native_0_60x_ts/__tests__/bundleOptions.ios.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,32 @@ describe('packager server', () => {
9494

9595
stopServer(instance);
9696
});
97+
98+
it('platform: ios requesting bundle is working correctly with unspecified query bundle options', async () => {
99+
const defaultMinifyOption = false;
100+
let instance = await startServerAsync(PORT, TEST_PROJECT_DIR);
101+
let results = await fetchBundle(PORT, 'ios', {
102+
dev: true,
103+
});
104+
validateBaseBundle(results.bundle, { platform: 'ios' });
105+
await fetchBundle(PORT, 'ios', {
106+
dev: true,
107+
});
108+
109+
await fetchBundle(PORT, 'ios', {
110+
dev: true,
111+
minify: defaultMinifyOption,
112+
});
113+
114+
results = await fetchBundle(PORT, 'ios', {
115+
dev: false,
116+
});
117+
118+
expect(results.response.status).toBe(501);
119+
expect(results.bundle.toString()).toEqual(
120+
'Changing query params after the bundle has been created is not supported. To see the changes you need to restart the Haul server.'
121+
);
122+
123+
stopServer(instance);
124+
});
97125
});

e2e/utils/bundle.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,21 @@ export function cleanup(projectDir: string) {
4242
export async function fetchBundle(
4343
port: number,
4444
platform: string,
45-
options?: { minify: boolean; dev: boolean }
45+
options?: { minify?: boolean; dev?: boolean }
4646
) {
47-
const builtOptions = options
48-
? `?dev=${options.dev}&minify=${options.minify}`
49-
: '';
47+
let builtOptions = '';
48+
if (options) {
49+
const queryBundleOptions = [
50+
options.dev !== undefined ? { name: 'dev', value: options.dev } : null,
51+
options.minify !== undefined
52+
? { name: 'minify', value: options.minify }
53+
: null,
54+
]
55+
.filter(Boolean)
56+
.map(option => `${option!.name}=${option!.value}`)
57+
.join('&');
58+
builtOptions = `?${queryBundleOptions}`;
59+
}
5060

5161
const response = await fetch(
5262
`http://localhost:${port}/index.${platform}.bundle${builtOptions}`

e2e/utils/createBuildTimeTestSuite.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default function createBuildTimeTestSuite(
5858
).toBeTruthy();
5959
}
6060

61-
expect(processTime).toBeLessThan(24000);
61+
expect(processTime).toBeLessThan(26000);
6262
});
6363
});
6464
}

fixtures/react_native_with_haul_0_60x/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"private": true,
55
"scripts": {
66
"android": "react-native run-android",
7+
"ios": "react-native run-ios",
78
"start": "node ../../packages/haul-cli/bin/haul.js start",
89
"test": "jest",
910
"haul": "node ../../packages/haul-cli/bin/haul.js",

fixtures/react_native_with_haul_0_60x_ts/App.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
} from 'react-native/Libraries/NewAppScreen';
1818

1919
const App = () => {
20-
console.log('dev check', __DEV__);
2120
return (
2221
<Fragment>
2322
<StatusBar barStyle="dark-content" />

packages/haul-core/src/server/setupCompilerRoutes.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import getBundleDataFromURL from '../utils/getBundleDataFromURL';
1010

1111
type Platform = string;
1212
type BundleOptions = { dev?: boolean; minify?: boolean; alreadySet?: boolean };
13+
type QueryBundleOptions = Omit<BundleOptions, 'alreadySet'>;
1314
type PlatformsBundleOptions = {
1415
[platform in Platform]: BundleOptions;
1516
};
@@ -105,7 +106,10 @@ export default function setupCompilerRoutes(
105106
hasWarnedDelta = true;
106107
}
107108

108-
const bundleOptionsFromQuery = getBundleOptionsFromQuery(request.query);
109+
const bundleOptionsFromQuery = getBundleOptionsFromQuery(
110+
request.query,
111+
cliBundleOptions
112+
);
109113
const isUserChangedOptions = isUserChangedAlreadySetBundleOptions(
110114
bundleOptions[platform],
111115
bundleOptionsFromQuery
@@ -207,31 +211,38 @@ function makeResponseFromCompilerResults(
207211
return response;
208212
}
209213

210-
function areBundleOptionsSet(bundleOptions: BundleOptions) {
214+
function areBundleOptionsSet(bundleOptions: QueryBundleOptions) {
211215
return bundleOptions.dev !== undefined || bundleOptions.minify !== undefined;
212216
}
213217

214-
function getBundleOptionsFromQuery(query: { minify?: boolean; dev?: boolean }) {
218+
function getBundleOptionsFromQuery(
219+
query: QueryBundleOptions,
220+
defaultOptions: BundleOptions
221+
) {
215222
let bundleOptions: BundleOptions = {};
216223

217224
if (query.minify === true) {
218225
bundleOptions.minify = true;
219226
} else if (query.minify === false) {
220227
bundleOptions.minify = false;
228+
} else if (query.minify === undefined) {
229+
bundleOptions.minify = defaultOptions.minify;
221230
}
222231

223232
if (query.dev === true) {
224233
bundleOptions.dev = true;
225234
} else if (query.dev === false) {
226235
bundleOptions.dev = false;
236+
} else if (query.dev === undefined) {
237+
bundleOptions.dev = defaultOptions.dev;
227238
}
228239

229240
return bundleOptions;
230241
}
231242

232243
function isUserChangedAlreadySetBundleOptions(
233244
bundleOptions: BundleOptions,
234-
bundleOptionsFromQuery: BundleOptions
245+
bundleOptionsFromQuery: QueryBundleOptions
235246
) {
236247
if (areBundleOptionsSet(bundleOptionsFromQuery)) {
237248
if (bundleOptions.alreadySet) {

0 commit comments

Comments
 (0)