-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta.js
50 lines (44 loc) · 1.43 KB
/
meta.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
/* eslint-env node */
/* global process */
import MagicString from 'magic-string';
import { validateURL } from '@shgysk8zer0/npm-utils/url';
function transformSource(source, id, mapping) {
const ms = new MagicString(source);
Object.entries(mapping).forEach(([search, replace]) => ms.replaceAll(search, replace));
if (ms.hasChanged()) {
const code = ms.toString();
const map = ms.generateMap({ source: id });
return { code, map };
}
}
export function rollupImportMeta({
baseURL = process?.env?.URL,
projectRoot = new URL(`${process.cwd()}/`, 'file://').href,
} = {}) {
if (! validateURL(baseURL)) {
throw new TypeError(`baseURL: "${baseURL}"" is not a valid URL.`);
} else if (! validateURL(projectRoot)) {
throw new TypeError(`projectRoot: "${projectRoot} is not a valid URL."`);
} else {
return {
transform(source, id) {
if (! source.includes('import.meta')) {
return;
} else if (id.startsWith('https:')) {
return transformSource(source, id, {
'${import.meta.url}': id,
'import.meta.url': `'${id}'`,
'import.meta.resolve(': `(path => new URL(path, '${id}').href)(`,
});
} else if (id.startsWith('file:')) {
const url = new URL(id.replace(projectRoot, baseURL)).href;
return transformSource(source, id, {
'${import.meta.url}': url,
'import.meta.url': `'${url}'`,
'import.meta.resolve(': `(path => new URL(path, '${url}').href)(`,
});
}
}
};
}
}