Finally, Professional
Frontend dev with:
ReactJS, Webpack & Symfony
♥’s
> Lead of the Symfony documentation team

> KnpLabs US - Symfony consulting, training & kumbaya
> Writer for KnpUniversity.com:
PHP & Symfony screencasts
packed with puns, unrelated
(but entertaining) illustrations
and coding challenges!
> Husband of the much more
talented @leannapelham
knpuniversity.com
twitter.com/weaverryan
¡Hola!
♥’s
Finally, Professional
Frontend dev with:
ReactJS, Webpack & Symfony
, ReactJS, webpack
@weaverryan
All of Modern JavaScript in
45 minutes!
ES6
the 12 new JS things they invent
during this presentation
, ES2015 , ECMAScript
, Babel
, NodeJS
npm , JSX …
… and of course …
Modern JavaScript
is a lot like…
@weaverryan
Game of Thrones
JavaScript
@weaverryan
GoT
Countless libraries and
competing standards fighting
for influence
Countless characters and
completing factions fighting
for influence
@weaverryan
You spent 6 months building
your site in <Cool.JS> only
to read on Twitter that:
“no self-respecting dev
uses that crap anymore”
That character you love and
followed for 2 seasons, was
just unceremoniously decapitated
JavaScript
GoT
@weaverryan
Plain, boring old JavaScript
JavaScript is a
(weird) language
IT JUST HAPPENS THAT BROWSERS CAN
EXECUTE THAT LANGUAGE
@weaverryan
// yay.js

var message = 'I like Java...Script';



console.log(message);
> node yay.js
I like Java...Script
NodeJS: server-side
JavaScript engine
npm: Composer
for NodeJS
@weaverryan
Follow along with the real code:
github.com/weaverryan/symfonycat-js
(hint: look at the history, each
thing we do is its own commit)
// web/js/productApp.js

var products = [

'Sheer Shears',

'Wool Hauling Basket',

'After-Shear (Fresh Cut Grass)',

'After-Shear (Morning Dew)'

];



var loopThroughProducts = function(callback) {

for (var i = 0, length = products.length; i < length; i++) {

callback(products[i]);

}

};



loopThroughProducts(function(product) {

console.log('Product: '+product);

});
{# app/Resources/views/default/products.html.twig' #}
<script src="{{ asset('js/productApp.js') }}"></script>
our store for
sheep (baaaa)
class ProductCollection

{

constructor(products) {

this.products = products;

}

}



let collection = new ProductCollection([

'Sheer Shears',

'Wool Hauling Basket',

'After-Shear (Fresh Cut Grass)',

'After-Shear (Morning Dew)',

]);

let prods = collection.getProducts();



let loopThroughProducts = function(callback) {

for (let i = 0, length = prods.length; i < length; i++) {

callback(collection.getProduct(i));

}

};



loopThroughProducts(product => console.log('Product: '+product));
what language
is this?
JavaScript
@weaverryan
ECMAScript
The official name of standard JavaScript
ES6/ES2015/Harmony
The 6th accepted (so official) version
of ECMAScript
class ProductCollection

{

constructor(products) {

this.products = products;

}

}



let collection = new ProductCollection([

'Sheer Shears',

'Wool Hauling Basket',

'After-Shear (Fresh Cut Grass)',

'After-Shear (Morning Dew)',

]);

let prods = collection.getProducts();



let loopThroughProducts = function(callback) {

for (let i = 0, length = prods.length; i < length; i++) {

callback(collection.getProduct(i));

}

};



loopThroughProducts(product => console.log('Product: '+product));
But will it run in a browser???
Maybe!
class ProductCollection

{

constructor(products) {

this.products = products;

}

}



let collection = new ProductCollection([

'Sheer Shears',

'Wool Hauling Basket',

'After-Shear (Fresh Cut Grass)',

'After-Shear (Morning Dew)',

]);

let prods = collection.getProducts();



let loopThroughProducts = function(callback) {

for (let i = 0, length = prods.length; i < length; i++) {

callback(collection.getProduct(i));

}

};



loopThroughProducts(product => console.log(product));
Proper class and
inheritance syntax
let: similar to var,
but different
function (product) {

console.log(product);

}
Now we just need to
wait 5 years for the
crappiest browsers
to support this
@weaverryan
Babel
… or do we?
A JS transpiler!
Babel is a NodeJS binary…
{

"name": "js-tutorial",

"version": "1.0.0"

}
1) Make a package.json file
2) Download babel
> npm install --save-dev babel-cli
@weaverryan
> ./node_modules/.bin/babel 
web/js/productApp.js 
-o web/builds/productApp.js
{# app/Resources/views/default/products.html.twig' #}
<script src="{{ asset('builds/productApp.js') }}"></script>
@weaverryan
> ./node_modules/.bin/babel 
web/js/productApp.js 
-o web/builds/productApp.js
{# app/Resources/views/default/products.html.twig' #}
<script src="{{ asset('builds/productApp.js') }}"></script>
But, this made no changes
js/productApp.js == builds/productApp.js
@weaverryan
Babel can transpile anything
CoffeeScript --> JavaScript
Coffee --> Tea
ES6 JS --> ES5 JS
* Each transformation is called a preset
1) Install the es2015 preset library
2) Add a .babelrc file
> npm install --save-dev babel-preset-es2015
{

"presets": [

"es2015"

]

}
@weaverryan
> ./node_modules/.bin/babel 
web/js/productApp.js 
-o web/builds/productApp.js
loopThroughProducts(

product => console.log('Product: '+product)

);
loopThroughProducts(function (product) {

return console.log('Product: ' + product);

});

