Skip to content

Commit 0888091

Browse files
authored
Merge pull request #52 from jsenv/many_entry_points
Many entry points
2 parents ef2ada2 + 62bc1c1 commit 0888091

61 files changed

Lines changed: 204 additions & 612 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"search.exclude": {
3+
"**/dist/": true
4+
}
5+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@jsenv/importmap-node-module",
3-
"version": "2.8.1",
3+
"version": "3.0.0",
44
"description": "Generate importmap for node_modules",
55
"license": "MIT",
66
"repository": {

readme.md

Lines changed: 85 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -77,29 +77,32 @@ await writeImportMapFiles({
7777
"./importmap_for_dev.importmap": {
7878
mappingsForNodeResolution: true,
7979
mappingsForDevDependencies: true,
80-
checkImportResolution: true,
8180
},
8281
"./importmap_for_prod.importmap": {
8382
mappingsForNodeResolution: true,
84-
checkImportResolution: true,
85-
removeUnusedMappings: true,
8683
},
8784
},
8885
})
8986
```
9087
9188
## projectDirectoryUrl
9289
93-
_projectDirectoryUrl_ parameter is a string url leading to a folder with a _package.json_.
94-
This parameters is **required** and accepted values are documented in [@jsenv/filesystem#assertAndNormalizeDirectoryUrl](https://github.com/jsenv/filesystem/blob/main/docs/API.md#assertandnormalizedirectoryurl)
90+
_projectDirectoryUrl_ is a string/url leading to a folder with a _package.json_.
91+
92+
_projectDirectoryUrl_ is **required**.
9593
9694
## importMapFiles
9795
98-
_importMapFiles_ parameter is an object where keys are importmap file relative urls and values are parameters controlling the mappings that will be written in the importmap file.
96+
_importMapFiles_ is an object where keys are file relative urls and value are objects configuring which mappings will be written in the importmap files.
97+
98+
_importMapFiles_ is **required**.
9999
100100
### mappingsForNodeResolution
101101
102-
When _mappingsForNodeResolution_ is enabled, the mappings required to implement node module resolution are generated.
102+
_mappingsForNodeResolution_ is a boolean. When enabled mappings required to implement node module resolution are generated.
103+
104+
_mappingsForNodeResolution_ is optional.
105+
103106
The following source of information are used to create complete and coherent mappings in the importmap.
104107
105108
- Your _package.json_
@@ -110,97 +113,130 @@ The following source of information are used to create complete and coherent map
110113
111114
### mappingsForDevDependencies
112115
113-
When enabled, `"devDependencies"` declared in your _package.json_ are included in the generated importMap.
116+
_mappingsForDevDependencies_ is a boolean. When enabled, `"devDependencies"` declared in your _package.json_ are included in the generated importMap.
114117
115-
### manualImportMap
118+
_mappingsForDevDependencies_ is optional.
116119
117-
_manualImportMapp_ parameter is an importMap object. Mappings declared in this parameter are added to mappings generated for node resolution. This can be used to provide additional mappings and/or override node mappings.
118-
This parameter is optional and by default it's an empty object.
120+
### runtime
121+
122+
_runtime_ is a string used to determine what to pick in [package.json conditions](https://nodejs.org/docs/latest-v16.x/api/packages.html#packages_conditions_definitions).
123+
124+
_runtime_ is optional and defaults to `"browser"`.
119125
120126
```js
121127
import { writeImportMapFiles } from "@jsenv/importmap-node-module"
122128

