mappel 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -5
- package/bin/mappel.mjs +124 -0
- package/package.json +2 -3
- package/src/index.mjs +91 -5
- package/bin/import-map.mjs +0 -92
package/README.md
CHANGED
|
@@ -9,15 +9,23 @@ It resolves each specifier through the package's own exports map — nested cond
|
|
|
9
9
|
A repo declares named layers in importmap.config.mjs, and a layer can exclude what another already resolves, so a page loads two maps and neither repeats the other. Layers can also drop a whole scope by prefix. A css specifier maps to a `.js` sibling that adds the file as a link, because a browser cannot import a stylesheet as a module. Pass `--css link` to get the specifier mapped away and the link tags printed instead.
|
|
10
10
|
|
|
11
11
|
```sh
|
|
12
|
-
mappel
|
|
13
|
-
mappel --layer
|
|
14
|
-
mappel --layer
|
|
15
|
-
mappel --layer min --
|
|
12
|
+
mappel # every layer, to the out in the config
|
|
13
|
+
mappel --layer min # one layer, to stdout
|
|
14
|
+
mappel --layer min --html # ready to paste into a page
|
|
15
|
+
mappel --layer min --dist-tag alpha # follow a channel instead of pinning
|
|
16
|
+
mappel --config packages/cdn/mappel.config.mjs --out dist
|
|
16
17
|
```
|
|
17
18
|
|
|
19
|
+
An option given on the command line wins; the config supplies what the command line
|
|
20
|
+
does not. With no arguments it looks for `mappel.config.mjs` from the current
|
|
21
|
+
directory upwards, so a package's build script is just `mappel`.
|
|
22
|
+
|
|
18
23
|
```js
|
|
19
|
-
//
|
|
24
|
+
// mappel.config.mjs
|
|
20
25
|
export default {
|
|
26
|
+
out: 'packages/cdn/dist', // relative to this file
|
|
27
|
+
workspaces: ['packages'],
|
|
28
|
+
split: { components: 'components' }, // a file per package in that layer
|
|
21
29
|
layers: {
|
|
22
30
|
base: { packages: ['@scope/core', '@scope/dom'] },
|
|
23
31
|
extra: { packages: ['@scope/extra'], excludes: ['base'] },
|
package/bin/mappel.mjs
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Write the import maps a workspace declares.
|
|
3
|
+
//
|
|
4
|
+
// mappel every layer in mappel.config.mjs, to its `out`
|
|
5
|
+
// mappel --layer min one layer, to stdout
|
|
6
|
+
// mappel --config packages/cdn/mappel.config.mjs --out dist
|
|
7
|
+
// mappel --layer min --dist-tag alpha --html
|
|
8
|
+
//
|
|
9
|
+
// With no arguments it finds mappel.config.mjs by walking up from the current
|
|
10
|
+
// directory, so a package's build script is just `mappel`.
|
|
11
|
+
|
|
12
|
+
import { existsSync, writeFileSync, mkdirSync } from 'node:fs';
|
|
13
|
+
import { dirname, join, resolve as resolvePath } from 'node:path';
|
|
14
|
+
import { pathToFileURL } from 'node:url';
|
|
15
|
+
import { buildLayer, writeAll, subpathsOf } from '../src/index.mjs';
|
|
16
|
+
|
|
17
|
+
const argv = process.argv.slice(2);
|
|
18
|
+
const flag = (name, fallback) => {
|
|
19
|
+
const i = argv.indexOf(`--${name}`);
|
|
20
|
+
return i === -1 ? fallback : argv[i + 1];
|
|
21
|
+
};
|
|
22
|
+
const has = (name) => argv.includes(`--${name}`);
|
|
23
|
+
|
|
24
|
+
const NAMES = ['mappel.config.mjs', 'mappel.config.js', 'importmap.config.mjs'];
|
|
25
|
+
|
|
26
|
+
function findConfig(from) {
|
|
27
|
+
let dir = resolvePath(from);
|
|
28
|
+
while (true) {
|
|
29
|
+
for (const name of NAMES) {
|
|
30
|
+
const candidate = join(dir, name);
|
|
31
|
+
if (existsSync(candidate)) return candidate;
|
|
32
|
+
}
|
|
33
|
+
const parent = dirname(dir);
|
|
34
|
+
if (parent === dir) return null;
|
|
35
|
+
dir = parent;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const explicit = flag('config', null);
|
|
40
|
+
const configPath = explicit ? resolvePath(explicit) : findConfig(process.cwd());
|
|
41
|
+
if (!configPath) {
|
|
42
|
+
process.stderr.write(
|
|
43
|
+
`[mappel] no ${NAMES[0]} here or above ${process.cwd()}\n` +
|
|
44
|
+
` point at one with --config, or see https://github.com/bambiste/mappel\n`,
|
|
45
|
+
);
|
|
46
|
+
process.exit(2);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let config;
|
|
50
|
+
try {
|
|
51
|
+
config = (await import(pathToFileURL(configPath).href)).default;
|
|
52
|
+
} catch (error) {
|
|
53
|
+
process.stderr.write(`[mappel] cannot read ${configPath}\n ${error.message}\n`);
|
|
54
|
+
process.exit(2);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// The config lives at the root of what it describes unless it says otherwise, so
|
|
58
|
+
// a path in it reads relative to the file rather than to where this was run from.
|
|
59
|
+
const base = dirname(configPath);
|
|
60
|
+
const root = resolvePath(flag('root', config.root ? join(base, config.root) : base));
|
|
61
|
+
|
|
62
|
+
const options = {
|
|
63
|
+
root,
|
|
64
|
+
target: flag('target', config.target ?? 'cdn'),
|
|
65
|
+
cdn: flag('cdn', config.cdn ?? 'https://unpkg.com'),
|
|
66
|
+
css: flag('css', config.css ?? 'loader'),
|
|
67
|
+
distTag: flag('dist-tag', config.distTag ?? null),
|
|
68
|
+
workspaces: config.workspaces ?? ['packages'],
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const layerName = flag('layer', null);
|
|
72
|
+
const splitInto = flag('split', null);
|
|
73
|
+
|
|
74
|
+
if (!layerName && !splitInto) {
|
|
75
|
+
const out = resolvePath(flag('out', config.out ? join(base, config.out) : join(base, 'dist')));
|
|
76
|
+
const written = writeAll(config, { ...options, out });
|
|
77
|
+
for (const { file, entries, split } of written) {
|
|
78
|
+
process.stderr.write(` ${file} — ${entries} ${split ? 'files' : 'entries'}\n`);
|
|
79
|
+
}
|
|
80
|
+
process.exit(0);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (splitInto) {
|
|
84
|
+
const dir = resolvePath(splitInto);
|
|
85
|
+
mkdirSync(dir, { recursive: true });
|
|
86
|
+
const layer = config.layers[layerName];
|
|
87
|
+
const names =
|
|
88
|
+
typeof layer.packages === 'function'
|
|
89
|
+
? layer.packages({ subpathsOf: (n) => subpathsOf(root, n, options.workspaces), root })
|
|
90
|
+
: layer.packages;
|
|
91
|
+
let written = 0;
|
|
92
|
+
for (const name of names) {
|
|
93
|
+
const one = buildLayer(
|
|
94
|
+
layerName,
|
|
95
|
+
{ ...config, layers: { ...config.layers, [layerName]: { ...layer, packages: [name] } } },
|
|
96
|
+
options,
|
|
97
|
+
);
|
|
98
|
+
if (Object.keys(one.imports).length === 0) continue;
|
|
99
|
+
writeFileSync(join(dir, name.split('/').pop() + '.json'), JSON.stringify({ imports: one.imports }, null, 2) + '\n');
|
|
100
|
+
written += 1;
|
|
101
|
+
}
|
|
102
|
+
process.stderr.write(`wrote ${written} maps to ${dir}\n`);
|
|
103
|
+
process.exit(0);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const { imports, stylesheets, unresolved } = buildLayer(layerName, config, options);
|
|
107
|
+
const json = JSON.stringify({ imports }, null, 2);
|
|
108
|
+
const out = flag('out', null);
|
|
109
|
+
|
|
110
|
+
if (out) {
|
|
111
|
+
writeFileSync(out, json + '\n');
|
|
112
|
+
process.stderr.write(`wrote ${out} — ${Object.keys(imports).length} entries\n`);
|
|
113
|
+
} else if (has('html')) {
|
|
114
|
+
const links =
|
|
115
|
+
options.css === 'link' ? stylesheets.map((h) => `<link rel="stylesheet" href="${h}" />`).join('\n') : '';
|
|
116
|
+
process.stdout.write(`<script type="importmap">\n${json}\n</script>\n${links ? links + '\n' : ''}`);
|
|
117
|
+
} else {
|
|
118
|
+
process.stdout.write(json + '\n');
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (unresolved.length) {
|
|
122
|
+
process.stderr.write('\nnot in the map (check they are installed):\n');
|
|
123
|
+
for (const u of unresolved.sort()) process.stderr.write(` ${u}\n`);
|
|
124
|
+
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mappel",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "generate the import map a page needs from a workspace: resolves every transitive subpath through each package exports map, in layers that compose",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"bin": {
|
|
8
|
-
"mappel": "./bin/
|
|
9
|
-
"import-map": "./bin/import-map.mjs"
|
|
8
|
+
"mappel": "./bin/mappel.mjs"
|
|
10
9
|
},
|
|
11
10
|
"exports": {
|
|
12
11
|
".": "./src/index.mjs",
|
package/src/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { readFileSync, existsSync } from 'node:fs';
|
|
1
|
+
import { readFileSync, existsSync, writeFileSync, mkdirSync } from 'node:fs';
|
|
2
2
|
import { dirname, join, normalize, relative } from 'node:path';
|
|
3
3
|
import { manifest, packageDir, resolveExport, splitSpecifier, subpathsOf } from './resolve.mjs';
|
|
4
4
|
|
|
@@ -25,7 +25,18 @@ function scanImports(file) {
|
|
|
25
25
|
* mapped to a url a browser can fetch.
|
|
26
26
|
*/
|
|
27
27
|
export function collect(specifiers, options) {
|
|
28
|
-
const {
|
|
28
|
+
const {
|
|
29
|
+
root,
|
|
30
|
+
target = 'cdn',
|
|
31
|
+
cdn = 'https://unpkg.com',
|
|
32
|
+
css = 'loader',
|
|
33
|
+
workspaces,
|
|
34
|
+
// The version in the url. A manifest version pins the map to exactly what was
|
|
35
|
+
// installed when it was written, which is what a published map wants. A dist
|
|
36
|
+
// tag follows a channel instead, so a page tracks it without being rebuilt —
|
|
37
|
+
// at the cost of the page changing when the channel does.
|
|
38
|
+
distTag = null,
|
|
39
|
+
} = options;
|
|
29
40
|
const imports = {};
|
|
30
41
|
const stylesheets = new Set();
|
|
31
42
|
const unresolved = new Set();
|
|
@@ -33,9 +44,9 @@ export function collect(specifiers, options) {
|
|
|
33
44
|
|
|
34
45
|
const urlFor = (name, dir, file) => {
|
|
35
46
|
const clean = file.replace(/^\.\//, '');
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
47
|
+
if (target === 'local') return '/' + relative(root, join(dir, clean)).split('\\').join('/');
|
|
48
|
+
const version = distTag === 'none' ? '' : '@' + (distTag || manifest(dir).version);
|
|
49
|
+
return `${cdn}/${name}${version}/${clean}`;
|
|
39
50
|
};
|
|
40
51
|
|
|
41
52
|
const add = (spec) => {
|
|
@@ -118,3 +129,78 @@ function helpers(options) {
|
|
|
118
129
|
}
|
|
119
130
|
|
|
120
131
|
export { subpathsOf, packageDir, resolveExport };
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Write every layer a config declares, plus any per-item split it asks for.
|
|
135
|
+
* This is what `mappel` does with no arguments: the config says where the
|
|
136
|
+
* workspace is and where the files go, so a repo needs no script of its own.
|
|
137
|
+
*/
|
|
138
|
+
export function writeAll(config, options) {
|
|
139
|
+
const out = options.out;
|
|
140
|
+
mkdirSync(out, { recursive: true });
|
|
141
|
+
|
|
142
|
+
const written = [];
|
|
143
|
+
for (const name of Object.keys(config.layers)) {
|
|
144
|
+
const { imports } = buildLayer(name, config, options);
|
|
145
|
+
const file = join(out, `${name}.json`);
|
|
146
|
+
writeFileSync(file, JSON.stringify({ imports }, null, 2) + '\n');
|
|
147
|
+
written.push({ file, entries: Object.keys(imports).length });
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// A combined file, for engines that take only one import map. Several maps in
|
|
151
|
+
// one page need Chromium 133 or newer. A layer of the same name means the config
|
|
152
|
+
// already has one, and writing both would silently overwrite the layer's file.
|
|
153
|
+
const combined = config.full === true || config.full === undefined ? 'full' : config.full;
|
|
154
|
+
if (config.full !== false && config.layers[combined]) {
|
|
155
|
+
throw new Error(
|
|
156
|
+
`[mappel] a layer is named "${combined}" and the combined file would overwrite it. ` +
|
|
157
|
+
`Set full: false, or full: "<other-name>".`,
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
if (config.full !== false) {
|
|
161
|
+
const imports = {};
|
|
162
|
+
for (const name of Object.keys(config.layers)) {
|
|
163
|
+
Object.assign(imports, buildLayer(name, config, options).imports);
|
|
164
|
+
}
|
|
165
|
+
const file = join(out, `${combined}.json`);
|
|
166
|
+
writeFileSync(
|
|
167
|
+
file,
|
|
168
|
+
JSON.stringify(
|
|
169
|
+
{ imports: Object.fromEntries(Object.entries(imports).sort(([a], [b]) => a.localeCompare(b))) },
|
|
170
|
+
null,
|
|
171
|
+
2,
|
|
172
|
+
) + '\n',
|
|
173
|
+
);
|
|
174
|
+
written.push({ file, entries: Object.keys(imports).length });
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
for (const [layerName, target] of Object.entries(config.split ?? {})) {
|
|
178
|
+
const dir = join(out, target);
|
|
179
|
+
mkdirSync(dir, { recursive: true });
|
|
180
|
+
const layer = config.layers[layerName];
|
|
181
|
+
const names =
|
|
182
|
+
typeof layer.packages === 'function'
|
|
183
|
+
? layer.packages({
|
|
184
|
+
subpathsOf: (n) => subpathsOf(options.root, n, options.workspaces),
|
|
185
|
+
root: options.root,
|
|
186
|
+
})
|
|
187
|
+
: layer.packages;
|
|
188
|
+
let count = 0;
|
|
189
|
+
for (const name of names) {
|
|
190
|
+
const one = buildLayer(
|
|
191
|
+
layerName,
|
|
192
|
+
{ ...config, layers: { ...config.layers, [layerName]: { ...layer, packages: [name] } } },
|
|
193
|
+
options,
|
|
194
|
+
);
|
|
195
|
+
if (Object.keys(one.imports).length === 0) continue;
|
|
196
|
+
writeFileSync(
|
|
197
|
+
join(dir, name.split('/').pop() + '.json'),
|
|
198
|
+
JSON.stringify({ imports: one.imports }, null, 2) + '\n',
|
|
199
|
+
);
|
|
200
|
+
count += 1;
|
|
201
|
+
}
|
|
202
|
+
written.push({ file: `${dir}/*.json`, entries: count, split: true });
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return written;
|
|
206
|
+
}
|
package/bin/import-map.mjs
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// Emit the import map a page needs, from the layers a repo defines.
|
|
3
|
-
//
|
|
4
|
-
// import-map --layer min
|
|
5
|
-
// import-map --layer components --split dist/components
|
|
6
|
-
// import-map --layer foundation --target local --html
|
|
7
|
-
//
|
|
8
|
-
// Layers come from importmap.config.mjs in the repo this runs in, so the tool
|
|
9
|
-
// stays generic and the policy stays where it belongs.
|
|
10
|
-
|
|
11
|
-
import { readFileSync, writeFileSync, mkdirSync } from 'node:fs';
|
|
12
|
-
import { join, resolve as resolvePath } from 'node:path';
|
|
13
|
-
import { pathToFileURL } from 'node:url';
|
|
14
|
-
import { buildLayer, subpathsOf } from '../src/index.mjs';
|
|
15
|
-
|
|
16
|
-
const argv = process.argv.slice(2);
|
|
17
|
-
const flag = (name, fallback) => {
|
|
18
|
-
const i = argv.indexOf(`--${name}`);
|
|
19
|
-
return i === -1 ? fallback : argv[i + 1];
|
|
20
|
-
};
|
|
21
|
-
const has = (name) => argv.includes(`--${name}`);
|
|
22
|
-
|
|
23
|
-
// The repo being scanned, not where this tool is installed — it lives in
|
|
24
|
-
// node_modules, and every path it resolves hangs off this.
|
|
25
|
-
const root = resolvePath(flag('root', process.cwd()));
|
|
26
|
-
const configPath = resolvePath(flag('config', join(root, 'importmap.config.mjs')));
|
|
27
|
-
|
|
28
|
-
let config;
|
|
29
|
-
try {
|
|
30
|
-
config = (await import(pathToFileURL(configPath).href)).default;
|
|
31
|
-
} catch (error) {
|
|
32
|
-
process.stderr.write(`[import-map] cannot read ${configPath}\n ${error.message}\n`);
|
|
33
|
-
process.exit(2);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const options = {
|
|
37
|
-
root,
|
|
38
|
-
target: flag('target', 'cdn'),
|
|
39
|
-
cdn: flag('cdn', 'https://unpkg.com'),
|
|
40
|
-
css: flag('css', 'loader'),
|
|
41
|
-
workspaces: config.workspaces ?? ['packages'],
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
const layerName = flag('layer', Object.keys(config.layers)[0]);
|
|
45
|
-
const splitInto = flag('split', null);
|
|
46
|
-
|
|
47
|
-
if (splitInto) {
|
|
48
|
-
const layer = config.layers[layerName];
|
|
49
|
-
const names =
|
|
50
|
-
typeof layer.packages === 'function'
|
|
51
|
-
? layer.packages({ subpathsOf: (n) => subpathsOf(root, n, options.workspaces), root })
|
|
52
|
-
: layer.packages;
|
|
53
|
-
mkdirSync(splitInto, { recursive: true });
|
|
54
|
-
let written = 0;
|
|
55
|
-
for (const name of names) {
|
|
56
|
-
const one = buildLayer(
|
|
57
|
-
layerName,
|
|
58
|
-
{ ...config, layers: { ...config.layers, [layerName]: { ...layer, packages: [name] } } },
|
|
59
|
-
options,
|
|
60
|
-
);
|
|
61
|
-
if (Object.keys(one.imports).length === 0) continue;
|
|
62
|
-
writeFileSync(
|
|
63
|
-
join(splitInto, name.split('/').pop() + '.json'),
|
|
64
|
-
JSON.stringify({ imports: one.imports }, null, 2) + '\n',
|
|
65
|
-
);
|
|
66
|
-
written += 1;
|
|
67
|
-
}
|
|
68
|
-
process.stderr.write(`wrote ${written} maps to ${splitInto}\n`);
|
|
69
|
-
process.exit(0);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const { imports, stylesheets, unresolved } = buildLayer(layerName, config, options);
|
|
73
|
-
const json = JSON.stringify({ imports }, null, 2);
|
|
74
|
-
|
|
75
|
-
const out = flag('out', null);
|
|
76
|
-
if (out) {
|
|
77
|
-
writeFileSync(out, json + '\n');
|
|
78
|
-
process.stderr.write(`wrote ${out} — ${Object.keys(imports).length} entries\n`);
|
|
79
|
-
} else if (has('html')) {
|
|
80
|
-
const links =
|
|
81
|
-
options.css === 'link'
|
|
82
|
-
? stylesheets.map((href) => `<link rel="stylesheet" href="${href}" />`).join('\n')
|
|
83
|
-
: '';
|
|
84
|
-
process.stdout.write(`<script type="importmap">\n${json}\n</script>\n${links ? links + '\n' : ''}`);
|
|
85
|
-
} else {
|
|
86
|
-
process.stdout.write(json + '\n');
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (unresolved.length) {
|
|
90
|
-
process.stderr.write('\nnot in the map (check they are installed):\n');
|
|
91
|
-
for (const u of unresolved.sort()) process.stderr.write(` ${u}\n`);
|
|
92
|
-
}
|