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

Package main sugar #44

Merged
merged 2 commits into from
Jun 28, 2018
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
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