Skip to content

Commit 19005bf

Browse files
empyricalfacebook-github-bot
authored andcommitted
Add support for out-of-tree platform plugins (facebook#20825)
Summary: This pull request adds the ability for a platform developer to provide a `"haste"` key under the `"rnpm"` key in their `package.json` which allows the packager to pick up that platform's javascript files. The intent is to remove the need to have custom platforms hardcoded in. This is inspired by the `"jest": { "haste": {} }` key used by jest. For example, React Native Dom would have an entry like: ```json { "rnpm": { "haste": { "providesModuleNodeModules": [ "react-native-dom" ], "platforms": [ "dom" ] } } } ``` Support for more keys (path blacklists perhaps?) could be added in the future. This succeeds facebook#20662, as per a discussion I had with matthargett. I've got an open discussion over here as well: react-native-community/discussions-and-proposals#21 Pull Request resolved: facebook#20825 Differential Revision: D9596429 Pulled By: hramos fbshipit-source-id: a02f0da0bea8870bdc45d55e23da8ccbc36249f2
1 parent 0ef6b98 commit 19005bf

File tree

4 files changed

+85
-31
lines changed

4 files changed

+85
-31
lines changed

jest/__tests__/hasteImpl-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ it('returns the correct haste name for a RN library file', () => {
3232
});
3333

3434
it('returns the correct haste name for a file with a platform suffix', () => {
35-
for (const platform of ['android', 'ios', 'native', 'web', 'windows']) {
35+
for (const platform of ['android', 'ios', 'native']) {
3636
expect(
3737
getHasteName(
3838
getPath(

jest/hasteImpl.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,25 @@
1111
'use strict';
1212

1313
const path = require('path');
14+
const findPlugins = require('../local-cli/core/findPlugins');
1415

15-
const ROOTS = [
16-
path.resolve(__dirname, '..') + path.sep,
17-
path.resolve(__dirname, '../../react-native-windows') + path.sep,
18-
path.resolve(__dirname, '../../react-native-dom') + path.sep,
19-
];
16+
const plugins = findPlugins([path.resolve(__dirname, '../../../')]);
17+
18+
// Detect out-of-tree platforms and add them to the whitelists
19+
const pluginRoots /*: Array<
20+
string,
21+
> */ = plugins.haste.providesModuleNodeModules.map(
22+
name => path.resolve(__dirname, '../../', name) + path.sep,
23+
);
24+
25+
const pluginNameReducers /*: Array<
26+
[RegExp, string],
27+
> */ = plugins.haste.platforms.map(name => [
28+
new RegExp(`^(.*)\.(${name})$`),
29+
'$1',
30+
]);
31+
32+
const ROOTS = [path.resolve(__dirname, '..') + path.sep, ...pluginRoots];
2033

2134
const BLACKLISTED_PATTERNS /*: Array<RegExp> */ = [
2235
/.*[\\\/]__(mocks|tests)__[\\\/].*/,
@@ -36,8 +49,10 @@ const NAME_REDUCERS /*: Array<[RegExp, string]> */ = [
3649
[/^(?:.*[\\\/])?([a-zA-Z0-9$_.-]+)$/, '$1'],
3750
// strip .js/.js.flow suffix
3851
[/^(.*)\.js(\.flow)?$/, '$1'],
39-
// strip .android/.ios/.native/.web suffix
40-
[/^(.*)\.(android|ios|native|web|windows|dom)$/, '$1'],
52+
// strip platform suffix
53+
[/^(.*)\.(android|ios|native)$/, '$1'],
54+
// strip plugin platform suffixes
55+
...pluginNameReducers,
4156
];
4257

4358
const haste = {

local-cli/core/findPlugins.js

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,36 +47,61 @@ const findPlatformsInPackage = pjson => {
4747
return path.join(pjson.name, pjson.rnpm.platform);
4848
};
4949

50+
const getEmptyPluginConfig = () => ({
51+
commands: [],
52+
platforms: [],
53+
haste: {
54+
platforms: [],
55+
providesModuleNodeModules: [],
56+
},
57+
});
58+
59+
const findHasteConfigInPackageAndConcat = (pjson, haste) => {
60+
if (!pjson.rnpm || !pjson.rnpm.haste) {
61+
return;
62+
}
63+
let pkgHaste = pjson.rnpm.haste;
64+
65+
if (pkgHaste.platforms) {
66+
haste.platforms = haste.platforms.concat(pkgHaste.platforms);
67+
}
68+
69+
if (pkgHaste.providesModuleNodeModules) {
70+
haste.providesModuleNodeModules = haste.providesModuleNodeModules.concat(
71+
pkgHaste.providesModuleNodeModules,
72+
);
73+
}
74+
};
75+
5076
const findPluginInFolder = folder => {
5177
const pjson = readPackage(folder);
5278

5379
if (!pjson) {
54-
return {commands: [], platforms: []};
80+
return getEmptyPluginConfig();
5581
}
5682

5783
const deps = union(
5884
Object.keys(pjson.dependencies || {}),
5985
Object.keys(pjson.devDependencies || {}),
6086
);
6187

62-
return deps.reduce(
63-
(acc, pkg) => {
64-
let commands = acc.commands;
65-
let platforms = acc.platforms;
66-
if (isRNPMPlugin(pkg)) {
67-
commands = commands.concat(pkg);
68-
}
69-
if (isReactNativePlugin(pkg)) {
70-
const pkgJson = readPackage(path.join(folder, 'node_modules', pkg));
71-
if (pkgJson) {
72-
commands = commands.concat(findPluginsInReactNativePackage(pkgJson));
73-
platforms = platforms.concat(findPlatformsInPackage(pkgJson));
74-
}
88+
return deps.reduce((acc, pkg) => {
89+
let commands = acc.commands;
90+
let platforms = acc.platforms;
91+
let haste = acc.haste;
92+
if (isRNPMPlugin(pkg)) {
93+
commands = commands.concat(pkg);
94+
}
95+
if (isReactNativePlugin(pkg)) {
96+
const pkgJson = readPackage(path.join(folder, 'node_modules', pkg));
97+
if (pkgJson) {
98+
commands = commands.concat(findPluginsInReactNativePackage(pkgJson));
99+
platforms = platforms.concat(findPlatformsInPackage(pkgJson));
100+
findHasteConfigInPackageAndConcat(pkgJson, haste);
75101
}
76-
return {commands: commands, platforms: platforms};
77-
},
78-
{commands: [], platforms: []},
79-
);
102+
}
103+
return {commands: commands, platforms: platforms, haste: haste};
104+
}, getEmptyPluginConfig());
80105
};
81106

82107
/**
@@ -89,5 +114,11 @@ module.exports = function findPlugins(folders) {
89114
return {
90115
commands: uniq(flatten(plugins.map(p => p.commands))),
91116
platforms: uniq(flatten(plugins.map(p => p.platforms))),
117+
haste: {
118+
platforms: uniq(flatten(plugins.map(p => p.haste.platforms))),
119+
providesModuleNodeModules: uniq(
120+
flatten(plugins.map(p => p.haste.providesModuleNodeModules)),
121+
),
122+
},
92123
};
93124
};

local-cli/core/index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ const defaultConfig = {
7070
hasteImplModulePath: require.resolve('../../jest/hasteImpl'),
7171

7272
getPlatforms(): Array<string> {
73-
return ['ios', 'android', 'windows', 'web', 'dom'];
73+
return ['ios', 'android', 'native', ...plugins.haste.platforms];
7474
},
7575

7676
getProvidesModuleNodeModules(): Array<string> {
77-
return ['react-native', 'react-native-windows', 'react-native-dom'];
77+
return ['react-native', ...plugins.haste.providesModuleNodeModules];
7878
},
7979
};
8080

@@ -132,9 +132,17 @@ async function getCliConfig(): Promise<RNConfig> {
132132
);
133133

134134
config.transformer.assetRegistryPath = ASSET_REGISTRY_PATH;
135-
config.resolver.hasteImplModulePath = config.resolver.hasteImplModulePath || defaultConfig.hasteImplModulePath;
136-
config.resolver.platforms = config.resolver.platforms || defaultConfig.getPlatforms();
137-
config.resolver.providesModuleNodeModules = config.resolver.providesModuleNodeModules || defaultConfig.getProvidesModuleNodeModules();
135+
config.resolver.hasteImplModulePath =
136+
config.resolver.hasteImplModulePath || defaultConfig.hasteImplModulePath;
137+
config.resolver.platforms = config.resolver.platforms
138+
? config.resolver.platforms.concat(defaultConfig.getPlatforms())
139+
: defaultConfig.getPlatforms();
140+
config.resolver.providesModuleNodeModules = config.resolver
141+
.providesModuleNodeModules
142+
? config.resolver.providesModuleNodeModules.concat(
143+
defaultConfig.getProvidesModuleNodeModules(),
144+
)
145+
: defaultConfig.getProvidesModuleNodeModules();
138146

139147
return {...defaultRNConfig, ...config};
140148
}

0 commit comments

Comments
 (0)