(specifier: string, documentPath: string, componentInfo?: {
packageName: string,
rootDir: string,
componentDir: string
}): FileRelativeUrl => {
if (whatwgUrl.parseURL(specifier) !== null) {
return specifier as FileRelativeUrl;
}
const importerFilepath = documentPath;
const dependencyFilepath = nodeResolve.sync(specifier, {
basedir: dirname(importerFilepath),
// It's invalid to load a .json or .node file as a module on the web,
// but this is what Node's resolution algorithm does
// (https://nodejs.org/api/modules.html#modules_all_together), so we
// also do it here for completeness. Without including these
// extensions the user will probably get a 404. With them, they'll
// probably get an invalid MIME type error (which is hopefully more
// useful).
extensions: ['.js', '.json', '.node'],
// Some packages use a non-standard alternative to the "main" field
// in their package.json to differentiate their ES module version.
packageFilter:
(packageJson:
{main?: string, module?: string, 'jsnext:main'?: string}) => {
packageJson.main = packageJson.module ||
packageJson['jsnext:main'] || packageJson.main;
return packageJson;
},
});
let relativeSpecifierUrl =
relative(dirname(importerFilepath), dependencyFilepath) as
FileRelativeUrl;
if (componentInfo !== undefined) {
// 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.
//
// Note that Polyserve will only set componentInfo if the particular
// request was for a URL path in the components/ directory.
const {packageName, rootDir, componentDir} = componentInfo;
const importerInRootPackage =
!pathIsInside(importerFilepath, componentDir);
const dependencyInRootPackage =
!pathIsInside(dependencyFilepath, componentDir);
if (importerInRootPackage && !dependencyInRootPackage) {
// A module from the root package, served from a components/ URL, is
// importing a module from a different package. In this case we need
// to fix up our relative path specifier, because on disk the
// dependency resolves to e.g. "./node_modules/foo", but in URL space
// it must resolve to "../foo".
//
// Note that the case where both the importer and the dependency are
// in the root package does not need to be fixed up, since the
// relative path works out the same.
const rootRelativeImporterPath = relative(rootDir, importerFilepath);
const effectiveImporterFilepath =
join(componentDir, packageName, rootRelativeImporterPath);
relativeSpecifierUrl = relative(
dirname(effectiveImporterFilepath),
dependencyFilepath) as FileRelativeUrl;
}
}
if (isWindows()) {
// normalize path separators to URL format
relativeSpecifierUrl =
relativeSpecifierUrl.replace(/\\/g, '/') as FileRelativeUrl;
}
if (!isPathSpecifier(relativeSpecifierUrl)) {
relativeSpecifierUrl = './' + relativeSpecifierUrl as FileRelativeUrl;
}
return relativeSpecifierUrl;
};
请发表评论