Skip to content

Commit

Permalink
Simplify File type: map is nullable, but not optional
Browse files Browse the repository at this point in the history
Summary:
Simplifies the `File` type by making `map` a non-optional, but nullable property.
Also adds helper functions for empty/virtual modules

Reviewed By: jeanlauliac

Differential Revision: D5111580

fbshipit-source-id: 9cab6634a9bdb0522dc36aec2abccaef9cf35339
  • Loading branch information
davidaurelio authored and facebook-github-bot committed May 23, 2017
1 parent 918bae2 commit 6b2122b
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 21 deletions.
6 changes: 2 additions & 4 deletions packager/src/ModuleGraph/Graph.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
const emptyFunction = require('fbjs/lib/emptyFunction'); const emptyFunction = require('fbjs/lib/emptyFunction');
const invariant = require('fbjs/lib/invariant'); const invariant = require('fbjs/lib/invariant');
const memoize = require('async/memoize'); const memoize = require('async/memoize');
const emptyModule = require('./module').empty;
const nullthrows = require('fbjs/lib/nullthrows'); const nullthrows = require('fbjs/lib/nullthrows');
const queue = require('async/queue'); const queue = require('async/queue');
const seq = require('async/seq'); const seq = require('async/seq');
Expand Down Expand Up @@ -49,9 +50,6 @@ type Async$Queue<T, C> = {
type LoadQueue = type LoadQueue =
Async$Queue<{id: string, parent: ?string}, Callback<File, Array<string>>>; Async$Queue<{id: string, parent: ?string}, Callback<File, Array<string>>>;


const createParentModule =
() => ({file: {code: '', type: 'script', path: ''}, dependencies: []});

const NO_OPTIONS = {}; const NO_OPTIONS = {};


exports.create = function create(resolve: ResolveFn, load: LoadFn): GraphFn { exports.create = function create(resolve: ResolveFn, load: LoadFn): GraphFn {
Expand Down Expand Up @@ -101,7 +99,7 @@ exports.create = function create(resolve: ResolveFn, load: LoadFn): GraphFn {
}; };


function createGraphHelpers(loadQueue, skip) { function createGraphHelpers(loadQueue, skip) {
const modules = new Map([[null, createParentModule()]]); const modules = new Map([[null, emptyModule()]]);


function collect( function collect(
path = null, path = null,
Expand Down
3 changes: 1 addition & 2 deletions packager/src/ModuleGraph/ModuleGraph.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ const defaults = require('../../defaults');
const nullthrows = require('fbjs/lib/nullthrows'); const nullthrows = require('fbjs/lib/nullthrows');
const parallel = require('async/parallel'); const parallel = require('async/parallel');
const seq = require('async/seq'); const seq = require('async/seq');

const virtualModule = require('./module').virtual;
const {virtualModule} = require('./output/util');


import type { import type {
Callback, Callback,
Expand Down
1 change: 1 addition & 0 deletions packager/src/ModuleGraph/__tests__/ModuleGraph-test.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('build setup', () => {
file: { file: {
code: 'var __DEV__=true,__BUNDLE_START_TIME__=' + code: 'var __DEV__=true,__BUNDLE_START_TIME__=' +
'this.nativePerformanceNow?nativePerformanceNow():Date.now();', 'this.nativePerformanceNow?nativePerformanceNow():Date.now();',
map: null,
path: '', path: '',
type: 'script', type: 'script',
}, },
Expand Down
28 changes: 28 additions & 0 deletions packager/src/ModuleGraph/module.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
'use strict';


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

exports.empty = (): Module => virtual('');

// creates a virtual module (i.e. not corresponding to a file on disk)
// with the given source code.
const virtual = exports.virtual = (code: string): Module => ({
dependencies: [],
file: {
code,
map: null,
path: '',
type: 'script',
},
});
16 changes: 2 additions & 14 deletions packager/src/ModuleGraph/output/util.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/ */
'use strict'; 'use strict';


const virtualModule = require('../module').virtual;

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


// Transformed modules have the form // Transformed modules have the form
Expand Down Expand Up @@ -77,17 +79,3 @@ exports.requireCallsTo = function* (
yield virtualModule(`require(${idForPath(module.file)});`); yield virtualModule(`require(${idForPath(module.file)});`);
} }
}; };

// creates a virtual module (i.e. not corresponding to a file on disk)
// with the given source code.
exports.virtualModule = virtualModule;
function virtualModule(code: string) {
return {
dependencies: [],
file: {
code,
path: '',
type: 'script',
},
};
}
2 changes: 1 addition & 1 deletion packager/src/ModuleGraph/types.flow.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Dependency = {|


export type File = {| export type File = {|
code: string, code: string,
map?: ?MappingsMap, map: ?MappingsMap,
path: string, path: string,
type: CodeFileTypes, type: CodeFileTypes,
|}; |};
Expand Down

0 comments on commit 6b2122b

Please sign in to comment.