Create a gist now

Instantly share code, notes, and snippets.

@tschaub /.gitignore
Last active Mar 24, 2017

What would you like to do?
OpenLayers + Browserify
/node_modules/
bundle.js

OpenLayers + Browserify

This example demonstrates how the ol package can be used with Browserify.

Clone the project:

git clone git@gist.github.com:4bfb209a8f809823f1495b2e4436018e.git ol-browserify

Install the project dependencies.

cd ol-browserify
npm install

Create a bundle for the browser.

npm run build

Open index.html to see the result.

open index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Using OpenLayers with Browserify</title>
<link rel="stylesheet" href="https://openlayers.org/en/latest/css/ol.css" type="text/css">
<style>
html, body {
margin: 0;
height: 100%;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script src="./bundle.js"></script>
</body>
</html>
import Map from 'ol/map';
import View from 'ol/view';
import TileLayer from 'ol/layer/tile';
import XYZ from 'ol/source/xyz';
new Map({
target: 'map',
layers: [
new TileLayer({
source: new XYZ({
url: 'https://{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png'
})
})
],
view: new View({
center: [0, 0],
zoom: 2
})
});
{
"name": "ol-browserify",
"version": "1.0.0",
"description": "Example using OpenLayers with Browserify",
"scripts": {
"build": "browserify main.js --transform [ babelify --plugins [ transform-es2015-modules-commonjs ] ] | uglifyjs -c -m > bundle.js"
},
"devDependencies": {
"babel-plugin-transform-es2015-modules-commonjs": "^6.22.0",
"babelify": "^7.3.0",
"browserify": "^14.0.0",
"uglify-js": "^2.7.5"
},
"dependencies": {
"ol": "^4.0.1-beta.2"
}
}

HamHamFonFon commented Mar 24, 2017 edited

I have made a little application with ES6 and Openlayers (and using browserify and babelify) but my package.json is a little different :
I'd like to know if it's different or it's the same thing :)

{
  "name": "Kurtography",
  "description": "Cartography application supported by Kuzzle",
  "main": "app.js",
  "scripts": {
    "build-map": "browserify -e app-map.js -o ./public/js/bundle.js",
    "start": "node app.js"
  },
  "dependencies": {
    "openlayers": "^3.15.1",
  },
  "devDependencies": {
    "babel-core": "^6.3.0",
    "babel-loader": "^6.0.0",
    "babel-plugin-transform-runtime": "^6.8.0",
    "babel-preset-es2015": "^6.0.0",
    "babel-runtime": "^5.8.0",
    "babelify": "^7.2.0",
    "browserify": "^13.0.0"
  },
  "browserify": {
    "transform": [
      [
        "babelify",
        {
          "presets": [
            "es2015"
          ]
        }
      ]
    ]
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/HamHamFonFon/kurtogaphy.git"
  },
  "author": "Stéphane MEAUDRE <smeaudre@kaliop.com> (S. Meaudre)",
}

Thank you for your answer
My source : https://github.com/HamHamFonFon/kurtography/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment