Skip to content

Commit 582f428

Browse files
author
Peter Hegman
committed
Hot fix to prevent mapbox-gl from being bundled and get demo up and running
- Change configuration to use rollup for bundles and wepback for demo - Move rollup configs into multiple files - Remove mapbox-gl import and reference TypeScript definitions instead
1 parent 9de1738 commit 582f428

10 files changed

+2936
-178
lines changed

build-configs/rollup.es.config.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import vue from 'rollup-plugin-vue'
2+
import commonjs from 'rollup-plugin-commonjs'
3+
import { terser } from 'rollup-plugin-terser'
4+
import resolve from 'rollup-plugin-node-resolve'
5+
import typescript from 'rollup-plugin-typescript2'
6+
7+
export default {
8+
input: 'src/wrapper.ts',
9+
output: {
10+
file: 'dist/vue-mapbox-gl.esm.js',
11+
format: 'es',
12+
name: 'Mapbox',
13+
exports: 'named',
14+
globals: {
15+
vue: 'Vue',
16+
'mapbox-gl': 'mapboxgl',
17+
},
18+
},
19+
plugins: [
20+
resolve(),
21+
commonjs(),
22+
vue({
23+
css: true,
24+
compileTemplate: true,
25+
}),
26+
typescript({ clean: true }),
27+
terser(),
28+
],
29+
external: ['vue', 'mapbox-gl'],
30+
}

build-configs/rollup.iife.config.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import vue from 'rollup-plugin-vue'
2+
import replace from 'rollup-plugin-replace'
3+
import commonjs from 'rollup-plugin-commonjs'
4+
import { terser } from 'rollup-plugin-terser'
5+
import resolve from 'rollup-plugin-node-resolve'
6+
import typescript from 'rollup-plugin-typescript2'
7+
8+
export default {
9+
input: 'src/wrapper.ts',
10+
output: {
11+
file: 'dist/vue-mapbox-gl.min.js',
12+
format: 'iife',
13+
name: 'Mapbox',
14+
exports: 'named',
15+
globals: {
16+
vue: 'Vue',
17+
'mapbox-gl': 'mapboxgl',
18+
},
19+
},
20+
plugins: [
21+
resolve(),
22+
commonjs(),
23+
vue({
24+
css: true,
25+
compileTemplate: true,
26+
}),
27+
typescript({ clean: true }),
28+
terser(),
29+
replace({
30+
'process.env.NODE_ENV': JSON.stringify('production'),
31+
'process.env.VUE_ENV': JSON.stringify('browser'),
32+
}),
33+
],
34+
external: ['vue', 'mapbox-gl'],
35+
}

build-configs/rollup.umd.config.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import vue from 'rollup-plugin-vue'
2+
import commonjs from 'rollup-plugin-commonjs'
3+
import { terser } from 'rollup-plugin-terser'
4+
import resolve from 'rollup-plugin-node-resolve'
5+
import typescript from 'rollup-plugin-typescript2'
6+
7+
export default {
8+
input: 'src/wrapper.ts',
9+
output: {
10+
file: 'dist/vue-mapbox-gl.umd.js',
11+
format: 'umd',
12+
name: 'Mapbox',
13+
exports: 'named',
14+
globals: {
15+
vue: 'Vue',
16+
'mapbox-gl': 'mapboxgl',
17+
},
18+
},
19+
plugins: [
20+
resolve(),
21+
commonjs(),
22+
vue({
23+
css: true,
24+
compileTemplate: true,
25+
}),
26+
typescript({ clean: true }),
27+
terser(),
28+
],
29+
external: ['vue', 'mapbox-gl'],
30+
}

