-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.js
98 lines (87 loc) · 2.96 KB
/
import.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* eslint-env node */
import { fileExists, readFile } from '@shgysk8zer0/npm-utils/fs';
import { readYAMLFile, isYAMLFile } from '@shgysk8zer0/npm-utils/yaml';
import { readJSONFile, isJSONFile } from '@shgysk8zer0/npm-utils/json';
import { ROOT } from '@shgysk8zer0/npm-utils/consts';
import { buildImportmap, getInvalidMapError, resolveImport } from '@shgysk8zer0/npm-utils/importmap';
import { isString, isBare } from '@shgysk8zer0/npm-utils/utils';
import { JS as JS_MIME } from '@shgysk8zer0/consts/mimes';
import { pathToURL } from '@shgysk8zer0/npm-utils/url';
import { dirname } from '@shgysk8zer0/npm-utils/path';
const JS_MIMES = ['text/javascript', JS_MIME];
const isJS = type => JS_MIMES.includes(type.toLowerCase());
const cached = new Map();
async function getFile(pathname) {
if (! await fileExists(pathname)) {
throw new Error(`${pathname} not found.`);
} else if (isYAMLFile(pathname)) {
return readYAMLFile(pathname);
} else if (isJSONFile(pathname)) {
return readJSONFile(pathname);
} else {
throw new TypeError(`Unsupported file type for ${pathname}.`);
}
}
export function rollupImport(importMaps = []) {
const importmap = new Map();
const maps = Array.isArray(importMaps) ? importMaps : [importMaps];
return {
name: '@shgysk8zer0/rollup-import',
async load(path) {
if (cached.has(path)) {
return cached.get(path);
} else {
switch(new URL(path).protocol) {
case 'file:':
return readFile(path).then(content => {
cached.set(path, content);
return content;
});
case 'https:':
case 'http:':
return fetch(path).then(async resp => {
if (! resp.ok) {
throw new Error(`<${path}> [${resp.status} ${resp.statusText}]`);
} else if (! isJS(resp.headers.get('Content-Type').split(';')[0].trim())) {
throw new TypeError(`Expected 'application/javascript' but got ${resp.headers.get('Content-Type')}`);
} else {
const content = await resp.text();
cached.set(path, content);
return content;
}
});
default:
throw new TypeError(`Unsupported protocol "${path}."`);
}
}
},
async buildStart(options) {
if (typeof options !== 'undefined') {
const mappings = maps.map(entry => isString(entry)
? getFile(entry, options) // Load from file
: entry // Use the Object
);
await buildImportmap(importmap, mappings);
const err = getInvalidMapError(importmap);
if (err instanceof Error) {
throw err;
}
}
},
resolveId(id, src, { /*assertions, custom,*/ isEntry }) {
// @TODO: Store `options.external` and use for return value?
if (isEntry) {
return { id: new URL(id, ROOT.href).href, external: false };
} else if (isBare(id)) {
const match = resolveImport(id, importmap);
if (match instanceof URL) {
return { id: match.href, external: false };
} else {
return null;
}
} else {
return { id: pathToURL(id, dirname(src)).href, external: false };
}
},
};
}