Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for resolving stringy exports.import field #18

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "resolvewithplus",
"version": "0.8.4",
"version": "0.8.5",
"license": "ISC",
"main": "resolvewithplus.mjs",
"readmeFilename": "README.md",
Expand Down
47 changes: 27 additions & 20 deletions resolvewithplus.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -82,27 +82,34 @@ export default (o => {
}
}

if (typeof esmexportsobj === 'string') {
indexval = esmexportsobj;
} else if (esmexportsobj && esmexportsobj['.']) {
if (typeof esmexportsobj['.'].import === 'string') {
indexval = esmexportsobj['.'].import;
}
if (esmexportsobj) {
if (typeof esmexportsobj === 'string') {
indexval = esmexportsobj;
} else if (typeof esmexportsobj.import === 'string') {
// "exports": {
// "import": "./index.mjs"
// }
indexval = esmexportsobj.import;
} else if (esmexportsobj['.']) {
if (typeof esmexportsobj['.'].import === 'string') {
indexval = esmexportsobj['.'].import;
}

// this export pattern used by "yargs"
//
// "exports": {
// ".": [{
// "import": "./index.mjs",
// "require": "./index.cjs"
// }, "./index.cjs" ]
// }
if (Array.isArray(esmexportsobj['.'])) {
indexval = esmexportsobj['.'].reduce((prev, elem) => {
return (typeof elem === 'object' && elem.import)
? elem.import
: prev;
}, null);
// this export pattern used by "yargs"
//
// "exports": {
// ".": [{
// "import": "./index.mjs",
// "require": "./index.cjs"
// }, "./index.cjs" ]
// }
if (Array.isArray(esmexportsobj['.'])) {
indexval = esmexportsobj['.'].reduce((prev, elem) => {
return (typeof elem === 'object' && elem.import)
? elem.import
: prev;
}, null);
}
}
}

Expand Down
68 changes: 66 additions & 2 deletions resolvewithplus.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Timestamp: 2017.04.23-23:31:33 (last modified)
// Author(s): bumblehead <[email protected]>

import url from 'url';
import test from 'ava';
import path from 'path';
import resolvewithplus from './resolvewithplus.mjs';
Expand Down Expand Up @@ -60,15 +61,20 @@ test('should return null when given id to withpath inaccessible module', t => {
});

test('should follow the behaviour of require.resolve', t => {
const dirname = path.dirname(url.fileURLToPath(import.meta.url));
// needed in case, resolvewith is cloned to a different directory name
const resolvewithrootdirname = path.basename(dirname);
const resolvewithresolved = path.resolve(`../${resolvewithrootdirname}/`);

t.is(
path.resolve('./resolvewithplus.mjs'),
resolvewithplus('../resolvewithplus', path.resolve('../resolvewithplus/')));
resolvewithplus(`../${resolvewithrootdirname}`, resolvewithresolved));

t.is(
path.resolve('./testfiles/testscript.js'),
resolvewithplus(
'./testfiles/testscript.js',
path.resolve('../resolvewithplus/')));
path.resolve(resolvewithresolved)));

t.is(
'path',
Expand Down Expand Up @@ -154,3 +160,61 @@ test('getasnode_module_paths, should return list of paths (posix)', t => {
t.deepEqual(
resolvewithplus.getasnode_module_paths(fullpath), paths);
});

test('should handle exports.import path definition', t => {
t.is(resolvewithplus.getbrowserindex({
name : 'test',
exports : {
types : './index.d.ts',
require : './index.js',
import : './index.mjs'
}
}), './index.mjs');
});

test('should handle exports["."].import path definition', t => {
// used by '[email protected]'
t.is(resolvewithplus.getbrowserindex({
name : 'test',
exports : {
'.' : {
require : './index.js',
import : './index.mjs'
}
}
}), './index.mjs');
});

test('should handle exports stringy path definition', t => {
// used by 'got'
t.is(resolvewithplus.getbrowserindex({
name : 'test',
exports : './index.mjs'
}), './index.mjs');
});

test('should handle mixed exports', t => {
// used by '[email protected]'
t.is(resolvewithplus.getbrowserindex({
name : 'test',
exports : {
'./package.json' : './package.json',
'.' : [ {
import : './index.mjs',
require : './index.cjs'
}, './index.cjs' ],
'./helpers' : {
import : './helpers/helpers.mjs',
require : './helpers/index.js'
},
'./browser' : {
import : './browser.mjs',
types : './browser.d.ts'
},
'./yargs' : [ {
import : './yargs.mjs',
require : './yargs'
}, './yargs' ]
}
}), './index.mjs');
});