SlideShare a Scribd company logo
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)
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)
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
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)
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 file
{# 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!

More Related Content

PDF
Keeping the frontend under control with Symfony and Webpack
ODP
CodeIgniter PHP MVC Framework
PDF
Utiliser Webpack dans une application Symfony
PDF
Symfony tips and tricks
KEY
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
PDF
Introducing Assetic: Asset Management for PHP 5.3
PDF
Choosing a Javascript Framework
PDF
Vuejs testing
Keeping the frontend under control with Symfony and Webpack
CodeIgniter PHP MVC Framework
Utiliser Webpack dans une application Symfony
Symfony tips and tricks
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Introducing Assetic: Asset Management for PHP 5.3
Choosing a Javascript Framework
Vuejs testing

What's hot (20)

PDF
Play vs Rails
PPTX
Node JS Express : Steps to Create Restful Web App
PDF
Future of Web Apps: Google Gears
PPT
Building Single Page Application (SPA) with Symfony2 and AngularJS
PDF
The JavaFX Ecosystem
PDF
Immutable Deployments with AWS CloudFormation and AWS Lambda
PPT
Writing Pluggable Software
PPTX
The road to Ember.js 2.0
PDF
Mobile Open Day: React Native: Crossplatform fast dive
PDF
Grand Rapids PHP Meetup: Behavioral Driven Development with Behat
PDF
Caldera Learn - LoopConf WP API + Angular FTW Workshop
PDF
Django Rest Framework and React and Redux, Oh My!
PDF
Bootstrat REST APIs with Laravel 5
PDF
WebGUI Developers Workshop
PDF
Introduction to AngularJS For WordPress Developers
PDF
From Hacker to Programmer (w/ Webpack, Babel and React)
PDF
Containers & Dependency in Ember.js
PDF
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
PDF
Single Page Web Apps As WordPress Admin Interfaces Using AngularJS & The Word...
PPTX
Real World Lessons in Progressive Web Application & Service Worker Caching
Play vs Rails
Node JS Express : Steps to Create Restful Web App
Future of Web Apps: Google Gears
Building Single Page Application (SPA) with Symfony2 and AngularJS
The JavaFX Ecosystem
Immutable Deployments with AWS CloudFormation and AWS Lambda
Writing Pluggable Software
The road to Ember.js 2.0
Mobile Open Day: React Native: Crossplatform fast dive
Grand Rapids PHP Meetup: Behavioral Driven Development with Behat
Caldera Learn - LoopConf WP API + Angular FTW Workshop
Django Rest Framework and React and Redux, Oh My!
Bootstrat REST APIs with Laravel 5
WebGUI Developers Workshop
Introduction to AngularJS For WordPress Developers
From Hacker to Programmer (w/ Webpack, Babel and React)
Containers & Dependency in Ember.js
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
Single Page Web Apps As WordPress Admin Interfaces Using AngularJS & The Word...
Real World Lessons in Progressive Web Application & Service Worker Caching
Ad

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

PDF
Webpack Encore Symfony Live 2017 San Francisco
PDF
Symfony Live 2018 - Développez votre frontend avec ReactJS et Symfony Webpack...
PPTX
SenchaCon 2016: The Modern Toolchain - Ross Gerbasi
PDF
Front-end build tools - Webpack
PPTX
A few good JavaScript development tools
PDF
ReactJS Workflows
PDF
Артем Яворский "Compile like it's 2017"
PDF
Angular Weekend
PPTX
Angular JS in 2017
PDF
(2018) Webpack Encore - Asset Management for the rest of us
PPT
CoffeeScript: A beginner's presentation for beginners copy
PDF
Introduction to Webpack - Ordina JWorks - CC JS & Web
PDF
Intro to React
PDF
Bundle your modules with Webpack
PDF
React Development with the MERN Stack
ODP
Javascript Update May 2013
PDF
Essentials in JavaScript App Bundling with Webpack
PDF
DownTheRabbitHole.js – How to Stay Sane in an Insane Ecosystem
PPTX
DotNet MVC and webpack + Babel + react
Webpack Encore Symfony Live 2017 San Francisco
Symfony Live 2018 - Développez votre frontend avec ReactJS et Symfony Webpack...
SenchaCon 2016: The Modern Toolchain - Ross Gerbasi
Front-end build tools - Webpack
A few good JavaScript development tools
ReactJS Workflows
Артем Яворский "Compile like it's 2017"
Angular Weekend
Angular JS in 2017
(2018) Webpack Encore - Asset Management for the rest of us
CoffeeScript: A beginner's presentation for beginners copy
Introduction to Webpack - Ordina JWorks - CC JS & Web
Intro to React
Bundle your modules with Webpack
React Development with the MERN Stack
Javascript Update May 2013
Essentials in JavaScript App Bundling with Webpack
DownTheRabbitHole.js – How to Stay Sane in an Insane Ecosystem
DotNet MVC and webpack + Babel + react
Ad

More from Ryan Weaver (19)

PDF
The Coolest Symfony Components you’ve never heard of - DrupalCon 2017
PDF
Symfony Guard Authentication: Fun with API Token, Social Login, JWT and more
PDF
Symfony: Your Next Microframework (SymfonyCon 2015)
PDF
Guard Authentication: Powerful, Beautiful Security
PDF
Twig: Friendly Curly Braces Invade Your Templates!
PDF
Master the New Core of Drupal 8 Now: with Symfony and Silex
PDF
Silex: Microframework y camino fácil de aprender Symfony
PDF
Drupal 8: Huge wins, a Bigger Community, and why you (and I) will Love it
PDF
Cool like a Frontend Developer: Grunt, RequireJS, Bower and other Tools
PDF
The Wonderful World of Symfony Components
PDF
A PHP Christmas Miracle - 3 Frameworks, 1 app
PDF
Symfony2: Get your project started
PDF
Symony2 A Next Generation PHP Framework
PDF
Hands-on with the Symfony2 Framework
PDF
Being Dangerous with Twig (Symfony Live Paris)
PDF
Being Dangerous with Twig
PDF
Doctrine2 In 10 Minutes
PDF
Dependency Injection: Make your enemies fear you
PDF
The Art of Doctrine Migrations
The Coolest Symfony Components you’ve never heard of - DrupalCon 2017
Symfony Guard Authentication: Fun with API Token, Social Login, JWT and more
Symfony: Your Next Microframework (SymfonyCon 2015)
Guard Authentication: Powerful, Beautiful Security
Twig: Friendly Curly Braces Invade Your Templates!
Master the New Core of Drupal 8 Now: with Symfony and Silex
Silex: Microframework y camino fácil de aprender Symfony
Drupal 8: Huge wins, a Bigger Community, and why you (and I) will Love it
Cool like a Frontend Developer: Grunt, RequireJS, Bower and other Tools
The Wonderful World of Symfony Components
A PHP Christmas Miracle - 3 Frameworks, 1 app
Symfony2: Get your project started
Symony2 A Next Generation PHP Framework
Hands-on with the Symfony2 Framework
Being Dangerous with Twig (Symfony Live Paris)
Being Dangerous with Twig
Doctrine2 In 10 Minutes
Dependency Injection: Make your enemies fear you
The Art of Doctrine Migrations

Recently uploaded (20)

PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
cuic standard and advanced reporting.pdf
PDF
Reimagining Insurance: Connected Data for Confident Decisions.pdf
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
Transforming Manufacturing operations through Intelligent Integrations
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Modernizing your data center with Dell and AMD
PDF
Event Presentation Google Cloud Next Extended 2025
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Big Data Technologies - Introduction.pptx
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
Chapter 2 Digital Image Fundamentals.pdf
PDF
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
PDF
REPORT: Heating appliances market in Poland 2024
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
cuic standard and advanced reporting.pdf
Reimagining Insurance: Connected Data for Confident Decisions.pdf
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Transforming Manufacturing operations through Intelligent Integrations
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Modernizing your data center with Dell and AMD
Event Presentation Google Cloud Next Extended 2025
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
20250228 LYD VKU AI Blended-Learning.pptx
MYSQL Presentation for SQL database connectivity
Big Data Technologies - Introduction.pptx
GamePlan Trading System Review: Professional Trader's Honest Take
Chapter 2 Digital Image Fundamentals.pdf
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
REPORT: Heating appliances market in Poland 2024
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
“AI and Expert System Decision Support & Business Intelligence Systems”

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

  • 1. Finally, Professional Frontend dev with: ReactJS, Webpack & Symfony ♥’s
  • 2. > 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!
  • 3. ♥’s Finally, Professional Frontend dev with: ReactJS, Webpack & Symfony
  • 4. , 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 …
  • 5. Modern JavaScript is a lot like… @weaverryan Game of Thrones
  • 6. JavaScript @weaverryan GoT Countless libraries and competing standards fighting for influence Countless characters and completing factions fighting for influence
  • 7. @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
  • 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 with the 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 name of 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 just need to wait 5 years for the crappiest browsers to support this
  • 19. @weaverryan Babel … or do we? A JS transpiler!
  • 20. 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
  • 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 transpile anything CoffeeScript --> JavaScript Coffee --> Tea ES6 JS --> ES5 JS * Each transformation is called a preset
  • 24. 1) Install the es2015 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 can use new (or experimental) features now @weaverryan Modern JavaScript has a build step Big Takeaway #1:
  • 28. The Classic Problem: If you 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 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>
  • 31. // 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
  • 33. Module loading in a browser is hard to do @weaverryan
  • 35. @weaverryan Webpack! • bundler • module loader • all-around nice guy
  • 36. Install webpack > npm install --save-dev webpack @weaverryan
  • 37. 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');
 
 // ...
  • 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 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>
  • 41. Wait! We lost our ES6->ES5 transformation!!! @weaverryan
  • 42. 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
  • 43. 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/
 }
 ]
 }
 };

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

  • 48. @weaverryan Dev Tools … because life is too short to run webpack after every change you make
  • 49. > 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!
  • 50. http://localhost:8080 - static assets are served - compiled assets are served dynamically
  • 51. 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">
  • 53. … or the real solution @weaverryan # app/config/parameters.yml
 parameters:
 # ...
 use_webpack_dev_server: true

  • 54. 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
  • 55. @weaverryan Status Update we can: • use ES6 features • import and export modules
  • 57. 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!
  • 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 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
  • 60. 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
  • 61. Yes, @weaverryan the one JS file 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 loader to config to simplify import '../css/productApp.css'; // webpack.config.js module.exports = {
 // ...
 module: {
 loaders: [
 // ...
 {
 test: /.css$/,
 loader: "style!css"
 }
 ]
 },
 };

  • 63. @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
  • 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 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"
 }
 ]

  • 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 your JavaScript as random code that executes
  • 68. Start @weaverryan thinking of your JavaScript as a single application with dependencies that are all packaged up together
  • 69. @weaverryan Unleashing the Power of NodeJS and ReactJS
  • 70. Like Composer, NodeJS has a lot of third-party libraries @weaverryan … and we can use them
  • 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'
  • 74. // 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')
 );
 });
  • 75. 
 <ProductApp myName="Ryan" /> 
 JSX React.createElement(
 ProductApp,
 {
 myName: "Ryan"
 }
 ) This is not real EcmaScript, but babel can handle it
  • 76. … but it doesn’t yet
  • 77. 1) Install the babel 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 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')
 );
 });

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

  • 84. // 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!
  • 85. // 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!
  • 86. 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')
 );
 });

  • 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: 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')
 );
 });

  • 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')
 );
 });
  • 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!'
 })
 },
 
 // ...
 }
  • 98. @weaverryan ES6/ES2015/ECMAScript 2015 The newest version of Javascript, not supported by all browsers
  • 99. @weaverryan Babel A tool that can transform JavaScript to different JavaScript presets A) ES6 js to “old” JS B) JSX to raw JS
  • 100. @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
  • 101. @weaverryan ReactJS A nifty frontend framework where you pass around and render props (immutable) and state (mutable)
  • 102. … but the JavaScript world moves quickly…