Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine
This tutorial has a new version, check it out!

Webpack Encore: A Party for your Assets

1:23:35

What you'll be learning

// composer.json
{
    "require": {
        "php": "^7.2.0",
        "ext-iconv": "*",
        "composer/package-versions-deprecated": "^1.11", // 1.11.99
        "doctrine/doctrine-bundle": "^1.6", // 1.8.1
        "doctrine/doctrine-cache-bundle": "^1.2", // 1.3.2
        "doctrine/doctrine-fixtures-bundle": "~3.0", // 3.0.2
        "doctrine/doctrine-migrations-bundle": "^1.2", // v1.3.1
        "doctrine/orm": "^2.5", // v2.7.2
        "friendsofsymfony/jsrouting-bundle": "^2.2", // 2.2.0
        "friendsofsymfony/user-bundle": "dev-master", // dev-master
        "sensio/framework-extra-bundle": "^5.1", // v5.1.5
        "symfony/asset": "^4.0", // v4.0.4
        "symfony/console": "^4.0", // v4.0.4
        "symfony/flex": "^1.0", // v1.17.6
        "symfony/form": "^4.0", // v4.0.4
        "symfony/framework-bundle": "^4.0", // v4.0.4
        "symfony/lts": "^4@dev", // dev-master
        "symfony/monolog-bundle": "^3.1", // v3.1.2
        "symfony/polyfill-apcu": "^1.0", // v1.7.0
        "symfony/serializer-pack": "^1.0", // v1.0.1
        "symfony/swiftmailer-bundle": "^3.1", // v3.1.6
        "symfony/twig-bundle": "^4.0", // v4.0.4
        "symfony/validator": "^4.0", // v4.0.4
        "symfony/yaml": "^4.0", // v4.0.4
        "twig/twig": "2.10.*" // v2.10.0
    },
    "require-dev": {
        "symfony/debug-pack": "^1.0", // v1.0.4
        "symfony/dotenv": "^4.0", // v4.0.4
        "symfony/phpunit-bridge": "^4.0", // v4.0.4
        "symfony/web-server-bundle": "^4.0" // v4.0.4
    }
}
// package.json
{
    "devDependencies": {
        "@symfony/webpack-encore": "^0.19.0", // 0.19.0
        "bootstrap": "3", // 3.3.7
        "copy-webpack-plugin": "^4.4.1", // 4.4.1
        "font-awesome": "4", // 4.7.0
        "jquery": "^3.3.1", // 3.3.1
        "node-sass": "^4.7.2", // 4.7.2
        "sass-loader": "^6.0.6", // 6.0.6
        "sweetalert2": "^7.11.0", // 7.11.0
        "webpack-notifier": "^1.5.1" // 1.5.1
    }
}

Webpack... it's amazing! It will change the way you develop, giving you the power to write JavaScript modules, use front-end frameworks and optimize your assets!

But... Webpack is hard! Heck, we created a 3 hour tutorial all about it! That's why Webpack Encore: a library that drastically simplifies Webpack, so you can get back to developing. In this tutorial, we'll cover:

  • Installing and getting Encore rocking!
  • Downloading & using external libraries, like jQuery
  • Using multiple JavaScript entry points
  • Pre-processing through Sass
  • Bonus features: enabling React.js, Vue or PostCSS processing
  • Sourcemaps & versioning
  • Optimizing with a "shared" commons entry
  • Handling jQuery plugins
  • The import & export syntax
  • production builds
  • automatic asset versioning

This is going to be a blast: an awesome new asset setup, without the headache!


Your Guides

Ryan Weaver


Join the Conversation?

44
Login or Register to join the conversation
Default user avatar
Default user avatar nacholibrev | posted 4 years ago

Great videos!

1 Reply

Cheers! Thanks for the nice comment :)

1 Reply
Default user avatar
Default user avatar Oliver Davies | posted 5 years ago

Looking forward to this! I've just found Webpack Encore and this weekend I've plugged it into my Sculpin site, a Symfony app for my local PHP user group, and a couple of Drupal sites. :)

1 Reply

"Webpack Encore: Frontend like a Pro!" or this ? I'm a bit confused, which of the two do you recommend considering that I use symfony 6 ? Thanks

Reply

Hey @pasquale_pellicani ,

