-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmapbox.postinstall.js
43 lines (39 loc) · 1.49 KB
/
mapbox.postinstall.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
const CleanCSS = require('clean-css')
const fs = require('fs')
const promiseToGetMinifiedCss = () => {
return new Promise((resolve, reject) => {
fs.readFile('node_modules/mapbox-gl/dist/mapbox-gl.css', 'utf8', (err, css) => {
if (err) return reject(err)
const minified = new CleanCSS({}).minify(css)
return resolve(minified.styles)
})
})
}
const promiseToGetCleanupJs = () => {
return new Promise((resolve, reject) => {
fs.readFile('node_modules/mapbox-gl/dist/mapbox-gl.js', 'utf8', (err, js) => {
if (err) return reject(err)
js = js.replace('this', 'window')
js = js.replace('//# sourceMappingURL=mapbox-gl.js.map', '')
return resolve(js)
})
})
}
const promiseToCreatePolymerCompatibleMapbox = () => {
return new Promise(async (resolve, reject) => {
const css = await promiseToGetMinifiedCss()
const js = await promiseToGetCleanupJs()
let newFileString = `const $_documentContainer = document.createElement('template');
$_documentContainer.innerHTML = \`<dom-module id="mapboxgl-styles"><template><style>${css}</style></template></dom-module>\`;
document.head.appendChild($_documentContainer.content);
${js}`
fs.writeFile('node_modules/mapbox-gl/dist/mapbox-gl-polymer.js', newFileString, 'utf8', err => {
if (err) return reject(err)
console.log('mapbox-gl postinstall step performed succesfully')
return resolve()
})
})
}
(async () => {
await promiseToCreatePolymerCompatibleMapbox()
})()