mirror of https://github.com/status-im/metro.git
Add getRunModuleStatement config param to configure the require() statements
Reviewed By: cpojer Differential Revision: D7334078 fbshipit-source-id: c19340567c634e3173ee707e92389eaaa4e724e9
This commit is contained in:
parent
a396616e4b
commit
6aed6433b0
|
@ -94,6 +94,12 @@ export type ConfigT = {
|
|||
*/
|
||||
getProvidesModuleNodeModules?: () => Array<string>,
|
||||
|
||||
/**
|
||||
* Specify the format of the initial require statements that are appended
|
||||
* at the end of the bundle. By default is `require(${moduleId});`
|
||||
*/
|
||||
getRunModuleStatement: (number | string) => string,
|
||||
|
||||
/**
|
||||
* Specify any additional source file extensions to be used by the packager.
|
||||
* For example, if you want to include a .ts file, you would return ['ts']
|
||||
|
@ -171,6 +177,8 @@ const DEFAULT = ({
|
|||
// node_modules/metro/
|
||||
getProjectRoots: () => [path.resolve(__dirname, '../..')],
|
||||
getProvidesModuleNodeModules: () => providesModuleNodeModules.slice(),
|
||||
getRunModuleStatement: (moduleId: number | string) =>
|
||||
`require(${JSON.stringify(moduleId)});`,
|
||||
getSourceExts: () => [],
|
||||
getTransformModulePath: () => require.resolve('./transformer.js'),
|
||||
getTransformOptions: async () => ({}),
|
||||
|
|
|
@ -38,6 +38,7 @@ const graph = {
|
|||
const options = {
|
||||
createModuleId: createModuleIdFactory(),
|
||||
dev: true,
|
||||
getRunModuleStatement: moduleId => `require(${JSON.stringify(moduleId)});`,
|
||||
runBeforeMainModule: [],
|
||||
runModule: true,
|
||||
sourceMapUrl: 'http://localhost/bundle.map',
|
||||
|
|
|
@ -37,11 +37,15 @@ const graph = {
|
|||
|
||||
const pre = [createModule('pre', [], 'script')[1]];
|
||||
|
||||
const getRunModuleStatement = moduleId =>
|
||||
`require(${JSON.stringify(moduleId)});`;
|
||||
|
||||
it('should return the RAM bundle info', async () => {
|
||||
expect(
|
||||
await getRamBundleInfo('/root/entry.js', pre, graph, {
|
||||
createModuleId: path => path,
|
||||
excludeSource: false,
|
||||
getRunModuleStatement,
|
||||
getTransformOptions: () => ({
|
||||
preloadedModules: {},
|
||||
ramGroups: [],
|
||||
|
@ -63,6 +67,7 @@ it('should use the preloadedModules and ramGroup configs to build a RAM bundle',
|
|||
const bundleInfo = await getRamBundleInfo('/root/entry.js', pre, graph, {
|
||||
createModuleId: path => path,
|
||||
excludeSource: false,
|
||||
getRunModuleStatement,
|
||||
getTransformOptions,
|
||||
dev: true,
|
||||
runBeforeMainModule: [],
|
||||
|
|
|
@ -32,6 +32,9 @@ const barModule = {
|
|||
output: {code: '__d(function() {/* code for bar */});'},
|
||||
};
|
||||
|
||||
const getRunModuleStatement = moduleId =>
|
||||
`require(${JSON.stringify(moduleId)});`;
|
||||
|
||||
it('should serialize a very simple bundle', () => {
|
||||
expect(
|
||||
plainJSBundle(
|
||||
|
@ -44,6 +47,7 @@ it('should serialize a very simple bundle', () => {
|
|||
{
|
||||
createModuleId: path => path,
|
||||
dev: true,
|
||||
getRunModuleStatement,
|
||||
runBeforeMainModule: [],
|
||||
runModule: true,
|
||||
sourceMapUrl: 'http://localhost/bundle.map',
|
||||
|
@ -72,6 +76,7 @@ it('should add runBeforeMainModule statements if found in the graph', () => {
|
|||
{
|
||||
createModuleId: path => path,
|
||||
dev: true,
|
||||
getRunModuleStatement,
|
||||
runBeforeMainModule: ['bar', 'non-existant'],
|
||||
runModule: true,
|
||||
sourceMapUrl: 'http://localhost/bundle.map',
|
||||
|
@ -101,6 +106,7 @@ it('should handle numeric module ids', () => {
|
|||
{
|
||||
createModuleId: createModuleIdFactory(),
|
||||
dev: true,
|
||||
getRunModuleStatement,
|
||||
runBeforeMainModule: ['bar', 'non-existant'],
|
||||
runModule: true,
|
||||
sourceMapUrl: 'http://localhost/bundle.map',
|
||||
|
@ -117,3 +123,32 @@ it('should handle numeric module ids', () => {
|
|||
].join('\n'),
|
||||
);
|
||||
});
|
||||
|
||||
it('outputs custom runModule statements', () => {
|
||||
expect(
|
||||
plainJSBundle(
|
||||
'foo',
|
||||
[polyfill],
|
||||
{
|
||||
dependencies: new Map([['foo', fooModule], ['bar', barModule]]),
|
||||
entryPoints: ['foo'],
|
||||
},
|
||||
{
|
||||
createModuleId: path => path,
|
||||
dev: true,
|
||||
getRunModuleStatement: moduleId =>
|
||||
`export default require(${JSON.stringify(moduleId)}).default;`,
|
||||
runBeforeMainModule: ['bar'],
|
||||
runModule: true,
|
||||
},
|
||||
),
|
||||
).toEqual(
|
||||
[
|
||||
'__d(function() {/* code for polyfill */});',
|
||||
'__d(function() {/* code for foo */},"foo",["bar"],"foo");',
|
||||
'__d(function() {/* code for bar */},"bar",[],"bar");',
|
||||
'export default require("bar").default;',
|
||||
'export default require("foo").default;',
|
||||
].join('\n'),
|
||||
);
|
||||
});
|
||||
|
|
|
@ -18,8 +18,9 @@ import type {Delta, Graph} from '../';
|
|||
import type {DependencyEdge} from '../traverseDependencies';
|
||||
|
||||
type Options = {|
|
||||
createModuleId: string => number | string,
|
||||
+createModuleId: string => number | string,
|
||||
+dev: boolean,
|
||||
+getRunModuleStatement: (number | string) => string,
|
||||
+runBeforeMainModule: $ReadOnlyArray<string>,
|
||||
+runModule: boolean,
|
||||
+sourceMapUrl: ?string,
|
||||
|
|
|
@ -24,10 +24,11 @@ import type {Graph} from '../DeltaCalculator';
|
|||
import type {DependencyEdge} from '../traverseDependencies';
|
||||
|
||||
type Options = {|
|
||||
createModuleId: string => number,
|
||||
+createModuleId: string => number,
|
||||
+dev: boolean,
|
||||
+excludeSource: boolean,
|
||||
getTransformOptions: ?GetTransformOptions,
|
||||
+getRunModuleStatement: number => string,
|
||||
+getTransformOptions: ?GetTransformOptions,
|
||||
+platform: ?string,
|
||||
+runBeforeMainModule: $ReadOnlyArray<string>,
|
||||
+runModule: boolean,
|
||||
|
|
|
@ -18,8 +18,9 @@ import type {Graph} from '../DeltaCalculator';
|
|||
import type {DependencyEdge} from '../traverseDependencies';
|
||||
|
||||
type Options = {|
|
||||
createModuleId: string => number | string,
|
||||
+createModuleId: string => number | string,
|
||||
+dev: boolean,
|
||||
+getRunModuleStatement: (number | string) => string,
|
||||
+runBeforeMainModule: $ReadOnlyArray<string>,
|
||||
+runModule: boolean,
|
||||
+sourceMapUrl: ?string,
|
||||
|
|
|
@ -59,6 +59,7 @@ describe('processRequest', () => {
|
|||
projectRoots: ['/root'],
|
||||
blacklistRE: null,
|
||||
cacheVersion: null,
|
||||
getRunModuleStatement: moduleId => `require(${JSON.stringify(moduleId)});`,
|
||||
polyfillModuleNames: null,
|
||||
reporter: require('../../lib/reporting').nullReporter,
|
||||
getModulesRunBeforeMainModule: () => ['InitializeCore'],
|
||||
|
|
|
@ -109,6 +109,7 @@ class Server {
|
|||
reporter: Reporter,
|
||||
resetCache: boolean,
|
||||
+getModulesRunBeforeMainModule: (entryFilePath: string) => Array<string>,
|
||||
+getRunModuleStatement: (number | string) => string,
|
||||
silent: boolean,
|
||||
+sourceExts: Array<string>,
|
||||
+transformCache: TransformCache,
|
||||
|
@ -158,6 +159,7 @@ class Server {
|
|||
extraNodeModules: options.extraNodeModules || {},
|
||||
getModulesRunBeforeMainModule: options.getModulesRunBeforeMainModule,
|
||||
getPolyfills: options.getPolyfills,
|
||||
getRunModuleStatement: options.getRunModuleStatement,
|
||||
getTransformOptions: options.getTransformOptions,
|
||||
globalTransformCache: options.globalTransformCache,
|
||||
hasteImplModulePath: options.hasteImplModulePath,
|
||||
|
@ -197,6 +199,7 @@ class Server {
|
|||
const {
|
||||
createModuleId,
|
||||
getModulesRunBeforeMainModule,
|
||||
getRunModuleStatement,
|
||||
silent,
|
||||
...bundlerOptionsFromServerOptions
|
||||
} = this._opts;
|
||||
|
@ -249,6 +252,7 @@ class Server {
|
|||
return {
|
||||
code: plainJSBundle(entryPoint, prepend, graph, {
|
||||
createModuleId: this._opts.createModuleId,
|
||||
getRunModuleStatement: this._opts.getRunModuleStatement,
|
||||
dev: options.dev,
|
||||
runBeforeMainModule: options.runBeforeMainModule,
|
||||
runModule: options.runModule,
|
||||
|
@ -291,6 +295,7 @@ class Server {
|
|||
createModuleId: this._opts.createModuleId,
|
||||
dev: options.dev,
|
||||
excludeSource: options.excludeSource,
|
||||
getRunModuleStatement: this._opts.getRunModuleStatement,
|
||||
getTransformOptions: this._opts.getTransformOptions,
|
||||
platform: options.platform,
|
||||
runBeforeMainModule: options.runBeforeMainModule,
|
||||
|
@ -699,6 +704,7 @@ class Server {
|
|||
{
|
||||
createModuleId: this._opts.createModuleId,
|
||||
dev: options.dev,
|
||||
getRunModuleStatement: this._opts.getRunModuleStatement,
|
||||
runBeforeMainModule: options.runBeforeMainModule,
|
||||
runModule: options.runModule,
|
||||
sourceMapUrl: options.sourceMapUrl,
|
||||
|
@ -760,6 +766,7 @@ class Server {
|
|||
result = {
|
||||
bundle: plainJSBundle(options.entryFile, prepend, graph, {
|
||||
createModuleId: this._opts.createModuleId,
|
||||
getRunModuleStatement: this._opts.getRunModuleStatement,
|
||||
dev: options.dev,
|
||||
runBeforeMainModule: options.runBeforeMainModule,
|
||||
runModule: options.runModule,
|
||||
|
|
|
@ -129,6 +129,7 @@ async function runMetro({
|
|||
getPolyfills: normalizedConfig.getPolyfills,
|
||||
getModulesRunBeforeMainModule:
|
||||
normalizedConfig.getModulesRunBeforeMainModule,
|
||||
getRunModuleStatement: normalizedConfig.getRunModuleStatement,
|
||||
getTransformOptions: normalizedConfig.getTransformOptions,
|
||||
globalTransformCache,
|
||||
hasteImplModulePath: normalizedConfig.hasteImplModulePath,
|
||||
|
|
|
@ -27,6 +27,9 @@ const ASSET_REGISTRY_PATH = path.resolve(
|
|||
'../basic_bundle/AssetRegistry',
|
||||
);
|
||||
|
||||
const getRunModuleStatement = moduleId =>
|
||||
`require(${JSON.stringify(moduleId)});`;
|
||||
|
||||
describe('basic_bundle', () => {
|
||||
const absPathRe = new RegExp(INPUT_PATH, 'g');
|
||||
const polyfill1 = path.join(INPUT_PATH, 'polyfill-1.js');
|
||||
|
@ -96,6 +99,7 @@ describe('basic_bundle', () => {
|
|||
dynamicDepsInPackages: 'reject',
|
||||
getModulesRunBeforeMainModule: () => ['InitializeCore'],
|
||||
getPolyfills: () => [polyfill1, polyfill2],
|
||||
getRunModuleStatement,
|
||||
projectRoots: [INPUT_PATH, POLYFILLS_PATH],
|
||||
transformCache: Metro.TransformCaching.none(),
|
||||
transformModulePath: require.resolve('../../transformer'),
|
||||
|
@ -120,6 +124,7 @@ describe('basic_bundle', () => {
|
|||
dynamicDepsInPackages: 'reject',
|
||||
getModulesRunBeforeMainModule: () => ['InitializeCore'],
|
||||
getPolyfills: () => [],
|
||||
getRunModuleStatement,
|
||||
projectRoots: [INPUT_PATH, POLYFILLS_PATH],
|
||||
transformCache: Metro.TransformCaching.none(),
|
||||
transformModulePath: require.resolve('../../transformer'),
|
||||
|
|
|
@ -180,6 +180,7 @@ function toServerOptions(options: Options): ServerOptions {
|
|||
extraNodeModules: options.extraNodeModules,
|
||||
getModulesRunBeforeMainModule: options.getModulesRunBeforeMainModule,
|
||||
getPolyfills: options.getPolyfills,
|
||||
getRunModuleStatement: options.getRunModuleStatement,
|
||||
getTransformOptions: options.getTransformOptions,
|
||||
globalTransformCache: options.globalTransformCache,
|
||||
hasteImplModulePath: options.hasteImplModulePath,
|
||||
|
|
|
@ -13,17 +13,18 @@
|
|||
import type {Graph} from '../DeltaBundler/DeltaCalculator';
|
||||
import type {DependencyEdge} from '../DeltaBundler/traverseDependencies';
|
||||
|
||||
type Options = {
|
||||
+createModuleId: string => number | string,
|
||||
type Options<T: number | string> = {
|
||||
+createModuleId: string => T,
|
||||
+getRunModuleStatement: T => string,
|
||||
+runBeforeMainModule: $ReadOnlyArray<string>,
|
||||
+runModule: boolean,
|
||||
+sourceMapUrl: ?string,
|
||||
};
|
||||
|
||||
function getAppendScripts(
|
||||
function getAppendScripts<T: number | string>(
|
||||
entryPoint: string,
|
||||
graph: Graph,
|
||||
options: Options,
|
||||
options: Options<T>,
|
||||
): $ReadOnlyArray<DependencyEdge> {
|
||||
const output = [];
|
||||
|
||||
|
@ -37,7 +38,7 @@ function getAppendScripts(
|
|||
dependencies: new Map(),
|
||||
inverseDependencies: new Set(),
|
||||
output: {
|
||||
code: `require(${JSON.stringify(options.createModuleId(path))});`,
|
||||
code: options.getRunModuleStatement(options.createModuleId(path)),
|
||||
source: '',
|
||||
map: [],
|
||||
type: 'script',
|
||||
|
|
|
@ -88,6 +88,7 @@ export type Options = {|
|
|||
enableBabelRCLookup: boolean,
|
||||
extraNodeModules?: {},
|
||||
getPolyfills: ({platform: ?string}) => $ReadOnlyArray<string>,
|
||||
+getRunModuleStatement: (number | string) => string,
|
||||
getTransformOptions?: GetTransformOptions,
|
||||
globalTransformCache: ?GlobalTransformCache,
|
||||
hasteImplModulePath?: string,
|
||||
|
|
Loading…
Reference in New Issue