This course is a legacy course, you can see it when you open any video page in it. And it links to the updated course which is https://symfonycasts.com/screencast/webpack-encore . So, yes, definitely take a look at the updated tutorial instead: https://symfonycasts.com/screencast/webpack-encore :)

Cheers!

Reply
isGzmn Avatar

Hello everyone!
Any advice to migrate from Assetic to Webpack?
I would like to start using Webpack in my actual projects :)

Reply

Hey @Is_g!

It's been a LONG time since we made this transition, so I can't remember exactly! I think the transition will probably need to be done "all at once". With Assetic, you basically list all of the JavaScript (or CSS files) you want to be combined via the javascripts/stylesheets tag. So probably the simplest thing would be to:

A) For each javascripts tag, create a new webpack entry. For example, for the javascripts tag in base.html.twig, I'd make an entry called "app".
B) In the app.js file, import all of the files that were previously in the javascripts blocks - e.g.


// assets/app.js
// import the files from your old javascripts block
import './first_file';
import './second_file';

C) Replace the javascripts tag with encore_entry_script_tags('app').

For the stylesheets in base.html.twig, I would also import those from assets/app.js:


// assets/app.js
import './first_file';
import './second_file';

import './stylesheets/app.css';

Then in app.css:


@import './first_file';
@import './second_file';

And use encore_entry_link_tags('app').

Let me know if that makes sense :). Once you have this working, THEN you can start to refactor your JavaScript files to properly "export" values and then import them - i.e. once you have things working, THEN you can think about taking advantage of Webpack's ability to import/export files.

Cheers!

Reply
Miloud B. Avatar
Miloud B. Avatar Miloud B. | posted 4 years ago

Hi,
do you think that we absolutely have to read the preview course "Webpack for Module Loading Awesomeness" before this one, to understand webpack-encore ? Or it's not required
Thanks !

@weaverryan need an answer please :) I am impatient to buy the course

Reply

Hey Nacer,

Good question! Well, it's not required, and most of the job Webpack Encore do for you, in some cases you need to see some examples and copy/paste some code and that's it. But if you want to completely understand how Webpack Encore works - you need to know Webpack, because Webpack Encore is just a nice wrapper around Webpack that tries to make things much simpler from the developer perspective. And if you need some complex things, it's good to know webpack because Webpack Encore capabilities are limited yet, it's a young library. So some complex stuff you might need to do directly with Webpack.

In short, you *can* start with Webpack Encore, and if you would need more information or would like know more about internals - complete another one course about Webpack.

I hope this helps!

Cheers!

Reply
Default user avatar
Default user avatar Anton Bagdatyev | Victor | posted 3 years ago

Which other course about Webpack internals, in order to fully master it without using Encore, would you advise?

Reply

Hey Anton,

We have Webpack Encore course: https://symfonycasts.com/sc... - and we recommend to use Encore instead of using Webpack directly. Actually, Webpack Encore is just a simple wrapper around Webpack that makes your life easier, especially if you're a backend developer. But it also make frontend developer's life easier as well. But in case you're interested in learning internals and know a bit more about Webpack and how it works - I'd recommend you to look at another course: https://symfonycasts.com/sc...

I hope this helps!

Cheers!

Reply
Default user avatar
Default user avatar Anton Bagdatyev | Victor | posted 3 years ago | edited

Thanks Victor! In the last link you gave me, it is mentioned: This tutorial covers Webpack 3, and Webpack 4 has now been released. But don't worry! The vast majority of the concepts are still the same. So, if you want to learn Webpack & module loading, you're still in the right place. The biggest differences are in the CommonsChunkPlugin area.

Will you ever release a course like that dealing with Webpack internals and pitfalls without Encore, but for Webpack 4 instead of Webpack 3 and using SplitChunksPlugin instead of the old CommonsChunkPlugin? Thanks!

Reply

Hey Anton Bagdatyev!

> Will you ever release a course like that dealing with Webpack internals and pitfalls without Encore, but for Webpack 4 instead of Webpack 3 and using `SplitChunksPlugin` instead of the old `CommonsChunkPlugin`?

