We are on a mission to refactor all the old<script>
y<link>
hang tagsforaof our molds.For the basic design, we're halfway there!there is onlyonescript tag, which points to theapplication
Prohibited:
96 lines templates/base.html.twig
<!doctypehtml> | |
<html language="inside"> | |
... lines 3 - 17 | |
<Body> | |
... lines 19 - 90 | |
{%blockjavascript%} | |
{{ encore_entry_script_tags('application') }} | |
{%final block%} | |
</Body> | |
</html> |
This isPerfect.
Back at the top, weDoesyou still have several binding tags including Bootstrap from a CDN,FontAwesome, which I apparently just inserted into mypublic/css
directory,and some custom css atstyles.css
:
96 lines templates/base.html.twig
<!doctypehtml> | |
<html language="inside"> | |
<head> | |
... lines 5 - 8 | |
{%block%} stylesheets | |
{{ encore_entry_link_tags('application') }} | |
<Link real="style sheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" cross origin="anonymous"> | |
<Link real="style sheet" href="{{ ativo('css/font-awesome.css') }}"> | |
<Link real="style sheet" href="{{ active('css/styles.css') }}"> | |
{%final block%} | |
</head> | |
... lines 17 - 94 | |
</html> |
First, remove Bootstrap!Nosamehow can we properly install JavaScript libraries with Yarn,we canWhat elseinstall css libraries!Quadra!
Insideaplicación.js
, we are already importing a singleapplication.css
office hour:
25 lines assets/js/app.js
... lines 1 - 7 | |
// whatever CSS you need will be generated in a single CSS file (app.css in this case) | |
to import '../css/aplicativo.css'; | |
... lines 10 - 25 |
Wecouldadd another importCorrecthere for Bootstrap CSS.instead Ito preferto import onlyoneCSS file per entry.so sinceWithinthis css file, we can use the pattern@to import
CSS syntaxto import other CSS files.For Webpack, these two approaches are identical.
Now youmay thatto be thinking:
Don't we need to install the bootstrap css library?
And yes!Well, I mean, no!Um, I mean, we already did!Insidenode_modules/
, To search forear/
.This directory contains JavaScript, butWhat elsecontains the Bootstrap CSS.
Importing CSS from node_modules
Mas... hum...In JavaScript, we can sayto import
then just the package name and...it just works!But wecan notrepeat the same trick for CSS.
Instead, we'll point you straight in the way we want, which,in this case it's probablydist/css/bootstrap.css
.That's how:@to import
,~ throw
is the way:/dist/css/bootstrap.css
:
2 lines assets/css/application.css
@to import '~bootstrap/dist/css/bootstrap.css'; |
Los~
part is special for CSS and Webpack.When you want to refer tonode_modules/
directory inside a CSS file,you have to start with~
🇧🇷 This isdifferentwhat javascriptwhere any road thatnoStart with.
should live innode_modules/
.After~
, it's just a normal boring road.
But yes...That's all we need!Move and update.This looks exactly the same!
Referencing only the package name
And... remember how I said that wecan notjust import CSS referencingonlythe package name?That was...a kind of lieShorten it to just~ throw
:
2 lines assets/css/application.css
@to import '~boot'; |
Go try it!Update and...the same!
That isit works thanks to a little extra feature we've added to Encore...which may become a more standard feature in the future.We already know that when we import a package by name in JavaScript,Webpack searches inpackage.json
, find itdirector
wrench....there it is and useThat isknow what you owefinallyimport thedist/js/bootstrap.js
office hour.
AnylibrariesWhat elseinclude thesestyle
obold
chaves.and when theyDoes, you just need@to import
~
and the package name.Since we're doing this from within a CSS file,know how to look insidepackage.json
forstyle
Wrench.
This issinglea shortcut to do exactly what we did before.
Bootstrap, comprove!Let's move on: The link tag below is for FontAwesome.Get rid of it and celebrate by removing thepublic/css/font-awesome.css
office houryall thissources/
directory.That's great!We're pulling out things I never should have done in the first place.
Then download FontAwesome with:
topic add font awesome --dev
When finished, go back tonode_modules/
and searchamazing font/
.I get it! Nice!Has directories forcss/
,any less/
,scss/
the format we want.And happily if you look insidepackage.json
, thatWhat elsethere is astyle
Wrench.
Bread eaten!Insideapplication.css
, add@import '~font-awesome'
:
3 lines assets/css/application.css
@to import '~boot'; | |
@to import '~amazing font'; |
Done. Find your browser and update.We'll see...down here, yes!That isis a FontAwesome icon.Still works!
But this iswaycooler than it looks!Internally, the FontAwesome CSS file references somesourcerecordsthat the user's browser needs to download: these files here.But... these files are not in ourpublic
directory...So shouldn't the paths to it be broken?
Closelynode_modules/
and take a look atpublic/build/
directory.Wow! where did he do itsources/
does the directory come from?When Webpack sees that a CSS filerefers toto a source file,Hearing thisthese sourcesinside itsources/
directory andrewriteor non-final codeapplication.css
office hourso that the source paths pointhere.yes, justyou drivethat.
It also automatically adds a hash to the filename based on the file.contents.So if we update the source file this hash will change automaticallyand the CSS would point this out automatically.This isfree of chargebrowser cache destruction.
Move our CSS to Encore
OKonemore link tag to go:
94 lines templates/base.html.twig
<!doctypehtml> | |
<html language="inside"> | |
<head> | |
... lines 5 - 8 | |
{%block%} stylesheets | |
... lines 10 - 11 | |
<Link real="style sheet" href="{{ active('css/styles.css') }}"> | |
{%final block%} | |
</head> | |
... lines 15 - 92 | |
</html> |
Tire!so opencss/styles.css
, copyalladdition, delete that file and thenapplication.css
,highlight the blue background and paste it!
246 lines assets/css/application.css
@to import '~boot'; | |
@to import '~amazing font'; | |
Body{ | |
position: relative; | |
background:#eeee; | |
minimum height:45 rem; | |
bottom padding:80px; | |
} | |
html{height:100%} | |
/* NAVIGATION */ | |
.navbar-bg{ | |
background:URL('../images/space-nav.jpg'); | |
background size:80%; | |
} | |
.Fold out menu,.Fold out menu.Show{ | |
Correct:0; | |
} | |
.brand space{ | |
cor:#fff; | |
font thickness: bold; | |
} | |
.nav-profile-img{ | |
ancho:50px; | |
he must:1pxsolid#fff; | |
} | |
.nav-tabs .nav-enlace:approach,.nav-tabs .nav-enlace:float{ | |
cor:#eeee; | |
} | |
/* ADVERTISEMENT */ | |
.ad space{ | |
background:#fff; | |
border radio:5px; | |
upper rail:5pxsolid green; | |
} | |
.advertising-img{ | |
ancho:150px; | |
height: automatic; | |
he must:2 pixelssolid#eeee; | |
border radio:5px; | |
} | |
.ad-text{ | |
font thickness: bold; | |
} | |
.quote-space{ | |
background:#fff; | |
upper margin:30px; | |
border radio:5px; | |
upper rail:5pxsolid shock pink; | |
} | |
/* ARTICLES */ | |
.Main article{ | |
he must:2 pixelssolid#eeee; | |
Background:#fff; | |
upper left ray:6 pixels; | |
top right edge radius:6 pixels; | |
} | |
.Main article Image{ | |
ancho:100%; | |
height:250px; | |
top right edge radius:5px; | |
upper left ray:5px; | |
upper rail:5pxsolid light blue; | |
} | |
.article-container{ | |
he must:1pxsolid#eeee; | |
upper left ray:5px; | |
bottom left ray:5px; | |
background:#fff; | |
} | |
.link to main article,.article-container one{ | |
text decoration: None; | |
cor:#000; | |
} | |
.link to main article:float{ | |
text decoration: None; | |
cor:#000; | |
} | |
.article title{ | |
minimum width:300px; | |
} | |
@media(maximum width: 440px) { | |
.article title{ | |
minimum width:100 pixels; | |
maximum width:245 px; | |
} | |
} | |
.article-img{ | |
height:100 pixels; | |
ancho:100 pixels; | |
upper left ray:5px; | |
bottom left ray:5px; | |
} | |
.article-author-img{ | |
height:25px; | |
he must:1pxsolid dark grey; | |
} | |
.Item details{ | |
font size: .8 em; | |
} | |
/* PROFILE */ | |
.profile-img{ | |
ancho:150px; | |
height: automatic; | |
he must:2 pixelssolid#fff; | |
} | |
.profile name{ | |
font size:1,5 em; | |
} | |
.my-item-container{ | |
background:#FFBC49; | |
he must: solid1px #eeee; | |
border radio:5px; | |
} | |
/* CREATE ARTICLE */ | |
.create-article-container{ | |
minimum width:400px; | |
background color: Light Blue; | |
border radio:5px; | |
} | |
/* SHOW ITEM PAGE */ | |
.show-item-container{ | |
ancho:100%; | |
background color:#fff; | |
} | |
.show-item-container.show-item-container-green-border{ | |
upper rail:3 pixelssolid green; | |
border radio:3 pixels; | |
} | |
.show-article-img{ | |
ancho:250px; | |
height: automatic; | |
border radio:5px; | |
} | |
.show-article-title{ | |
font size:2em; | |
} | |
.like-article,.like-article:float{ | |
cor: red; | |
text decoration: None; | |
} | |
@media(maximum width: 991 px) { | |
.show-article-title{ | |
font size:1,5 em; | |
} | |
.show-article-title-container{ | |
maximum width:220px; | |
} | |
} | |
.article-text{ | |
upper margin:20 pixels; | |
} | |
.share-icons eu{ | |
font size:1,5 em; | |
} | |
comment recipient{ | |
maximum width:600 px; | |
} | |
.comment-img{ | |
ancho:50px; | |
height: automatic; | |
he must:1pxsolid dark grey; | |
} | |
commentator name{ | |
font thickness: bold; | |
} | |
.comment form{ | |
minimum width:500px; | |
} | |
@media(maximum width: 767px) { | |
.comment form{ | |
minimum width:260px; | |
} | |
comment recipient{ | |
maximum width:280px; | |
} | |
} | |
/* GREEK */ | |
.baseboard{ | |
position: absolute; | |
low:0; | |
ancho:100%; | |
height:60px;/* Set the fixed height of the footer here */ | |
line height:60px;/* Vertically center the text here */ | |
background color:#fff; | |
upper margin:10px; | |
} | |
/* Sort it */ | |
.sortable-ghost{ | |
background color: Light Blue; | |
} | |
.drag-handle{ | |
cursor: to take; | |
} |
It's a simple step, so...it should work right?No! Take a look at the failed build:
Module not found: cannot be resolved
../images/navegación-espacial.jpg
in ourassets/css/
directory.
It doesn't show the exact file, but we only have one.Ah,here it isthe problem:
246 lines assets/css/application.css
... lines 1 - 12 | |
/* NAVIGATION */ | |
.navbar-bg{ | |
background:URL('../images/space-nav.jpg'); | |
... line 17 | |
} | |
... lines 19 - 246 |
PhpStorm is also very angry about this.This background image refers to../images/
, What was itPerfectwhen the code lived inpublic/css/
directory.But when we change it, we break that path!
This is incredible!Instead of being silently unaware that we've done it, we get aaccumulatemistake.Incredible! We can't open roads without Webpackyelling.
To fix this, let's "cut" all theimages/
directory and move it toactive/
chainer.Yes, it's over.But Encore doesn't know how to recompile...then make a small change and save.Build successfully!
Go see.Update! It works!it's includedBest, look ataccumulate/
chainer.we have aimages/
directory withspaceship.jpg
not inside.singlejust like fonts, Webpack sees our way,realizewhatspaceship.jpg
It has to be publicand so transfers it toconstruction/images/
directoryand rewrite theBackground image
code in the final css to point here.
The moral is this: everyoneweWhat we need to do is worry about writing our code correctly:using the appropriate relative paths from the source CSS file to the source image file.Webpack handles the nasty details.
now thisThatbreak some<img>
tags on our website that refer to some of these files.Now that they are not inpublic/
directory...Do not workWe'll take care of that shortly.
But next, we're going to get more out of our CSS using Sass.
This tutorial works great with symfony5!
Symfony 4 Webpack Encore
What PHP libraries does this tutorial use?
// composer.json{ "requerir": { "php": "^7.1.3", "ext-iconv": "*", "aws/aws-sdk-php": "^3.87", // 3.91 .4 "compositor/versiones-paquete-en desuso": "^1.11", // 1.11.99 "knplabs/knp-markdown-bundle": "^1.7", // 1.7.1 "knplabs/knp-paginator- bundle ": "^2.7", // v2.8.0 "knplabs/knp-time-bundle": "^1.8", // 1.9.0 "league/flysystem-aws-s3-v3": "^1.0", / / 1.0.22 "league/flysystem-cached-adapter": "^1.0", // 1.0.9 "liip/imagine-bundle": "^2.1", // 2.1.0 "nexylan/slack-bundle" : "^2.0,<2.2.0", // v2.1.0 "oneup/flysystem-bundle": "^3.0", // 3.0.3 "php-http/guzzle6-adapter": "^1.1", / / v1.1.1 "sensio/framework-extra-bundle": "^5.1", // v5.3.1 "stof/doctrine-extensions-bundle": "^1.3", // v1.3.0 "symfony/asset": " ^4.0", // v4.2.5 "symfony/console": "^4.0", // v4.2.5 "symfony/flex": "^1.9", // v1.17.6 "symfony/form": "^ 4.0 ", // v4.2.5 "symfony/framework-bundle": "^4.0", // v4.2.5 "symfony/orm-pack": "^1.0", // v1.0.6 "symfony/security-bundle " : "^4.0", // v4.2.5 "symfony/paquete serializador": " ^1.0", // v1.0 .2 "symfony/twig-bundle": "^4.0", // v4.2.5 "symfony/validator": "^4.0", // v4.2.5 "symfony/web- server-bundle": "^4.0" , // v4.2.5 "symfony/webpack-encore-bundle": "^1.4", // v1.5.0 "symfony/yaml": "^4.0", // v4. 2.5 "twig/extensiones": "^ 1.5" // v1.5.4 }, "require-dev": { "doctrine/doctrine-fixtures-bundle": "^3.0", // 3.1.0 "easycorp/easy- log-manipulador": "^1.0. 2", // v1.0.7 "fzaninotto/faker": "^1.7", // v1.8.0 "symfony/debug-bundle": "^3.3|^4.0", // v4.2.5 "symfony/dotenv" : "^4.0", // v4.2.5 "symfony/maker-bundle": "^1.0", // v1.11.5 "symfony/monolog-bundle": "^3.0", // v3.3.1 "symfony/ phpunit-bridge": "^3.3|^4.0", // v4.2.5 "symfony/profiler-pack": "^1.0", // v1.0.4 "symfony/var-dumper": "^3.3|^4.0 " // v4.2.5 }}
What JavaScript libraries does this tutorial use?
// paquete.json{ "devDependencies": { "@symfony/webpack-encore": "^0.27.0", // 0.27.0 "autocomplete.js": "^0.36.0", "autoprefixer": " ^9.5.1", // 9.5.1 "bootstrap": "^4.3.1", // 4.3.1 "core-js": "^3.0.0", // 3.0.1 "dropzone": " ^5.5.1", // 5.5.1 "fuente impresionante": "^4.7.0", // 4.7.0 "jquery": "^3.4.0", // 3.4.0 "popper.js" : "^1.15.0", "postcss-loader": "^3.0.0", // 3.0.0 "sass": "^1.29.0", // 1.29.0 "sass-loader": "^ 7.0 .1", // 7.3.1 "sortablejs": "^1.8.4", // 1.8.4 "webpack-notifier": "^1.6.0" // 1.7.0 }}