Top Banner
Guia de sobrevivência JS no mundo Open Source Rio.JS Conference 2013 - Leo Balter
79

Guia de Sobrevivência JS no mundo Open Source

Dec 17, 2014

Download

Technology

Leonardo Balter

Apresentada no RioJS Conf 2013
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Guia de Sobrevivência JS no mundo Open Source

Guia de sobrevivência JS no mundo Open SourceRio.JS Conference 2013 - Leo Balter

Page 2: Guia de Sobrevivência JS no mundo Open Source

JS no mundo Open Source?

Page 3: Guia de Sobrevivência JS no mundo Open Source

JS no mundo Open Source?

JS de terceiros!

WAT?

Page 4: Guia de Sobrevivência JS no mundo Open Source

Desafio Desenvolvedor

• 1 ambiente

• 1 versão da linguagem

• sem script no nome

Page 5: Guia de Sobrevivência JS no mundo Open Source
Page 6: Guia de Sobrevivência JS no mundo Open Source

Desafio DesenvolvedorJavaScript

• Vários ambientes (mobile, desktop, geladeira)

• Chrome, Firefox, Opera

• IE 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17...

• Qual é mesmo a versão do JS?

Page 7: Guia de Sobrevivência JS no mundo Open Source
Page 8: Guia de Sobrevivência JS no mundo Open Source

JS que funciona em qualquer site, inclusive nos que não são seus!

Page 9: Guia de Sobrevivência JS no mundo Open Source

Primeiros perigos

Page 10: Guia de Sobrevivência JS no mundo Open Source

Same Origin Policy

HTTP HTTP

HTTPS HTTPS

www.foo.com/bla www.foo.com/bos

Page 11: Guia de Sobrevivência JS no mundo Open Source

Same Origin PolicyHTTP HTTPS

HTTPS HTTP

bla.foo.com bos.foo.com

Page 12: Guia de Sobrevivência JS no mundo Open Source

Como driblar o SOP?

• JSONP (Jaydson P)

• CORS (Cross Origin Resource Sharing)

• Post Message https://developer.mozilla.org/en-US/docs/DOM/window.postMessage

• easyXDM http://easyxdm.net/wp/

Page 13: Guia de Sobrevivência JS no mundo Open Source

Como se não bastasse...

Page 14: Guia de Sobrevivência JS no mundo Open Source

Peraí, um JS de terceiros está bloqueando a apresentação

Page 15: Guia de Sobrevivência JS no mundo Open Source
Page 16: Guia de Sobrevivência JS no mundo Open Source

Nunca bloquear páginas alheias

Page 17: Guia de Sobrevivência JS no mundo Open Source

<div id="fb-root"></div><script> // Load the SDK Asynchronously (function(d){ var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; if (d.getElementById(id)) {return;} js = d.createElement('script'); js.id = id; js.async = true; js.src = "//connect.facebook.net/en_US/all.js#xfbml=1"; ref.parentNode.insertBefore(js, ref); }(document));</script><div class="fb-like"></div>

Page 18: Guia de Sobrevivência JS no mundo Open Source

<div id="fb-root"></div><script> // Load the SDK Asynchronously (function(d){ var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; if (d.getElementById(id)) {return;} js = d.createElement('script'); js.id = id; js.async = true; js.src = "//connect.facebook.net/en_US/all.js#xfbml=1"; ref.parentNode.insertBefore(js, ref); }(document));</script><div class="fb-like"></div>

Page 19: Guia de Sobrevivência JS no mundo Open Source

<div id="fb-root"></div><script> // Load the SDK Asynchronously (function(d){ var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; if (d.getElementById(id)) {return;} js = d.createElement('script'); js.id = id; js.async = true; js.src = "//connect.facebook.net/en_US/all.js#xfbml=1"; ref.parentNode.insertBefore(js, ref); }(document));</script><div class="fb-like"></div>

Page 20: Guia de Sobrevivência JS no mundo Open Source

<div id="fb-root"></div><script> // Load the SDK Asynchronously (function(d){ var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; if (d.getElementById(id)) {return;} js = d.createElement('script'); js.id = id; js.async = true; js.src = "//connect.facebook.net/en_US/all.js#xfbml=1"; ref.parentNode.insertBefore(js, ref); }(document));</script><div class="fb-like"></div>

Page 21: Guia de Sobrevivência JS no mundo Open Source

<div id="fb-root"></div><script> // Load the SDK Asynchronously (function(d){ var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; if (d.getElementById(id)) {return;} js = d.createElement('script'); js.id = id; js.async = true; js.src = "//connect.facebook.net/en_US/all.js#xfbml=1"; ref.parentNode.insertBefore(js, ref); }(document));</script><div class="fb-like"></div>

Page 22: Guia de Sobrevivência JS no mundo Open Source

<div id="fb-root"></div><script> // Load the SDK Asynchronously (function(d){ var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; if (d.getElementById(id)) {return;} js = d.createElement('script'); js.id = id; js.async = true; js.src = "//connect.facebook.net/en_US/all.js#xfbml=1"; ref.parentNode.insertBefore(js, ref); }(document));</script><div class="fb-like"></div>

Page 23: Guia de Sobrevivência JS no mundo Open Source

<div id="fb-root"></div><script> // Load the SDK Asynchronously (function(d){ var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; if (d.getElementById(id)) {return;} js = d.createElement('script'); js.id = id; js.async = true; js.src = "//connect.facebook.net/en_US/all.js#xfbml=1"; ref.parentNode.insertBefore(js, ref); }(document));</script><div class="fb-like"></div>

Page 24: Guia de Sobrevivência JS no mundo Open Source

<div id="fb-root"></div><script> // Load the SDK Asynchronously (function(d){ var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; if (d.getElementById(id)) {return;} js = d.createElement('script'); js.id = id; js.async = true; js.src = "//connect.facebook.net/en_US/all.js#xfbml=1"; ref.parentNode.insertBefore(js, ref); }(document));</script><div class="fb-like"></div>

Page 25: Guia de Sobrevivência JS no mundo Open Source

<script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-xxxxxxx-x']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();</script>

Page 26: Guia de Sobrevivência JS no mundo Open Source

<script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-xxxxxxx-x']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();</script>

Page 27: Guia de Sobrevivência JS no mundo Open Source

<script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-xxxxxxx-x']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();</script>

Page 28: Guia de Sobrevivência JS no mundo Open Source

<script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-xxxxxxx-x']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();</script>

Page 29: Guia de Sobrevivência JS no mundo Open Source

<script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-xxxxxxx-x']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();</script>

Page 30: Guia de Sobrevivência JS no mundo Open Source

<script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-xxxxxxx-x']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();</script>

Page 31: Guia de Sobrevivência JS no mundo Open Source

<script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-xxxxxxx-x']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();</script>

Page 32: Guia de Sobrevivência JS no mundo Open Source

Você pode otimizar o seu Google Analytics

http://mathiasbynens.be/notes/async-analytics-snippet

Page 34: Guia de Sobrevivência JS no mundo Open Source

Antipatterns comuns

• Muitas globais poluindo o contexto da página

• Modificar os objetos nativos do JS

• JavaScript inline

• document.write()

Page 35: Guia de Sobrevivência JS no mundo Open Source

“Knowledge of anti-patterns is critical for success. Once we are able to

recognize such anti-patterns, we're able to refactor our code to negate

them so that the overall quality of our solutions improves instantly.”

http://addyosmani.com/resources/essentialjsdesignpatterns/book/#antipatterns

Addy Osmani:

Page 36: Guia de Sobrevivência JS no mundo Open Source

“Conhecer os antipadrões é crítico para alcançar sucesso. Uma vez que

conseguimos reconhecer os antipadrões, conseguimos refatorar nosso código para evitá-los de modo

que a qualidade geral de nossas soluções melhore instantaneamente.”

http://addyosmani.com/resources/essentialjsdesignpatterns/book/#antipatterns

Addy Osmani (em pt-BR):

Page 37: Guia de Sobrevivência JS no mundo Open Source

Agora para tudo e vamos falar de jQuery!

Page 38: Guia de Sobrevivência JS no mundo Open Source
Page 39: Guia de Sobrevivência JS no mundo Open Source
Page 40: Guia de Sobrevivência JS no mundo Open Source

Repositório de Plugins jQuery

Como faz?

Page 41: Guia de Sobrevivência JS no mundo Open Source

Passo a passo

• Após criar um repositório no GitHub, crie um webhook apontando para a URL:

http://plugins.jquery.com/postreceive-hook

Page 43: Guia de Sobrevivência JS no mundo Open Source
Page 44: Guia de Sobrevivência JS no mundo Open Source
Page 45: Guia de Sobrevivência JS no mundo Open Source
Page 46: Guia de Sobrevivência JS no mundo Open Source

???

Page 47: Guia de Sobrevivência JS no mundo Open Source

Você ainda não tem conta no GitHub?

É de graça!

Page 48: Guia de Sobrevivência JS no mundo Open Source

Passo a passo

• Repositório GitHub WebHook para http://plugins.jquery.com/postreceive-hook

• Criar um arquivo de manifesto “seu-plugin.jquery.json”

Page 49: Guia de Sobrevivência JS no mundo Open Source

Arquivo de Manifesto?http://plugins.jquery.com/docs/publish/

Page 50: Guia de Sobrevivência JS no mundo Open Source

{ "name": "color", "title": "jQuery Color", "description": "jQuery plugin for color manipulation and animation support.", "keywords": [ "color", "animation" ], "version": "2.1.2", "author": { "name": "jQuery Foundation and other contributors", "url": "https://github.com/jquery/jquery-color/blob/2.1.2/AUTHORS.txt" }, "maintainers": [ { "name": "Corey Frang", "email": "[email protected]", "url": "http://gnarf.net" } ], "licenses": [ { "type": "MIT", "url": "https://github.com/jquery/jquery-color/blob/2.1.2/MIT-LICENSE.txt" } ], "bugs": "https://github.com/jquery/jquery-color/issues", "homepage": "https://github.com/jquery/jquery-color", "docs": "https://github.com/jquery/jquery-color", "download": "http://code.jquery.com/#color", "dependencies": { "jquery": ">=1.5" }}

Page 51: Guia de Sobrevivência JS no mundo Open Source

{ "name": "color", "title": "jQuery Color", "description": "jQuery plugin for color manipulation and animation support.", "keywords": [ "color", "animation" ], "version": "2.1.2", "author": { "name": "jQuery Foundation and other contributors", "url": "https://github.com/jquery/jquery-color/blob/2.1.2/AUTHORS.txt" }, "maintainers": [ { "name": "Corey Frang", "email": "[email protected]", "url": "http://gnarf.net" } ], "licenses": [ { "type": "MIT", "url": "https://github.com/jquery/jquery-color/blob/2.1.2/MIT-LICENSE.txt" } ], "bugs": "https://github.com/jquery/jquery-color/issues", "homepage": "https://github.com/jquery/jquery-color", "docs": "https://github.com/jquery/jquery-color", "download": "http://code.jquery.com/#color", "dependencies": { "jquery": ">=1.5" }}

Page 52: Guia de Sobrevivência JS no mundo Open Source

{ "name": "color", "title": "jQuery Color", "description": "jQuery plugin for color manipulation and animation support.", "keywords": [ "color", "animation" ], "version": "2.1.2", "author": { "name": "jQuery Foundation and other contributors", "url": "https://github.com/jquery/jquery-color/blob/2.1.2/AUTHORS.txt" }, "maintainers": [ { "name": "Corey Frang", "email": "[email protected]", "url": "http://gnarf.net" } ], "licenses": [ { "type": "MIT", "url": "https://github.com/jquery/jquery-color/blob/2.1.2/MIT-LICENSE.txt" } ], "bugs": "https://github.com/jquery/jquery-color/issues", "homepage": "https://github.com/jquery/jquery-color", "docs": "https://github.com/jquery/jquery-color", "download": "http://code.jquery.com/#color", "dependencies": { "jquery": ">=1.5" }}

Page 53: Guia de Sobrevivência JS no mundo Open Source

{ "name": "color", "title": "jQuery Color", "description": "jQuery plugin for color manipulation and animation support.", "keywords": [ "color", "animation" ], "version": "2.1.2", "author": { "name": "jQuery Foundation and other contributors", "url": "https://github.com/jquery/jquery-color/blob/2.1.2/AUTHORS.txt" }, "maintainers": [ { "name": "Corey Frang", "email": "[email protected]", "url": "http://gnarf.net" } ], "licenses": [ { "type": "MIT", "url": "https://github.com/jquery/jquery-color/blob/2.1.2/MIT-LICENSE.txt" } ], "bugs": "https://github.com/jquery/jquery-color/issues", "homepage": "https://github.com/jquery/jquery-color", "docs": "https://github.com/jquery/jquery-color", "download": "http://code.jquery.com/#color", "dependencies": { "jquery": ">=1.5" }}

Page 54: Guia de Sobrevivência JS no mundo Open Source

{ "name": "color", "title": "jQuery Color", "description": "jQuery plugin for color manipulation and animation support.", "keywords": [ "color", "animation" ], "version": "2.1.2", "author": { "name": "jQuery Foundation and other contributors", "url": "https://github.com/jquery/jquery-color/blob/2.1.2/AUTHORS.txt" }, "maintainers": [ { "name": "Corey Frang", "email": "[email protected]", "url": "http://gnarf.net" } ], "licenses": [ { "type": "MIT", "url": "https://github.com/jquery/jquery-color/blob/2.1.2/MIT-LICENSE.txt" } ], "bugs": "https://github.com/jquery/jquery-color/issues", "homepage": "https://github.com/jquery/jquery-color", "docs": "https://github.com/jquery/jquery-color", "download": "http://code.jquery.com/#color", "dependencies": { "jquery": ">=1.5" }}

Page 55: Guia de Sobrevivência JS no mundo Open Source

{ "name": "color", "title": "jQuery Color", "description": "jQuery plugin for color manipulation and animation support.", "keywords": [ "color", "animation" ], "version": "2.1.2", "author": { "name": "jQuery Foundation and other contributors", "url": "https://github.com/jquery/jquery-color/blob/2.1.2/AUTHORS.txt" }, "maintainers": [ { "name": "Corey Frang", "email": "[email protected]", "url": "http://gnarf.net" } ], "licenses": [ { "type": "MIT", "url": "https://github.com/jquery/jquery-color/blob/2.1.2/MIT-LICENSE.txt" } ], "bugs": "https://github.com/jquery/jquery-color/issues", "homepage": "https://github.com/jquery/jquery-color", "docs": "https://github.com/jquery/jquery-color", "download": "http://code.jquery.com/#color", "dependencies": { "jquery": ">=1.5" }}

Page 56: Guia de Sobrevivência JS no mundo Open Source

{ "name": "color", "title": "jQuery Color", "description": "jQuery plugin for color manipulation and animation support.", "keywords": [ "color", "animation" ], "version": "2.1.2", "author": { "name": "jQuery Foundation and other contributors", "url": "https://github.com/jquery/jquery-color/blob/2.1.2/AUTHORS.txt" }, "maintainers": [ { "name": "Corey Frang", "email": "[email protected]", "url": "http://gnarf.net" } ], "licenses": [ { "type": "MIT", "url": "https://github.com/jquery/jquery-color/blob/2.1.2/MIT-LICENSE.txt" } ], "bugs": "https://github.com/jquery/jquery-color/issues", "homepage": "https://github.com/jquery/jquery-color", "docs": "https://github.com/jquery/jquery-color", "download": "http://code.jquery.com/#color", "dependencies": { "jquery": ">=1.5" }}

Page 57: Guia de Sobrevivência JS no mundo Open Source

{ "name": "color", "title": "jQuery Color", "description": "jQuery plugin for color manipulation and animation support.", "keywords": [ "color", "animation" ], "version": "2.1.2", "author": { "name": "jQuery Foundation and other contributors", "url": "https://github.com/jquery/jquery-color/blob/2.1.2/AUTHORS.txt" }, "maintainers": [ { "name": "Corey Frang", "email": "[email protected]", "url": "http://gnarf.net" } ], "licenses": [ { "type": "MIT", "url": "https://github.com/jquery/jquery-color/blob/2.1.2/MIT-LICENSE.txt" } ], "bugs": "https://github.com/jquery/jquery-color/issues", "homepage": "https://github.com/jquery/jquery-color", "docs": "https://github.com/jquery/jquery-color", "download": "http://code.jquery.com/#color", "dependencies": { "jquery": ">=1.5" }}

Page 58: Guia de Sobrevivência JS no mundo Open Source
Page 59: Guia de Sobrevivência JS no mundo Open Source
Page 60: Guia de Sobrevivência JS no mundo Open Source

O que queremos automatizar?

Page 61: Guia de Sobrevivência JS no mundo Open Source
Page 62: Guia de Sobrevivência JS no mundo Open Source
Page 63: Guia de Sobrevivência JS no mundo Open Source

npm uninstall -g grunt

Se você tem instalada a versão 0.3.x ou anterior

do Grunt:

Page 64: Guia de Sobrevivência JS no mundo Open Source
Page 65: Guia de Sobrevivência JS no mundo Open Source

‣ npm install -g grunt-cli

Page 66: Guia de Sobrevivência JS no mundo Open Source

‣ npm install -g grunt-cli

‣ npm install -g grunt-init

Page 67: Guia de Sobrevivência JS no mundo Open Source

‣ npm install -g grunt-cli

‣ npm install -g grunt-init

‣ git clone [email protected]:gruntjs/grunt-init-jquery.git ~/.grunt-init/jquery

Page 68: Guia de Sobrevivência JS no mundo Open Source

‣ npm install -g grunt-cli

‣ npm install -g grunt-init

‣ git clone [email protected]:gruntjs/grunt-init-jquery.git ~/.grunt-init/jquery

‣ # entre na pasta que vai criar o seu projeto

Page 69: Guia de Sobrevivência JS no mundo Open Source

‣ npm install -g grunt-cli

‣ npm install -g grunt-init

‣ git clone [email protected]:gruntjs/grunt-init-jquery.git ~/.grunt-init/jquery

‣ # entre na pasta que vai criar o seu projeto

‣ grunt-init jquery

Page 70: Guia de Sobrevivência JS no mundo Open Source

‣ npm install -g grunt-cli

‣ npm install -g grunt-init

‣ git clone [email protected]:gruntjs/grunt-init-jquery.git ~/.grunt-init/jquery

‣ # entre na pasta que vai criar o seu projeto

‣ grunt-init jquery

‣ npm install grunt --save-dev

Page 71: Guia de Sobrevivência JS no mundo Open Source

‣ npm install -g grunt-cli

‣ npm install -g grunt-init

‣ git clone [email protected]:gruntjs/grunt-init-jquery.git ~/.grunt-init/jquery

‣ # entre na pasta que vai criar o seu projeto

‣ grunt-init jquery

‣ npm install grunt --save-dev

‣ npm install

Page 72: Guia de Sobrevivência JS no mundo Open Source
Page 73: Guia de Sobrevivência JS no mundo Open Source

‣ # brincou com o plugin?

Page 74: Guia de Sobrevivência JS no mundo Open Source

‣ # brincou com o plugin?

‣ # fez commit?

Page 75: Guia de Sobrevivência JS no mundo Open Source

‣ # brincou com o plugin?

‣ # fez commit?

‣ git tag 0.1.0

Page 76: Guia de Sobrevivência JS no mundo Open Source

‣ # brincou com o plugin?

‣ # fez commit?

‣ git tag 0.1.0

‣ git push origin --tags

Page 77: Guia de Sobrevivência JS no mundo Open Source
Page 78: Guia de Sobrevivência JS no mundo Open Source
Page 79: Guia de Sobrevivência JS no mundo Open Source

Obrigado!@leobalter