mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-09 22:43:10 +08:00
Summary: in new-style builds, the prelude is not wrapped into an IIFE, so `global` is not available. Since the bundle as a whole is not running in strict mode, we can access the global object with `this`. Reviewed By: jeanlauliac Differential Revision: D5051731 fbshipit-source-id: 4003b5e57ba8626e38e68e92d5778c2c59ca69a5
103 lines
2.8 KiB
JavaScript
103 lines
2.8 KiB
JavaScript
/**
|
|
* Copyright (c) 2017-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.
|
|
*/
|
|
'use strict';
|
|
|
|
jest.disableAutomock();
|
|
|
|
const ModuleGraph = require('../ModuleGraph');
|
|
const defaults = require('../../../defaults');
|
|
|
|
const FILE_TYPE = 'module';
|
|
|
|
describe('build setup', () => {
|
|
const buildSetup = ModuleGraph.createBuildSetup(graph);
|
|
const noOptions = {};
|
|
const noEntryPoints = [];
|
|
|
|
it('adds a prelude containing start time and `__DEV__` to the build', done => {
|
|
buildSetup(noEntryPoints, noOptions, (error, result) => {
|
|
expect(error).toEqual(null);
|
|
|
|
const [prelude] = result.modules;
|
|
expect(prelude).toEqual({
|
|
dependencies: [],
|
|
file: {
|
|
code: 'var __DEV__=true,__BUNDLE_START_TIME__=' +
|
|
'this.nativePerformanceNow?nativePerformanceNow():Date.now();',
|
|
path: '',
|
|
type: 'script',
|
|
},
|
|
});
|
|
done();
|
|
});
|
|
});
|
|
|
|
it('sets `__DEV__` to false in the prelude if optimization is enabled', done => {
|
|
buildSetup(noEntryPoints, {optimize: true}, (error, result) => {
|
|
const [prelude] = result.modules;
|
|
expect(prelude.file.code)
|
|
.toEqual('var __DEV__=false,__BUNDLE_START_TIME__=' +
|
|
'this.nativePerformanceNow?nativePerformanceNow():Date.now();');
|
|
done();
|
|
});
|
|
});
|
|
|
|
it('places the module system implementation directly after the prelude', done => {
|
|
buildSetup(noEntryPoints, noOptions, (error, result) => {
|
|
const [, moduleSystem] = result.modules;
|
|
expect(moduleSystem).toEqual({
|
|
dependencies: [],
|
|
file: {
|
|
code: '',
|
|
path: defaults.moduleSystem,
|
|
type: FILE_TYPE,
|
|
},
|
|
});
|
|
done();
|
|
});
|
|
});
|
|
|
|
it('places polyfills after the module system', done => {
|
|
buildSetup(noEntryPoints, noOptions, (error, result) => {
|
|
const polyfills =
|
|
Array.from(result.modules).slice(2, 2 + defaults.polyfills.length);
|
|
expect(polyfills).toEqual(defaults.polyfills.map(moduleFromPath));
|
|
done();
|
|
});
|
|
});
|
|
|
|
it('places all entry points at the end', done => {
|
|
const entryPoints = ['a', 'b', 'c'];
|
|
buildSetup(entryPoints, noOptions, (error, result) => {
|
|
expect(Array.from(result.modules).slice(-3))
|
|
.toEqual(entryPoints.map(moduleFromPath));
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
|
|
function moduleFromPath(path) {
|
|
return {
|
|
dependencies: [],
|
|
file: {
|
|
code: '',
|
|
path,
|
|
type: FILE_TYPE,
|
|
},
|
|
};
|
|
}
|
|
|
|
function graph(entryPoints, platform, options, callback) {
|
|
const modules = Array.from(entryPoints, moduleFromPath);
|
|
callback(null, {
|
|
entryModules: modules,
|
|
modules,
|
|
});
|
|
}
|