I can answer that :). Probably.... no ;). This "Split chunks" thing is a *super* complex part of Webpack... and it's *likely* not one that you will ever need to worry about. If you want to be extra comfortable with Webpack so that you're *really* comfortable with Encore, I'd still recommend the Webpack tutorial that Victor linked to. It *is* becoming more out-of-date, but it will show you how Webpack works and how its configuration system works. You'll then be much more comfortable with how Encore is (basically) just a Webpack config generator :). Oh, but skip the CommonsChunk stuff entirely - that is now SUPER old.

Cheers!

Reply
Default user avatar
Default user avatar Anton Bagdatyev | weaverryan | posted 3 years ago

OK, thank you very much!

Reply

Hey Anton,

Yes, it might happen in the future, but unfortunately not soon. We're actively working on some new tutorials right now (even one JS tutorial), and have some pending tutorials as well. In other words, a lot of good new stuff is coming, but not Webpack yet, so I can't say when it might be released.

Anyway, the content of that Webpack course is still valid, and the difference is minor. Moreover, we have some notes in that course that would help you if you're using Webpack 4.

And as always, if you have any problems following the course - please, let us know in the comments below videos and we will try to help you!

Cheers!

Reply
Default user avatar
Default user avatar Anton Bagdatyev | Victor | posted 3 years ago

Great, thanks!

Reply
Miloud B. Avatar
Miloud B. Avatar Miloud B. | Victor | posted 4 years ago | edited

victor

Perfect, thanks a lot,

Cheers!

Reply
Default user avatar
Default user avatar Daniel Kronika | posted 5 years ago

Hi there! Thank you very much for your courses about ECMAScript6 and Weback/Webpack Encore! But there is one thing I think about: In the first tutorial you explained a lot about babel to compile new JS to older browsers. In the Webpack Encore course you did‘t talk about babel anymore. So my question is if it isn‘t still necessary to compile new JS with babel when using Webpack Encore? Maybe because this functionality is already included automatically? Or if not: what would be the best practice to compile JS with babel when using Webpack Encore? Thanks a lot for your help and advices!!!

Reply

Hey Daniel Kronika!

Very good attention to detail! Yes, this it is still necessary to transpile with Babel (actually, I'll say more about this in a moment). And YES, with Encore, it happens automatically :). Encore automatically transpiles things using the Babel "env" preset and includes some configuration (https://github.com/symfony/... that tells Babel to transpile any code needed so that your JavaScript is compatible with all browsers that are used by more than 1% of the population.

So actually, back to this question:

> is it still necessary to compile new JS with babel when using Webpack

This answer I gave above is yes. However, it really depends on your users. If your users use newer browsers, you can tweak the 1% config (that I listed above) so that *less* features are transpiled. Basically, as your users use newer browsers, less features must be transpiled. Transpiling makes your final JS a little bit larger. I leave the 1%, but if you want, you can look at your analytics, and use the Encore.configureBabel() method (https://github.com/symfony/... to tweak that "browsers" config to some other value.

I hope that clarifies! Cheers!

Reply

Ryan. This means that we can use all ES6 features and webpack will transpile it?

Reply

Hey micayael

If you are using Webpack by yourself, then no, you will have to setup babel to actually do it, but if you are using Encore, then you don't have to :)

Cheers!

Reply

Hi, there!
Thanks for Your tutorials, they are great!

PhpStorm can't recognise webpack.config.js:
"Can't analyse webpack.config.js: coding assistance will ignore module resolution rules in this file. Possible reasons: this file is not a valid webpack configuration file or its format is not currently supported by the IDE".
What am i doing wrong with PhpStorm settings?
Thanks!

Best Regards :)

Reply
Victor Avatar Victor | SFCASTS | niumis | posted 5 years ago | edited

Hey Niumis,

Thanks for your feedback!

What are you doing to get this error? Eventually, Encore.getWebpackConfig() should return a valid webpack config. Do you export the result like: module.exports = Encore.getWebpackConfig();? Do you have any JS errors in Chrome's Console when loading a page which use Webpack features?

Cheers!

Reply

Hey, Victor!

Thank you for reply!

I just load symfony-demo app into PhpStorm. Nothing change in project and PhpStorm preferences. PhpStorm says "Can't analyse webpack.config.js: ..." and "Unresolved function or method" in all functions in Encore block. Maybe need configure PhpStorm or something else?
Sorry for my bad English...

Cheers :)

P.S. Chrome's Console it's ok, nothing errors found.

Reply
Default user avatar

Hi, here's how I did it to avoid PHPStorm error : https://gist.github.com/plf...

1 Reply

Hey Pilou,

Wow, what a tricky workaround! Thank you for sharing it with others! :)

Cheers!

Reply

We have an issue open on the official Encore repository about this problem in PhpStorm. It CAN be ignored, but you actually lose auto-completion for any aliases or resolve paths defined in your webpack config. If you care enough, the gist above is a bit ugly, but honestly, works great :).

Reply

Hey niumis!

So, let me answer this in 2 parts :).

1) "Can't analyse webpack.config.js": this is nothing to worry about. PhpStorm recently added webpack.config.js support. But since Encore takes care of all of the complex stuff for you, it's ok that PhpStorm doesn't help us.

2) "Unresolved function or method": this is indeed a bug in Encore. It doesn't affect anything, but due to a change we made a few months ago, PhpStorm now has a hard time auto-completing methods. It's something we should be able to fix in Encore pretty soon.

Cheers!

Reply

Okay, I now understand. Thanks, guys!

Best Regards!

Reply
Default user avatar
Default user avatar Ankhzet | weaverryan | posted 5 years ago | edited

weaverryan , "this is nothing to worry about"? "it's ok that PhpStorm doesn't help us"?
But it's more like "Encore stands in the way of PhpStorm", not "PhpStorm doesn't help us".
In current implementation, when you use Encore in webpack.config, you are _forced_ to use it _only_ via Encore bin. If you try to use such config in any way, except through Encore, it will just complain "Blah blah blah Are you trying to use index.js directly? Rly? Blah blah...", 'cause Encore's webpack config builder requires some webpack runtime configuration data, which is provided by Encore's bin script. Webpack's config files, in general, are supposed to be self-contained to be interusable with other tools (as their only purpose is to provide build config object, no side effects are expected). With Encore, it will be only usable by Encore itself.
Thus, no Webpack integration with ESLint, no Webpack integration with PhpStorm, no Webpack integration with, well, anything. Except Encore, ofc. Not critical, sure, but being unable to use, f.e., Webpack's resolver config is a cons.

Don't get me wrong, Encore is definitely a great tool, but mostly if your project is more backend'y, than frontend'y. Not so great for SPA projects and alike, or for projects that have extensive client-side part, 'cause it breaks integration with some must-have tools in frontend dev toolkit.

Is it going to be fixed, somewhen, somehow?
Thanx in advance.

Reply

Hey there!

Sorry for my slow reply - the holidays :)

First, hmm, I suppose you could say that about Encore standing in the way of PhpStorm :). I disagree, but fair enough! Encore is a webpack config generator. So, if you decide to use Encore, then you're no longer writing webpack config directly anymore. This means that any tools that help with this config - e.g. PhpStorm - will not work. Btw, the reason we require the encore config is because we need to capture certain flags passed to encore so that we can adjust our generated config. And actually, you can work around this: https://github.com/symfony/webpack-encore/pull/115. Perhaps this just isn't documented well enough?

(Encore is ...) not so great for SPA projects and alike, or for projects that have extensive client-side part, 'cause it breaks integration with some must-have tools in frontend dev toolkit.

But I am curious about this. What tools do you mean that won't work? Does the configureRuntimeEnvironment() method I listed above solve these issues?

Is it going to be fixed, somewhen, somehow?

You're referring to the bug about the PhpStorm auto-completion. We have an issue on it: https://github.com/symfony/webpack-encore/issues/151. I hope we'll fix it soon - or someone in the community can :). I've had to focus on other parts of Symfony for the Symfony 4 release, but will come back to Encore for some great changes soon.

Cheers!

Reply
Default user avatar

Yay, thats really what I was searching for!

> Perhaps this just isn't documented well enough?
Definitely. There's just _nothing_ in the docs, about the possibility to being able to keep webpack configs at least semi-autonomous.

> What tools do you mean that won't work?
Any tools, that are relaying on webpack resolvers to do their job. F.e. ESLint+vue-plugin wouldn't be able to process ".vue" files, it would stumble upon trying to resolve imports in that files. That's not to mention aliases, externals, etc. Same goes for some testing tools (had a hard time trying to configure Wallaby+jasmine). Also, PhpStorm becomes unavailable to resolve aliases in imports (which is a thing on itself), and thus type-completion/hinting becomes less-precise. Also, it becomes quite a riddle to try to configure ESLint's rule 'import/extensions'. etc. etc.

