From 19946745f7653edecad0be80e8abbdf2c0e6e2fc Mon Sep 17 00:00:00 2001 From: Harshil Shah Date: Tue, 31 Jan 2017 03:35:00 -0800 Subject: [PATCH] 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 --- local-cli/core/default.config.js | 17 +++++++++++++---- local-cli/server/server.js | 7 +------ local-cli/{server => util}/findSymlinksPaths.js | 0 3 files changed, 14 insertions(+), 10 deletions(-) rename local-cli/{server => util}/findSymlinksPaths.js (100%) diff --git a/local-cli/core/default.config.js b/local-cli/core/default.config.js index 1c4d01a6901cf6..c3fb67a4eb8462 100644 --- a/local-cli/core/default.config.js +++ b/local-cli/core/default.config.js @@ -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); }, }; diff --git a/local-cli/server/server.js b/local-cli/server/server.js index 72e9a017b96e92..879c5995f407ad 100644 --- a/local-cli/server/server.js +++ b/local-cli/server/server.js @@ -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' + diff --git a/local-cli/server/findSymlinksPaths.js b/local-cli/util/findSymlinksPaths.js similarity index 100% rename from local-cli/server/findSymlinksPaths.js rename to local-cli/util/findSymlinksPaths.js