Skip to content

Commit 1aba68d

Browse files
committed
updating Encore docs for 1.0
1 parent d7899c3 commit 1aba68d

File tree

10 files changed

+85
-175
lines changed

10 files changed

+85
-175
lines changed

frontend.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ Optimizing
6161
* :doc:`Using a CDN </frontend/encore/cdn>`
6262
* :doc:`/frontend/encore/code-splitting`
6363
* :doc:`/frontend/encore/split-chunks`
64-
* :doc:`Creating a "Shared" entry for re-used modules </frontend/encore/shared-entry>`
6564
* :doc:`/frontend/encore/url-loader`
6665

6766
Guides

frontend/encore/advanced-config.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ To do that, modify the config after fetching it from Encore:
1212
1313
// webpack.config.js
1414
15-
var Encore = require('@symfony/webpack-encore');
15+
const Encore = require('@symfony/webpack-encore');
1616
1717
// ... all Encore config here
1818
1919
// fetch the config, then modify it!
20-
var config = Encore.getWebpackConfig();
20+
const config = Encore.getWebpackConfig();
2121
2222
// add an extension
2323
config.resolve.extensions.push('json');
@@ -208,8 +208,8 @@ The following code is equivalent:
208208
The following loaders are configurable with ``configureLoaderRule()``:
209209
- ``javascript`` (alias ``js``)
210210
- ``css``
211-
- ``images``
212-
- ``fonts``
211+
- ``images`` (but use ``configureImageRule()`` instead)
212+
- ``fonts`` (but use ``configureFontRule()`` instead)
213213
- ``sass`` (alias ``scss``)
214214
- ``less``
215215
- ``stylus``

frontend/encore/copy-files.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ To reference an image tag from inside a JavaScript file, *require* the file:
1616
1717
// returns the final, public path to this file
1818
// path is relative to this file - e.g. assets/images/logo.png
19-
const logoPath = require('../images/logo.png');
19+
import logoPath from '../images/logo.png';
2020
2121
let html = `<img src="${logoPath}" alt="ACME logo">`;
2222

frontend/encore/dev-server.rst

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ While developing, instead of using ``yarn encore dev --watch``, you can use the
88
99
$ yarn encore dev-server
1010
11-
This builds and serves the front-end assets from a new server. This server runs at
12-
``localhost:8080`` by default, meaning your build assets are available at ``localhost:8080/build``.
13-
This server does not actually write the files to disk; instead it servers them from memory,
11+
This builds and serves the front-end assets from a new server. This server runs at
12+
``localhost:8080`` by default, meaning your build assets are available at ``localhost:8080/build``.
13+
This server does not actually write the files to disk; instead it servers them from memory,
1414
allowing for hot module reloading.
1515

16-
As a consequence, the ``link`` and ``script`` tags need to point to the new server. If you're using the
17-
``encore_entry_script_tags()`` and ``encore_entry_link_tags()`` Twig shortcuts (or are
18-
:ref:`processing your assets through entrypoints.json <load-manifest-files>` in some other way),
16+
As a consequence, the ``link`` and ``script`` tags need to point to the new server. If you're using the
17+
``encore_entry_script_tags()`` and ``encore_entry_link_tags()`` Twig shortcuts (or are
18+
:ref:`processing your assets through entrypoints.json <load-manifest-files>` in some other way),
1919
you're done: the paths in your templates will automatically point to the dev server.
2020

2121
Enabling HTTPS using the Symfony Web Server
@@ -33,6 +33,40 @@ server SSL certificate:
3333
# Windows
3434
$ encore dev-server --https --pfx=%UserProfile%\.symfony\certs\default.p12
3535
36+
To avoid repeating yourself, you can also update ``package.json`` to include
37+
these options automatically:
38+
39+
.. code-block:: diff
40+
41+
{
42+
...
43+
"scripts": {
44+
- "dev-server": "encore dev-server",
45+
+ "dev-server": "encore dev-server --https --pfx=$HOME/.symfony/certs/default.p12 --allowed-hosts=mydomain.wip",
46+
...
47+
}
48+
}
49+
50+
If you experience issues related to CORS (Cross Origin Resource Sharing), add
51+
the ``--disable-host-check`` and ``--port`` options to the ``dev-server``
52+
command in the ``package.json`` file:
53+
54+
.. code-block:: diff
55+
56+
{
57+
...
58+
"scripts": {
59+
- "dev-server": "encore dev-server",
60+
+ "dev-server": "encore dev-server --port 8080 --disable-host-check",
61+
...
62+
}
63+
}
64+
65+
.. caution::
66+
67+
Beware that `it's not recommended to disable host checking`_ in general, but
68+
here it's required to solve the CORS issue.
69+
3670
dev-server Options
3771
------------------
3872

@@ -69,49 +103,17 @@ method in your ``webpack.config.js`` file:
69103
Hot Module Replacement HMR
70104
--------------------------
71105

72-
Encore *does* support `HMR`_ for :doc:`Vue.js </frontend/encore/vuejs>`, but
73-
does *not* work for styles anywhere at this time. To activate it, pass the ``--hot``
74-
option:
75-
76-
.. code-block:: terminal
77-
78-
$ ./node_modules/.bin/encore dev-server --hot
79-
80-
If you want to use SSL with self-signed certificates, add the ``--https``,
81-
``--pfx=``, and ``--allowed-hosts`` options to the ``dev-server`` command in
82-
the ``package.json`` file:
83-
84-
.. code-block:: diff
106+
Hot module replacement is a superpower of the ``dev-server`` where styles and
107+
(in some cases) JavaScript can automatically update without needing to reload
108+
your page. HMR works automatically with CSS (as long as you're using the
109+
``dev-server`` and Encore 1.0 or higher) but only works with some JavaScript
110+
(like :doc:`Vue.js </frontend/encore/vuejs>`).
85111

86-
{
87-
...
88-
"scripts": {
89-
- "dev-server": "encore dev-server",
90-
+ "dev-server": "encore dev-server --https --pfx=$HOME/.symfony/certs/default.p12 --allowed-hosts=mydomain.wip",
91-
...
92-
}
93-
}
94-
95-
If you experience issues related to CORS (Cross Origin Resource Sharing), add
96-
the ``--disable-host-check`` and ``--port`` options to the ``dev-server``
97-
command in the ``package.json`` file:
98-
99-
.. code-block:: diff
100-
101-
{
102-
...
103-
"scripts": {
104-
- "dev-server": "encore dev-server",
105-
+ "dev-server": "encore dev-server --port 8080 --disable-host-check",
106-
...
107-
}
108-
}
109-
110-
.. caution::
111-
112-
Beware that `it's not recommended to disable host checking`_ in general, but
113-
here it's required to solve the CORS issue.
112+
.. versionadded:: 1.0.0
114113

114+
Before Encore 1.0, you needed to pass a ``--hot`` flag at the command line
115+
to enable HMR. You also needed to disable CSS extraction to enable HMR for
116+
CSS. That is no longer needed.
115117

116118
.. _`webpack-dev-server`: https://webpack.js.org/configuration/dev-server/
117119
.. _`HMR`: https://webpack.js.org/concepts/hot-module-replacement/

frontend/encore/installation.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ is the main config file for both Webpack and Webpack Encore:
5454

5555
.. code-block:: javascript
5656
57-
var Encore = require('@symfony/webpack-encore');
57+
const Encore = require('@symfony/webpack-encore');
5858
5959
// Manually configure the runtime environment if not already configured yet by the "encore" command.
6060
// It's useful when you use tools that rely on webpack.config.js file.

frontend/encore/reactjs.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Using React? First add some dependencies with Yarn:
1111
.. code-block:: terminal
1212
1313
$ yarn add @babel/preset-react --dev
14-
$ yarn add react react-dom prop-types
14+
$ yarn add react react-dom prop-types --dev
1515
1616
Enable react in your ``webpack.config.js``:
1717

frontend/encore/shared-entry.rst

Lines changed: 0 additions & 42 deletions
This file was deleted.

