-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #255 from iambumblehead/pnp-resolver
PnP resolver
- Loading branch information
Showing
7 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { isBuiltin } from 'node:module' | ||
import { pathToFileURL } from 'node:url' | ||
|
||
const pnpapi = process.versions.pnp && (await import('pnpapi')).default | ||
|
||
export default pnpapi && ((id, parent) => { | ||
if (isBuiltin(id)) { | ||
return id.startsWith('node:') ? id : `node:${id}` | ||
} | ||
|
||
if (id === 'import') { | ||
return null | ||
} | ||
|
||
const path = pnpapi.resolveRequest(id, parent) | ||
|
||
return path !== null ? pathToFileURL(path).href : null | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
delete process.versions.pnp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import module from 'node:module' | ||
|
||
async function resolve (specifier, context, next) { | ||
return next( | ||
specifier === 'pnpapi' | ||
? '../tests/local/pnp/api.js' | ||
: specifier, context) | ||
} | ||
|
||
module.register && (process.versions.pnp = '3') && module.register(` | ||
data:text/javascript, | ||
export ${encodeURIComponent(resolve)}`.slice(1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import test from 'node:test' | ||
import assert from 'node:assert/strict' | ||
import module from 'node:module' | ||
import { fileURLToPath } from 'node:url' | ||
import resolvewithplus from 'resolvewithplus' | ||
import '../local/pnp/enable.js' | ||
import esmock from 'esmock' | ||
import '../local/pnp/disable.js' | ||
import pnpapi from '../local/pnp/api.js' | ||
|
||
const resolver = (id, parent) => { | ||
const url = resolvewithplus(id, parent) | ||
return url !== null ? fileURLToPath(url) : null | ||
} | ||
|
||
test.beforeEach(() => { | ||
delete pnpapi.resolveRequest | ||
}) | ||
|
||
test('should work with pnp resolver', async ({ mock }) => { | ||
if (!module.register) | ||
return assert.ok('skip test') | ||
|
||
pnpapi.resolveRequest = mock.fn(resolver) | ||
|
||
const main = await esmock('../local/main.js', { | ||
'../local/mainUtil.js': { | ||
createString: () => 'test string' | ||
} | ||
}) | ||
|
||
assert.equal(typeof main, 'function') | ||
assert.strictEqual(main(), 'main string, test string') | ||
assert.equal(pnpapi.resolveRequest.mock.callCount(), 2) | ||
}) |