@jsenv/import-map
Advanced tools
Comparing version 5.0.0 to 5.2.0
@@ -63,3 +63,3 @@ 'use strict'; | ||
if (!isAbsoluteSpecifier(value)) { | ||
throw new Error("".concat(name, " must be an url and no scheme found, got ").concat(value)); | ||
throw new Error("".concat(name, " must be a url and no scheme found, got ").concat(value)); | ||
} | ||
@@ -494,2 +494,7 @@ }; | ||
assertImportMap(importMap); | ||
if (typeof folderRelativeName !== "string") { | ||
throw new TypeError("folderRelativeName must be a string, got ".concat(folderRelativeName)); | ||
} | ||
var into = "/".concat(folderRelativeName, "/"); | ||
@@ -496,0 +501,0 @@ var imports = importMap.imports, |
{ | ||
"name": "@jsenv/import-map", | ||
"version": "5.0.0", | ||
"version": "5.2.0", | ||
"license": "MIT", | ||
@@ -10,3 +10,4 @@ "repository": { | ||
"publishConfig": { | ||
"access": "public" | ||
"access": "public", | ||
"registry": "https://registry.npmjs.org" | ||
}, | ||
@@ -43,3 +44,3 @@ "main": "dist/commonjs/main.js", | ||
"@jsenv/bundling": "5.15.0", | ||
"@jsenv/codecov-upload": "1.7.0", | ||
"@jsenv/codecov-upload": "1.8.0", | ||
"@jsenv/eslint-config": "10.0.0", | ||
@@ -46,0 +47,0 @@ "@jsenv/execution": "5.11.0", |
152
readme.md
# jsenv-import-map | ||
> importMap programmatic implementation | ||
[![npm package](https://img.shields.io/npm/v/@jsenv/import-map.svg)](https://www.npmjs.com/package/@jsenv/import-map) | ||
[![build](https://travis-ci.com/jsenv/jsenv-import-map.svg?branch=master)](http://travis-ci.com/jsenv/jsenv-import-map) | ||
[![ci status](https://github.com/jsenv/jsenv-import-map/workflows/ci/badge.svg)](https://github.com/jsenv/jsenv-import-map/actions) | ||
[![codecov](https://codecov.io/gh/jsenv/jsenv-import-map/branch/master/graph/badge.svg)](https://codecov.io/gh/jsenv/jsenv-import-map) | ||
> importMap programmatic implementation | ||
## Introduction | ||
`jsenv-import-map` implements importMap in native js. It is used by jsenv to use importMap even if they are not yet supported.<br /> | ||
`jsenv-import-map` is a javasScript implementation of the importMap specification. It is written using es modules and is compatible with Node.js.<br /> | ||
I made this project because jsenv uses importMap but they are not yet available in browsers.<br /> | ||
— see [importMap spec](https://github.com/WICG/import-maps) | ||
It has the following exports | ||
`@jsenv/import-map` has the following exports: | ||
- `applyImportMap` | ||
- `composeTwoImportMaps` | ||
- `normalizeImportMap` | ||
- `resolveImport` | ||
- `resolveSpecifier` | ||
- `wrapImportMap` | ||
- [applyImportMap](#applyimportmap) | ||
- [composeTwoImportMaps](#composetwoimportmaps) | ||
- [normalizeImportMap](#normalizeimportmap) | ||
- [resolveImport](#resolveimport) | ||
- [resolveSpecifier](#resolvespecifier) | ||
- [wrapImportMap](#wrapimportmap) | ||
## applyImportMap | ||
> takes { `importMap`, `href`, `importerHref` } and returns either the `href` remapped by `importMap` or the original `href`. | ||
```js | ||
import { applyImportMap } from "@jsenv/import-map" | ||
const href = "http://domain.com/foo" | ||
const importMap = { | ||
imports: { | ||
"http://domain.com/foo": "http://domain.com/bar", | ||
}, | ||
} | ||
const hrefRemapped = applyImportMap({ | ||
href, | ||
importMap, | ||
}) | ||
console.log(hrefRemapped) | ||
``` | ||
The code above logs `"http://domain.com/bar"`.<br /> | ||
The provided `importMap` specifiers must be absolute and sorted to work as expected.<br /> | ||
You can use [normalizeImportMap](#normalizeimportmap) to do that.<br /> | ||
— see [applyImportMap source code](./src/applyImportMap/applyImportMap.js) | ||
## composeTwoImportMaps | ||
> takes (`leftImportMap`, `rightImportMap`) and returns an importMap being the composition of the two. | ||
```js | ||
import { composeTwoImportMaps } from "@jsenv/import-map" | ||
const leftImportMap = { | ||
imports: { | ||
foo: "bar", | ||
}, | ||
} | ||
const rightImportMap = { | ||
imports: { | ||
foo: "whatever", | ||
}, | ||
} | ||
const importMap = composeTwoImportMaps(leftImportMap, rightImportMap) | ||
console.log(importMap.imports.foo) | ||
``` | ||
The code above logs `"whatever"`. | ||
— see [composeTwoImportMaps source code](./src/composeTwoImportMaps/composeTwoImportMaps.js) | ||
## normalizeImportMap | ||
> takes (`importMap`, `href`) and returns an importMap where relative specifier are resolved against `href` and sorted. | ||
```js | ||
import { normalizeImportMap } from "@jsenv/import-map" | ||
const importMap = { | ||
imports: { | ||
foo: "http://cdndomain.com/bar", | ||
}, | ||
} | ||
const href = "http://mydomain.com" | ||
const importMapNormalized = normalizeImportMap(importMap, href) | ||
console.log(importMapNormalized.imports["http://mydomain.com/foo"]) | ||
``` | ||
The code above logs `"http://cdndomain.com/bar"`. | ||
— see [normalizeImportMap source code](./src/normalizeImportMap/normalizeImportMap.js) | ||
## resolveImport | ||
> takes { specifier, importer, importMap, defaultExtension } and returns an url. | ||
> takes { `specifier`, `importer`, `importMap`, `defaultExtension` } and returns a url. | ||
```js | ||
import { resolvePath } from "@jsenv/module-resolution" | ||
import { resolveImport } from "@jsenv/import-map" | ||
@@ -43,12 +120,55 @@ const importUrl = resolveImport({ | ||
The code above logs `http://domain.com/main.js`. | ||
The code above logs `"http://domain.com/main.js"`. | ||
— see [resolveImport source code](./src/resolveImport/resolveImport.js) | ||
## resolveSpecifier | ||
> takes (`specifier`, `importer`) and returns `specifier` resolved against `importer`. | ||
```js | ||
import { resolveSpecifier } from "@jsenv/import-map" | ||
const specifier = "../file.js" | ||
const importer = "http://mydomain.com/folder/index.js" | ||
const specifierResolved = resolveSpecifier(specifier, importer) | ||
console.log(specifierResolved) | ||
``` | ||
The code above logs `"http://mydomain.com/file.js"`. | ||
— see [resolveSpecifier source code](./src/resolveSpecifier/resolveSpecifier.js) | ||
## wrapImportMap | ||
> takes (`importMap`, `folderRelativeName`) and returns an importMap wrapped inside `folderRelativeName`. | ||
```js | ||
import { wrapImportMap } from "@jsenv/import-map" | ||
const importMap = { | ||
imports: { | ||
foo: "bar", | ||
}, | ||
} | ||
const folderRelativeName = "/dist" | ||
const importMapWrapped = wrapImportMap(specifier, importer) | ||
console.log(importMapWrapped.imports.foo) | ||
``` | ||
The code above logs `"/dist/bar"`.<br /> | ||
This feature is not part of the spec but is usefull to redirect your imports inside a given folder.<br /> | ||
— see [wrapImportMap source code](./src/wrapImportMap/wrapImportMap.js) | ||
## Installation | ||
```console | ||
npm install @jsenv/import-map@1.0.0 | ||
npm install @jsenv/import-map@5.3.0 | ||
``` | ||
```console | ||
yarn add @jsenv/import-map@1.0.0 | ||
yarn add @jsenv/import-map@5.3.0 | ||
``` |
@@ -8,4 +8,4 @@ import { isAbsoluteSpecifier } from "./resolveSpecifier/absoluteSpecifier.js" | ||
if (!isAbsoluteSpecifier(value)) { | ||
throw new Error(`${name} must be an url and no scheme found, got ${value}`) | ||
throw new Error(`${name} must be a url and no scheme found, got ${value}`) | ||
} | ||
} |
@@ -24,2 +24,5 @@ /** | ||
assertImportMap(importMap) | ||
if (typeof folderRelativeName !== "string") { | ||
throw new TypeError(`folderRelativeName must be a string, got ${folderRelativeName}`) | ||
} | ||
@@ -26,0 +29,0 @@ const into = `/${folderRelativeName}/` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
89878
983
174
0