frontend/encore/simple-example.rst

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ application: it will *require* all of the dependencies it needs (e.g. jQuery or
1919
2020
import './styles/app.css';
2121
22-
// import $ from 'jquery';
23-
2422
Encore's job (via Webpack) is simple: to read and follow *all* of the ``require()``
2523
statements and create one final ``app.js`` (and ``app.css``) that contains *everything*
2624
your app needs. Encore can do a lot more: minify files, pre-process Sass/LESS,
@@ -35,7 +33,7 @@ of your project. It already holds the basic config you need:
3533
.. code-block:: javascript
3634
3735
// webpack.config.js
38-
var Encore = require('@symfony/webpack-encore');
36+
const Encore = require('@symfony/webpack-encore');
3937
4038
Encore
4139
// directory where compiled assets will be stored
@@ -130,6 +128,8 @@ filename(s) to render. This file is *especially* useful because you can
130128
:doc:`enable versioning </frontend/encore/versioning>` or
131129
:doc:`point assets to a CDN </frontend/encore/cdn>` without making *any* changes to your
132130
template: the paths in ``entrypoints.json`` will always be the final, correct paths.
131+
And if you use :doc:`splitEntryChunks() </frontend/split-chunks>` (where Webpack splits the output into even
132+
more files), all the necessary ``script`` and ``link`` tags will render automatically.
133133

134134
If you're *not* using Symfony, you can ignore the ``entrypoints.json`` file and
135135
point to the final, built file directly. ``entrypoints.json`` is only required for
@@ -147,13 +147,13 @@ some optional features.
147147
Requiring JavaScript Modules
148148
----------------------------
149149

150-
Webpack is a module bundler, which means that you can ``require`` other JavaScript
150+
Webpack is a module bundler, which means that you can ``import`` other JavaScript
151151
files. First, create a file that exports a function:
152152

153153
.. code-block:: javascript
154154
155155
// assets/greet.js
156-
module.exports = function(name) {
156+
export default function(name) {
157157
return `Yo yo ${name} - welcome to Encore!`;
158158
};
159159
@@ -163,19 +163,19 @@ We'll use jQuery to print this message on the page. Install it via:
163163
164164
$ yarn add jquery --dev
165165
166-
Great! Use ``require()`` to import ``jquery`` and ``greet.js``:
166+
Great! Use ``import`` to import ``jquery`` and ``greet.js``:
167167

168168
.. code-block:: diff
169169
170170
// assets/app.js
171171
// ...
172172
173173
+ // loads the jquery package from node_modules
174-
+ var $ = require('jquery');
174+
+ import jquery from 'jquery';
175175
176176
+ // import the function from greet.js (the .js extension is optional)
177177
+ // ./ (or ../) means to look for a local file
178-
+ var greet = require('./greet');
178+
+ import greet from './greet';
179179
180180
+ $(document).ready(function() {
181181
+ $('body').prepend('<h1>'+greet('jill')+'</h1>');
@@ -185,37 +185,6 @@ That's it! If you previously ran ``encore dev --watch``, your final, built files
185185
have already been updated: jQuery and ``greet.js`` have been automatically
186186
added to the output file (``app.js``). Refresh to see the message!
187187

188-
The import and export Statements
189-
--------------------------------
190-
191-
Instead of using ``require()`` and ``module.exports`` like shown above, JavaScript
192-
provides an alternate syntax based on the `ECMAScript 6 modules`_ that includes
193-
the ability to use dynamic imports.
194-
195-
To export values using the alternate syntax, use ``export``:
196-
197-
.. code-block:: diff
198-
199-
// assets/greet.js
200-
- module.exports = function(name) {
201-
+ export default function(name) {
202-
return `Yo yo ${name} - welcome to Encore!`;
203-
};
204-
205-
To import values, use ``import``:
206-
207-
.. code-block:: diff
208-
209-
// assets/app.js
210-
- require('../styles/app.css');
211-
+ import './styles/app.css';
212-
213-
- var $ = require('jquery');
214-
+ import $ from 'jquery';
215-
216-
- var greet = require('./greet');
217-
+ import greet from './greet';
218-
219188
.. _multiple-javascript-entries:
220189

221190
Page-Specific JavaScript or CSS (Multiple Entries)

frontend/encore/split-chunks.rst

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ To enable this, call ``splitEntryChunks()``:
2323
2424
2525
Now, each output file (e.g. ``homepage.js``) *may* be split into multiple file
26-
(e.g. ``homepage.js``, ``vendor~homepage.js``). This means that you *may* need to
27-
include *multiple* ``script`` tags (or ``link`` tags for CSS) in your template.
28-
Encore creates an :ref:`entrypoints.json <encore-entrypointsjson-simple-description>`
26+
(e.g. ``homepage.js`` & ``vendors-node_modules_jquery_dist_jquery_js.js`` - the
27+
filename of the second will be less obvious when you build for production). This
28+
means that you *may* need to include *multiple* ``script`` tags (or ``link`` tags
29+
for CSS) in your template. Encore creates an :ref:`entrypoints.json <encore-entrypointsjson-simple-description>`
2930
file that lists exactly which CSS and JavaScript files are needed for each entry.
3031

3132
If you're using the ``encore_entry_link_tags()`` and ``encore_entry_script_tags()``
@@ -37,9 +38,9 @@ tags as needed:
3738

3839
{#
3940
May now render multiple script tags:
40-
<script src="/build/runtime.js"></script>
41-
<script src="/build/vendor~homepage.js"></script>
42-
<script src="/build/homepage.js"></script>
41+
<script src="/build/runtime.js" defer></script>
42+
<script src="/build/vendors-node_modules_jquery_dist_jquery_js.js" defer></script>
43+
<script src="/build/homepage.js" defer></script>
4344
#}
4445
{{ encore_entry_script_tags('homepage') }}
4546

0 commit comments

Comments
 (0)