forked from seia-soto/snowpack-plugin-import-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-imports.js
38 lines (33 loc) · 1 KB
/
generate-imports.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const ignoreSome = (ignoreValue) => (ignoreEntry) => {
if (ignoreEntry instanceof RegExp) {
return ignoreEntry.test(ignoreValue);
}
return ignoreEntry === ignoreValue;
};
const defaultFormatImportValue = ({ baseUrl, dependency }) => {
return `${baseUrl}/${dependency.name}@${dependency.version}`;
};
const generateImports = (options = {}) => {
const baseUrl = (options.baseUrl || 'https://cdn.skypack.dev').replace(
/\/$/,
''
);
const dependencies = options.dependencies || {};
const ignore = options.ignore || [];
const formatImportValue =
options.formatImportValue || defaultFormatImportValue;
return Object.fromEntries(
Object.entries(dependencies)
.map(([name, version]) => {
if (ignore.some(ignoreSome(name))) {
return undefined;
}
return [
name,
formatImportValue({ baseUrl, dependency: { name, version } }),
];
})
.filter((e) => e) // remove undefined
);
};
module.exports = generateImports;