source:
built:
But we can use new (or experimental) features now
@weaverryan
Modern JavaScript
has a build step
Big Takeaway #1:
@weaverryan
New to ES6:
JavaScript Modules!
The Classic Problem:
If you want to organize your JS into
multiple files, you need to manually
include all those script tags!
@weaverryan
// web/js/ProductCollection.js


class ProductCollection

{

constructor(products) {

this.products = products;

}



getProducts() {

return this.products;

}



getProduct(i) {

return this.products[i];

}

}



export ProductCollection;

@weaverryan
// web/js/productApp.js



import ProductCollection from './ProductCollection';



var collection = new ProductCollection([

'Sheer Shears',

'Wool Hauling Basket',

'After-Shear (Fresh Cut Grass)',

'After-Shear (Morning Dew)',

]);



// ...
{# app/Resources/views/default/products.html.twig' #}
<script src="{{ asset('builds/productApp.js') }}"></script>
// web/js/productApp.js



import ProductCollection from './ProductCollection';



var collection = new ProductCollection([

'Sheer Shears',

'Wool Hauling Basket',

'After-Shear (Fresh Cut Grass)',

'After-Shear (Morning Dew)',

]);



// ...
{# app/Resources/views/default/products.html.twig' #}
<script src="{{ asset('builds/productApp.js') }}"></script>
> ./node_modules/.bin/babel 
web/js/productApp.js 
-o web/builds/productApp.js
Module loading in a
browser is hard to do
@weaverryan
@weaverryan
Introducing…
@weaverryan
Webpack!
• bundler
• module loader
• all-around nice guy
Install webpack
> npm install --save-dev webpack
@weaverryan
Use require instead of import/export *
* I’ll tell you why later
// web/js/ProductCollection.js

class ProductCollection

{

// ...

}



module.exports = ProductCollection;

// web/js/productApp.js

var ProductCollection = require('./ProductCollection');



// ...
Go webpack Go!
> ./node_modules/.bin/webpack 
web/js/productApp.js 
web/builds/productApp.js
The one built file contains
the code from both source files
Optional config to make it easier to use:
// webpack.config.js

module.exports = {

entry: {

product: './web/js/productApp.js'

},

output: {

path: './web/builds',

filename: '[name].js',

publicPath: '/builds/'

}

};

builds/product.js
{# app/Resources/views/default/products.html.twig' #}
<script src="{{ asset('builds/product.js') }}"></script>
> ./node_modules/.bin/webpack
@weaverryan
Wait!
We lost our ES6->ES5
transformation!!!
@weaverryan
Hey webpack!
Yo! When you load .js files,
can you run them through
Babel for me?
- kthxbai <3 Ryan
webpack loaders allow you to transform files
as they’re loaded
1) Install the babel-loader
2) Activate the loader in webpack.config.js
> npm install --save-dev babel-loader
module.exports = {

// ...

module: {

loaders: [

{

test: /.js$/,

loader: "babel-loader",
exclude: /node_modules/

}

]

}

};

> ./node_modules/.bin/webpack
@weaverryan
Module loading
+
ES6 Support
Use import/export now if you prefer
// web/js/ProductCollection.js

class ProductCollection

{

// ...

}



export default ProductCollection;

// web/js/productApp.js

import ProductCollection from './ProductCollection';



// ...

> ./node_modules/.bin/webpack
@weaverryan
@weaverryan
Dev Tools
… because life is too short to run
webpack after every change you make
> npm install webpack-dev-server --save-dev
> ./node_modules/.bin/webpack-dev-server 
--content-base=./web/
@weaverryan
1) Install the webpack-dev-server
2) Run that!
http://localhost:8080
- static assets are served
- compiled assets are served dynamically
Wait!
@weaverryan
Don’t I need to update all my script tags?
<script

src="{{ asset('builds/product.js') }}">
<script

src="http://localost:8080/builds/product.js">
# app/config/config.yml

framework:

assets:

base_url: http://localhost:8080
Boom!
@weaverryan
… or the real solution
@weaverryan
# app/config/parameters.yml

