Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ The CLI supports the following options:
- `--entrypoint <file>`: Confirm the specified file and its transitive dependencies can be resolved using the generated import map. Auto-enabled when importmap is written into an HTML file. Can be specified multiple times.
- `--dev`: Include devDependencies from `package.json`. Also favor `"development"` in [package exports](https://nodejs.org/docs/latest-v16.x/api/packages.html#packages_conditions_definitions)</a><sup>↗</sup>.
- `--keep-unused`: Keep all mappings, even if they are not currently used by entry file or its transitive dependencies.
- `--config <file>`: Read additional settings from the given JSON configuration file.

#### Configuration file

A configuration file in JSON format can be specified with the `--config` CLI option. The following example shows such a JSON file with all supported properties set to their default values:

```json
{
"dir": ".",
"dev": false,
"keepUnused": false,
"entryPoints": [],
"manualImportmap": {},
"packagesManualOverrides": {}
}
```

All properties are optional and CLI options like `--dev`, `--entrypoint` and `--keep-unused` will override the values read from the config. The objects `manualImportmap` and `packagesManualOverrides` have the same format as the corresponding objects in the API.

### API

Expand Down
21 changes: 15 additions & 6 deletions src/cli.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node

import { readFile } from "node:fs/promises";
import { pathToFileURL } from "node:url";
import { parseArgs } from "node:util";
import { writeImportmaps } from "./main.js";
Expand All @@ -21,10 +22,12 @@ const options = {
"keep-unused": {
type: "boolean",
},
"config": {
type: "string",
},
};
const { values, positionals } = parseArgs({ options, allowPositionals: true });
const outfile = positionals[0];
values.entrypoint ??= [];

if (values.help || positionals.length === 0) {
usage();
Expand All @@ -46,21 +49,26 @@ if (
process.exit(1);
}

const config = values.config ? JSON.parse(await readFile(values.config, "utf-8")) : {};
const dev = values.dev ?? config.dev;

const currentDirectoryUrl = pathToFileURL(`${process.cwd()}/`);
await writeImportmaps({
directoryUrl: new URL(values.dir || ".", currentDirectoryUrl),
directoryUrl: new URL(values.dir ?? config.dir ?? ".", currentDirectoryUrl),
importmaps: {
[outfile]: {
nodeMappings: {
devDependencies: values.dev,
packageUserConditions: values.dev ? ["development"] : [],
devDependencies: dev,
packageUserConditions: dev ? ["development"] : [],
},
importResolution: {
entryPoints: values.entryPoints,
entryPoints: values.entrypoint ?? config.entryPoints ?? [],
},
keepUnusedMappings: values["keep-unused"],
keepUnusedMappings: values["keep-unused"] ?? config.keepUnused,
manualImportmap: config.manualImportmap,
},
},
packagesManualOverrides: config.packagesManualOverrides,
});

function usage() {
Expand All @@ -76,6 +84,7 @@ Options:
--entrypoint file.js Confirm the specified file and its transitive dependencies can be resolved using the generated import map. Can be specified multiple times.
--dev Include devDependencies from package.json and pick "developement" in package conditions.
--keep-unused Remove mappings not used by any entrypoint or their transitive dependencies. Requires --entrypoint.
--config config.json Read additional settings from the given JSON configuration file.

For more advanced options, see the API.`);
}