@import-maps/resolve
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -6,2 +6,13 @@ # Change Log | ||
## [0.1.1](https://github.com/open-wc/open-wc/compare/@import-maps/resolve@0.1.0...@import-maps/resolve@0.1.1) (2019-06-29) | ||
### Bug Fixes | ||
* **resolve:** support relative node paths ([#521](https://github.com/open-wc/open-wc/issues/521)) ([e4c51b7](https://github.com/open-wc/open-wc/commit/e4c51b7)) | ||
# 0.1.0 (2019-06-23) | ||
@@ -8,0 +19,0 @@ |
@@ -18,5 +18,13 @@ "use strict"; | ||
}); | ||
Object.defineProperty(exports, "mergeImportMaps", { | ||
enumerable: true, | ||
get: function () { | ||
return _utils.mergeImportMaps; | ||
} | ||
}); | ||
var _parser = require("./parser.js"); | ||
var _resolver = require("./resolver.js"); | ||
var _resolver = require("./resolver.js"); | ||
var _utils = require("./utils.js"); |
@@ -10,4 +10,8 @@ "use strict"; | ||
var _path = _interopRequireDefault(require("path")); | ||
var _utils = require("./utils.js"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/* eslint-disable no-restricted-syntax */ | ||
@@ -81,7 +85,15 @@ const supportedBuiltInModules = new Set([`${_utils.BUILT_IN_MODULE_SCHEME}:blank`]); | ||
const normalizedSpecifier = asURL ? asURL.href : specifier; | ||
const scriptURLString = scriptURL; | ||
let nodeSpecifier = null; | ||
if (scriptURL.includes('::')) { | ||
const [rootPath, basePath] = scriptURL.split('::'); | ||
const dirPath = specifier.startsWith('/') ? '' : _path.default.dirname(basePath); | ||
nodeSpecifier = _path.default.normalize(_path.default.join(rootPath, dirPath, specifier)); | ||
} | ||
const scriptURLString = scriptURL.split('::').join(''); | ||
for (const [scopePrefix, scopeImports] of Object.entries(parsedImportMap.scopes)) { | ||
if (scopePrefix === scriptURLString || scopePrefix.endsWith('/') && scriptURLString.startsWith(scopePrefix)) { | ||
const scopeImportsMatch = resolveImportsMatch(normalizedSpecifier, scopeImports); | ||
const scopeImportsMatch = resolveImportsMatch(nodeSpecifier || normalizedSpecifier, scopeImports); | ||
@@ -109,3 +121,7 @@ if (scopeImportsMatch) { | ||
if (nodeSpecifier && (specifier.startsWith('/') || specifier.startsWith('.'))) { | ||
return nodeSpecifier; | ||
} | ||
throw new TypeError(`Unmapped bare specifier "${specifier}"`); | ||
} |
@@ -10,2 +10,3 @@ "use strict"; | ||
exports.tryURLLikeSpecifierParse = tryURLLikeSpecifierParse; | ||
exports.mergeImportMaps = mergeImportMaps; | ||
exports.BUILT_IN_MODULE_PROTOCOL = exports.BUILT_IN_MODULE_SCHEME = void 0; | ||
@@ -61,2 +62,17 @@ | ||
return null; | ||
} | ||
function mergeImportMaps(mapA, mapB) { | ||
const mapAImports = mapA && mapA.imports ? mapA.imports : {}; | ||
const mapBImports = mapB && mapB.imports ? mapB.imports : {}; | ||
const mapAScopes = mapA && mapA.scopes ? mapA.scopes : {}; | ||
const mapBScopes = mapB && mapB.scopes ? mapB.scopes : {}; | ||
return { | ||
imports: { ...mapAImports, | ||
...mapBImports | ||
}, | ||
scopes: { ...mapAScopes, | ||
...mapBScopes | ||
} | ||
}; | ||
} |
{ | ||
"name": "@import-maps/resolve", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Read and resolve urls via an import map", | ||
@@ -42,3 +42,3 @@ "author": "open-wc", | ||
}, | ||
"gitHead": "14ef29f5b19683ffd4c5f9e484c8aba7131c6b10" | ||
"gitHead": "ba985399f63c46250b3e884e74e59f2546b42f52" | ||
} |
@@ -26,12 +26,18 @@ # Resolve import-maps | ||
// a simple example | ||
const mapCache = null; | ||
const importMapCache = null; | ||
function myResolve(specifier) { | ||
const currentDir = process.cwd(); | ||
if (!mapCache) { | ||
const mapString = fs.readFileSync(path.join(currentDir, 'import-map.json'), 'utf-8'); | ||
mapCache = parseFromString(mapString, currentDir); | ||
const rootDir = process.cwd(); | ||
const basePath = importer ? importer.replace(rootDir, `${rootDir}::`) : `${rootDir}::`; | ||
if (!importMapCache) { | ||
const mapString = fs.readFileSync(path.join(rootDir, 'import-map.json'), 'utf-8'); | ||
mapCache = parseFromString(mapString, basePath); | ||
} | ||
return specifier => resolve(specifier, mapCache, path.join(`${currentDir}::`, 'index.html'); | ||
const relativeSource = source.replace(rootDir, ''); | ||
const resolvedPath = resolve(relativeSource, importMapCache, basePath); | ||
if (resolvedPath) { | ||
return resolvedPath; | ||
} | ||
} | ||
@@ -38,0 +44,0 @@ ``` |
20764
356
73