Skip to content

Commit

Permalink
Add package-as-string sugar
Browse files Browse the repository at this point in the history
This adds the package main sugar case from WICG#4, as well as more test cases in related areas.
  • Loading branch information
guybedford authored and domenic committed Jun 28, 2018
1 parent 14586d7 commit e404575
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 9 deletions.
25 changes: 16 additions & 9 deletions package-name-map/src/package-name-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
export interface Scope {
path_prefix?: string;
packages?: {[name: string]: Package};
packages?: {[name: string]: Package | string};
scopes?: {[path: string]: Scope};
}

Expand Down Expand Up @@ -68,21 +68,28 @@ function validateScope(scope: Scope) {
Object.values(scope.scopes).forEach(validateScope);
}
}
function validatePackage([pkgName, pkg]: [string, Package]) {
function validatePackage([pkgName, pkg]: [string, Package | string]) {
// package name validation
if (pkgName.startsWith('/') || pkgName.endsWith('/')) {
throw new Error(
`Invalid package name ${pkgName}, package names cannot start or end with a path separator.`
);
}
if (pkg.path !== undefined && typeof pkg.path !== 'string') {
throw new Error(
`Invalid package for ${pkgName}, path expected to be a string.`
);
if (typeof pkg === 'object') {
if (pkg.path !== undefined && typeof pkg.path !== 'string') {
throw new Error(
`Invalid package for ${pkgName}, path expected to be a string.`
);
}
if (pkg.main !== undefined && typeof pkg.main !== 'string') {
throw new Error(
`Invalid package for ${pkgName}, main expected to be a string.`
);
}
}
if (pkg.main !== undefined && typeof pkg.main !== 'string') {
else if (typeof pkg !== 'string') {
throw new Error(
`Invalid package for ${pkgName}, main expected to be a string.`
`Invalid package for ${pkgName}, must be a string or package object.`
);
}
}
Expand Down Expand Up @@ -259,7 +266,7 @@ const findPackage = (
if (scope.packages) {
for (const [pkgName, pkg] of Object.entries(scope.packages)) {
if (isPathSegmentPrefix(pkgName, specifier)) {
foundPackage = pkg;
foundPackage = typeof pkg === 'string' ? {main: pkg} : pkg;
foundPackageName = pkgName;
break;
}
Expand Down
54 changes: 54 additions & 0 deletions package-name-map/src/test/package-name-map_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,60 @@ suite('PackageNameMap', () => {
});
});

suite('package main variations', () => {
const map = new PackageNameMap(
{
path_prefix: '/node_modules',
packages: {
lodash: '/lodash.js',
'lodash-es': {},
moment: {
main: 'http://moment.com/moment.js'
},
'@polymer/polymer': 'index.js',
'@polymer/polymer-foo': {
main: '../polymer-foo.js'
}
}
},
baseURL
);

test('supports package string case for main', () => {
assert.equal(map.resolve('lodash', referrerURL), 'http://foo.com/lodash.js');
});

test('package string case for submodules errors', () => {
assert.equal(map.resolve('lodash/x', referrerURL), 'http://foo.com/node_modules/lodash/x');
});

test('package empty object case for main', () => {
try {
map.resolve('lodash-es', referrerURL);
assert.fail('Should error');
}
catch (e) {
assert.equal(e.message, 'Cannot resolve specifier lodash-es, no main found for package lodash-es');
}
});

test('package empty object case for submodules', () => {
assert.equal(map.resolve('lodash-es/x', referrerURL), 'http://foo.com/node_modules/lodash-es/x');
});

test('supports URL main', () => {
assert.equal(map.resolve('moment', referrerURL), 'http://moment.com/moment.js');
});

test('supports relative main sugar', () => {
assert.equal(map.resolve('@polymer/polymer', referrerURL), 'http://foo.com/node_modules/@polymer/polymer/index.js');
});

test('supports relative URL main', () => {
assert.equal(map.resolve('@polymer/polymer-foo', referrerURL), 'http://foo.com/node_modules/@polymer/polymer-foo.js');
});
});

suite('path_prefix', () => {
const map = new PackageNameMap(
{
Expand Down

0 comments on commit e404575

Please sign in to comment.