mirror of
https://github.com/status-im/react-native.git
synced 2025-01-12 10:34:57 +00:00
Add support for out-of-tree platform plugins (#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 #20662, as per a discussion I had with matthargett. I've got an open discussion over here as well: https://github.com/react-native-community/discussions-and-proposals/issues/21 Pull Request resolved: https://github.com/facebook/react-native/pull/20825 Differential Revision: D9596429 Pulled By: hramos fbshipit-source-id: a02f0da0bea8870bdc45d55e23da8ccbc36249f2
This commit is contained in:
parent
a549a5377e
commit
03476a225e
@ -32,7 +32,7 @@ it('returns the correct haste name for a RN library file', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('returns the correct haste name for a file with a platform suffix', () => {
|
it('returns the correct haste name for a file with a platform suffix', () => {
|
||||||
for (const platform of ['android', 'ios', 'native', 'web', 'windows']) {
|
for (const platform of ['android', 'ios', 'native']) {
|
||||||
expect(
|
expect(
|
||||||
getHasteName(
|
getHasteName(
|
||||||
getPath(
|
getPath(
|
||||||
|
@ -11,12 +11,25 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const findPlugins = require('../local-cli/core/findPlugins');
|
||||||
|
|
||||||
const ROOTS = [
|
const plugins = findPlugins([path.resolve(__dirname, '../../../')]);
|
||||||
path.resolve(__dirname, '..') + path.sep,
|
|
||||||
path.resolve(__dirname, '../../react-native-windows') + path.sep,
|
// Detect out-of-tree platforms and add them to the whitelists
|
||||||
path.resolve(__dirname, '../../react-native-dom') + path.sep,
|
const pluginRoots /*: Array<
|
||||||
];
|
string,
|
||||||
|
> */ = plugins.haste.providesModuleNodeModules.map(
|
||||||
|
name => path.resolve(__dirname, '../../', name) + path.sep,
|
||||||
|
);
|
||||||
|
|
||||||
|
const pluginNameReducers /*: Array<
|
||||||
|
[RegExp, string],
|
||||||
|
> */ = plugins.haste.platforms.map(name => [
|
||||||
|
new RegExp(`^(.*)\.(${name})$`),
|
||||||
|
'$1',
|
||||||
|
]);
|
||||||
|
|
||||||
|
const ROOTS = [path.resolve(__dirname, '..') + path.sep, ...pluginRoots];
|
||||||
|
|
||||||
const BLACKLISTED_PATTERNS /*: Array<RegExp> */ = [
|
const BLACKLISTED_PATTERNS /*: Array<RegExp> */ = [
|
||||||
/.*[\\\/]__(mocks|tests)__[\\\/].*/,
|
/.*[\\\/]__(mocks|tests)__[\\\/].*/,
|
||||||
@ -36,8 +49,10 @@ const NAME_REDUCERS /*: Array<[RegExp, string]> */ = [
|
|||||||
[/^(?:.*[\\\/])?([a-zA-Z0-9$_.-]+)$/, '$1'],
|
[/^(?:.*[\\\/])?([a-zA-Z0-9$_.-]+)$/, '$1'],
|
||||||
// strip .js/.js.flow suffix
|
// strip .js/.js.flow suffix
|
||||||
[/^(.*)\.js(\.flow)?$/, '$1'],
|
[/^(.*)\.js(\.flow)?$/, '$1'],
|
||||||
// strip .android/.ios/.native/.web suffix
|
// strip platform suffix
|
||||||
[/^(.*)\.(android|ios|native|web|windows|dom)$/, '$1'],
|
[/^(.*)\.(android|ios|native)$/, '$1'],
|
||||||
|
// strip plugin platform suffixes
|
||||||
|
...pluginNameReducers,
|
||||||
];
|
];
|
||||||
|
|
||||||
const haste = {
|
const haste = {
|
||||||
|
@ -47,11 +47,37 @@ const findPlatformsInPackage = pjson => {
|
|||||||
return path.join(pjson.name, pjson.rnpm.platform);
|
return path.join(pjson.name, pjson.rnpm.platform);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getEmptyPluginConfig = () => ({
|
||||||
|
commands: [],
|
||||||
|
platforms: [],
|
||||||
|
haste: {
|
||||||
|
platforms: [],
|
||||||
|
providesModuleNodeModules: [],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const findHasteConfigInPackageAndConcat = (pjson, haste) => {
|
||||||
|
if (!pjson.rnpm || !pjson.rnpm.haste) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let pkgHaste = pjson.rnpm.haste;
|
||||||
|
|
||||||
|
if (pkgHaste.platforms) {
|
||||||
|
haste.platforms = haste.platforms.concat(pkgHaste.platforms);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pkgHaste.providesModuleNodeModules) {
|
||||||
|
haste.providesModuleNodeModules = haste.providesModuleNodeModules.concat(
|
||||||
|
pkgHaste.providesModuleNodeModules,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const findPluginInFolder = folder => {
|
const findPluginInFolder = folder => {
|
||||||
const pjson = readPackage(folder);
|
const pjson = readPackage(folder);
|
||||||
|
|
||||||
if (!pjson) {
|
if (!pjson) {
|
||||||
return {commands: [], platforms: []};
|
return getEmptyPluginConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
const deps = union(
|
const deps = union(
|
||||||
@ -59,24 +85,23 @@ const findPluginInFolder = folder => {
|
|||||||
Object.keys(pjson.devDependencies || {}),
|
Object.keys(pjson.devDependencies || {}),
|
||||||
);
|
);
|
||||||
|
|
||||||
return deps.reduce(
|
return deps.reduce((acc, pkg) => {
|
||||||
(acc, pkg) => {
|
let commands = acc.commands;
|
||||||
let commands = acc.commands;
|
let platforms = acc.platforms;
|
||||||
let platforms = acc.platforms;
|
let haste = acc.haste;
|
||||||
if (isRNPMPlugin(pkg)) {
|
if (isRNPMPlugin(pkg)) {
|
||||||
commands = commands.concat(pkg);
|
commands = commands.concat(pkg);
|
||||||
|
}
|
||||||
|
if (isReactNativePlugin(pkg)) {
|
||||||
|
const pkgJson = readPackage(path.join(folder, 'node_modules', pkg));
|
||||||
|
if (pkgJson) {
|
||||||
|
commands = commands.concat(findPluginsInReactNativePackage(pkgJson));
|
||||||
|
platforms = platforms.concat(findPlatformsInPackage(pkgJson));
|
||||||
|
findHasteConfigInPackageAndConcat(pkgJson, haste);
|
||||||
}
|
}
|
||||||
if (isReactNativePlugin(pkg)) {
|
}
|
||||||
const pkgJson = readPackage(path.join(folder, 'node_modules', pkg));
|
return {commands: commands, platforms: platforms, haste: haste};
|
||||||
if (pkgJson) {
|
}, getEmptyPluginConfig());
|
||||||
commands = commands.concat(findPluginsInReactNativePackage(pkgJson));
|
|
||||||
platforms = platforms.concat(findPlatformsInPackage(pkgJson));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return {commands: commands, platforms: platforms};
|
|
||||||
},
|
|
||||||
{commands: [], platforms: []},
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -89,5 +114,11 @@ module.exports = function findPlugins(folders) {
|
|||||||
return {
|
return {
|
||||||
commands: uniq(flatten(plugins.map(p => p.commands))),
|
commands: uniq(flatten(plugins.map(p => p.commands))),
|
||||||
platforms: uniq(flatten(plugins.map(p => p.platforms))),
|
platforms: uniq(flatten(plugins.map(p => p.platforms))),
|
||||||
|
haste: {
|
||||||
|
platforms: uniq(flatten(plugins.map(p => p.haste.platforms))),
|
||||||
|
providesModuleNodeModules: uniq(
|
||||||
|
flatten(plugins.map(p => p.haste.providesModuleNodeModules)),
|
||||||
|
),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -70,11 +70,11 @@ const defaultConfig = {
|
|||||||
hasteImplModulePath: require.resolve('../../jest/hasteImpl'),
|
hasteImplModulePath: require.resolve('../../jest/hasteImpl'),
|
||||||
|
|
||||||
getPlatforms(): Array<string> {
|
getPlatforms(): Array<string> {
|
||||||
return ['ios', 'android', 'windows', 'web', 'dom'];
|
return ['ios', 'android', 'native', ...plugins.haste.platforms];
|
||||||
},
|
},
|
||||||
|
|
||||||
getProvidesModuleNodeModules(): Array<string> {
|
getProvidesModuleNodeModules(): Array<string> {
|
||||||
return ['react-native', 'react-native-windows', 'react-native-dom'];
|
return ['react-native', ...plugins.haste.providesModuleNodeModules];
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -132,9 +132,17 @@ async function getCliConfig(): Promise<RNConfig> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
config.transformer.assetRegistryPath = ASSET_REGISTRY_PATH;
|
config.transformer.assetRegistryPath = ASSET_REGISTRY_PATH;
|
||||||
config.resolver.hasteImplModulePath = config.resolver.hasteImplModulePath || defaultConfig.hasteImplModulePath;
|
config.resolver.hasteImplModulePath =
|
||||||
config.resolver.platforms = config.resolver.platforms || defaultConfig.getPlatforms();
|
config.resolver.hasteImplModulePath || defaultConfig.hasteImplModulePath;
|
||||||
config.resolver.providesModuleNodeModules = config.resolver.providesModuleNodeModules || defaultConfig.getProvidesModuleNodeModules();
|
config.resolver.platforms = config.resolver.platforms
|
||||||
|
? config.resolver.platforms.concat(defaultConfig.getPlatforms())
|
||||||
|
: defaultConfig.getPlatforms();
|
||||||
|
config.resolver.providesModuleNodeModules = config.resolver
|
||||||
|
.providesModuleNodeModules
|
||||||
|
? config.resolver.providesModuleNodeModules.concat(
|
||||||
|
defaultConfig.getProvidesModuleNodeModules(),
|
||||||
|
)
|
||||||
|
: defaultConfig.getProvidesModuleNodeModules();
|
||||||
|
|
||||||
return {...defaultRNConfig, ...config};
|
return {...defaultRNConfig, ...config};
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user