Skip to content

Commit

Permalink
Add symlinks under node_modules as part of projectRoots
Browse files Browse the repository at this point in the history
Summary:
Support symlinks under `node_modules` for all local-cli commands. PR facebook#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 facebook#11810

Differential Revision: D4487741

fbshipit-source-id: 87fe44194134d086dca4eaca99ee5742d6eadb69
  • Loading branch information
harshil07 authored and nicktate committed Feb 7, 2017
1 parent c689b2f commit 8b575e5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
17 changes: 13 additions & 4 deletions local-cli/core/default.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand All @@ -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.
*
Expand Down Expand Up @@ -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);
},
};

Expand Down
7 changes: 1 addition & 6 deletions local-cli/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' +
Expand Down
File renamed without changes.

0 comments on commit 8b575e5

Please sign in to comment.