123129
await writeImportMapFiles({
124130
projectDirectoryUrl: new URL("./", import.meta.url),
125131
importMapFiles: {
126-
"./test.importmap": {
132+
"./browser.importmap": {
127133
mappingsForNodeResolution: true,
128-
manualImportMap: {
129-
imports: {
130-
"#env": "./env.js",
131-
},
132-
},
134+
runtime: "browser",
135+
},
136+
"./node.importmap": {
137+
mappingsForNodeResolution: true,
138+
runtime: "node",
133139
},
134140
},
135141
})
136142
```
137143
138-
### checkImportResolution
144+
### packageUserConditions
139145
140-
_checkImportResolution_ is a boolean parameter controlling if script tries to resolve all import found in your js files using the importmap.
146+
_packageUserConditions_ is an array controlling which conditions are favored in [package.json conditions](https://nodejs.org/dist/latest-v15.x/docs/api/packages.html#packages_conditions_definitions).
141147
142-
It is recommended to enable this parameter, it gives more confidence in the generated importmap and outputs nice warnings in case some import cannot be resolved.
148+
_packageUserConditions_ is optional.
143149
144-
The import resolution starts from your project entry point which is determined using your package.json "exports" or "main" field.
150+
```js
151+
import { writeImportMapFiles } from "@jsenv/importmap-node-module"
145152

146-
This import resolution check is auto enabled when [removeUnusedMappings](#removeUnusedMappings) or [extensionlessAutomapping](#extensionlessAutomapping) are used.
153+
await writeImportMapFiles({
154+
projectDirectoryUrl: new URL("./", import.meta.url),
155+
importMapFiles: {
156+
"./dev.importmap": {
157+
mappingsForNodeResolution: true,
158+
packageUserConditions: ["development"],
159+
},
160+
"./prod.importmap": {
161+
mappingsForNodeResolution: true,
162+
packageUserConditions: ["production"],
163+
},
164+
},
165+
})
166+
```
147167
148-
### removeUnusedMappings
168+
### manualImportMap
149169
150-
_removeUnusedMappings_ parameter is a boolean controlling if mappings will be treeshaked according to the import found in your files.
170+
_manualImportMap_ is an object containing mappings that will be added to the importmap. This can be used to provide additional mappings and/or override node mappings.
151171
152-
During development, you can start or stop using a mapping often so it's convenient to have all mappings.
172+
_manualImportMap_ is optional.
153173
154-
In production you likely want to keep only the mappings actually used by your js files. In that case enable removeUnusedMappings: it will drastically decrease the importmap file size.
174+
```js
175+
import { writeImportMapFiles } from "@jsenv/importmap-node-module"
155176

156-
### runtime
177+
await writeImportMapFiles({
178+
projectDirectoryUrl: new URL("./", import.meta.url),
179+
importMapFiles: {
180+
"./test.importmap": {
181+
mappingsForNodeResolution: true,
182+
manualImportMap: {
183+
imports: {
184+
"#env": "./env.js",
185+
},
186+
},
187+
},
188+
},
189+
})
190+
```
191+
192+
### entryPointsToCheck
193+
194+
_entryPointsToCheck_ is an array composed of string representing file relative urls. Each file is considered as an entry point using the import mappings. For each entry point, _writeImportMapFiles_ will check if import can be resolved and repeat this process for every static and dynamic import.
157195
158-
A string parameter indicating where the importmap will be used. The default runtime is `"browser"`.
159-
The runtime is used to determine what to pick in [package.json conditions](https://nodejs.org/docs/latest-v16.x/api/packages.html#packages_conditions_definitions).
196+
_entryPointsToCheck_ is optional.
160197
161198
```js
162199
import { writeImportMapFiles } from "@jsenv/importmap-node-module"
163200

164201
await writeImportMapFiles({
165202
projectDirectoryUrl: new URL("./", import.meta.url),
166203
importMapFiles: {
167-
"./browser.importmap": {
168-
mappingsForNodeResolution: true,
169-
runtime: "browser",
170-
},
171-
"./node.importmap": {
204+
"./project.importmap": {
172205
mappingsForNodeResolution: true,
173-
runtime: "node",
206+
entryPointsToCheck: ["./main.js"],
174207
},
175208
},
176209
})
177210
```
178211
179-
### packageUserConditions
212+
It is recommended to use _entryPointsToCheck_ as it gives confidence in the generated importmap. When an import cannot be resolved, a warning is logged.
180213
181-
Controls which conditions are favored in [package.json conditions](https://nodejs.org/dist/latest-v15.x/docs/api/packages.html#packages_conditions_definitions).
214+
### removeUnusedMappings
215+
216+
_removeUnusedMappings_ is a boolean. When enabled mappings will be treeshaked according to the import found in js files. It must be used with _entryPointsToCheck_.
182217
183218
```js
184219
import { writeImportMapFiles } from "@jsenv/importmap-node-module"
185220

186221
await writeImportMapFiles({
187222
projectDirectoryUrl: new URL("./", import.meta.url),
188223
importMapFiles: {
189-
"./dev.importmap": {
190-
mappingsForNodeResolution: true,
191-
packageUserConditions: ["development"],
192-
},
193-
"./prod.importmap": {
224+
"./test.importmap": {
194225
mappingsForNodeResolution: true,
195-
packageUserConditions: ["production"],
226+
entryPointsToCheck: ["./main.js"],
227+
removeUnusedMappings: true,
196228
},
197229
},
198230
})
199231
```
200232
233+
During development, you can start or stop using a mapping often so it's convenient to have all mappings.
234+
235+
In production you likely want to keep only the mappings actually used by your js files. In that case enable removeUnusedMappings: it will drastically decrease the importmap file size.
236+
201237
### extensionlessAutomapping
202238
203-
_extensionlessAutomapping_ parameter is a boolean controlling if mappings are generated for import(s) without extension found in your js files. Should be combined with _magicExtensions_ as shown in the code below.
239+
_extensionlessAutomapping_ is a boolean. When enabled mappings are generated for import(s) without extension found in your js files. It must be used with _entryPointsToCheck_ and _magicExtensions_.
204240
205241
```js
206242
import { writeImportMapFiles } from "@jsenv/importmap-node-module"
@@ -210,16 +246,19 @@ await writeImportMapFiles({
210246
importMapFiles: {
211247
"./test.importmap": {
212248
mappingsForNodeResolution: true,
249+
entryPointsToCheck: ["./main.js"],
250+
extensionlessAutomapping: true,
251+
magicExtensions: [".js"],
213252
},
214253
},
215254
})
216255
```
217256
218257
## packagesManualOverrides
219258
220-
Ideally package.json should use `"exports"` field documented in https://nodejs.org/dist/latest-v16.x/docs/api/packages.html#packages_package_entry_points. But not every one has updated to this new field yet.
259+
_packagesManualOverrides_ is an object that can be used to override some of your dependencies package.json.
221260
222-
_packagesManualOverrides_ parameter is an object that can be used to override some of your dependencies package.json.
261+
_packagesManualOverrides_ exists in case some of your dependencies use non standard fields to configure their entry points in their _package.json_. Ideally they should use `"exports"` field documented in https://nodejs.org/dist/latest-v16.x/docs/api/packages.html#packages_package_entry_points. But not every one has updated to this new field yet.
223262
224263
```js
225264
import { writeImportMapFiles } from "@jsenv/importmap-node-module"

src/internal/from-js/resolveProjectEntryPoint.js

Lines changed: 0 additions & 150 deletions
This file was deleted.

0 commit comments

Comments
 (0)