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

Commit

Permalink
fix: esm module resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Feb 7, 2020
1 parent 892bb3f commit ffe30fb
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions typescript-deno-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,23 +209,22 @@ class DenoPlugin implements ts_module.server.PluginModule {
}
}

const originModuleNames = moduleNames
.// resolve module from Import Maps
// eg. `import_map.json`
const originModuleNames = moduleNames // resolve module from Import Maps
.// eg. `import_map.json`
// {
// "imports": {
// "http/": "https://deno.land/std/http/"
// }
// }
// resolve `http/server.ts` -> `https://deno.land/std/http/server.ts`
map(name => resolveImportMap(importMaps, name))
.// cover `https://example.com/mod.ts` -> `$DENO_DIR/deps/https/example.com/mod.ts`
map(convertRemoteToLocalCache)
.// if module is ESM. Then the module name may contain url query and url hash
// We need to remove it
map(trimQueryAndHashFromPath)
.// for ESM support
// Some modules do not specify the domain name, but the root directory of the domain name
map(name => resolveImportMap(
importMaps,
name
)) // cover `https://example.com/mod.ts` -> `$DENO_DIR/deps/https/example.com/mod.ts`
.map(convertRemoteToLocalCache) // if module is ESM. Then the module name may contain url query and url hash
.// We need to remove it
map(trimQueryAndHashFromPath) // for ESM support
.// Some modules do not specify the domain name, but the root directory of the domain name
// eg. `$DENO_DIR/deps/https/dev.jspm.io/react`
// import { dew } from "/npm:react@16.12.0/index.dew.js";
// export default dew();
Expand Down Expand Up @@ -414,9 +413,21 @@ function resolveFromDenoDir(
return (moduleName: string): string => {
if (isResolveInDenoModule && moduleName.indexOf("/") === 0) {
const paths = moduleName.split("/");

const denoDepsFilepath = path.join(getDenoDir(), "deps");

paths.shift(); // remove `/` prefix of url path

return path.join(path.dirname(currentFileAbsolutePath), ...paths);
const urlPaths = currentFileAbsolutePath
.replace(denoDepsFilepath, "")
.split(path.sep);

urlPaths.shift(); // remove prefix of filepath `path.sep`

const protocol = urlPaths[0];
const domainName = urlPaths[1];

return path.join(denoDepsFilepath, protocol, domainName, ...paths);
}
return moduleName;
};
Expand Down

0 comments on commit ffe30fb

Please sign in to comment.