Skip to content

Commit

Permalink
feat: Support running under yarn pnp (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyfarrell committed Nov 1, 2019
1 parent 69682ef commit 15d949b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 23 deletions.
8 changes: 4 additions & 4 deletions node-preload-legacy-require.js
Expand Up @@ -11,17 +11,17 @@ function generateRequire(filename) {
return `--require ${filename}`;
}

function processNodePath(value) {
function processNodePath(value, dir) {
const existing = value === '' ? [] : value.split(path.delimiter);
if (existing.includes(__dirname)) {
if (existing.includes(dir)) {
return value;
}

return existing.concat(__dirname).join(path.delimiter);
return existing.concat(dir).join(path.delimiter);
}

module.exports = {
generateRequire,
processNodePath,
needsPathEnv: __dirname.includes(' ')
needsPathEnv: dir => dir.includes(' ')
};
2 changes: 1 addition & 1 deletion node-preload-modern-require.js
Expand Up @@ -8,5 +8,5 @@ module.exports = {
generateRequire,
// The export explicitly exists but is undefined as it will never be called
processNodePath: undefined,
needsPathEnv: false
needsPathEnv: () => false
};
38 changes: 31 additions & 7 deletions node-preload-singleton.js
Expand Up @@ -65,20 +65,36 @@ function findPreloadModule() {
}

const nodeOptionRequireSelf = generateRequire(require.resolve('./node-preload.js'));
/* istanbul ignore next: yarn specific */
const pnpapiFile = () => process.versions.pnp && require.resolve('pnpapi');

const preloadList = loadPreloadList();
const propagate = loadPropagated();
const preloadModule = findPreloadModule();

function processNodeOptions(value) {
if (value.includes(nodeOptionRequireSelf)) {
return value;
/* istanbul ignore next: yarn specific */
const requirePNPAPI = (process.versions.pnp && generateRequire(pnpapiFile())) || '';
/* istanbul ignore next: yarn specific */
if (process.versions.pnp) {
/* Need pnpapi to be first, remove so we can reinsert before node-preload */
value = value.replace(requirePNPAPI, '');
}

if (!value.includes(nodeOptionRequireSelf)) {
if (value !== '') {
value = ' ' + value;
}

value = `${nodeOptionRequireSelf}${value}`;
}

if (value !== '') {
value = ' ' + value;
/* istanbul ignore next: yarn specific */
if (process.versions.pnp) {
value = `${requirePNPAPI} ${value}`;
}

return `${nodeOptionRequireSelf}${value}`;
return value;
}

function processEnvPairs(options) {
Expand All @@ -94,8 +110,16 @@ function processEnvPairs(options) {
env.NODE_POLYFILL_PRELOAD = preloadList.join(path.delimiter);

/* istanbul ignore next */
if (needsPathEnv) {
env.NODE_PATH = processNodePath(env.NODE_PATH || '');
if (needsPathEnv(__dirname)) {
env.NODE_PATH = processNodePath(env.NODE_PATH || '', __dirname);
}

/* istanbul ignore next: yarn specific */
if (process.versions.pnp) {
const pnpapiDir = path.dirname(pnpapiFile());
if (needsPathEnv(pnpapiDir)) {
env.NODE_PATH = processNodePath(env.NODE_PATH || '', pnpapiDir);
}
}

env.NODE_POLYFILL_PROPAGATE_ENV = JSON.stringify(Object.keys(propagate));
Expand Down
17 changes: 9 additions & 8 deletions test/legacy-require.js
Expand Up @@ -9,11 +9,12 @@ const noSpaceFile = path.resolve('/dir/file.js');
const spaceFile = path.resolve('/space dir/file.js');

test('exports', async t => {
t.is(typeof legacyRequire, 'object');
t.type(legacyRequire, 'object');
t.deepEqual(Object.keys(legacyRequire).sort(), ['generateRequire', 'needsPathEnv', 'processNodePath']);
t.is(typeof generateRequire, 'function');
t.is(typeof processNodePath, 'function');
t.is(needsPathEnv, __dirname.includes(' '));
t.type(generateRequire, 'function');
t.type(processNodePath, 'function');
t.is(needsPathEnv(noSpaceFile), false);
t.is(needsPathEnv(spaceFile), true);
});

test('generateRequire', async t => {
Expand All @@ -29,8 +30,8 @@ test('generateRequire', async t => {
test('processNodePath', async t => {
const dirname = path.resolve(__dirname, '..');

t.is(processNodePath(''), dirname);
t.is(processNodePath(dirname), dirname);
t.is(processNodePath([dirname, __dirname].join(path.delimiter)), [dirname, __dirname].join(path.delimiter));
t.is(processNodePath(__dirname), [__dirname, dirname].join(path.delimiter));
t.is(processNodePath('', dirname), dirname);
t.is(processNodePath(dirname, dirname), dirname);
t.is(processNodePath([dirname, __dirname].join(path.delimiter), dirname), [dirname, __dirname].join(path.delimiter));
t.is(processNodePath(__dirname, dirname), [__dirname, dirname].join(path.delimiter));
});
8 changes: 5 additions & 3 deletions test/modern-require.js
Expand Up @@ -9,11 +9,13 @@ const noSpaceFile = path.resolve('/dir/file.js');
const spaceFile = path.resolve('/space dir/file.js');

test('exports', async t => {
t.is(typeof modernRequire, 'object');
t.type(modernRequire, 'object');
t.deepEqual(Object.keys(modernRequire).sort(), ['generateRequire', 'needsPathEnv', 'processNodePath']);
t.is(typeof generateRequire, 'function');
t.type(generateRequire, 'function');
t.is(processNodePath, undefined);
t.is(needsPathEnv, false);
t.type(needsPathEnv, 'function');
t.is(needsPathEnv(noSpaceFile), false);
t.is(needsPathEnv(spaceFile), false);
});

test('generateRequire', async t => {
Expand Down

0 comments on commit 15d949b

Please sign in to comment.