Skip to content

Commit

Permalink
Add module resolver for aliasing react-native
Browse files Browse the repository at this point in the history
  • Loading branch information
ptmt committed Jun 5, 2017
1 parent 267ab5e commit 4564d56
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 1 deletion.
3 changes: 3 additions & 0 deletions local-cli/core/default.config.js
Expand Up @@ -15,6 +15,7 @@ const flatten = require('lodash').flatten;
const android = require('./android');
const findAssets = require('./findAssets');
const ios = require('./ios');
const macos = require('./macos');
const windows = require('./windows');
const wrapCommands = require('./wrapCommands');
const findPlugins = require('./findPlugins');
Expand Down Expand Up @@ -77,6 +78,7 @@ const config = {
const rnpm = getRNPMConfig(folder);

return Object.assign({}, rnpm, {
macos: macos.dependencyConfig(folder, rnpm.ios || {}),
ios: ios.projectConfig(folder, rnpm.ios || {}),
android: android.projectConfig(folder, rnpm.android || {}),
windows: windows.projectConfig(folder, rnpm.windows || {}),
Expand All @@ -90,6 +92,7 @@ const config = {
);

return Object.assign({}, rnpm, {
macos: macos.dependencyConfig(folder, rnpm.ios || {}),
ios: ios.dependencyConfig(folder, rnpm.ios || {}),
android: android.dependencyConfig(folder, rnpm.android || {}),
windows: windows.dependencyConfig(folder, rnpm.windows || {}),
Expand Down
60 changes: 60 additions & 0 deletions local-cli/core/macos/findProject.js
@@ -0,0 +1,60 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

const glob = require('glob');
const path = require('path');

/**
* Glob pattern to look for xcodeproj
*/
const GLOB_PATTERN = '**/*.xcodeproj';

/**
* Regexp matching all test projects
*/
const TEST_PROJECTS = /test|example|sample/i;

/**
* Base iOS folder
*/
const IOS_BASE = 'macos';

/**
* These folders will be excluded from search to speed it up
*/
const GLOB_EXCLUDE_PATTERN = ['**/@(Pods|node_modules)/**'];

/**
* Finds iOS project by looking for all .xcodeproj files
* in given folder.
*
* Returns first match if files are found or null
*
* Note: `./ios/*.xcodeproj` are returned regardless of the name
*/
module.exports = function findProject(folder) {
const projects = glob
.sync(GLOB_PATTERN, {
cwd: folder,
ignore: GLOB_EXCLUDE_PATTERN,
})
.filter(project => {
return path.dirname(project) === IOS_BASE || !TEST_PROJECTS.test(project);
})
.sort((projectA, projectB) => {
return path.dirname(projectA) === IOS_BASE ? -1 : 1;
});

if (projects.length === 0) {
return null;
}

return projects[0];
};
55 changes: 55 additions & 0 deletions local-cli/core/macos/index.js
@@ -0,0 +1,55 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

const findProject = require('./findProject');
const path = require('path');

/**
* For libraries specified without an extension, add '.tbd' for those that
* start with 'lib' and '.framework' to the rest.
*/
const mapSharedLibaries = (libraries) => {
return libraries.map(name => {
if (path.extname(name)) {
return name;
}
return name + (name.indexOf('lib') === 0 ? '.tbd' : '.framework');
});
};

/**
* Returns project config by analyzing given folder and applying some user defaults
* when constructing final object
*/
exports.projectConfig = function projectConfigIOS(folder, userConfig) {
const project = userConfig.project || findProject(folder);

/**
* No iOS config found here
*/
if (!project) {
return null;
}

const projectPath = path.join(folder, project);

return {
sourceDir: path.dirname(projectPath),
folder: folder,
pbxprojPath: path.join(projectPath, 'project.pbxproj'),
projectPath: projectPath,
projectName: path.basename(projectPath),
libraryFolder: userConfig.libraryFolder || 'Libraries',
sharedLibraries: mapSharedLibaries(userConfig.sharedLibraries || []),
plist: userConfig.plist || [],
};
};

exports.dependencyConfig = exports.projectConfig;
2 changes: 1 addition & 1 deletion local-cli/util/Config.js
Expand Up @@ -98,7 +98,7 @@ const defaultConfig: ConfigT = {
getAssetExts: () => [],
getBlacklistRE: () => blacklist(),
getPlatforms: () => [],
getProjectRoots: () => [process.cwd()],
// getProjectRoots: () => [process.cwd()],
getProvidesModuleNodeModules: () => providesModuleNodeModules.slice(),
getSourceExts: () => [],
getTransformModulePath: () => path.resolve(__dirname, '../../packager/transformer'),
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -139,6 +139,7 @@
"babel-core": "^6.24.1",
"babel-generator": "^6.24.1",
"babel-plugin-external-helpers": "^6.18.0",
"babel-plugin-module-resolver": "^2.7.1",
"babel-plugin-syntax-trailing-function-commas": "^6.20.0",
"babel-plugin-transform-async-to-generator": "6.16.0",
"babel-plugin-transform-flow-strip-types": "^6.21.0",
Expand Down

0 comments on commit 4564d56

Please sign in to comment.