build-configs/webpack.demo.config.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const path = require('path')
2+
const webpack = require('webpack')
3+
const VueLoaderPlugin = require('vue-loader/lib/plugin')
4+
const HtmlWebpackPlugin = require('html-webpack-plugin')
5+
6+
module.exports = {
7+
entry: {
8+
index: path.resolve(__dirname, '../src/demo.ts'),
9+
},
10+
output: {
11+
path: path.resolve(__dirname, '../dist'),
12+
filename: 'demo.min.js',
13+
},
14+
module: {
15+
rules: [
16+
{
17+
test: /\.vue$/,
18+
loader: 'vue-loader',
19+
},
20+
{
21+
test: /\.tsx?$/,
22+
loader: 'ts-loader',
23+
exclude: /node_modules/,
24+
options: {
25+
appendTsSuffixTo: [/\.vue$/],
26+
},
27+
},
28+
{
29+
test: /\.css$/,
30+
use: ['vue-style-loader', 'css-loader'],
31+
},
32+
],
33+
},
34+
resolve: {
35+
extensions: ['.ts', '.js', '.vue', '.json'],
36+
alias: {
37+
vue$: 'vue/dist/vue.esm.js',
38+
},
39+
},
40+
plugins: [
41+
new VueLoaderPlugin(),
42+
new HtmlWebpackPlugin({
43+
template: path.resolve(__dirname, '../src/index.html'),
44+
}),
45+
new webpack.ProvidePlugin({
46+
mapboxgl: 'mapbox-gl',
47+
}),
48+
new webpack.DefinePlugin({
49+
'process.env': {
50+
ACCESS_TOKEN: JSON.stringify(process.env.ACCESS_TOKEN),
51+
},
52+
}),
53+
],
54+
}

package.json

+12-8
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,40 @@
2727
"@typescript-eslint/parser": "^1.13.0",
2828
"@vue/eslint-config-prettier": "^5.0.0",
2929
"@vue/eslint-config-typescript": "^4.0.0",
30+
"css-loader": "^3.2.0",
3031
"eslint": "^6.1.0",
3132
"eslint-plugin-prettier": "^3.1.0",
3233
"eslint-plugin-vue": "^5.2.3",
34+
"html-webpack-plugin": "^3.2.0",
3335
"husky": "^3.0.3",
3436
"mapbox-gl": "^1.3.0",
3537
"prettier": "1.18.2",
3638
"pretty-quick": "^1.11.1",
3739
"rimraf": "^2.6.3",
3840
"rollup": "^1.19.2",
3941
"rollup-plugin-commonjs": "^10.0.2",
40-
"rollup-plugin-generate-html-template": "^1.2.0",
4142
"rollup-plugin-node-resolve": "^5.2.0",
4243
"rollup-plugin-replace": "^2.2.0",
43-
"rollup-plugin-serve": "^1.0.1",
4444
"rollup-plugin-terser": "^5.1.1",
4545
"rollup-plugin-typescript2": "^0.22.1",
4646
"rollup-plugin-vue": "^5.0.1",
47+
"ts-loader": "^6.0.4",
4748
"typescript": "^3.5.3",
4849
"vue": "^2.6.10",
4950
"vue-class-component": "^7.1.0",
51+
"vue-loader": "^15.7.1",
5052
"vue-property-decorator": "^8.2.1",
51-
"vue-template-compiler": "^2.6.10"
53+
"vue-style-loader": "^4.1.2",
54+
"vue-template-compiler": "^2.6.10",
55+
"webpack": "^4.39.3",
56+
"webpack-cli": "^3.3.7"
5257
},
5358
"scripts": {
5459
"build": "rimraf dist & yarn build:umd & yarn build:es & yarn build:unpkg & yarn build:demo",
55-
"build:umd": "rollup --config rollup.config.js --format umd --file dist/vue-mapbox-gl.umd.js --environment BUILD:prod,FORMAT:umd",
56-
"build:es": "rollup --config rollup.config.js --format es --file dist/vue-mapbox-gl.esm.js --environment BUILD:prod,FORMAT:es",
57-
"build:unpkg": "rollup --config rollup.config.js --format iife --file dist/vue-mapbox-gl.min.js --environment BUILD:prod,FORMAT:iife",
58-
"build:demo": "rollup --config rollup.config.js --format iife --file dist/demo.min.js --environment BUILD:demo,FORMAT:iife",
59-
"serve": "rimraf dist & rollup --config rollup.config.js --format iife --file dist/demo.min.js --environment BUILD:dev,FORMAT:iife --watch",
60+
"build:umd": "rollup --config build-configs/rollup.umd.config.js",
61+
"build:es": "rollup --config build-configs/rollup.es.config.js",
62+
"build:unpkg": "rollup --config build-configs/rollup.iife.config.js",
63+
"build:demo": "webpack --config build-configs/webpack.demo.config.js --mode production",
6064
"lint": "./node_modules/.bin/eslint src/**/*.{vue,ts}"
6165
},
6266
"dependencies": {},

rollup.config.js

-56
This file was deleted.

