-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreact-native-start-with-link.js
105 lines (94 loc) · 2.75 KB
/
react-native-start-with-link.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
100
101
102
103
104
105
/**
* - check symlink in depencency and devDepency
* - if found, generate rn-cli-config.js
* - react-native start with rn-cli-config
*/
// https://github.com/facebook/react-native/issues/14400
/* eslint-disable */
const packageJson = require('./package.json');
const fs = require('fs');
const exec = require('child_process').execSync;
const RN_CLI_CONFIG_NAME = `metro.config.js`;
main();
function main() {
const deps = Object.keys(
Object.assign({}, packageJson.dependencies, packageJson.devDependencies),
);
const symlinkPathes = getSymlinkPathes(deps);
generateRnCliConfig(symlinkPathes, RN_CLI_CONFIG_NAME);
runBundlerWithConfig(RN_CLI_CONFIG_NAME);
}
function getSymlinkPathes(deps) {
const depLinks = [];
const depPathes = [];
deps.forEach((dep) => {
const stat = fs.lstatSync('node_modules/' + dep);
if (stat.isSymbolicLink()) {
depLinks.push(dep);
depPathes.push(fs.realpathSync('node_modules/' + dep));
}
});
console.log('Starting react native with symlink modules:');
console.log(
depLinks.map((link, i) => ' ' + link + ' -> ' + depPathes[i]).join('\n'),
);
return depPathes;
}
function generateRnCliConfig(symlinkPathes, configName) {
const fileBody = `
var path = require('path');
var blacklist;
try {
blacklist = require('metro-config/src/defaults/exclusionList');
} catch(e) {
blacklist = require('metro/src/blacklist');
}
var config = {
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: true,
},
}),
},
watchFolders: [
${symlinkPathes
.map((path) => `path.resolve('${path}')`)
.map((path) => path.replace(/\\/g, '//'))}
],
resolver: {
blacklistRE: blacklist([
${symlinkPathes
.map((path) => path.replace(/\\/g, '//'))
.map(
(path) =>
`/${path.replace(
/\//g,
'[\\/\\\\]',
)}[\\/\\\\]node_modules[\\/\\\\]react-native[\\/\\\\].*/`,
)}
]),
extraNodeModules: {
'assert': require.resolve('assert/'),
'buffer': require.resolve('buffer/'),
'constants': require.resolve('constants-browserify'),
'crypto': require.resolve('react-native-crypto'),
'events': require.resolve('events/'),
'process': require.resolve('process/browser.js'),
'react-native': path.resolve(__dirname, 'node_modules/react-native'),
'stream': require.resolve('readable-stream'),
'vm': require.resolve('vm-browserify')
},
},
};
module.exports = config;
`;
fs.writeFileSync(configName, fileBody);
}
function runBundlerWithConfig(configName) {
exec(
`node node_modules/react-native/local-cli/cli.js start --config ${configName} --reset-cache`,
{ stdio: [0, 1, 2] },
);
}