Skip to content

Instantly share code, notes, and snippets.

@ospfranco
Last active March 18, 2019 08:45
Show Gist options
  • Save ospfranco/8497e9e83d4efad306b9111aa31d8f84 to your computer and use it in GitHub Desktop.
Save ospfranco/8497e9e83d4efad306b9111aa31d8f84 to your computer and use it in GitHub Desktop.
Haste Resolver with @providesModule support for react-native 0.59
/**
* From Oscar:
*
* This entire file is copied from hasteImpl.js from within ReactNative's core repo. if you want a < 0.58 version, ask for it
* I have it on some older project, this is working for 0.59.
* stuff you need to change (for 0.59 to work):
* 1) place this file in the root of your project
* 2) install the cli (it has been removed from react-native core package): yarn add @react-native-community/cli
* 3) on your root folder you should now have a metro.config.js file, add the following keys:
* resolver: {
* providesModuleNodeModules: [
* "react-native"
* ],
* hasteImplModulePath: "./../../../../../haste_resolver.js"
* }
* 4) you might need to start metro by clearing the cache: yarn start -- --reset-cache
*
* You can now add jsDoc @providesModule comment on top of your files to use absolute imports, ex:
* @providesModule store
*
* and then on your other file
* import foo from 'store';
*/
'use strict';
const path = require('path');
const fs = require('fs');
// const stream = fs.createWriteStream('stream.txt', { flags: 'a' })
const findPlugins = require('./node_modules/@react-native-community/cli/build/tools/findPlugins');
const plugins = findPlugins.default([path.resolve(__dirname)]);
const SKILLZ_ROOT = path.resolve(__dirname, 'src') + path.sep;
// Detect out-of-tree platforms and add them to the whitelists
const pluginRoots /*: Array<
string,
> */ = plugins.haste.providesModuleNodeModules.map(
name => path.resolve(__dirname, 'node_modules/', name) + path.sep,
);
const pluginNameReducers /*: Array<
[RegExp, string],
> */ = plugins.haste.platforms.map(name => [
new RegExp(`^(.*)\.(${name})$`),
'$1',
]);
const ROOTS = [path.resolve(__dirname, 'node_modules/react-native/') + path.sep, ...pluginRoots];
const BLACKLISTED_PATTERNS /*: Array<RegExp> */ = [
/.*[\\\/]__(mocks|tests)__[\\\/].*/,
/^Libraries[\\\/]Animated[\\\/]src[\\\/]polyfills[\\\/].*/,
/^Libraries[\\\/]Renderer[\\\/]fb[\\\/].*/,
];
const WHITELISTED_PREFIXES /*: Array<string> */ = [
'IntegrationTests',
'Libraries',
'ReactAndroid',
'RNTester',
];
const NAME_REDUCERS /*: Array<[RegExp, string]> */ = [
// extract basename
[/^(?:.*[\\\/])?([a-zA-Z0-9$_.-]+)$/, '$1'],
// strip .js/.js.flow suffix
[/^(.*)\.js(\.flow)?$/, '$1'],
// strip platform suffix
[/^(.*)\.(android|ios|native|App)$/, '$1'],
// strip plugin platform suffixes
...pluginNameReducers,
];
const haste = {
/*
* @return {string|void} hasteName for module at filePath; or undefined if
* filePath is not a haste module
*/
getHasteName(
filePath /*: string */,
sourceCode /*: ?string */,
) /*: string | void */ {
// stream.write('testing path: ' + filePath + '\n');
if (!isHastePath(filePath)) {
/**
* INSERT CUSTOM SEARCH FUNCTION HERE.
*/
if (filePath.startsWith(SKILLZ_ROOT)) {
const matches = fs.readFileSync(filePath, 'utf8').match(/@providesModule ([^\n]+)/);
if (!matches) {
return undefined;
}
return matches[1];
}
return undefined;
}
const hasteName = NAME_REDUCERS.reduce(
(name, [pattern, replacement]) => name.replace(pattern, replacement),
filePath,
);
return hasteName;
},
};
function isHastePath(filePath /*: string */) /*: boolean */ {
if (!filePath.endsWith('.js') && !filePath.endsWith('.js.flow')) {
return false;
}
const root = ROOTS.find(r => filePath.startsWith(r));
if (!root) {
return false;
}
filePath = filePath.substr(root.length);
if (BLACKLISTED_PATTERNS.some(pattern => pattern.test(filePath))) {
return false;
}
if (!WHITELISTED_PREFIXES.some(prefix => filePath.startsWith(prefix))) {
return false;
} else {
return true;
}
}
module.exports = haste;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment