This repository has been archived by the owner on Oct 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browerify-la-construction-a-la-volee.html
278 lines (215 loc) · 19.1 KB
/
browerify-la-construction-a-la-volee.html
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<!DOCTYPE html>
<html lang="en">
<head>
<title>Javascript: la construction à la volée (watching) avec Browserify</title>
<meta charset="utf-8" />
<link href="https://techblog.mappy.com/feeds/rss.xml" type="application/atom+xml" rel="alternate" title="Mappy Labs Full Atom Feed" />
<link href="https://techblog.mappy.com/feeds/web/rss.xml" type="application/atom+xml" rel="alternate" title="Mappy Labs Categories Atom Feed" />
<!-- Mobile viewport optimized: j.mp/bplateviewport -->
<meta name="viewport" content="width=device-width,initial-scale=1, maximum-scale=1">
<link rel="stylesheet" type="text/css" href="./theme/gumby.css" />
<link rel="stylesheet" type="text/css" href="./theme/style.css" />
<link rel="stylesheet" type="text/css" href="./theme/pygment.css" />
<link rel="icon" type="image/png" href="images/favicon-96x96.png" sizes="96x96">
<link rel="icon" type="image/png" href="images/favicon-16x16.png" sizes="16x16">
<link rel="icon" type="image/png" href="images/favicon-32x32.png" sizes="32x32">
</head>
<body id="index" class="home">
<div class="container">
<header id="banner" class="body">
<h1><a href="/"><img src="/images/logo.png" /> Labs</a></h1>
<div id="navigation" class="navbar row">
<a href="#" gumby-trigger="#navigation > ul" class="toggle"><i class="icon-menu"></i></a>
</div>
</header><!-- /#banner -->
<div class="row">
<section id="content" class="body">
<div class="row">
<div class="ten columns">
<header>
<h2 class="entry-title">
<a href="./browerify-la-construction-a-la-volee.html" rel="bookmark"
title="Permalink to Javascript: la construction à la volée (watching) avec Browserify">Javascript: la construction à la volée (watching) avec Browserify</a></h2>
</header>
<footer class="post-info">
<abbr class="published" title="2016-10-06T00:00:00+02:00">
Thu 06 October 2016
</abbr>
<address class="vcard author">
By <a class="url fn" href="./author/vivien-ripoche.html">Vivien Ripoche</a>
</address>
</footer><!-- /.post-info -->
<div class="entry-content">
<h1>Browserify, c'est quoi déjà ?</h1>
<p>De manière simple, <a href="http://browserify.org/">browserify</a> permet d'avoir accès à l'écosystème de NodeJS (<code>NPM</code>, <code>CommonJS</code>) avec des scripts destinés au navigateur (browser).</p>
<p>Après avoir installé le module browserify via <code>NPM</code>, il peut s'utiliser directement en ligne de commande :</p>
<div class="highlight"><pre><span></span>browserify main.js -o bundle.js
</pre></div>
<p>Mais est plus souvent utilisé en tant que librairie dans un script NodeJS :</p>
<div class="highlight"><pre><span></span><span class="kd">var</span> <span class="nx">fs</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'fs'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">browserify</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'browserify'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">bundleWS</span> <span class="o">=</span> <span class="nx">fs</span><span class="p">.</span><span class="nx">createWriteStream</span><span class="p">(</span><span class="nx">__dirname</span> <span class="o">+</span> <span class="s1">'/bundle.js'</span><span class="p">);</span>
<span class="nx">browserify</span><span class="p">({</span>
<span class="nx">entries</span><span class="o">:</span> <span class="s2">"main.js"</span>
<span class="p">}).</span><span class="nx">bundle</span><span class="p">().</span><span class="nx">pipe</span><span class="p">(</span><span class="nx">bundleWS</span><span class="p">);</span>
</pre></div>
<p>On remarque immédiatement que <code>browserify</code> utilise des flux textes ou <a href="http://www.sandersdenardi.com/readable-writable-transform-streams-node/">text streams</a> comme format de sortie via <code>bundle()</code>.
Cela permet une grande souplesse mais ausi de pouvoir l'utiliser avec d'autres outils utilisant les streams comme <a href="http://gulpjs.com/">gulp</a>.
Attention car ce dernier utilise un format de stream spécifique, basé sur le système de fichier : <a href="https://github.com/gulpjs/vinyl">vinyl</a>. Il faudra donc convertir le <code>text stream</code> en ce format avant d'utiliser des pipes gulp.</p>
<p>Browserify propose des <code>transforms</code>, qui sont des tranformateurs de flux, comme la minification (<code>uglify</code>) ou l'ajout de scripts de librairies tiers comme <code>bower</code> ou <code>shim</code>.
On peut aussi lui ajouter des greffons (<code>plugins</code>) afin d'ajouter à <code>browserify</code> d'autres capacités comme le <code>watching</code> avec Watchify.</p>
<h1>A quoi consiste la construction à la volée (watching) ?</h1>
<p>Il s'agit littéralement de vérifier l'état d'un ensemble de fichiers et de déclencher un processus à chaque modification.</p>
<p>Par extension, on appelle également <code>watching</code> :</p>
<ul>
<li>la construction d'un nouveau livrable à chaque modification,</li>
<li>et dans le cas de scripts navigateurs, le rechargement de la page (<code>livereload</code>)</li>
</ul>
<h1>Comment le met-on en place ?</h1>
<h2>Watchify</h2>
<p><a href="https://github.com/substack/watchify">Watchify</a> est un plugin <code>browserify</code>. Il peut s'ajouter à la configuration grâce à l'attribut <code>plugin</code> ou via un <code>wrapper</code> :</p>
<div class="highlight"><pre><span></span><span class="kd">var</span> <span class="nx">fs</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'fs'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">browserify</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'browserify'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">watchify</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'watchify'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">bundleWS</span> <span class="o">=</span> <span class="nx">fs</span><span class="p">.</span><span class="nx">createWriteStream</span><span class="p">(</span><span class="nx">__dirname</span> <span class="o">+</span> <span class="s1">'/bundle.js'</span><span class="p">);</span>
<span class="c1">//config</span>
<span class="nx">browserify</span><span class="p">(</span><span class="nb">Object</span><span class="p">.</span><span class="nx">assign</span><span class="p">({</span>
<span class="nx">entries</span><span class="o">:</span> <span class="s2">"main.js"</span><span class="p">,</span>
<span class="nx">plugin</span><span class="o">:</span> <span class="p">[</span><span class="nx">watchify</span><span class="p">]</span>
<span class="p">},</span> <span class="nx">watchify</span><span class="p">.</span><span class="nx">args</span><span class="p">)).</span><span class="nx">bundle</span><span class="p">().</span><span class="nx">pipe</span><span class="p">(</span><span class="nx">bundleWS</span><span class="p">);</span>
</pre></div>
<p>Ou</p>
<div class="highlight"><pre><span></span><span class="kd">var</span> <span class="nx">fs</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'fs'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">browserify</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'browserify'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">watchify</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'watchify'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">bundleWS</span> <span class="o">=</span> <span class="nx">fs</span><span class="p">.</span><span class="nx">createWriteStream</span><span class="p">(</span><span class="nx">__dirname</span> <span class="o">+</span> <span class="s1">'/bundle.js'</span><span class="p">);</span>
<span class="c1">//wrapping</span>
<span class="nx">watchify</span><span class="p">(</span><span class="nx">browserify</span><span class="p">(</span><span class="nb">Object</span><span class="p">.</span><span class="nx">assign</span><span class="p">({</span>
<span class="nx">entries</span><span class="o">:</span> <span class="s2">"main.js"</span><span class="p">,</span>
<span class="p">},</span> <span class="nx">watchify</span><span class="p">.</span><span class="nx">args</span><span class="p">))).</span><span class="nx">bundle</span><span class="p">().</span><span class="nx">pipe</span><span class="p">(</span><span class="nx">bundleWS</span><span class="p">);</span>
</pre></div>
<p>Vous avez sûrement remarqué qu'on ajoute en plus des <code>watchify.args</code> à browserify, ces arguments sont :</p>
<div class="highlight"><pre><span></span><span class="p">{</span>
<span class="nx">cache</span><span class="o">:</span> <span class="p">{},</span>
<span class="nx">packageCache</span><span class="o">:</span> <span class="p">{}</span>
<span class="p">}</span>
</pre></div>
<p>En fait, ces options permettent d'activer le cache des sources et des modules <code>NPM</code> de <code>browserify</code>. Cela permet à <code>watchify</code> de reconstruire les livrables de manière incrémentale.</p>
<p>Il est évident que la création du livrable est nettement plus rapide de cette manière, d'ailleurs <code>watchify</code> impose ces options et ne fait rien le cas échéant.</p>
<p>Seulement quelques millisecondes sont nécessaires pour reconstruire les sources grâce à la construction incrémentale :</p>
<p><img alt="Watchify" src="images/javascript/watchify.gif"></p>
<h2>Livereload</h2>
<p>Une fois les sources construites, il est pratique de recharger la page immédiatement et automatiquement. C’est là qu’intervient <code>livereload</code>, qui grâce aux <a href="https://developer.mozilla.org/fr/docs/WebSockets">WebSockets</a>, recharge la page à chaque reconstruction.</p>
<p>Nous allons utiliser <code>gulp-livereload</code> et voir ainsi comment on combine les différents <code>streams</code> :</p>
<div class="highlight"><pre><span></span><span class="kd">var</span> <span class="nx">browserify</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'browserify'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">watchify</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'watchify'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">source</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'vinyl-source-stream'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">livereload</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'gulp-livereload'</span><span class="p">);</span>
<span class="nx">browserify</span><span class="p">(</span><span class="nb">Object</span><span class="p">.</span><span class="nx">assign</span><span class="p">({</span>
<span class="nx">entries</span><span class="o">:</span> <span class="s2">"main.js"</span><span class="p">,</span>
<span class="nx">plugins</span><span class="o">:</span> <span class="p">[</span><span class="nx">watchify</span><span class="p">]</span>
<span class="p">},</span> <span class="nx">watchify</span><span class="p">.</span><span class="nx">args</span><span class="p">)).</span><span class="nx">bundle</span><span class="p">()</span>
<span class="c1">//Create Vinyl write streams from conventional write text streams</span>
<span class="p">.</span><span class="nx">pipe</span><span class="p">(</span><span class="nx">source</span><span class="p">(</span><span class="nx">__dirname</span> <span class="o">+</span> <span class="s1">'/bundle.js'</span><span class="p">));</span>
<span class="p">.</span><span class="nx">pipe</span><span class="p">(</span><span class="nx">livereload</span><span class="p">);</span>
</pre></div>
<p>Et oui, <code>gulp-livereload</code> a juste besoin d'être "pipé" pour être fonctionnel (la puissance des <code>streams</code> et de gulp...).</p>
<p>Ensuite, l'appel au script <code>livereload.js</code> doit être ajouté dans la page HTML :</p>
<div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">script</span> <span class="na">src</span><span class="o">=</span><span class="s">"http://localhost:35729/livereload.js"</span><span class="p">></</span><span class="nt">script</span><span class="p">></span>
</pre></div>
<p>Il peut aussi être chargé via un plugin navigateur comme <a href="https://chrome.google.com/webstore/detail/livereload/jnihajbhpnppcggbcgedagnkighmdlei">Livereload</a> (Chrome/Chromium) pour ne pas polluer la page.
Si HTTPS est nécéssaire (comme sur le site Mappy), préférez le plugin <a href="https://chrome.google.com/webstore/detail/remotelivereload/jlppknnillhjgiengoigajegdpieppei">RemoteLivereload</a> (Chrome/Chromium) à Livereload.</p>
<p>Au sujet d'HTTPS, voici conmment on ajoute la clé et le certificat à <code>gulp-livereload</code> :</p>
<div class="highlight"><pre><span></span><span class="p">...</span>
<span class="kd">var</span> <span class="nx">livereload</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'gulp-livereload'</span><span class="p">);</span>
<span class="nx">livereload</span><span class="p">.</span><span class="nx">listen</span><span class="p">({</span>
<span class="nx">key</span><span class="o">:</span> <span class="nx">fs</span><span class="p">.</span><span class="nx">readFileSync</span><span class="p">(</span><span class="nx">__dirname</span> <span class="o">+</span> <span class="s1">'/private.key'</span><span class="p">),</span>
<span class="nx">cert</span><span class="o">:</span> <span class="nx">fs</span><span class="p">.</span><span class="nx">readFileSync</span><span class="p">(</span><span class="nx">__dirname</span> <span class="o">+</span> <span class="s1">'/public.pem'</span><span class="p">)</span>
<span class="p">});</span>
<span class="nx">browserify</span><span class="p">(</span><span class="nb">Object</span><span class="p">.</span><span class="nx">assign</span><span class="p">({</span>
<span class="p">...</span>
</pre></div>
</div><!-- /.entry-content -->
</div><!-- /.eleven.columns -->
<div class="four columns" id="sidebar">
<h4>Pages</h4>
<ul>
</ul>
<h4>Categories</h4>
<ul>
<li><a href="./category/agile.html">Agile</a></li>
<li><a href="./category/android.html">Android</a></li>
<li><a href="./category/gis.html">GIS</a></li>
<li><a href="./category/mapping.html">Mapping</a></li>
<li><a href="./category/solr.html">Solr</a></li>
<li><a href="./category/web.html">Web</a></li>
</ul>
<h4>Tags</h4>
<ul>
<li class="tag-4"><a href="./tag/panorama.html">panorama</a></li>
<li class="tag-4"><a href="./tag/responsive.html">responsive</a></li>
<li class="tag-4"><a href="./tag/osm.html">osm</a></li>
<li class="tag-1"><a href="./tag/javascript.html">javascript</a></li>
<li class="tag-4"><a href="./tag/retrospective.html">rétrospective</a></li>
<li class="tag-4"><a href="./tag/gis.html">GIS</a></li>
<li class="tag-4"><a href="./tag/sotm.html">sotm</a></li>
<li class="tag-4"><a href="./tag/android.html">android</a></li>
<li class="tag-4"><a href="./tag/agilite.html">agilité</a></li>
<li class="tag-4"><a href="./tag/webgl.html">webgl</a></li>
<li class="tag-4"><a href="./tag/openlr.html">openlr</a></li>
<li class="tag-3"><a href="./tag/postgis.html">postGIS</a></li>
<li class="tag-3"><a href="./tag/mapnik.html">mapnik</a></li>
<li class="tag-4"><a href="./tag/abtest.html">abtest</a></li>
<li class="tag-3"><a href="./tag/leaflet.html">leaflet</a></li>
<li class="tag-4"><a href="./tag/python.html">python</a></li>
<li class="tag-3"><a href="./tag/backbone.html">backbone</a></li>
<li class="tag-1"><a href="./tag/opensource.html">opensource</a></li>
<li class="tag-1"><a href="./tag/francais.html">français</a></li>
<li class="tag-4"><a href="./tag/watch.html">watch</a></li>
<li class="tag-4"><a href="./tag/meetup.html">meetup</a></li>
<li class="tag-4"><a href="./tag/browserify.html">browserify</a></li>
<li class="tag-4"><a href="./tag/opengl.html">opengl</a></li>
<li class="tag-4"><a href="./tag/docker.html">docker</a></li>
<li class="tag-3"><a href="./tag/solr.html">solr</a></li>
<li class="tag-2"><a href="./tag/nodejs.html">node.js</a></li>
<li class="tag-2"><a href="./tag/webperfs.html">webperfs</a></li>
<li class="tag-2"><a href="./tag/english.html">english</a></li>
<li class="tag-4"><a href="./tag/livereload.html">livereload</a></li>
</ul>
<nav class="widget">
<h4>Links</h4>
<ul>
<li><a href="https://www.mappy.com/">Mappy</a></li>
<li><a href="https://play.google.com/store/apps/details?id=com.mappy.app">Appli Android</a></li>
<li><a href="https://itunes.apple.com/fr/app/mappy-itineraire-et-recherche/id313834655?mt=8">Appli iOS</a></li>
<li><a href="http://corporate.mappy.com">Blog Mappy</a></li>
<li><a href="http://corporate.mappy.com/faq/integrez-mappy/">API Mappy</a></li>
</ul>
</nav>
</div> </div><!-- /.row -->
</section>
</div><!-- /.row -->
</div><!-- /.container -->
<div class="container.nopad bg">
<footer id="credits" class="row">
<div class="seven columns left-center">
<address id="about" class="vcard body">
Proudly powered by <a href="http://getpelican.com/">Pelican</a>,
which takes great advantage of <a href="http://python.org">Python</a>.
<br />
Based on the <a target="_blank" href="http://gumbyframework.com">Gumby Framework</a>
</address>
</div>
<div class="seven columns">
<div class="row">
<ul class="socbtns">
<li><div class="btn primary"><a href="https://github.com/Mappy" target="_blank">Github</a></div></li>
<li><div class="btn twitter"><a href="https://twitter.com/Mappy" target="_blank">Twitter</a></div></li>
<li><div class="btn facebook"><a href="https://www.facebook.com/MappyOnline" target="_blank">Facebook</a></div></li>
</ul>
</div>
</div>
</footer>
</div>
</body>
</html>