Installing Encore

Installing Encore

First, make sure you install Node.js and also the Yarn package manager. The following instructions depend on whether you are installing Encore in a Symfony application or not.

Installing Encore in Symfony Applications

Run these commands to install both the PHP and JavaScript dependencies in your project:

  1. $ composer require symfony/webpack-encore-bundle
  2. $ yarn install

If you are using Symfony Flex, this will install and enable the WebpackEncoreBundle, create the assets/ directory, add a webpack.config.js file, and add node_modules/ to .gitignore. You can skip the rest of this article and go write your first JavaScript and CSS by reading Encore: Setting up your Project!

If you are not using Symfony Flex, you’ll need to create all these directories and files by yourself following the instructions shown in the next section.

Installing Encore in non Symfony Applications

Install Encore into your project via Yarn:

  1. $ yarn add @symfony/webpack-encore --dev
  2. # if you prefer npm, run this command instead:
  3. $ npm install @symfony/webpack-encore --save-dev

This command creates (or modifies) a package.json file and downloads dependencies into a node_modules/ directory. Yarn also creates/updates a yarn.lock (called package-lock.json if you use npm).

Tip

You should commit package.json and yarn.lock (or package-lock.json if using npm) to version control, but ignore node_modules/.

Creating the webpack.config.js File

Next, create a new webpack.config.js file at the root of your project. This is the main config file for both Webpack and Webpack Encore:

  1. var Encore = require('@symfony/webpack-encore');
  2. // Manually configure the runtime environment if not already configured yet by the "encore" command.
  3. // It's useful when you use tools that rely on webpack.config.js file.
  4. if (!Encore.isRuntimeEnvironmentConfigured()) {
  5. Encore.configureRuntimeEnvironment(process.env.NODE_ENV || 'dev');
  6. }
  7. Encore
  8. // directory where compiled assets will be stored
  9. .setOutputPath('public/build/')
  10. // public path used by the web server to access the output path
  11. .setPublicPath('/build')
  12. // only needed for CDN's or sub-directory deploy
  13. //.setManifestKeyPrefix('build/')
  14. /*
  15. * ENTRY CONFIG
  16. *
  17. * Add 1 entry for each "page" of your app
  18. * (including one that's included on every page - e.g. "app")
  19. *
  20. * Each entry will result in one JavaScript file (e.g. app.js)
  21. * and one CSS file (e.g. app.css) if your JavaScript imports CSS.
  22. */
  23. .addEntry('app', './assets/js/app.js')
  24. //.addEntry('page1', './assets/js/page1.js')
  25. //.addEntry('page2', './assets/js/page2.js')
  26. // When enabled, Webpack "splits" your files into smaller pieces for greater optimization.
  27. .splitEntryChunks()
  28. // will require an extra script tag for runtime.js
  29. // but, you probably want this, unless you're building a single-page app
  30. .enableSingleRuntimeChunk()
  31. /*
  32. * FEATURE CONFIG
  33. *
  34. * Enable & configure other features below. For a full
  35. * list of features, see:
  36. * https://symfony.com/doc/current/frontend.html#adding-more-features
  37. */
  38. .cleanupOutputBeforeBuild()
  39. .enableBuildNotifications()
  40. .enableSourceMaps(!Encore.isProduction())
  41. // enables hashed filenames (e.g. app.abc123.css)
  42. .enableVersioning(Encore.isProduction())
  43. // enables @babel/preset-env polyfills
  44. .configureBabelPresetEnv((config) => {
  45. config.useBuiltIns = 'usage';
  46. config.corejs = 3;
  47. })
  48. // enables Sass/SCSS support
  49. //.enableSassLoader()
  50. // uncomment if you use TypeScript
  51. //.enableTypeScriptLoader()
  52. // uncomment to get integrity="..." attributes on your script & link tags
  53. // requires WebpackEncoreBundle 1.4 or higher
  54. //.enableIntegrityHashes(Encore.isProduction())
  55. // uncomment if you're having problems with a jQuery plugin
  56. //.autoProvidejQuery()
  57. // uncomment if you use API Platform Admin (composer require api-admin)
  58. //.enableReactPreset()
  59. //.addEntry('admin', './assets/js/admin.js')
  60. ;
  61. module.exports = Encore.getWebpackConfig();

Next, open the new assets/js/app.js file which contains some JavaScript code and imports some CSS:

  1. // assets/js/app.js
  2. /*
  3. * Welcome to your app's main JavaScript file!
  4. *
  5. * We recommend including the built version of this JavaScript file
  6. * (and its CSS file) in your base layout (base.html.twig).
  7. */
  8. // any CSS you import will output into a single css file (app.css in this case)
  9. import '../css/app.css';
  10. // Need jQuery? Install it with "yarn add jquery", then uncomment to import it.
  11. // import $ from 'jquery';
  12. console.log('Hello Webpack Encore! Edit me in assets/js/app.js');

And the new assets/css/app.css file:

  1. /* assets/css/app.css */
  2. body {
  3. background-color: lightgray;
  4. }

You’ll customize and learn more about these file in Encore: Setting up your Project.

Caution

Some of the documentation will use features that are specific to Symfony or Symfony’s WebpackEncoreBundle. These are optional, and are special ways of pointing to the asset paths generated by Encore that enable features like versioning and split chunks.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.