src/Demo.vue

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<mapbox
3-
:access-token="process.env.ACCESS_TOKEN"
3+
:access-token="accessToken"
44
:map-options="{
55
style: 'mapbox://styles/mapbox/light-v9',
66
center: [-96, 37.8],
@@ -34,6 +34,7 @@ import { Point } from 'geojson'
3434
import mapboxgl from 'mapbox-gl'
3535
import Mapbox from './Mapbox.vue'
3636
import Component from 'vue-class-component'
37+
import { Prop } from 'vue-property-decorator'
3738
import PopupContent from './PopupContent.vue'
3839
// @ts-ignore TypeScript definitions for mapbox-gl-draw not yet available
3940
import MapboxDraw from '@mapbox/mapbox-gl-draw/dist/mapbox-gl-draw'
@@ -43,6 +44,8 @@ import { Map, MapLayerMouseEvent, GeolocateControl } from 'mapbox-gl'
4344
components: { Mapbox },
4445
})
4546
export default class Demo extends Vue {
47+
@Prop({ required: true }) readonly accessToken: string
48+
4649
initalized(map: Map): void {
4750
console.log('Map Initalized')
4851
@@ -136,7 +139,7 @@ export default class Demo extends Vue {
136139
}
137140
</script>
138141

139-
<style lang="scss" scoped>
142+
<style scoped>
140143
#map {
141144
width: 100%;
142145
height: 500px;

src/Mapbox.vue

+10-16
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,14 @@
99
</template>
1010

1111
<script lang="ts">
12+
declare global {
13+
const mapboxgl: typeof mapboxgl
14+
}
15+
1216
import Vue from 'vue'
1317
import Component from 'vue-class-component'
1418
import { Prop, Emit } from 'vue-property-decorator'
1519
16-
import {
17-
Map,
18-
EventData,
19-
MapEventType,
20-
MapboxOptions,
21-
GeolocateControl,
22-
FullscreenControl,
23-
MapLayerEventType,
24-
} from 'mapbox-gl'
25-
import mapboxgl from 'mapbox-gl'
2620
import ScaleControlOptions from './interfaces/scale-control-options.interface'
2721
import AttributionControl from './interfaces/attribution-control-options.interface'
2822
import GeolocateControlOptions from './interfaces/geolocate-control-options.interface'
@@ -32,7 +26,7 @@ import FullscreenControlOptions from './interfaces/navigation-control-options.in
3226
@Component
3327
export default class Mapbox extends Vue {
3428
@Prop({ required: true }) readonly accessToken: string
35-
@Prop({ required: true }) readonly mapOptions: MapboxOptions
29+
@Prop({ required: true }) readonly mapOptions: mapboxgl.MapboxOptions
3630
@Prop({
3731
default: () => {
3832
return {
@@ -88,7 +82,7 @@ export default class Mapbox extends Vue {
8882
})
8983
readonly attributionControl!: AttributionControl
9084
91-
private map!: Map
85+
private map!: mapboxgl.Map
9286
9387
mounted(): void {
9488
// Initialze Map
@@ -106,7 +100,7 @@ export default class Mapbox extends Vue {
106100
}
107101
108102
@Emit()
109-
mapInit(): Map {
103+
mapInit(): mapboxgl.Map {
110104
mapboxgl.accessToken = this.accessToken
111105
112106
// Add container to options object
@@ -120,7 +114,7 @@ export default class Mapbox extends Vue {
120114
return map
121115
}
122116
123-
registerMapEvents(map: Map): void {
117+
registerMapEvents(map: mapboxgl.Map): void {
124118
const availableEvents: string[] = [
125119
'error',
126120
'load',
@@ -198,7 +192,7 @@ export default class Mapbox extends Vue {
198192
availableEventsWithLayerSupport.indexOf(parsedEventType) > -1
199193
) {
200194
map.on(
201-
parsedEventType as keyof MapLayerEventType,
195+
parsedEventType as keyof mapboxgl.MapLayerEventType,
202196
layerId,
203197
event => {
204198
this.$emit(eventType, map, event)
@@ -214,7 +208,7 @@ export default class Mapbox extends Vue {
214208
}
215209
}
216210
217-
addControls(map: Map): void {
211+
addControls(map: mapboxgl.Map): void {
218212
// Nav Control
219213
if (this.navControl.show) {
220214
const nav = new mapboxgl.NavigationControl(this.navControl.options)

src/demo.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import Vue from 'vue'
22
import Demo from './Demo.vue'
33

44
new Vue({
5-
render: h => h(Demo),
5+
render: h => h(Demo, { props: { accessToken: process.env.ACCESS_TOKEN } }),
66
}).$mount('#app')

0 commit comments

Comments
 (0)