Skip to content

Commit

Permalink
Enable Flow in server and serializer tests
Browse files Browse the repository at this point in the history
Summary:
Changelog: [Internal]

Enables Flow in `Server-test`, `baseBytecodeBundle-test`, `baseJSBundle-test` and `getRamBundleInfo-test`, to unblock additional changes to the serializer in support of [lazy bundling](react-native-community/discussions-and-proposals#605).

Reviewed By: jacdebug

Differential Revision: D44184128

fbshipit-source-id: 53981514e17c901f351b3d2bb40b7ffaf5e875a6
  • Loading branch information
motiz88 authored and facebook-github-bot committed Mar 21, 2023
1 parent 5caa240 commit 8a43c11
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,40 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @oncall react_native
*/

'use strict';

import type {Module, TransformInputOptions} from '../../types.flow';

import CountingSet from '../../../lib/CountingSet';

const baseBytecodeBundle = require('../baseBytecodeBundle');
const {compile, validateBytecodeModule} = require('metro-hermes-compiler');
const path = require('path');

const transformOptions: TransformInputOptions = {
customTransformOptions: {},
dev: true,
hot: true,
minify: true,
platform: 'web',
runtimeBytecodeVersion: 900,
type: 'module',
unstable_transformProfile: 'default',
};

const polyfillCode = '__d(function() {/* code for polyfill */});';
const polyfillBytecode = compile(polyfillCode, {
sourceURL: 'polyfill-source',
}).bytecode;
const polyfill = {
const polyfill: Module<> = {
path: '/polyfill',
dependencies: new Map(),
inverseDependencies: new CountingSet<string>(),
output: [
{
type: 'js/script',
Expand All @@ -36,9 +55,18 @@ const fooModuleCode = '__d(function() {/* code for foo */});';
const fooModuleBytecode = compile(fooModuleCode, {
sourceURL: 'foo-source',
}).bytecode;
const fooModule = {
const fooModule: Module<> = {
path: '/root/foo',
dependencies: new Map([['./bar', {absolutePath: '/root/bar', data: {}}]]),
dependencies: new Map([
[
'./bar',
{
absolutePath: '/root/bar',
data: {data: {asyncType: null, locs: [], key: './bar'}, name: './bar'},
},
],
]),
inverseDependencies: new CountingSet(),
output: [
{
type: 'js/module',
Expand All @@ -62,9 +90,10 @@ const barModuleCode = '__d(function() {/* code for bar */});';
const barModuleBytecode = compile(barModuleCode, {
sourceURL: 'bar-source',
}).bytecode;
const barModule = {
const barModule: Module<> = {
path: '/root/bar',
dependencies: new Map(),
inverseDependencies: new CountingSet(['/root/foo']),
output: [
{
type: 'js/module',
Expand All @@ -84,7 +113,7 @@ const barModule = {
getSource: () => Buffer.from('bar-source'),
};

const getRunModuleStatement = moduleId =>
const getRunModuleStatement = (moduleId: number | string) =>
`require(${JSON.stringify(moduleId)});`;

it('should generate a bundle', () => {
Expand All @@ -96,17 +125,25 @@ it('should generate a bundle', () => {
['/root/foo', fooModule],
['/root/bar', barModule],
]),
entryPoints: ['foo'],
entryPoints: new Set(['/root/foo']),
transformOptions,
},
{
processModuleFilter: () => true,
asyncRequireModulePath: '',
// $FlowFixMe[incompatible-call] createModuleId assumes numeric IDs - is this too strict?
createModuleId: filePath => path.basename(filePath),
dev: true,
getRunModuleStatement,
includeAsyncPaths: false,
inlineSourceMap: false,
modulesOnly: false,
processModuleFilter: () => true,
projectRoot: '/root',
runBeforeMainModule: [],
runModule: true,
serverRoot: '/root',
sourceMapUrl: 'http://localhost/bundle.map',
sourceUrl: null,
},
);

Expand Down Expand Up @@ -137,18 +174,25 @@ it('does not add polyfills when `modulesOnly` is used', () => {
['/root/foo', fooModule],
['/root/bar', barModule],
]),
entryPoints: ['foo'],
entryPoints: new Set(['foo']),
transformOptions,
},
{
processModuleFilter: () => true,
asyncRequireModulePath: '',
// $FlowFixMe[incompatible-call] createModuleId assumes numeric IDs - is this too strict?
createModuleId: filePath => path.basename(filePath),
dev: true,
getRunModuleStatement,
includeAsyncPaths: false,
inlineSourceMap: false,
modulesOnly: true,
processModuleFilter: () => true,
projectRoot: '/root',
runBeforeMainModule: [],
runModule: true,
serverRoot: '/root',
sourceMapUrl: 'http://localhost/bundle.map',
sourceUrl: null,
},
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @oncall react_native
*/

'use strict';

import type {Module, TransformInputOptions} from '../../types.flow';

import CountingSet from '../../../lib/CountingSet';

const createModuleIdFactory = require('../../../lib/createModuleIdFactory');
const baseJSBundle = require('../baseJSBundle');
const path = require('path');

const polyfill = {
const polyfill: Module<> = {
path: '/polyfill',
dependencies: new Map(),
inverseDependencies: new CountingSet(),
output: [
{
type: 'js/script',
Expand All @@ -24,9 +32,18 @@ const polyfill = {
getSource: () => Buffer.from('polyfill-source'),
};

const fooModule = {
const fooModule: Module<> = {
path: '/root/foo',
dependencies: new Map([['./bar', {absolutePath: '/root/bar', data: {}}]]),
dependencies: new Map([
[
'./bar',
{
absolutePath: '/root/bar',
data: {data: {asyncType: null, locs: [], key: './bar'}, name: './bar'},
},
],
]),
inverseDependencies: new CountingSet(),
output: [
{
type: 'js/module',
Expand All @@ -40,9 +57,10 @@ const fooModule = {
getSource: () => Buffer.from('foo-source'),
};

const barModule = {
const barModule: Module<> = {
path: '/root/bar',
dependencies: new Map(),
inverseDependencies: new CountingSet(['/root/foo']),
output: [
{
type: 'js/module',
Expand All @@ -56,9 +74,20 @@ const barModule = {
getSource: () => Buffer.from('bar-source'),
};

const getRunModuleStatement = moduleId =>
const getRunModuleStatement = (moduleId: number | string) =>
`require(${JSON.stringify(moduleId)});`;

const transformOptions: TransformInputOptions = {
customTransformOptions: {},
dev: true,
hot: true,
minify: true,
platform: 'web',
runtimeBytecodeVersion: null,
type: 'module',
unstable_transformProfile: 'default',
};

it('should generate a very simple bundle', () => {
expect(
baseJSBundle(
Expand All @@ -69,17 +98,25 @@ it('should generate a very simple bundle', () => {
['/root/foo', fooModule],
['/root/bar', barModule],
]),
entryPoints: ['foo'],
entryPoints: new Set(['/root/foo']),
transformOptions,
},
{
processModuleFilter: () => true,
asyncRequireModulePath: '',
// $FlowFixMe[incompatible-call] createModuleId assumes numeric IDs - is this too strict?
createModuleId: filePath => path.basename(filePath),
dev: true,
getRunModuleStatement,
includeAsyncPaths: false,
inlineSourceMap: false,
modulesOnly: false,
processModuleFilter: () => true,
projectRoot: '/root',
runBeforeMainModule: [],
runModule: true,
serverRoot: '/root',
sourceMapUrl: 'http://localhost/bundle.map',
sourceUrl: null,
},
),
).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -111,17 +148,25 @@ it('should add runBeforeMainModule statements if found in the graph', () => {
['/root/foo', fooModule],
['/root/bar', barModule],
]),
entryPoints: ['/root/foo'],
entryPoints: new Set(['/root/foo']),
transformOptions,
},
{
processModuleFilter: () => true,
asyncRequireModulePath: '',
// $FlowFixMe[incompatible-call] createModuleId assumes numeric IDs - is this too strict?
createModuleId: filePath => path.basename(filePath),
dev: true,
getRunModuleStatement,
includeAsyncPaths: false,
inlineSourceMap: false,
modulesOnly: false,
processModuleFilter: () => true,
projectRoot: '/root',
runBeforeMainModule: ['/root/bar', 'non-existant'],
runModule: true,
serverRoot: '/root',
sourceMapUrl: 'http://localhost/bundle.map',
sourceUrl: null,
},
).post,
).toMatchInlineSnapshot(`
Expand All @@ -141,17 +186,24 @@ it('should handle numeric module ids', () => {
['/root/foo', fooModule],
['/root/bar', barModule],
]),
entryPoints: ['/root/foo'],
entryPoints: new Set(['/root/foo']),
transformOptions,
},
{
processModuleFilter: () => true,
asyncRequireModulePath: '',
createModuleId: createModuleIdFactory(),
dev: true,
getRunModuleStatement,
includeAsyncPaths: false,
inlineSourceMap: false,
modulesOnly: false,
processModuleFilter: () => true,
projectRoot: '/root',
runBeforeMainModule: ['/root/bar', 'non-existant'],
runModule: true,
serverRoot: '/root',
sourceMapUrl: 'http://localhost/bundle.map',
sourceUrl: null,
},
).modules,
).toMatchInlineSnapshot(`
Expand All @@ -178,17 +230,26 @@ it('outputs custom runModule statements', () => {
['/root/foo', fooModule],
['/root/bar', barModule],
]),
entryPoints: ['/root/foo'],
entryPoints: new Set(['/root/foo']),
transformOptions,
},
{
processModuleFilter: () => true,
asyncRequireModulePath: '',
// $FlowFixMe[incompatible-call] createModuleId assumes numeric IDs - is this too strict?
createModuleId: filePath => path.basename(filePath),
dev: true,
getRunModuleStatement: moduleId =>
`export default require(${JSON.stringify(moduleId)}).default;`,
includeAsyncPaths: false,
inlineSourceMap: false,
modulesOnly: false,
processModuleFilter: () => true,
projectRoot: '/root',
runBeforeMainModule: ['/root/bar'],
runModule: true,
serverRoot: '/root',
sourceMapUrl: null,
sourceUrl: null,
},
).post,
).toMatchInlineSnapshot(`
Expand All @@ -206,17 +267,25 @@ it('should add an inline source map to a very simple bundle', () => {
['/root/foo', fooModule],
['/root/bar', barModule],
]),
entryPoints: ['foo'],
entryPoints: new Set(['/root/foo']),
transformOptions,
},
{
processModuleFilter: () => true,
asyncRequireModulePath: '',
// $FlowFixMe[incompatible-call] createModuleId assumes numeric IDs - is this too strict?
createModuleId: filePath => path.basename(filePath),
dev: true,
getRunModuleStatement,
includeAsyncPaths: false,
inlineSourceMap: true,
modulesOnly: false,
processModuleFilter: () => true,
projectRoot: '/root',
runBeforeMainModule: [],
runModule: true,
inlineSourceMap: true,
serverRoot: '/root',
sourceMapUrl: null,
sourceUrl: null,
},
);
expect(bundle.post.slice(0, bundle.post.lastIndexOf('base64'))).toEqual(
Expand Down Expand Up @@ -248,18 +317,25 @@ it('does not add polyfills when `modulesOnly` is used', () => {
['/root/foo', fooModule],
['/root/bar', barModule],
]),
entryPoints: ['foo'],
entryPoints: new Set(['/root/foo']),
transformOptions,
},
{
processModuleFilter: () => true,
asyncRequireModulePath: '',
// $FlowFixMe[incompatible-call] createModuleId assumes numeric IDs - is this too strict?
createModuleId: filePath => path.basename(filePath),
dev: true,
getRunModuleStatement,
includeAsyncPaths: false,
inlineSourceMap: false,
modulesOnly: true,
processModuleFilter: () => true,
projectRoot: '/root',
runBeforeMainModule: [],
runModule: true,
serverRoot: '/root',
sourceMapUrl: 'http://localhost/bundle.map',
sourceUrl: null,
},
),
).toMatchInlineSnapshot(`
Expand Down
Loading

0 comments on commit 8a43c11

Please # to comment.