Symfony - Webpack Encore
Who am I?
Backend developer
PHP enthusiast
CTO at Darkmira
Freelance web architect
Occasional lecturer
Javascript hater!
2
Community involvement
ScotlandPHP (conference, November 8th and 9th)
DarkmiraTour Brasil (conference, June 7th to 9th)
DijonTech (meetup, sometimes in March)
AFUP Lorraine ("super apéro", March 14th, in a bar
in Metz)
3
by SpaceFox
article
Sans déconner : après huit mois d'observation,
ma conclusion la plus logique est que 95 % de la
communauté Javascript est composée de
macaques sous cocaïne. Je ne vois pas d'autres
explication logique à l'état général de
l'environnement.
“
“
4
5
Javascript alternative?
What a success!
Q. Is Dart supported by my browser?
Although no production browsers can execute
Dart code directly, all modern browsers can
execute Dart code that as been compiled to
JavaScript.
“
“
6
by Krishna Chaitanya Acondy
article
Can Monkeys Write JavaScript? An Impartial
Analysis
“
“
7
What's your task runner?
Gulp
Grunt
Brunch
Package manager?
Bower
NPM
Yarn
8
https://www.npmtrends.com/bower-vs-brunch-vs-
grunt-vs-gulp-vs-yarn-vs-npm-vs-webpack
9
♥ Webpack ♥
11
♥ Webpack + Symfony ♥
Symfony Webpack Encore = Webpack made easy!
Symfony ships with a pure-JavaScript library -
called Webpack Encore - that makes working
with CSS and JavaScript a joy. You can use it, use
something else, or just create static CSS and JS
les in your public/ directory and include them in
your templates.
“
“
12
Basic usage: in a Symfony project
13
composer require symfony/skeleton myproject
cd myproject
composer require twig
Add a route
index:
path: /
controller: AppControllerHomepage::handle
And create the controller and template.
14
final class Homepage
{
private $renderer;
public function __construct(Environment $renderer)
{
$this->renderer = $renderer;
}
public function handle(): Response
{
return new Response($this->renderer->render(
'homepage.html.twig'
));
}
}
15
{% extends 'base.html.twig' %}
{% block body %}
<h1>Hello World!</h1>
{% endblock %}
{% block title %}Hello World!{% endblock %}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{% block title %}Welcome!{% endblock %}</title
{% block stylesheets %}{% endblock %}
</head>
<body>
{% block body %}{% endblock %}
{% block javascripts %}{% endblock %}
</body>
</html>
16
17
Adding Bootstrap
CDN?
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/c
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/
18
Adding Bootstrap
Downloading les manually?
<script src="{{ asset('js/bootstrap.min.js') }}"></script>
<link href="{{ asset('css/bootstrap.min.css') }}" rel="styleshee
19
Adding Bootstrap
composer require encore
Adds
package.json
webpack.config.js
config/packages/webpack_encore.yaml
assets
css => app.css
js => app.js
20
Install yarn dependencies
yarn install
Adds
node_modules
21
config/packages/webpack_encore.yaml
webpack_encore:
# The path where Encore is building the assets.
# This should match Encore.setOutputPath() in webpack.config
output_path: '%kernel.project_dir%/public/build'
22
package.json
{
"devDependencies": {
"@symfony/webpack-encore": "^0.22.0",
"webpack-notifier": "^1.6.0"
},
"license": "UNLICENSED",
"private": true,
"scripts": {
"dev-server": "encore dev-server",
"dev": "encore dev",
"watch": "encore dev --watch",
"build": "encore production --progress"
}
}
23
webpack.config.js
var Encore = require('@symfony/webpack-encore');
Encore
.setOutputPath('public/build/')
.setPublicPath('/build')
.addEntry('app', './assets/js/app.js')
.enableSingleRuntimeChunk()
.cleanupOutputBeforeBuild()
.enableBuildNotifications()
.enableSourceMaps(!Encore.isProduction())
// enables hashed filenames (e.g. app.abc123.css)
.enableVersioning(Encore.isProduction())
;
module.exports = Encore.getWebpackConfig();
24
assets/js/app.js
assets/css/app.css
body {
background-color: lightgray;
}
require('../css/app.css');
console.log('Hello Webpack Encore! Edit me in assets/js/app.js'
25
Add assets to the template
{% block stylesheets %}
{{ encore_entry_link_tags('app') }}
{% endblock %}
{% block javascripts %}
{{ encore_entry_script_tags('app') }}
{% endblock %}
26
yarn encore dev
27
28
Still no Bootstrap...
yarn add bootstrap jquery popper.js
assets/js/app.js
yarn encore dev --watch
require('../css/app.css');
require('../../node_modules/bootstrap/dist/css/bootstrap.css'
require('bootstrap');
29
30
Wahoo! It really saved me some time!
- Said no one, ever
“
“
31
Enable extra features
Override bootstrap the right way:
yarn add sass-loader node-sass --dev
assets/js/app/js
// import '../css/app.css';
import '../scss/app.scss';
webpack.config.js
Encore
.enableSassLoader()
;
32
assets/js/app.js
require('../scss/app.scss');
assets/scss/app.scss
$red: purple !default;
@import "~bootstrap/scss/bootstrap";
Ref: node_modules/bootstrap/scss/_variables.scss
33
34
What is generated?
35
public/build/entrypoints.json
{
"entrypoints": {
"app": {
"js": [
"/build/runtime.js",
"/build/app.js"
],
"css": [
"/build/app.css"
]
}
}
}
36
public/build/manifest.json
{
"build/app.css": "/build/app.css",
"build/app.js": "/build/app.js",
"build/runtime.js": "/build/runtime.js"
}
37
Let's generate for prod
yarn encore prod
webpack.config.js :
.enableVersioning(Encore.isProduction())
38
HTTP Cache busting
39
public/build/manifest.json
{
"build/app.css": "/build/app.c12d85ce.css",
"build/app.js": "/build/app.27210d01.js",
"build/runtime.js": "/build/runtime.fa8f03f5.js"
}
40
public/build/entrypoints.json
{
"entrypoints": {
"app": {
"js": [
"/build/runtime.fa8f03f5.js",
"/build/app.27210d01.js"
],
"css": [
"/build/app.c12d85ce.css"
]
}
}
}
41
Add an image to the template
Add the image assets/image/webpack.svg
Edit assets/js/app.js , add
require('../image/webpack.svg');
Edit templates/homepage.html.twig , add the image tag:
The image bene ts of the cache busting/asset
versioning policy.
<img src="{{ asset('build/images/webpack.svg') }}" alt="Demo ima
42
You might not need
everything everywhere!
43
Use entries
webpack.config.js :
Encore
.addEntry('app', './assets/js/app.js')
.addEntry('home', './assets/js/home.js')
;
assets/js/app.js :
require('../scss/app.scss');
assets/js/home.js :
require('../image/webpack.svg');
44
Update the template
templates/homepage.html.twig :
{% block stylesheets %}
{{ encore_entry_link_tags('app') }}
{{ encore_entry_link_tags('home') }}
{% endblock %}
{% block javascripts %}
{{ encore_entry_script_tags('app') }}
{{ encore_entry_script_tags('home') }}
{% endblock %}
45
More Javascript?
46
No! Typescript FTW
yarn add ts-loader typescript --dev
tsconfig.json :
{}
webpack.config.js :
Encore
.enableTypeScriptLoader()
;
47
assets/ts/Person.ts :
export interface Person {
firstName: string;
lastName: string;
}
assets/ts/LambdaPerson.ts :
import {Person} from "./Person";
export class LabdaPerson implements Person {
public constructor(public firstName: string, public lastName
}
}
48
assets/ts/Greeter.ts :
assets/js/app.js :
import {Person} from "./Person";
export function greeter(person: Person) {
return "Hello, " + person.firstName + " " + person.lastName;
}
import {greeter} from "../ts/Greeter";
import {LabdaPerson} from "../ts/LambdaPerson";
require('../scss/app.scss');
require('../image/webpack.svg');
let user = new LabdaPerson('Jane', 'Doe');
document.getElementById('hello').innerHTML = greeter(user);49
50
Standalone
Webpack encore only, no composer required:
yarn add @symfony/webpack-encore --dev
Create your own webpack.config.js .
Read from public/build/entrypoints.json and
public/build/manifest.json
51
Javascript with almost no Javascript?

More Related Content

PDF
"今" 使えるJavaScriptのトレンド
PDF
Consegi 2010 - Dicas de Desenvolvimento Web com Ruby
PDF
lecture5
PDF
[jqconatx] Adaptive Images for Responsive Web Design
PDF
Mume HTML5 Intro
PDF
Progressive web and the problem of JavaScript
PDF
Mastering Grunt
PDF
Fisl 11 - Dicas de Desenvolvimento Web com Ruby
"今" 使えるJavaScriptのトレンド
Consegi 2010 - Dicas de Desenvolvimento Web com Ruby
lecture5
[jqconatx] Adaptive Images for Responsive Web Design
Mume HTML5 Intro
Progressive web and the problem of JavaScript
Mastering Grunt
Fisl 11 - Dicas de Desenvolvimento Web com Ruby

What's hot (20)

PPTX
Nahlédněte za oponu VersionPressu
PPT
PDF
Developing for Mobile
PDF
Cool like a Frontend Developer: Grunt, RequireJS, Bower and other Tools
PDF
JavaScript Promises and the issue of Progress - SmashingConf Freiburg Jam Ses...
PDF
Developing web applications in 2010
PDF
Javascript is your (Auto)mate
PDF
HTML5, The Open Web, and what it means for you - MDN Hack Day, Sao Paulo
PDF
20111014 mu me_html5
PDF
Go Mobile with Apache Cordova, Zagreb 2014
PDF
Keypoints html5
PDF
React & The Art of Managing Complexity
PPTX
JavaScript Performance Patterns
PDF
Creating Responsive Experiences
PDF
Grooscript gr8conf 2015
PDF
JavaScript APIs - The Web is the Platform
PDF
Grooscript greach 2015
PDF
Professional web development with libraries
PDF
Web-Performance
PDF
S314011 - Developing Composite Applications for the Cloud with Apache Tuscany
Nahlédněte za oponu VersionPressu
Developing for Mobile
Cool like a Frontend Developer: Grunt, RequireJS, Bower and other Tools
JavaScript Promises and the issue of Progress - SmashingConf Freiburg Jam Ses...
Developing web applications in 2010
Javascript is your (Auto)mate
HTML5, The Open Web, and what it means for you - MDN Hack Day, Sao Paulo
20111014 mu me_html5
Go Mobile with Apache Cordova, Zagreb 2014
Keypoints html5
React & The Art of Managing Complexity
JavaScript Performance Patterns
Creating Responsive Experiences
Grooscript gr8conf 2015
JavaScript APIs - The Web is the Platform
Grooscript greach 2015
Professional web development with libraries
Web-Performance
S314011 - Developing Composite Applications for the Cloud with Apache Tuscany
Ad

Similar to AFUP Lorraine - Symfony Webpack Encore (20)

PDF
Always on! Or not?
PDF
Everything is Awesome - Cutting the Corners off the Web
PDF
Streamlining Your Applications with Web Frameworks
KEY
PDF
HTML5 tutorial: canvas, offfline & sockets
PDF
Having Fun with Play
PDF
Always on! ... or not?
PDF
lecture5
PPT
Build Your Own CMS with Apache Sling
PDF
How to make Ajax Libraries work for you
PDF
JavaScript Libraries: The Big Picture
KEY
[Coscup 2012] JavascriptMVC
PPTX
10 ways to make your code rock
PDF
Front End Development for Back End Java Developers - Jfokus 2020
PDF
[convergese] Adaptive Images in Responsive Web Design
PDF
Burn down the silos! Helping dev and ops gel on high availability websites
PDF
Mozilla Web Apps - Super-VanJS
PPTX
Nodejs.meetup
ODP
Drupal Theme Development - DrupalCon Chicago 2011
PPTX
Intro To Node.js
Always on! Or not?
Everything is Awesome - Cutting the Corners off the Web
Streamlining Your Applications with Web Frameworks
HTML5 tutorial: canvas, offfline & sockets
Having Fun with Play
Always on! ... or not?
lecture5
Build Your Own CMS with Apache Sling
How to make Ajax Libraries work for you
JavaScript Libraries: The Big Picture
[Coscup 2012] JavascriptMVC
10 ways to make your code rock
Front End Development for Back End Java Developers - Jfokus 2020
[convergese] Adaptive Images in Responsive Web Design
Burn down the silos! Helping dev and ops gel on high availability websites
Mozilla Web Apps - Super-VanJS
Nodejs.meetup
Drupal Theme Development - DrupalCon Chicago 2011
Intro To Node.js
Ad

More from Engineor (6)

PDF
Open close principle, on a dit étendre, pas extends !
PDF
Acceptance testing in php with Codeception - Techmeetup Edinburgh
PDF
Codeception: introduction to php testing (v2 - Aberdeen php)
PDF
Apigility introduction v2 (glasgow php)
PDF
Introduction to Apigility
PDF
Codeception: introduction to php testing
Open close principle, on a dit étendre, pas extends !
Acceptance testing in php with Codeception - Techmeetup Edinburgh
Codeception: introduction to php testing (v2 - Aberdeen php)
Apigility introduction v2 (glasgow php)
Introduction to Apigility
Codeception: introduction to php testing

Recently uploaded (20)

PDF
4 layer Arch & Reference Arch of IoT.pdf
DOCX
Basics of Cloud Computing - Cloud Ecosystem
PDF
Co-training pseudo-labeling for text classification with support vector machi...
PDF
Transform-Your-Supply-Chain-with-AI-Driven-Quality-Engineering.pdf
PDF
Advancing precision in air quality forecasting through machine learning integ...
PDF
Human Computer Interaction Miterm Lesson
PDF
The-2025-Engineering-Revolution-AI-Quality-and-DevOps-Convergence.pdf
PDF
The-Future-of-Automotive-Quality-is-Here-AI-Driven-Engineering.pdf
PDF
Dell Pro Micro: Speed customer interactions, patient processing, and learning...
PDF
NewMind AI Weekly Chronicles – August ’25 Week IV
PDF
Early detection and classification of bone marrow changes in lumbar vertebrae...
PDF
Connector Corner: Transform Unstructured Documents with Agentic Automation
PDF
Transform-Quality-Engineering-with-AI-A-60-Day-Blueprint-for-Digital-Success.pdf
PPTX
agenticai-neweraofintelligence-250529192801-1b5e6870.pptx
PPTX
Build automations faster and more reliably with UiPath ScreenPlay
PDF
LMS bot: enhanced learning management systems for improved student learning e...
PPTX
Microsoft User Copilot Training Slide Deck
PDF
Planning-an-Audit-A-How-To-Guide-Checklist-WP.pdf
PDF
Transform-Your-Factory-with-AI-Driven-Quality-Engineering.pdf
PDF
giants, standing on the shoulders of - by Daniel Stenberg
4 layer Arch & Reference Arch of IoT.pdf
Basics of Cloud Computing - Cloud Ecosystem
Co-training pseudo-labeling for text classification with support vector machi...
Transform-Your-Supply-Chain-with-AI-Driven-Quality-Engineering.pdf
Advancing precision in air quality forecasting through machine learning integ...
Human Computer Interaction Miterm Lesson
The-2025-Engineering-Revolution-AI-Quality-and-DevOps-Convergence.pdf
The-Future-of-Automotive-Quality-is-Here-AI-Driven-Engineering.pdf
Dell Pro Micro: Speed customer interactions, patient processing, and learning...
NewMind AI Weekly Chronicles – August ’25 Week IV
Early detection and classification of bone marrow changes in lumbar vertebrae...
Connector Corner: Transform Unstructured Documents with Agentic Automation
Transform-Quality-Engineering-with-AI-A-60-Day-Blueprint-for-Digital-Success.pdf
agenticai-neweraofintelligence-250529192801-1b5e6870.pptx
Build automations faster and more reliably with UiPath ScreenPlay
LMS bot: enhanced learning management systems for improved student learning e...
Microsoft User Copilot Training Slide Deck
Planning-an-Audit-A-How-To-Guide-Checklist-WP.pdf
Transform-Your-Factory-with-AI-Driven-Quality-Engineering.pdf
giants, standing on the shoulders of - by Daniel Stenberg

AFUP Lorraine - Symfony Webpack Encore