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

View File

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

View File

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

View File

@ -60,7 +60,7 @@ const getBabelRC = (function() {
// use the Babel config provided with react-native. // use the Babel config provided with react-native.
if (!projectBabelRCPath || !fs.existsSync(projectBabelRCPath)) { if (!projectBabelRCPath || !fs.existsSync(projectBabelRCPath)) {
babelRC = json5.parse( 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 // Require the babel-preset's listed in the default babel config