Skip to content

Commit a539628

Browse files
author
Damien Maillard
committed
12.0.0
This major release is mostly about keeping import map for node module and writing importmap file separated. - replace generateImportMapForProjectPackage by getImportMapFromNodeModules - getImportMapFromNodeModules only return importmap, it does not write to filesystem anymore. - add getImportMapFromFile - add generateImportMapForProject - generate importmap for package self ref by default see nodejs/node#31002 - rename some parameter like favoredExports becoming packagesExportsPreference - ensure a package self reference is stronger than self dev dependency
1 parent 456f5f2 commit a539628

File tree

34 files changed

+113
-117
lines changed

34 files changed

+113
-117
lines changed

docs/advanced.md

-77
This file was deleted.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@jsenv/node-module-import-map",
3-
"version": "12.0.0-alpha.0",
3+
"version": "12.0.0",
44
"description": "Generate importmap for node_modules.",
55
"license": "MIT",
66
"repository": {

readme.md

+76-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ Generate importmap for node_modules.
1919
- [Step 1 - Setup basic project](#step-1---setup-project)
2020
- [Step 2 - Generate project importMap](#step-2---generate-project-importMap)
2121
- [Custom node module resolution](#custom-node-module-resolution)
22-
- [Advanced usage](#Advanced-usage)
22+
- [generateImportMapForProject](#generateImportMapForProject)
23+
- [importMapFile](#importMapFile)
24+
- [importMapFileRelativeUrl](#importMapFileRelativeUrl)
25+
- [importMapFileLog](#importMapFileLog)
26+
- [getImportMapFromFile](#getImportMapFromFile)
27+
- [importMapFileUrl](#importMapFileUrl)
2328

2429
# Presentation
2530

@@ -135,8 +140,75 @@ We do this because importMap are used on the web where a file outside project fo
135140
136141
In practice it does not impact you because node modules are inside your project folder. If not, write all your dependencies in your `package.json` and re-run `npm install`.
137142
138-
# Advanced usage
143+
# generateImportMapForProject
139144
140-
This repository also provides the ability to compose several import map into a final importmap file.
145+
`generateImportMapForProject` is an async function receiving an array of promise resolving to importmaps. It awaits for every importmap, compose them into one and write it into a file.
141146
142-
[docs/advanced.md](./docs/advanced.md)
147+
> This function is meant to be responsible of generating the final importMap file that a project uses.
148+
149+
For example code below will generate an import map from node_modules + a file + an inline importmap.
150+
151+
```js
152+
import {
153+
getImportMapFromNodeModules,
154+
getImportMapFromFile,
155+
generateImportMapForProject,
156+
} from "@jsenv/node-module-import-map"
157+
158+
const projectDirectoryUrl = new URL("./", import.meta.url)
159+
const customImportMapFileUrl = new URL("./import-map-custom.importmap", projectDirectoryUrl)
160+
const importMapInputs = [
161+
getImportMapFromNodeModules({
162+
projectDirectoryUrl,
163+
projectPackageDevDependenciesIncluded: true,
164+
}),
165+
getImportMapFromFile(customImportMapFileUrl),
166+
{
167+
imports: {
168+
foo: "./bar.js",
169+
},
170+
},
171+
]
172+
173+
await generateImportMapForProject(importMapInputs, {
174+
projectDirectoryUrl,
175+
importMapFileRelativeUrl: "./import-map.importmap",
176+
})
177+
```
178+
179+
— source code at [src/generateImportMapForProject.js](./src/generateImportMapForProject.js).
180+
181+
## importMapInputs
182+
183+
`importMapInputs` is an array of importmap object or promise resolving to importmap objects. This parameter is optional and is an empty array by default.
184+
185+
> When `importMapInputs` is empty a warning is emitted and `generateImportMapForProject` write an empty importmap file.
186+
187+
## importMapFile
188+
189+
`importMapFile` parameter is a boolean controling if importMap is written to a file. This parameters is optional and enabled by default.
190+
191+
## importMapFileRelativeUrl
192+
193+
`importMapFileRelativeUrl` parameter is a string controlling where importMap file is written. This parameter is optional and by default it's `"./import-map.importmap"`.
194+
195+
## importMapFileLog
196+
197+
`importMapFileLog` parameter a boolean controlling if there is log in the terminal when importMap file is written. This parameter is optional and by default it's enabled.
198+
199+
# getImportMapFromFile
200+
201+
`getImportMapFromFile` is an async function reading importmap from a file.
202+
203+
```js
204+
import { getImportMapFromFile } from "@jsenv/node-module-import-map"
205+
206+
const importMapFileUrl = new URL("./import-map.importmap", import.meta.url)
207+
const importMap = await getImportMapFromFile(importMapFileUrl)
208+
```
209+
210+
— source code at [src/getImportMapFromFile.js](../src/getImportMapFromFile.js).
211+
212+
## importMapFileUrl
213+
214+
`importMapFileUrl` parameter a string or an url leading to the importmap file. This parameter is **required**.

src/generateImportMapForProject.js

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ export const generateImportMapForProject = async (
2626
async () => {
2727
projectDirectoryUrl = assertAndNormalizeDirectoryUrl(projectDirectoryUrl)
2828

29+
if (importMapInputs.length === 0) {
30+
console.warn(`importMapInputs is empty, the generated importmap will be empty`)
31+
}
32+
2933
const importMaps = await Promise.all(importMapInputs)
3034

3135
const importMap = importMaps.reduce((previous, current) => {

src/getImportMapFromNodeModules.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const getImportMapFromNodeModules = async ({
2929
// pass ["import", "browser", "require"] to read browser first if defined
3030
packagesExportsPreference = ["import", "node", "require"],
3131
packagesExportsIncluded = true,
32-
packagesSelfImport = true,
32+
packagesSelfReference = true,
3333
packagesImportsIncluded = true,
3434
packagesManualOverrides = {},
3535
}) =>
@@ -152,7 +152,7 @@ export const getImportMapFromNodeModules = async ({
152152
})
153153
}
154154

155-
if (packagesSelfImport) {
155+
if (packagesSelfReference) {
156156
const { packageIsRoot, packageDirectoryRelativeUrl } = packageInfo
157157

158158
// allow import 'package-name/dir/file.js' in package-name files
@@ -191,7 +191,7 @@ export const getImportMapFromNodeModules = async ({
191191
// packageDirectoryUrlExpected,
192192
} = packageInfo
193193

194-
if (packageIsRoot && packagesSelfImport) {
194+
if (packageIsRoot && packagesSelfReference) {
195195
Object.keys(importsForPackageExports).forEach((from) => {
196196
const to = importsForPackageExports[from]
197197
addImportMapping({
@@ -258,7 +258,7 @@ export const getImportMapFromNodeModules = async ({
258258
},
259259
}) => {
260260
const self = packageIsRoot || packageIsProject
261-
if (self && !packagesSelfImport) return
261+
if (self && !packagesSelfReference) return
262262

263263
const mainFileUrl = await resolvePackageMain({
264264
packageFileUrl,

test/getImportMapFromNodeModules/circular/circular.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const testDirectoryUrl = resolveUrl("./", import.meta.url)
66

77
const actual = await getImportMapFromNodeModules({
88
projectDirectoryUrl: testDirectoryUrl,
9-
packagesSelfImport: false,
9+
packagesSelfReference: false,
1010
})
1111
const expected = {
1212
imports: {

test/getImportMapFromNodeModules/exports-and-main/exports-and-main.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const testDirectoryUrl = resolveUrl("./", import.meta.url)
66

77
const actual = await getImportMapFromNodeModules({
88
projectDirectoryUrl: testDirectoryUrl,
9-
packagesSelfImport: false,
9+
packagesSelfReference: false,
1010
})
1111
const expected = {
1212
imports: {

test/getImportMapFromNodeModules/exports-complex/exports-complex.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const testDirectoryUrl = resolveUrl("./", import.meta.url)
66

77
const actual = await getImportMapFromNodeModules({
88
projectDirectoryUrl: testDirectoryUrl,
9-
packagesSelfImport: false,
9+
packagesSelfReference: false,
1010
})
1111
const expected = {
1212
imports: {

test/getImportMapFromNodeModules/exports-directory-scoped/exports-directory-scoped.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const testDirectoryUrl = resolveUrl("./", import.meta.url)
66

77
const actual = await getImportMapFromNodeModules({
88
projectDirectoryUrl: testDirectoryUrl,
9-
packagesSelfImport: false,
9+
packagesSelfReference: false,
1010
})
1111
const expected = {
1212
imports: {

test/getImportMapFromNodeModules/exports-directory/exports-directory.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const testDirectoryUrl = resolveUrl("./", import.meta.url)
66

77
const actual = await getImportMapFromNodeModules({
88
projectDirectoryUrl: testDirectoryUrl,
9-
packagesSelfImport: false,
9+
packagesSelfReference: false,
1010
})
1111
const expected = {
1212
imports: {

test/getImportMapFromNodeModules/exports-file-conditional/exports-file-conditional.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const testDirectoryUrl = resolveUrl("./", import.meta.url)
88
const importMap = await getImportMapFromNodeModules({
99
projectDirectoryUrl: testDirectoryUrl,
1010
packagesExportsPreference: ["browser"],
11-
packagesSelfImport: false,
11+
packagesSelfReference: false,
1212
})
1313
const actual = importMap
1414
const expected = {
@@ -25,7 +25,7 @@ const testDirectoryUrl = resolveUrl("./", import.meta.url)
2525
const importMap = await getImportMapFromNodeModules({
2626
projectDirectoryUrl: testDirectoryUrl,
2727
packagesExportsPreference: [],
28-
packagesSelfImport: false,
28+
packagesSelfReference: false,
2929
})
3030
const actual = importMap
3131
const expected = {

test/getImportMapFromNodeModules/exports-file-deep-2/exports-file-deep-2.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const testDirectoryUrl = resolveUrl("./", import.meta.url)
66

77
const importMap = await getImportMapFromNodeModules({
88
projectDirectoryUrl: testDirectoryUrl,
9-
packagesSelfImport: false,
9+
packagesSelfReference: false,
1010
})
1111
const actual = importMap
1212
const expected = {

test/getImportMapFromNodeModules/exports-file-deep/exports-file-deep.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const testDirectoryUrl = resolveUrl("./", import.meta.url)
66

77
const importMap = await getImportMapFromNodeModules({
88
projectDirectoryUrl: testDirectoryUrl,
9-
packagesSelfImport: false,
9+
packagesSelfReference: false,
1010
})
1111
const actual = importMap
1212
const expected = {

test/getImportMapFromNodeModules/exports-file/exports-file.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const testDirectoryUrl = resolveUrl("./", import.meta.url)
66

77
const importMap = await getImportMapFromNodeModules({
88
projectDirectoryUrl: testDirectoryUrl,
9-
packagesSelfImport: false,
9+
packagesSelfReference: false,
1010
})
1111
const actual = importMap
1212
const expected = {

test/getImportMapFromNodeModules/exports-string/exports-string.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const testDirectoryUrl = resolveUrl("./", import.meta.url)
66

77
const importMap = await getImportMapFromNodeModules({
88
projectDirectoryUrl: testDirectoryUrl,
9-
packagesSelfImport: false,
9+
packagesSelfReference: false,
1010
})
1111
const actual = importMap
1212
const expected = {

test/getImportMapFromNodeModules/exports-sugar-conditional/exports-sugar-conditional.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const testDirectoryUrl = resolveUrl("./", import.meta.url)
88
const importMap = await getImportMapFromNodeModules({
99
projectDirectoryUrl: testDirectoryUrl,
1010
packagesExportsPreference: ["browser"],
11-
packagesSelfImport: false,
11+
packagesSelfReference: false,
1212
})
1313
const actual = importMap
1414
const expected = {
@@ -23,7 +23,7 @@ const testDirectoryUrl = resolveUrl("./", import.meta.url)
2323
{
2424
const importMap = await getImportMapFromNodeModules({
2525
projectDirectoryUrl: testDirectoryUrl,
26-
packagesSelfImport: false,
26+
packagesSelfReference: false,
2727
})
2828
const actual = importMap
2929
const expected = {

test/getImportMapFromNodeModules/exports-sugar/exports-sugar.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const testDirectoryUrl = resolveUrl("./", import.meta.url)
66

77
const importMap = await getImportMapFromNodeModules({
88
projectDirectoryUrl: testDirectoryUrl,
9-
packagesSelfImport: false,
9+
packagesSelfReference: false,
1010
})
1111
const actual = importMap
1212
const expected = {

test/getImportMapFromNodeModules/imports-leading-slash/imports-leading-slash.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const testDirectoryUrl = resolveUrl("./", import.meta.url)
66

77
const actual = await getImportMapFromNodeModules({
88
projectDirectoryUrl: testDirectoryUrl,
9-
packagesSelfImport: false,
9+
packagesSelfReference: false,
1010
})
1111
const expected = {
1212
imports: {

test/getImportMapFromNodeModules/inside/inside.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const testDirectoryUrl = resolveUrl("./", import.meta.url)
77

88
const importMap = await getImportMapFromNodeModules({
99
projectDirectoryUrl: testDirectoryUrl,
10-
packagesSelfImport: false,
10+
packagesSelfReference: false,
1111
})
1212
const actual = importMap
1313
const expected = {

test/getImportMapFromNodeModules/main-directory-trailing/main-directory-trailing.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const testDirectoryUrl = resolveUrl("./", import.meta.url)
66

77
const actual = await getImportMapFromNodeModules({
88
projectDirectoryUrl: testDirectoryUrl,
9-
packagesSelfImport: false,
9+
packagesSelfReference: false,
1010
})
1111
const expected = {
1212
imports: {

test/getImportMapFromNodeModules/main-directory/main-directory.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const testDirectoryUrl = resolveUrl("./", import.meta.url)
66

77
const actual = await getImportMapFromNodeModules({
88
projectDirectoryUrl: testDirectoryUrl,
9-
packagesSelfImport: false,
9+
packagesSelfReference: false,
1010
})
1111
const expected = {
1212
imports: {

test/getImportMapFromNodeModules/main-jsnext/main-jsnext.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const testDirectoryUrl = resolveUrl("./", import.meta.url)
66

77
const actual = await getImportMapFromNodeModules({
88
projectDirectoryUrl: testDirectoryUrl,
9-
packagesSelfImport: false,
9+
packagesSelfReference: false,
1010
})
1111
const expected = {
1212
imports: {

0 commit comments

Comments
 (0)