Add symlinks under node_modules as part of projectRoots

Summary:
Support symlinks under `node_modules` for all local-cli commands. PR https://github.com/facebook/react-native/pull/9009 only adds symlink support to the packager.

But other cli commands like `react-native bundle` creates its own instance of packager that doesn't have symlinks as part of its project roots, which results in the bundler breaking since it cannot find modules that you have symlinked.

This change ensures all `local-cli` commands add symlinks to its project roots.

Test plan (required)

1.  Create a symlink in node_modules (for instance use npm/yarn link)
2. Run `react-native bundle`.
Closes https://github.com/facebook/react-native/pull/11810

Differential Revision: D4487741

fbshipit-source-id: 87fe44194134d086dca4eaca99ee5742d6eadb69
This commit is contained in:
Harshil Shah 2017-01-31 03:35:00 -08:00 committed by Facebook Github Bot
parent af111ab2ac
commit bce6ece5f6
3 changed files with 14 additions and 10 deletions

View File

@ -22,6 +22,9 @@ const windows = require('./windows');
const wrapCommands = require('./wrapCommands');
const findPlugins = require('./findPlugins');
const findSymlinksPaths = require('../util/findSymlinksPaths');
const NODE_MODULES = path.resolve(__dirname, '..', '..', '..');
import type {ConfigT} from './index';
const getRNPMConfig = (folder) =>
@ -32,6 +35,9 @@ const attachPackage = (command, pkg) => Array.isArray(command)
? command.map(cmd => attachPackage(cmd, pkg))
: { ...command, pkg };
const addSymlinkToRoots = (roots) =>
roots.concat(findSymlinksPaths(NODE_MODULES, roots));
/**
* Default configuration for the CLI.
*
@ -97,18 +103,21 @@ const config: ConfigT = {
getProjectRoots() {
const root = process.env.REACT_NATIVE_APP_ROOT;
if (root) {
return [path.resolve(root)];
return addSymlinkToRoots([path.resolve(root)]);
}
var roots;
if (__dirname.match(/node_modules[\/\\]react-native[\/\\]local-cli[\/\\]core$/)) {
// Packager is running from node_modules.
// This is the default case for all projects created using 'react-native init'.
return [path.resolve(__dirname, '../../../..')];
roots = [path.resolve(__dirname, '../../../..')];
} else if (__dirname.match(/Pods[\/\\]React[\/\\]packager$/)) {
// React Native was installed using CocoaPods.
return [path.resolve(__dirname, '../../../..')];
roots = [path.resolve(__dirname, '../../../..')];
} else {
return [path.resolve(__dirname, '../..')];
roots = [path.resolve(__dirname, '../..')];
}
return addSymlinkToRoots(roots);
},
};

View File

@ -9,20 +9,15 @@
'use strict';
const chalk = require('chalk');
const findSymlinksPaths = require('./findSymlinksPaths');
const formatBanner = require('./formatBanner');
const path = require('path');
const runServer = require('./runServer');
const NODE_MODULES = path.resolve(__dirname, '..', '..', '..');
/**
* Starts the React Native Packager Server.
*/
function server(argv, config, args) {
const roots = args.projectRoots.concat(args.root);
args.projectRoots = roots.concat(
findSymlinksPaths(NODE_MODULES, roots)
);
args.projectRoots.concat(args.root);
console.log(formatBanner(
'Running packager on port ' + args.port + '.\n\n' +