parameters:

# ...

use_webpack_dev_server: true

class AppKernel extends Kernel

{

// ...



public function registerContainerConfiguration(LoaderInterface $loader)

{

// ...



$loader->load(function(ContainerBuilder $container) {

if ($container->getParameter('use_webpack_dev_server')) {

$container->loadFromExtension('framework', [

'assets' => [

'base_url' => 'http://localhost:8080'

]

]);

}

});

}

}
… or the real solution
@weaverryan
@weaverryan
Status Update
we can:
• use ES6 features
• import and export modules
@weaverryan
CSS: An un-handled
dependency of your JS app
Could we do this?
// web/js/productApp.js

import ProductCollection from './ProductCollection';



// could this somehow load that CSS for us?

import '../css/productApp.css';



// ...
Loader!
module.exports = {

// ...

module: {

loaders: [

{

test: /.js$/,

exclude: /node_modules/,

loader: "babel-loader"

}

]

}

};

webpack loaders allow you to transform files
as they’re loaded
Remember:
1) Install the css-loader
2) Activate the loader just for this file
> npm install css-loader --save-dev
import 'css!../css/productApp.css';
this transforms the CSS into a JS data-
structure… but does nothing with it
1) Install the style-loader
> npm install style-loader --save-dev
import 'style!css!../css/productApp.css';
inlines the CSS on the page in
a style tag
2) Activate both loaders for this file
Yes,
@weaverryan
the one JS file now holds the contents of
two JS files and a CSS ���le
{# app/Resources/views/default/products.html.twig' #}
<script src="{{ asset('builds/product.js') }}"></script>
Move the loader to config to simplify
import '../css/productApp.css';
// webpack.config.js
module.exports = {

// ...

module: {

loaders: [

// ...

{

test: /.css$/,

loader: "style!css"

}

]

},

};

@weaverryan
Ah, but what should my
image paths look like?
/* productApp.css */

.product-price {

color: green;

background-image: url('../images/logo.png');

}

http://example.com/products/5/photos/../images/logo.png
http://example.com/products/5/photos
This broke webpack!
Yes, webpack parses the CSS and tries to
load file imports and url() references
(i.e. to images & fonts)
1) Install the file-loader & url-loader
> npm install file-loader url-loader --save-dev
2) Activate the loader for .png files
// webpack.config.js
// ...
loaders: [

// ...

{

test: /.png/,

loader: "url-loader?limit=10000"

}

]

{

test: /.png/,

loader: "url-loader?limit=10000"

}
For .png files < 10kb
image is turned into a “data url”
and inlined in the CSS
For .png files > 10kb
image is copied to builds/ and the
new URL is written into the CSS
Stop
@weaverryan
thinking of your JavaScript as
random code that executes
Start
@weaverryan
thinking of your JavaScript as
a single application with dependencies
that are all packaged up together
@weaverryan
Unleashing the Power of
NodeJS and ReactJS
Like Composer,
NodeJS has a lot of
third-party libraries
@weaverryan
… and we can use them
lodash
@weaverryan
JavaScript utility library
1) Install it
> npm install lodash --save-dev
2) Use it
// web/js/productApp.js

// ...

import _ from 'lodash';



_.each(collection.getProducts(), function(product) {

// ...

});
@weaverryan
'./ProductCollection'
vs
'lodash'
@weaverryan
ReactJS
// web/js/productApp.js

import React from 'react';

import ReactDOM from 'react-dom';



var ProductApp = React.createClass({

render: function() {

return (

<h1>Yay!</h1>

)

}

});
??????
$(document).ready(function() {

ReactDOM.render(

<ProductApp/>,

document.getElementById('product-app')

);

});


<ProductApp myName="Ryan" /> 

JSX
React.createElement(

ProductApp,

{

myName: "Ryan"

}

)
This is not real EcmaScript,
but babel can handle it
… but it doesn’t yet
1) Install the babel preset
> npm install --save-dev babel-preset-react
2) Add the preset in .babelrc
@weaverryan
{

"presets": [

"es2015",

"react"

]

}
… nope - still not working
// productApp.js

import React from 'react';

import ReactDOM from 'react-dom';



var ProductApp = React.createClass({

render: function() {

return (

<h1>Yay!</h1>

)

}

});



$(document).ready(function() {

ReactDOM.render(

<ProductApp/>,

document.getElementById('product-app')

);

});

> npm install --save-dev react react-dom
… nope - still not working
It’s alive, but huge!
*file size would be much smaller in reality,
due to missing uglify and other production settings
@weaverryan
ReactJS is pretty easy
The setup to get here
is tough
React-101: props
@weaverryan
// web/js/Components/ProductList.js

import React from 'react';



var ProductList = React.createClass({

// ...

});



module.exports = ProductList;

// web/js/productApp.js

import ReactDOM from 'react-dom';

import ProductList from './Components/ProductList';



$(document).ready(function() {

ReactDOM.render(

<ProductList message="Great Products!" />,

document.getElementById('product-app')

);

});

It’s a prop!
// web/js/Components/ProductList.js

import React from 'react';



var ProductList = React.createClass({

render: function() {

return (

<h1>{this.props.message}</h1>

)

}

});



module.exports = ProductList;

hello again prop!
collection props
@weaverryan
// web/js/productApp.js



var startingProducts = [

'Sheer Shears',

'Wool Hauling Basket',

'After-Shear (Fresh Cut Grass)',

'After-Shear (Morning Dew)',

];



$(document).ready(function() {

ReactDOM.render(

<ProductList initialProducts={startingProducts} />,

document.getElementById('product-app')

);

});

// web/js/Components/ProductApp.js

import _ from 'lodash';



var ProductList = React.createClass({

render: function() {

var productRows = [];

_.each(this.props.initialProducts, function(product) {

productRows.push(

<tr>

<td>{product}</td>

<td className="product-price">

{Math.round(Math.random()*50)}

</td>

</tr>

);

});



// ...

}

});
// web/js/Components/ProductApp.js

import _ from 'lodash';



var ProductList = React.createClass({

render: function() {

var productRows = [];

// ...



return (

<div>

<h1>{this.props.message}</h1>



<table className="table">

<tbody>{productRows}</tbody>

</table>

</div>

)

}

});
React 101: initial data
@weaverryan
// web/js/productApp.js



var startingProducts = [

'Sheer Shears',

'Wool Hauling Basket',

'After-Shear (Fresh Cut Grass)',

'After-Shear (Morning Dew)',

];



$(document).ready(function() {

ReactDOM.render(

<ProductList initialProducts={startingProducts} />,

document.getElementById('product-app')

);

});

@weaverryan
{# app/Resources/views/default/products.html.twig #}

<script>

window.startingProducts = [

'Sheer Shears',

'Wool Hauling Basket',

'After-Shear (Fresh Cut Grass)',

'After-Shear (Morning Dew)',

];

</script>
// web/js/productApp.js

var startingProducts = window.startingProducts;



$(document).ready(function() {

ReactDOM.render(

<ProductApp initialProducts={startingProducts} />,

document.getElementById('product-app')

);

});
React 101: state
@weaverryan
var ProductApp = React.createClass({

render: function() {

return (

<div>

<h1>{this.props.message}</h1>

</div>

)

}

});
var ProductApp = React.createClass({

render: function() {

return (

<div>

<h1>{this.state.message}</h1>
<button onClick={this.handleClick}>

New Message

</button>

</div>

)

}

});
var ProductApp = React.createClass({


getInitialState: function() {

return {

message: 'Product List!';

}

}



// ...

}
var ProductApp = React.createClass({

render: function() {

return (

<div>

<h1>{this.state.message}</h1>
<button onClick={this.handleClick}>

New Message

</button>

</div>

)

}

});
var ProductApp = React.createClass({



handleClick: function(e) {

e.preventDefault();



this.setState({

message: 'New Message!'

})

},



// ...

}
@weaverryan
Putting it all together
@weaverryan
ES6/ES2015/ECMAScript 2015
The newest version of Javascript,
not supported by all browsers
@weaverryan
Babel
A tool that can transform JavaScript
to different JavaScript
presets
A) ES6 js to “old” JS
B) JSX to raw JS
@weaverryan
Webpack
A tool that follows imports to bundle
JavaScript, CSS, and anything else you
dream up into one JavaScript package
loaders
A) JS through Babel
B) CSS to inlined styles
C) images copied, paths used
@weaverryan
ReactJS
A nifty frontend framework where you pass
around and render props (immutable)
and state (mutable)
… but the JavaScript
world moves quickly…
Ryan Weaver
@weaverryan
THANK YOU!

Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

  • 1.
    Finally, Professional Frontend devwith: ReactJS, Webpack & Symfony ♥’s
  • 2.
    > Lead ofthe Symfony documentation team
 > KnpLabs US - Symfony consulting, training & kumbaya > Writer for KnpUniversity.com: PHP & Symfony screencasts packed with puns, unrelated (but entertaining) illustrations and coding challenges! > Husband of the much more talented @leannapelham knpuniversity.com twitter.com/weaverryan ¡Hola!
  • 3.
    ♥’s Finally, Professional Frontend devwith: ReactJS, Webpack & Symfony
  • 4.
    , ReactJS, webpack @weaverryan Allof Modern JavaScript in 45 minutes! ES6 the 12 new JS things they invent during this presentation , ES2015 , ECMAScript , Babel , NodeJS npm , JSX … … and of course …
  • 5.
    Modern JavaScript is alot like… @weaverryan Game of Thrones
  • 6.
    JavaScript @weaverryan GoT Countless libraries and competingstandards fighting for influence Countless characters and completing factions fighting for influence
  • 7.
    @weaverryan You spent 6months building your site in <Cool.JS> only to read on Twitter that: “no self-respecting dev uses that crap anymore” That character you love and followed for 2 seasons, was just unceremoniously decapitated JavaScript GoT
  • 8.
  • 9.
    JavaScript is a (weird)language IT JUST HAPPENS THAT BROWSERS CAN EXECUTE THAT LANGUAGE
  • 10.
    @weaverryan // yay.js
 var message= 'I like Java...Script';
 
 console.log(message); > node yay.js I like Java...Script NodeJS: server-side JavaScript engine npm: Composer for NodeJS
  • 11.
    @weaverryan Follow along withthe real code: github.com/weaverryan/symfonycat-js (hint: look at the history, each thing we do is its own commit)
  • 12.
    // web/js/productApp.js
 var products= [
 'Sheer Shears',
 'Wool Hauling Basket',
 'After-Shear (Fresh Cut Grass)',
 'After-Shear (Morning Dew)'
 ];
 
 var loopThroughProducts = function(callback) {
 for (var i = 0, length = products.length; i < length; i++) {
 callback(products[i]);
 }
 };
 
 loopThroughProducts(function(product) {
 console.log('Product: '+product);
 }); {# app/Resources/views/default/products.html.twig' #} <script src="{{ asset('js/productApp.js') }}"></script> our store for sheep (baaaa)
  • 14.
    class ProductCollection
 {
 constructor(products) {
 this.products= products;
 }
 }
 
 let collection = new ProductCollection([
 'Sheer Shears',
 'Wool Hauling Basket',
 'After-Shear (Fresh Cut Grass)',
 'After-Shear (Morning Dew)',
 ]);
 let prods = collection.getProducts();
 
 let loopThroughProducts = function(callback) {
 for (let i = 0, length = prods.length; i < length; i++) {
 callback(collection.getProduct(i));
 }
 };
 
 loopThroughProducts(product => console.log('Product: '+product)); what language is this? JavaScript
  • 15.
    @weaverryan ECMAScript The official nameof standard JavaScript ES6/ES2015/Harmony The 6th accepted (so official) version of ECMAScript
  • 16.
    class ProductCollection
 {
 constructor(products) {
 this.products= products;
 }
 }
 
 let collection = new ProductCollection([
 'Sheer Shears',
 'Wool Hauling Basket',
 'After-Shear (Fresh Cut Grass)',
 'After-Shear (Morning Dew)',
 ]);
 let prods = collection.getProducts();
 
 let loopThroughProducts = function(callback) {
 for (let i = 0, length = prods.length; i < length; i++) {
 callback(collection.getProduct(i));
 }
 };
 
 loopThroughProducts(product => console.log('Product: '+product)); But will it run in a browser??? Maybe!
  • 17.
    class ProductCollection
 {
 constructor(products) {
 this.products= products;
 }
 }
 
 let collection = new ProductCollection([
 'Sheer Shears',
 'Wool Hauling Basket',
 'After-Shear (Fresh Cut Grass)',
 'After-Shear (Morning Dew)',
 ]);
 let prods = collection.getProducts();
 
 let loopThroughProducts = function(callback) {
 for (let i = 0, length = prods.length; i < length; i++) {
 callback(collection.getProduct(i));
 }
 };
 
 loopThroughProducts(product => console.log(product)); Proper class and inheritance syntax let: similar to var, but different function (product) {
 console.log(product);
 }
  • 18.
    Now we justneed to wait 5 years for the crappiest browsers to support this
  • 19.
    @weaverryan Babel … or dowe? A JS transpiler!
  • 20.
    Babel is aNodeJS binary… {
 "name": "js-tutorial",
 "version": "1.0.0"
 } 1) Make a package.json file 2) Download babel > npm install --save-dev babel-cli @weaverryan
  • 21.
    > ./node_modules/.bin/babel web/js/productApp.js -o web/builds/productApp.js {# app/Resources/views/default/products.html.twig' #} <script src="{{ asset('builds/productApp.js') }}"></script> @weaverryan
  • 22.
    > ./node_modules/.bin/babel web/js/productApp.js -o web/builds/productApp.js {# app/Resources/views/default/products.html.twig' #} <script src="{{ asset('builds/productApp.js') }}"></script> But, this made no changes js/productApp.js == builds/productApp.js @weaverryan
  • 23.
    Babel can transpileanything CoffeeScript --> JavaScript Coffee --> Tea ES6 JS --> ES5 JS * Each transformation is called a preset
  • 24.
    1) Install thees2015 preset library 2) Add a .babelrc file > npm install --save-dev babel-preset-es2015 {
 "presets": [
 "es2015"
 ]
 } @weaverryan
  • 25.
    > ./node_modules/.bin/babel web/js/productApp.js -o web/builds/productApp.js loopThroughProducts(
 product => console.log('Product: '+product)
 ); loopThroughProducts(function (product) {
 return console.log('Product: ' + product);
 });
 source: built:
  • 26.
    But we canuse new (or experimental) features now @weaverryan Modern JavaScript has a build step Big Takeaway #1:
  • 27.
  • 28.
    The Classic Problem: Ifyou want to organize your JS into multiple files, you need to manually include all those script tags! @weaverryan
  • 29.
    // web/js/ProductCollection.js 
 class ProductCollection
 {
 constructor(products){
 this.products = products;
 }
 
 getProducts() {
 return this.products;
 }
 
 getProduct(i) {
 return this.products[i];
 }
 }
 
 export ProductCollection;
 @weaverryan
  • 30.
    // web/js/productApp.js
 
 import ProductCollectionfrom './ProductCollection';
 
 var collection = new ProductCollection([
 'Sheer Shears',
 'Wool Hauling Basket',
 'After-Shear (Fresh Cut Grass)',
 'After-Shear (Morning Dew)',
 ]);
 
 // ... {# app/Resources/views/default/products.html.twig' #} <script src="{{ asset('builds/productApp.js') }}"></script>
  • 31.
    // web/js/productApp.js
 
 import ProductCollectionfrom './ProductCollection';
 
 var collection = new ProductCollection([
 'Sheer Shears',
 'Wool Hauling Basket',
 'After-Shear (Fresh Cut Grass)',
 'After-Shear (Morning Dew)',
 ]);
 
 // ... {# app/Resources/views/default/products.html.twig' #} <script src="{{ asset('builds/productApp.js') }}"></script> > ./node_modules/.bin/babel web/js/productApp.js -o web/builds/productApp.js
  • 33.
    Module loading ina browser is hard to do @weaverryan
  • 34.
  • 35.
    @weaverryan Webpack! • bundler • moduleloader • all-around nice guy
  • 36.
    Install webpack > npminstall --save-dev webpack @weaverryan
  • 37.
    Use require insteadof import/export * * I’ll tell you why later // web/js/ProductCollection.js
 class ProductCollection
 {
 // ...
 }
 
 module.exports = ProductCollection;
 // web/js/productApp.js
 var ProductCollection = require('./ProductCollection');
 
 // ...
  • 38.
    Go webpack Go! >./node_modules/.bin/webpack web/js/productApp.js web/builds/productApp.js The one built file contains the code from both source files
  • 39.
    Optional config tomake it easier to use: // webpack.config.js
 module.exports = {
 entry: {
 product: './web/js/productApp.js'
 },
 output: {
 path: './web/builds',
 filename: '[name].js',
 publicPath: '/builds/'
 }
 };
 builds/product.js {# app/Resources/views/default/products.html.twig' #} <script src="{{ asset('builds/product.js') }}"></script>
  • 40.
  • 41.
    Wait! We lost ourES6->ES5 transformation!!! @weaverryan
  • 42.
    Hey webpack! Yo! Whenyou load .js files, can you run them through Babel for me? - kthxbai <3 Ryan webpack loaders allow you to transform files as they’re loaded
  • 43.
    1) Install thebabel-loader 2) Activate the loader in webpack.config.js > npm install --save-dev babel-loader module.exports = {
 // ...
 module: {
 loaders: [
 {
 test: /.js$/,
 loader: "babel-loader", exclude: /node_modules/
 }
 ]
 }
 };

  • 44.
  • 45.
  • 46.
    Use import/export nowif you prefer // web/js/ProductCollection.js
 class ProductCollection
 {
 // ...
 }
 
 export default ProductCollection;
 // web/js/productApp.js
 import ProductCollection from './ProductCollection';
 
 // ...

  • 47.
  • 48.
    @weaverryan Dev Tools … becauselife is too short to run webpack after every change you make
  • 49.
    > npm installwebpack-dev-server --save-dev > ./node_modules/.bin/webpack-dev-server --content-base=./web/ @weaverryan 1) Install the webpack-dev-server 2) Run that!
  • 50.
    http://localhost:8080 - static assetsare served - compiled assets are served dynamically
  • 51.
    Wait! @weaverryan Don’t I needto update all my script tags? <script
 src="{{ asset('builds/product.js') }}"> <script
 src="http://localost:8080/builds/product.js">
  • 52.
  • 53.
    … or thereal solution @weaverryan # app/config/parameters.yml
 parameters:
 # ...
 use_webpack_dev_server: true

  • 54.
    class AppKernel extendsKernel
 {
 // ...
 
 public function registerContainerConfiguration(LoaderInterface $loader)
 {
 // ...
 
 $loader->load(function(ContainerBuilder $container) {
 if ($container->getParameter('use_webpack_dev_server')) {
 $container->loadFromExtension('framework', [
 'assets' => [
 'base_url' => 'http://localhost:8080'
 ]
 ]);
 }
 });
 }
 } … or the real solution @weaverryan
  • 55.
    @weaverryan Status Update we can: •use ES6 features • import and export modules
  • 56.
  • 57.
    Could we dothis? // web/js/productApp.js
 import ProductCollection from './ProductCollection';
 
 // could this somehow load that CSS for us?
 import '../css/productApp.css';
 
 // ... Loader!
  • 58.
    module.exports = {
 //...
 module: {
 loaders: [
 {
 test: /.js$/,
 exclude: /node_modules/,
 loader: "babel-loader"
 }
 ]
 }
 };
 webpack loaders allow you to transform files as they’re loaded Remember:
  • 59.
    1) Install thecss-loader 2) Activate the loader just for this file > npm install css-loader --save-dev import 'css!../css/productApp.css'; this transforms the CSS into a JS data- structure… but does nothing with it
  • 60.
    1) Install thestyle-loader > npm install style-loader --save-dev import 'style!css!../css/productApp.css'; inlines the CSS on the page in a style tag 2) Activate both loaders for this file
  • 61.
    Yes, @weaverryan the one JSfile now holds the contents of two JS files and a CSS file {# app/Resources/views/default/products.html.twig' #} <script src="{{ asset('builds/product.js') }}"></script>
  • 62.
    Move the loaderto config to simplify import '../css/productApp.css'; // webpack.config.js module.exports = {
 // ...
 module: {
 loaders: [
 // ...
 {
 test: /.css$/,
 loader: "style!css"
 }
 ]
 },
 };

  • 63.
    @weaverryan Ah, but whatshould my image paths look like? /* productApp.css */
 .product-price {
 color: green;
 background-image: url('../images/logo.png');
 }
 http://example.com/products/5/photos/../images/logo.png http://example.com/products/5/photos
  • 64.
    This broke webpack! Yes,webpack parses the CSS and tries to load file imports and url() references (i.e. to images & fonts)
  • 65.
    1) Install thefile-loader & url-loader > npm install file-loader url-loader --save-dev 2) Activate the loader for .png files // webpack.config.js // ... loaders: [
 // ...
 {
 test: /.png/,
 loader: "url-loader?limit=10000"
 }
 ]

  • 66.
    {
 test: /.png/,
 loader: "url-loader?limit=10000"
 } For.png files < 10kb image is turned into a “data url” and inlined in the CSS For .png files > 10kb image is copied to builds/ and the new URL is written into the CSS
  • 67.
    Stop @weaverryan thinking of yourJavaScript as random code that executes
  • 68.
    Start @weaverryan thinking of yourJavaScript as a single application with dependencies that are all packaged up together
  • 69.
    @weaverryan Unleashing the Powerof NodeJS and ReactJS
  • 70.
    Like Composer, NodeJS hasa lot of third-party libraries @weaverryan … and we can use them
  • 71.
  • 72.
    1) Install it >npm install lodash --save-dev 2) Use it // web/js/productApp.js
 // ...
 import _ from 'lodash';
 
 _.each(collection.getProducts(), function(product) {
 // ...
 }); @weaverryan './ProductCollection' vs 'lodash'
  • 73.
  • 74.
    // web/js/productApp.js
 import Reactfrom 'react';
 import ReactDOM from 'react-dom';
 
 var ProductApp = React.createClass({
 render: function() {
 return (
 <h1>Yay!</h1>
 )
 }
 }); ?????? $(document).ready(function() {
 ReactDOM.render(
 <ProductApp/>,
 document.getElementById('product-app')
 );
 });
  • 75.
    
 <ProductApp myName="Ryan" />
 JSX React.createElement(
 ProductApp,
 {
 myName: "Ryan"
 }
 ) This is not real EcmaScript, but babel can handle it
  • 76.
    … but itdoesn’t yet
  • 77.
    1) Install thebabel preset > npm install --save-dev babel-preset-react 2) Add the preset in .babelrc @weaverryan {
 "presets": [
 "es2015",
 "react"
 ]
 }
  • 78.
    … nope -still not working
  • 79.
    // productApp.js
 import Reactfrom 'react';
 import ReactDOM from 'react-dom';
 
 var ProductApp = React.createClass({
 render: function() {
 return (
 <h1>Yay!</h1>
 )
 }
 });
 
 $(document).ready(function() {
 ReactDOM.render(
 <ProductApp/>,
 document.getElementById('product-app')
 );
 });

  • 80.
    > npm install--save-dev react react-dom … nope - still not working
  • 81.
    It’s alive, buthuge! *file size would be much smaller in reality, due to missing uglify and other production settings
  • 82.
    @weaverryan ReactJS is prettyeasy The setup to get here is tough
  • 83.
    React-101: props @weaverryan // web/js/Components/ProductList.js
 importReact from 'react';
 
 var ProductList = React.createClass({
 // ...
 });
 
 module.exports = ProductList;

  • 84.
    // web/js/productApp.js
 import ReactDOMfrom 'react-dom';
 import ProductList from './Components/ProductList';
 
 $(document).ready(function() {
 ReactDOM.render(
 <ProductList message="Great Products!" />,
 document.getElementById('product-app')
 );
 });
 It’s a prop!
  • 85.
    // web/js/Components/ProductList.js
 import Reactfrom 'react';
 
 var ProductList = React.createClass({
 render: function() {
 return (
 <h1>{this.props.message}</h1>
 )
 }
 });
 
 module.exports = ProductList;
 hello again prop!
  • 86.
    collection props @weaverryan // web/js/productApp.js
 
 varstartingProducts = [
 'Sheer Shears',
 'Wool Hauling Basket',
 'After-Shear (Fresh Cut Grass)',
 'After-Shear (Morning Dew)',
 ];
 
 $(document).ready(function() {
 ReactDOM.render(
 <ProductList initialProducts={startingProducts} />,
 document.getElementById('product-app')
 );
 });

  • 87.
    // web/js/Components/ProductApp.js
 import _from 'lodash';
 
 var ProductList = React.createClass({
 render: function() {
 var productRows = [];
 _.each(this.props.initialProducts, function(product) {
 productRows.push(
 <tr>
 <td>{product}</td>
 <td className="product-price">
 {Math.round(Math.random()*50)}
 </td>
 </tr>
 );
 });
 
 // ...
 }
 });
  • 88.
    // web/js/Components/ProductApp.js
 import _from 'lodash';
 
 var ProductList = React.createClass({
 render: function() {
 var productRows = [];
 // ...
 
 return (
 <div>
 <h1>{this.props.message}</h1>
 
 <table className="table">
 <tbody>{productRows}</tbody>
 </table>
 </div>
 )
 }
 });
  • 89.
    React 101: initialdata @weaverryan // web/js/productApp.js
 
 var startingProducts = [
 'Sheer Shears',
 'Wool Hauling Basket',
 'After-Shear (Fresh Cut Grass)',
 'After-Shear (Morning Dew)',
 ];
 
 $(document).ready(function() {
 ReactDOM.render(
 <ProductList initialProducts={startingProducts} />,
 document.getElementById('product-app')
 );
 });

  • 90.
    @weaverryan {# app/Resources/views/default/products.html.twig #}
 <script>
 window.startingProducts= [
 'Sheer Shears',
 'Wool Hauling Basket',
 'After-Shear (Fresh Cut Grass)',
 'After-Shear (Morning Dew)',
 ];
 </script> // web/js/productApp.js
 var startingProducts = window.startingProducts;
 
 $(document).ready(function() {
 ReactDOM.render(
 <ProductApp initialProducts={startingProducts} />,
 document.getElementById('product-app')
 );
 });
  • 91.
  • 92.
    var ProductApp =React.createClass({
 render: function() {
 return (
 <div>
 <h1>{this.props.message}</h1>
 </div>
 )
 }
 });
  • 93.
    var ProductApp =React.createClass({
 render: function() {
 return (
 <div>
 <h1>{this.state.message}</h1> <button onClick={this.handleClick}>
 New Message
 </button>
 </div>
 )
 }
 });
  • 94.
    var ProductApp =React.createClass({ 
 getInitialState: function() {
 return {
 message: 'Product List!';
 }
 }
 
 // ...
 }
  • 95.
    var ProductApp =React.createClass({
 render: function() {
 return (
 <div>
 <h1>{this.state.message}</h1> <button onClick={this.handleClick}>
 New Message
 </button>
 </div>
 )
 }
 });
  • 96.
    var ProductApp =React.createClass({
 
 handleClick: function(e) {
 e.preventDefault();
 
 this.setState({
 message: 'New Message!'
 })
 },
 
 // ...
 }
  • 97.
  • 98.
    @weaverryan ES6/ES2015/ECMAScript 2015 The newestversion of Javascript, not supported by all browsers
  • 99.
    @weaverryan Babel A tool thatcan transform JavaScript to different JavaScript presets A) ES6 js to “old” JS B) JSX to raw JS
  • 100.
    @weaverryan Webpack A tool thatfollows imports to bundle JavaScript, CSS, and anything else you dream up into one JavaScript package loaders A) JS through Babel B) CSS to inlined styles C) images copied, paths used
  • 101.
    @weaverryan ReactJS A nifty frontendframework where you pass around and render props (immutable) and state (mutable)
  • 102.
    … but theJavaScript world moves quickly…
  • 103.