forked from mapbox/mapbox-gl-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-loader.js
52 lines (42 loc) · 1.66 KB
/
node-loader.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
import flowRemoveTypes from '@mapbox/flow-remove-types';
import {dataToEsm} from '@rollup/pluginutils';
const glslRe = /\.glsl$/;
const jsonRe = /\.json$/;
export async function resolve(specifier, context, nextResolve) {
if (glslRe.test(specifier)) {
const url = new URL(specifier, context.parentURL).href;
return {url, shortCircuit: true};
}
if (specifier == 'tracked_parameters_proxy') {
specifier = './internal/tracked_parameters_mock.js';
}
return nextResolve(specifier);
}
export async function load(url, context, nextLoad) {
if (context.format === 'module') {
const {source: rawSource} = await nextLoad(url, context);
const source = rawSource.toString();
if (source.indexOf('@flow') >= 0) {
const transformedSource = flowRemoveTypes(source).toString();
return {format: 'module', source: transformedSource, shortCircuit: true};
}
}
if (glslRe.test(url)) {
const {source: rawSource} = await nextLoad(url, {...context, format: 'module'});
const source = `export default \`${rawSource.toString()}\``;
return {format: 'module', source, shortCircuit: true};
}
if (jsonRe.test(url)) {
const {source: rawSource} = await nextLoad(url, {...context,
// Force import assertions as "assert { type: 'json' }"
importAssertions: {type: 'json'}
});
const source = dataToEsm(JSON.parse(rawSource.toString()), {
preferConst: true,
namedExports: true,
indent: ' '
});
return {format: 'module', source, shortCircuit: true};
}
return nextLoad(url);
}