-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
index.js
99 lines (85 loc) · 3.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict';
const { getDefaultConfig } = require('@expo/metro-config');
const debug = require('debug')('workspaces');
const findYarnWorkspaceRoot = require('find-yarn-workspace-root');
// TODO: Use the vendored metro config in a future version after SDK 41 is released
// const { getDefaultConfig } = require('expo/metro-config');
const { assetExts, sourceExts } = require('metro-config/src/defaults/defaults');
const path = require('path');
const getSymlinkedNodeModulesForDirectory = require('./common/get-symlinked-modules');
let exclusionList;
try {
// blacklist was removed in the metro-config version used by
// react-native@0.64, but the interface is the same as the replacement,
// exclusionList, so we can use blacklist if it's available and otherwise
// use exclusionList.
exclusionList = require('metro-config/src/defaults/blacklist');
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
// Require exclusionList after attempting to load blacklist, so if an error is
// thrown then it is the same as if we only required exclusionList.
exclusionList = require('metro-config/src/defaults/exclusionList');
}
/**
* Returns a configuration object in the format expected for "metro.config.js" files. The
* configuration:
*
* * includes the Yarn workspace root in Metro's list of root directories
* * resolves symlinked packages, namely workspaces
* * excludes all modules from Haste's module system (providesModule)
* * excludes modules in the native Android and Xcode projects
*/
exports.createMetroConfiguration = function createMetroConfiguration(projectPath) {
projectPath = path.resolve(projectPath);
debug(`Creating a Metro configuration for the project at %s`, projectPath);
const {
// Remove the React Native reporter.
reporter,
...defaultConfig
} = getDefaultConfig(projectPath);
let watchFolders;
let extraNodeModules;
const workspaceRootPath = findYarnWorkspaceRoot(projectPath);
if (workspaceRootPath) {
debug(`Found Yarn workspace root at %s`, workspaceRootPath);
watchFolders = [workspaceRootPath];
extraNodeModules = {
...getSymlinkedNodeModulesForDirectory(workspaceRootPath),
...getSymlinkedNodeModulesForDirectory(projectPath),
};
} else {
debug(`Could not find Yarn workspace root`);
watchFolders = [];
extraNodeModules = getSymlinkedNodeModulesForDirectory(projectPath);
}
return {
...defaultConfig,
// Search for modules from the project's root directory
projectRoot: projectPath,
// Include npm packages from the workspace root, where packages are hoisted
watchFolders,
resolver: {
...defaultConfig.resolver,
// test-suite includes a db asset
assetExts: [...assetExts, 'db'],
// Include .cjs files
sourceExts: [...sourceExts, 'cjs'],
// Make the symlinked packages visible to Metro
extraNodeModules,
// Use Node-style module resolution instead of Haste everywhere
providesModuleNodeModules: [],
// Ignore JS files in the native Android and Xcode projects
blacklistRE: exclusionList([
/.*\/android\/React(Android|Common)\/.*/,
/.*\/versioned-react-native\/.*/,
]),
},
transformer: {
...defaultConfig.transformer,
// Ignore file-relative Babel configurations and apply only the project's
enableBabelRCLookup: false,
},
};
};