diff --git a/node-preload-legacy-require.js b/node-preload-legacy-require.js index 94b158d..a1e5de6 100644 --- a/node-preload-legacy-require.js +++ b/node-preload-legacy-require.js @@ -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(' ') }; diff --git a/node-preload-modern-require.js b/node-preload-modern-require.js index 72835ab..37e176c 100644 --- a/node-preload-modern-require.js +++ b/node-preload-modern-require.js @@ -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 }; diff --git a/node-preload-singleton.js b/node-preload-singleton.js index d3b61bb..b52014b 100644 --- a/node-preload-singleton.js +++ b/node-preload-singleton.js @@ -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) { @@ -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)); diff --git a/test/legacy-require.js b/test/legacy-require.js index 35dcbef..f8b8b6c 100644 --- a/test/legacy-require.js +++ b/test/legacy-require.js @@ -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 => { @@ -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)); }); diff --git a/test/modern-require.js b/test/modern-require.js index 8ae4ee2..1c8f208 100644 --- a/test/modern-require.js +++ b/test/modern-require.js @@ -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 => {