Skip to content

Latest commit

 

History

History
148 lines (110 loc) · 5.78 KB

frontend.rst

File metadata and controls

148 lines (110 loc) · 5.78 KB

Front-end Tools: Handling CSS & JavaScript

Symfony gives you the flexibility to choose any front-end tools you want. This could be dead-simple - like putting CSS & JS directly in the public/ directory - or more advanced - like scaffolding your front-end with a tool like Next.js.

However, Symfony does come with two powerful options to help you build a modern, fast frontend, and enjoy the process:

  • Webpack Encore <frontend-webpack-encore> is a powerful tool built with Node.js on top of Webpack that allows you to write modern CSS & JavaScript and handle things like JSX (React), Vue or TypeScript.
  • AssetMapper <frontend-asset-mapper>, is a production-ready simpler alternative to Webpack Encore that runs entirely in PHP.
Encore AssetMapper
Production Ready? yes yes
Stable? yes yes
Requirements Node.js none: pure PHP
Requires a build step? yes no
Works in all browsers? yes yes
Supports Stimulus/UX yes yes
Supports Sass/Tailwind yes yes <asset-mapper-tailwind>
Supports React, Vue, Svelte? yes yes [1] <ux-note-1>
Supports TypeScript yes no [1] <ux-note-1>

[1] Using JSX (React), Vue or TypeScript with AssetMapper is possible, but you'll need to use their native tools for pre-compilation. Also, some features (like Vue single-file components) cannot be compiled down to pure JavaScript that can be executed by a browser.

Webpack Encore

Do you prefer video tutorials? Check out the Webpack Encore screencast series.

Webpack Encore is a simpler way to integrate Webpack into your application. It wraps Webpack, giving you a clean & powerful API for bundling JavaScript modules, pre-processing CSS & JS and compiling and minifying assets. Encore gives you a professional asset system that's a delight to use.

Encore is inspired by Webpacker and Mix, but stays in the spirit of Webpack: using its features, concepts and naming conventions for a familiar feel. It aims to solve the most common Webpack use cases.

Tip

Encore is made by Symfony and works beautifully in Symfony applications. But it can be used in any PHP application and even with other server-side programming languages!

Encore Documentation

Getting Started

  • Installation </frontend/encore/installation>
  • Using Webpack Encore </frontend/encore/simple-example>

Adding more Features

  • CSS Preprocessors: Sass, LESS, etc. </frontend/encore/css-preprocessors>
  • PostCSS and autoprefixing </frontend/encore/postcss>
  • Enabling React.js </frontend/encore/reactjs>
  • Enabling Vue.js (vue-loader) </frontend/encore/vuejs>
  • /frontend/encore/copy-files
  • Configuring Babel </frontend/encore/babel>
  • Source maps </frontend/encore/sourcemaps>
  • Enabling TypeScript (ts-loader) </frontend/encore/typescript>

Optimizing

  • Versioning (and the entrypoints.json/manifest.json files) </frontend/encore/versioning>
  • Using a CDN </frontend/encore/cdn>
  • /frontend/encore/code-splitting
  • /frontend/encore/split-chunks
  • /frontend/encore/url-loader

Guides

  • Using Bootstrap CSS & JS </frontend/encore/bootstrap>
  • jQuery and Legacy Applications </frontend/encore/legacy-applications>
  • Passing Information from Twig to JavaScript </frontend/encore/server-data>
  • webpack-dev-server and Hot Module Replacement (HMR) </frontend/encore/dev-server>
  • Adding custom loaders & plugins </frontend/encore/custom-loaders-plugins>
  • Advanced Webpack Configuration </frontend/encore/advanced-config>
  • Using Encore in a Virtual Machine </frontend/encore/virtual-machine>

Issues & Questions

  • FAQ & Common Issues </frontend/encore/faq>

Full API

AssetMapper

AssetMapper is an alternative to Webpack Encore that runs entirely in PHP without any complex build steps. It leverages the importmap feature of your browser, which is available in all browsers thanks to a polyfill.

Read the AssetMapper Documentation </frontend/asset_mapper>

Stimulus & Symfony UX Components

To learn about Stimulus & the UX Components, see: the StimulusBundle Documentation

Other Front-End Articles

  • /frontend/create_ux_bundle
  • /frontend/custom_version_strategy