-
Notifications
You must be signed in to change notification settings - Fork 2
/
rollup.config.dev.js
112 lines (110 loc) · 4.44 KB
/
rollup.config.dev.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import typescript from "@rollup/plugin-typescript";
import html from "@rollup/plugin-html";
import css from "rollup-plugin-css-only";
import serve from "rollup-plugin-serve";
import livereload from "rollup-plugin-livereload";
const mapboxglToken = process.env.mapboxglToken;
export default [
{
input: "src/index.ts",
output: {
dir: "dev",
format: "es",
},
plugins: [
resolve(),
commonjs(),
typescript(),
html({
fileName: "index.html",
template: () => {
const attribute = `lang="en"`;
const meta = `<meta charset="utf-8">`;
const title = `mapbox-gl-path`;
const linkFavicon = `<link rel="shortcut icon" type="image/x-icon" href="https://makina-corpus.com/favicon.ico">`;
const linkMapbox = `<link href="https://api.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.css" rel="stylesheet" />`;
const linkPage = `<link href="./mapbox-gl-path.css" rel="stylesheet" />`;
const links = `${linkFavicon}${linkMapbox}${linkPage}`;
const content = `<div id="map" style="width: 100vw; height: 100vh;"></div>`;
const scriptMapbox = `<script src="https://api.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.js"></script>`;
const layersCustomisation = `{
pointLayerList: [{
paint: {
"circle-radius": 10,
"circle-color": "#FFFFFF",
"circle-stroke-width": 1,
"circle-stroke-color": "#0D47A1",
},
}, {
paint: {
"text-color": "#B71C1C"
},
type: "symbol",
layout: {
"text-field": ["to-string", ["+", ["get", "index"], 1]],
"text-allow-overlap": true,
},
}],
lineLayerList: [{
paint: { "line-width": 4, "line-color": "#0D47A1" },
}],
phantomJunctionLineLayerList: [{
paint: {
"line-width": 4,
"line-color": "#0D47A1",
"line-dasharray": [1, 1],
},
}],
}`;
const featureCollection = `undefined`;
const directionsThemes = `[
{ theme: "cycling" },
{ theme: "walking", selected: true }
].map(({ theme, selected }, index) => ({
id: index,
name: "Mapbox "+theme,
selected,
getPathByCoordinates: async (coordinates) =>
await fetch(
'https://api.mapbox.com/directions/v5/mapbox/'+theme+'/'+coordinates.join(';')+'?geometries=geojson&overview=full&access_token=${mapboxglToken}',
{
method: "GET",
headers: { "Content-Type": "application/json" },
}
)
.then((response) => response.json())
.then((data) => data.code === "Ok"
? { coordinates: data.routes[0].geometry.coordinates, waypoints: { departure: data.waypoints[0].location, arrival: data.waypoints[1].location }}
: undefined
)
}))`;
const scriptPage = `
<script type="module">
import MapboxPathControl from "./index.js";
mapboxgl.accessToken = "${mapboxglToken}";
var map = new mapboxgl.Map({ container: "map", style: "mapbox://styles/mapbox/streets-v11", center: [2.21, 46.22], zoom: 5 });
map.on('error', ({ error }) => {
// Invalid access token
console.error(error)
document.write(error.message)
});
window.mapboxPathControl = new MapboxPathControl({
layersCustomisation: ${layersCustomisation},
featureCollection: ${featureCollection},
directionsThemes: ${directionsThemes},
});
map.addControl(window.mapboxPathControl);
</script>
`;
const scripts = `${scriptMapbox}${scriptPage}`;
return `<!DOCTYPE html><html ${attribute}><head>${meta}<title>${title}</title>${links}</head><body style="margin: 0">${content}${scripts}</body></html>`;
},
}),
css({ output: "./dev/mapbox-gl-path.css" }),
serve({ contentBase: "dev", port: 9000 }),
livereload(),
],
},
];