Skip to content
This repository has been archived by the owner on May 1, 2019. It is now read-only.

Commit

Permalink
Fix own-package component directory handling in node resolution. (#941)
Browse files Browse the repository at this point in the history
  • Loading branch information
aomarks authored and justinfagnani committed Apr 8, 2018
1 parent 42a13c0 commit 99fa55c
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 17 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -5,7 +5,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

<!-- ## Unreleased -->
## Unreleased
* Fix module resolution in the case of root packages served from a component
directory.
<!-- Add new, unreleased changes here. -->

## [3.0.0-pre.21] - 2018-04-05
Expand Down
29 changes: 13 additions & 16 deletions src/javascript/resolve-specifier-node.ts
Expand Up @@ -67,26 +67,24 @@ export const resolve =
},
});

let relativeSpecifierUrl =
relative(dirname(documentPath), resolvedSpecifier) as FileRelativeUrl;

// If this is a component-style URL layout, rewrite the path to reach out
// of the current package, rather than directly into the component
// directory.
let effectiveDocumentPath = documentPath;
if (componentInfo !== undefined) {
const {packageName, rootDir, componentDir} = componentInfo;
if (pathIsInside(resolvedSpecifier, componentDir)) {
const componentDirRelativeToPackage =
relative(join(rootDir, packageName), rootDir);
const relativePathFromComponentDir =
relative(componentDir, resolvedSpecifier);
relativeSpecifierUrl =
join(
componentDirRelativeToPackage,
relativePathFromComponentDir) as FileRelativeUrl;
const isRootPackageRequest = !pathIsInside(documentPath, componentDir);
if (isRootPackageRequest) {
// Special handling for servers like Polyserve which, when serving a
// package "foo", will map the URL "/components/foo" to the root
// package directory, so that "foo" can make correct relative path
// references to its dependencies.
const rootRelativePath = relative(rootDir, documentPath);
effectiveDocumentPath =
join(componentDir, packageName, rootRelativePath);
}
}

let relativeSpecifierUrl = relative(
dirname(effectiveDocumentPath), resolvedSpecifier) as FileRelativeUrl;

if (isWindows()) {
// normalize path separators to URL format
relativeSpecifierUrl =
Expand All @@ -98,5 +96,4 @@ export const resolve =
}

return relativeSpecifierUrl;

};
96 changes: 96 additions & 0 deletions src/test/javascript/resolve-specifier-node_test.ts
@@ -0,0 +1,96 @@
/**
* @license
* Copyright (c) 2018 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at
* http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at
* http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at
* http://polymer.github.io/PATENTS.txt
*/

import {assert} from 'chai';
import * as path from 'path';

import {resolve} from '../../javascript/resolve-specifier-node';

const rootDir = path.resolve(
__dirname,
'..',
'..',
'..',
'src',
'test',
'static',
'javascript',
'resolve-specifier-node');

const rootMain = path.join(rootDir, 'root.js');
const componentDir = path.join(rootDir, 'node_modules');
const shallowDepMain = path.join(componentDir, 'shallow', 'shallow.js');
const scopedDepMain = path.join(componentDir, '@scope', 'scoped', 'scoped.js');

const shallowRootComponentInfo = {
packageName: 'root',
rootDir,
componentDir
};

const scopedRootComponentInfo = {
packageName: '@scope/root',
rootDir,
componentDir
};

suite('resolve', async() => {

test('non-component root to shallow dep', async() => {
assert.equal(
resolve('shallow', rootMain), './node_modules/shallow/shallow.js');
});

test('non-component root to scoped dep', async() => {
assert.equal(
resolve('@scope/scoped', rootMain),
'./node_modules/@scope/scoped/scoped.js');
});

test('shallow dep to scoped dep', async() => {
assert.equal(
resolve('@scope/scoped', shallowDepMain, shallowRootComponentInfo),
'../@scope/scoped/scoped.js');
});

test('scoped dep to shallow dep', async() => {
assert.equal(
resolve('shallow', scopedDepMain, shallowRootComponentInfo),
'../../shallow/shallow.js');
});

test('component-root to shallow dep', async() => {
assert.equal(
resolve('shallow', rootMain, shallowRootComponentInfo),
'../shallow/shallow.js');
});

test('component-root to scoped dep', async() => {
assert.equal(
resolve('@scope/scoped', rootMain, shallowRootComponentInfo),
'../@scope/scoped/scoped.js');
});

test('scoped-component-root to shallow dep', async() => {
assert.equal(
resolve('shallow', rootMain, scopedRootComponentInfo),
'../../shallow/shallow.js');
});

test('scoped-component-root to scoped dep', async() => {
assert.equal(
resolve('@scope/scoped', rootMain, scopedRootComponentInfo),
'../scoped/scoped.js');
});
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.

0 comments on commit 99fa55c

Please sign in to comment.