> You're referring to the bug about the PhpStorm auto-completion.
No, that one is bearable, I was referring to webpack configs becoming unusable outside of Encore's bin in general. As far as I can tell, '.configureRuntimeEnvironment()' would be enough to fix that, thank you very much for the guidance!

Reply

Hey here!

> > Perhaps this just isn't documented well enough?
> Definitely. There's just _nothing_ in the docs, about the possibility to being able to keep webpack configs at least semi-autonomous.

I've got an issue open to fix this :).

> > What tools do you mean that won't work?
> Any tools, that are relaying on webpack resolvers to do their job. F.e. ESLint+vue-plugin wouldn't be able to process ".vue" files, it would stumble upon trying to resolve imports in that files. That's not to mention aliases, externals, etc. Same goes for some testing tools (had a hard time trying to configure Wallaby+jasmine). Also, PhpStorm becomes unavailable to resolve aliases in imports (which is a thing on itself), and thus type-completion/hinting becomes less-precise. Also, it becomes quite a riddle to try to configure ESLint's rule 'import/extensions'. etc. etc.

Yes, now I'm more aware/understanding about the PhpStorm aliases issue. We have any issue open on this too - but, of course, this could be tricky to fix :/.

About ESLint+vue-plugin, can you tell me more about this? Why does Encore make this a problem versus how Webpack works?

> > You're referring to the bug about the PhpStorm auto-completion.
No, that one is bearable, I was referring to webpack configs becoming unusable outside of Encore's bin in general. As far as I can tell, '.configureRuntimeEnvironment()' would be enough to fix that, thank you very much for the guidance!

Awesome :).

Cheers!

Reply
Adrian Avatar

Hi guys - one question - why you require everything with --dev parameter? Isn't jquery module needed in production? I really don't get it :)

Reply

Hey Adrian!

Great question - I get this a lot actually. Basically.... it doesn't matter :). But, technically, everything should be done as a dev dependency. The reason is that you actually do NOT need the jquery module on your production server: you only need the jquery module to exist during your *build* process so that you can create the final .js and .css files that are needed on production. In fact, we don't need ANY of the Webpack, Encore or node stuff on production at all.

But, like I said, it doesn't really matter. It would only matter in the following situation: you are actually building a Node web app that WILL be running on your production server in addition to using Webpack Encore. In that case, the modules needed for the web app should be required without --dev, but the Webpack Encore stuff *should* have --dev. That would allow you to include only the "Node web app" dependencies on production. Super weird edge-case... but there it is ;)

Cheers!

Reply
Adrian Avatar

Ok, you're right, thanks Ryan! :)

Reply
Default user avatar
Default user avatar Jeremy Jumeau | posted 5 years ago

Great! I started using Encore a couple of days ago, it's amazingly easy to setup, I'm excited to learn deeper from its creator! Thank you Ryan and good luck!

Reply

Woohoo! Really glad to hear about your success already - that makes my day! More to come :)

Reply
Default user avatar
Default user avatar Jeremy Jumeau | weaverryan | posted 5 years ago

:)

Reply
Kaizoku Avatar
Kaizoku Avatar Kaizoku | posted 5 years ago

With SF4 released, we really need this !
Can't wait.

Reply
Zorpen Avatar
Zorpen Avatar Zorpen | posted 5 years ago | edited

there Are we there yet? ;)

Reply

Hey Zorpen!

Bah, not yet! With the Symfony 4 release, we need to get the first big Symfony tutorial out first. But this will be next! I'm hoping to release a lot of good stuff in a few weeks / January. Thanks for your patience!

Cheers!

Reply
Zorpen Avatar
Zorpen Avatar Zorpen | weaverryan | posted 5 years ago | edited

Thanks weaverryan
Will Joyful Development with Symfony 4 be some kind of fork of AquaNote, or brand new project?
Either way - can't wait!

Reply

Brand new project :) And we *just* designed it: "Deep Space Junk". More soon....

Reply
Cat in space

"Houston: no signs of life"
Start the conversation!

userVoice