Use `require.resolve` rather than `path.resolve(__dirname, ...)` to find files

Summary: Here, we change the mechanism of finding files from `path.resolve(__dirname, ...)` to `require.resolve(...)`. The latter depends on the location of one file rather than two, and is usually more readable and intention revealing.

Reviewed By: cpojer

Differential Revision: D7858479

fbshipit-source-id: 98373cc0860faa791e60d5f11502d762fd22a409
This commit is contained in:
David Aurelio 2018-05-03 06:08:35 -07:00 committed by Facebook Github Bot
parent 81b49fa89b
commit c157c9a1f3
4 changed files with 8 additions and 15 deletions

View File

@ -30,7 +30,7 @@ describe('code transformation worker:', () => {
'arbitrary/file.js',
`local/file.js`,
'someReallyArbitrary(code)',
path.join(__dirname, '../../../transformer.js'),
require.resolve('metro/src/transformer.js'),
true,
{
dev: true,
@ -59,7 +59,7 @@ describe('code transformation worker:', () => {
'arbitrary/file.js',
`local/file.js`,
'arbitrary(code)',
path.join(__dirname, '../../../transformer.js'),
require.resolve('metro/src/transformer.js'),
false,
{
dev: true,
@ -94,7 +94,7 @@ describe('code transformation worker:', () => {
'const b = require("b");',
'import c from "./c";',
].join('\n'),
path.join(__dirname, '../../../transformer.js'),
require.resolve('metro/src/transformer.js'),
false,
{
dev: true,

View File

@ -16,7 +16,6 @@ jest.mock('../../../package.json', () => ({
}));
const getTransformCacheKeyFn = require('../getTransformCacheKeyFn');
const path = require('path');
describe('getTransformCacheKeyFn', () => {
it('Should return always the same key for the same params', async () => {
@ -26,10 +25,7 @@ describe('getTransformCacheKeyFn', () => {
cacheVersion: '1.0',
dynamicDepsInPackages: 'arbitrary',
projectRoots: [__dirname],
transformModulePath: path.resolve(
__dirname,
'../../defaultTransform.js',
),
transformModulePath: require.resolve('metro/src/defaultTransform.js'),
})(),
).toMatchSnapshot();
});
@ -40,7 +36,7 @@ describe('getTransformCacheKeyFn', () => {
cacheVersion: '1.0',
dynamicDepsInPackages: 'arbitrary',
projectRoots: [__dirname],
transformModulePath: path.resolve(__dirname, '../../defaultTransform.js'),
transformModulePath: require.resolve('metro/src/defaultTransform.js'),
};
const changedParams = [
@ -54,10 +50,7 @@ describe('getTransformCacheKeyFn', () => {
},
{
...baseParams,
transformModulePath: path.resolve(
__dirname,
'../../../src/transformer.js',
),
transformModulePath: require.resolve('metro/src/transformer.js'),
},
];

View File

@ -29,7 +29,7 @@ function getPreludeCode({
}
function formatExtraVars(extraVars) {
let assignments = [];
const assignments = [];
for (const key in extraVars) {
assignments.push(`${key}=${JSON.stringify(extraVars[key])}`);
}

View File

@ -60,7 +60,7 @@ const getBabelRC = (function() {
// use the Babel config provided with react-native.
if (!projectBabelRCPath || !fs.existsSync(projectBabelRCPath)) {
babelRC = json5.parse(
fs.readFileSync(path.resolve(__dirname, '..', 'rn-babelrc.json')),
fs.readFileSync(require.resolve('metro/rn-babelrc.json')),
);
// Require the babel-preset's listed in the default babel config