Skip to content

Commit c2b36a2

Browse files
committed
more work on encore
1 parent 53f9ffe commit c2b36a2

File tree

10 files changed

+209
-291
lines changed

10 files changed

+209
-291
lines changed

frontend/encore/babel.rst

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ Configuring Babel
22
=================
33

44
`Babel`_ is automatically configured for all ``.js`` and ``.jsx`` files via the
5-
``babel-loader`` with sensible defaults (e.g. with the ``env`` preset and
6-
``react`` if requested).
5+
``babel-loader`` with sensible defaults (e.g. with the ``@babel/preset-env`` and
6+
``@babel/preset-react`` if requested).
77

88
Need to extend the Babel configuration further? The easiest way is via
99
``configureBabel()``:
@@ -16,17 +16,37 @@ Need to extend the Babel configuration further? The easiest way is via
1616
Encore
1717
// ...
1818
19-
// first, install any presets you want to use (e.g. yarn add babel-preset-es2017)
20-
// then, modify the default Babel configuration
2119
.configureBabel(function(babelConfig) {
2220
// add additional presets
23-
babelConfig.presets.push('es2017');
21+
// babelConfig.presets.push('@babel/preset-flow');
2422
2523
// no plugins are added by default, but you can add some
2624
// babelConfig.plugins.push('styled-jsx/babel');
25+
}, {
26+
// node_modules is not processed through Babel by default
27+
// but you can whitelist specific modules to process
28+
// include_node_modules: ['foundation-sites']
29+
30+
// or completely control the exclude
31+
// exclude: /bower_components/
2732
})
2833
;
2934
35+
Configuring Browser Targets
36+
---------------------------
37+
38+
The ``@babel/preset-env`` preset rewrites your JavaScript so that the final syntax
39+
will work in whatever browsers you want. To configure the browsers that you need
40+
to support, see :ref:`browserslist_package_config`.
41+
42+
After change our "browerslist" config, you will need to manually remove the babel
43+
cache directory:
44+
45+
.. code-block:: terminal
46+
47+
$ On Unix run this command. On Windows, clear this directory manually
48+
$ rm -rf node_modules/.cache/babel-loader/
49+
3050
Creating a .babelrc File
3151
------------------------
3252

@@ -37,21 +57,7 @@ Babel, but it has a downside: as soon as a ``.babelrc`` file is present,
3757
if you call ``Encore.enableReactPreset()``, the ``react`` preset will *not*
3858
automatically be added to Babel: you must add it yourself in ``.babelrc``.
3959

40-
An example ``.babelrc`` file might look like this:
41-
42-
.. code-block:: json
43-
44-
{
45-
presets: [
46-
['env', {
47-
modules: false,
48-
targets: {
49-
browsers: '> 1%',
50-
uglify: true
51-
},
52-
useBuiltIns: true
53-
}]
54-
]
55-
}
60+
As soon as a ``.babelrc`` file is present, it will take priority over the Babel
61+
configuration added by Encore.
5662

5763
.. _`Babel`: http://babeljs.io/

frontend/encore/css-preprocessors.rst

Lines changed: 16 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,7 @@
1-
CSS Preprocessors: Sass, LESS, etc.
2-
===================================
1+
CSS Preprocessors: Sass, LESS, Stylus, etc.
2+
===========================================
33

4-
Using Sass
5-
----------
6-
7-
To use the Sass pre-processor, install the dependencies:
8-
9-
.. code-block:: terminal
10-
11-
$ yarn add --dev sass-loader node-sass
12-
13-
And enable it in ``webpack.config.js``:
14-
15-
.. code-block:: javascript
16-
17-
// webpack.config.js
18-
// ...
19-
20-
Encore
21-
// ...
22-
.enableSassLoader()
23-
;
24-
25-
That's it! All files ending in ``.sass`` or ``.scss`` will be pre-processed. You
26-
can also pass options to ``sass-loader``:
4+
To use the Sass, LESS or Stylus pre-processors, enable the one you want in ``webpack.config.js``:
275

286
.. code-block:: javascript
297
@@ -32,46 +10,24 @@ can also pass options to ``sass-loader``:
3210
3311
Encore
3412
// ...
35-
.enableSassLoader(function(options) {
36-
// https://github.com/sass/node-sass#options
37-
// options.includePaths = [...]
38-
});
39-
;
40-
41-
Using LESS
42-
----------
43-
44-
To use the LESS pre-processor, install the dependencies:
4513
46-
.. code-block:: terminal
14+
// enable just the one you want
4715
48-
$ yarn add --dev less-loader less
49-
50-
And enable it in ``webpack.config.js``:
51-
52-
.. code-block:: javascript
53-
54-
// webpack.config.js
55-
// ...
16+
// processes files ending in .scss or .sass
17+
.enableSassLoader()
5618
57-
Encore
58-
// ...
19+
// processes files ending in .less
5920
.enableLessLoader()
60-
;
6121
62-
That's it! All files ending in ``.less`` will be pre-processed. You can also pass
63-
options to ``less-loader``:
22+
// processes files ending in .styl
23+
.enableStylusLoader()
24+
;
6425
65-
.. code-block:: javascript
26+
Then restart Encore. When you do, it will give you a command you can run to
27+
install any missing dependencies. After running that command and restarting
28+
Encore, you're done!
6629

67-
// webpack.config.js
68-
// ...
30+
You can also pass configuration options to each of the loaders. See the
31+
`Encore's index.js file`_ for detailed documentation.
6932

70-
Encore
71-
// ...
72-
.enableLessLoader(function(options) {
73-
// https://github.com/webpack-contrib/less-loader#examples
74-
// http://lesscss.org/usage/#command-line-usage-options
75-
// options.relativeUrls = false;
76-
});
77-
;
33+
.. _`Encore's index.js file`: https://github.com/symfony/webpack-encore/blob/master/index.js

frontend/encore/installation.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ run:
1010

1111
.. code-block:: terminal
1212
13-
$ composer require symfony/webpack-encore-pack
13+
$ composer require encore
1414
$ yarn install
1515
1616
This will create a ``webpack.config.js`` file, add the ``assets/`` directory, and

frontend/encore/page-specific-assets.rst

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,7 @@ If you're creating a single page app (SPA), then you probably only need to defin
55
*one* entry in ``webpack.config.js``. But if you have multiple pages, you might
66
want page-specific CSS and JavaScript.
77

8-
For example, suppose you have a checkout page that has its own JavaScript. Create
9-
a new ``checkout`` entry:
10-
11-
.. code-block:: diff
12-
13-
// webpack.config.js
14-
15-
Encore
16-
// an existing entry
17-
.addEntry('app', './assets/js/app.js')
18-
19-
+ .addEntry('checkout', './assets/js/checkout.js')
20-
;
21-
22-
Inside ``checkout.js``, add or require the JavaScript and CSS you need. Then, just
23-
call the ``encore_entry_link_tags()`` and ``encore_entry_script_tags()`` functions
24-
*only* on the checkout page to include the new ``script`` and ``link`` tags
25-
(if any ``link`` tag is needed):
26-
27-
.. code-block:: twig
28-
29-
{# templates/order/checkout.html.twig #}
30-
{# ... #}
31-
32-
{#
33-
Assuming you're using Symfony's standard base.html.twig setup, add
34-
to the stylesheets and javascript blocks
35-
#}
36-
37-
{% block javascripts %}
38-
{{ parent() }}
39-
40-
{{ encore_entry_script_tags('checkout') }}
41-
{% endblock %}
42-
43-
{% block stylesheets %}
44-
{{ parent() }}
45-
46-
{{ encore_entry_link_tags('checkout') }}
47-
{% endblock %}
8+
To learn how to set this up, see the :ref:`multiple-javascript-entries` example.
489

4910
Multiple Entries Per Page?
5011
--------------------------
@@ -55,10 +16,10 @@ you need.
5516

5617
However, it's pretty common to need to include some global JavaScript and CSS on
5718
every page. For that reason, it usually makes sense to have one entry (e.g. ``app``)
58-
that contains this global code and is included on every page (i.e. it's included
59-
in the *layout* of your app). This means that you will always have one, global entry
60-
on every page (e.g. ``app``) and you *may* have one page-specific JavaScript and
61-
CSS file from a page-specific entry (e.g. ``checkout``).
19+
that contains this global code (both JavaScript & CSS) and is included on every
20+
page (i.e. it's included in the *layout* of your app). This means that you will
21+
always have one, global entry on every page (e.g. ``app``) and you *may* have one
22+
page-specific JavaScript and CSS file from a page-specific entry (e.g. ``checkout``).
6223

6324
.. tip::
6425

frontend/encore/postcss.rst

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Then, Enable the loader in Encore!
3535
+ .enablePostCssLoader()
3636
;
3737
38+
Because you just modified ``webpack.config.js``, stop and restart Encore.
39+
3840
That's it! The ``postcss-loader`` will now be used for all CSS, Sass, etc files.
3941
You can also pass options to the `postcss-loader`_ by passing a callback:
4042

@@ -51,6 +53,8 @@ You can also pass options to the `postcss-loader`_ by passing a callback:
5153
+ })
5254
;
5355
56+
.. _browserslist_package_config:
57+
5458
Adding browserslist to package.json
5559
-----------------------------------
5660

@@ -61,17 +65,16 @@ support. The best-practice is to configure this directly in your ``package.json`
6165
.. code-block:: diff
6266
6367
{
64-
+ "browserslist": [ "last 2 versions", "ios >= 8" ]
68+
+ "browserslist": [
69+
+ "> 0.5%",
70+
+ "last 2 versions",
71+
+ "Firefox ESR",
72+
+ "not dead"
73+
+ ]
6574
}
6675
6776
See `browserslist`_ for more details on the syntax.
6877

69-
.. note::
70-
71-
Encore uses `babel-preset-env`_, which *also* needs to know which browsers you
72-
want to support. But this does *not* read the ``browserslist`` config key. You
73-
must configure the browsers separately via :doc:`configureBabel() </frontend/encore/babel>`.
74-
7578
.. _`PostCSS`: http://postcss.org/
7679
.. _`autoprefixing`: https://github.com/postcss/autoprefixer
7780
.. _`linting`: https://stylelint.io/

frontend/encore/reactjs.rst

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
Enabling React.js
22
=================
33

4-
Using React? Make sure you have React installed, along with the `babel-preset-react`_:
4+
Using React? First enable support for it in ``webpack.config.js``:
55

6-
.. code-block:: terminal
7-
8-
$ yarn add --dev react react-dom prop-types babel-preset-react
9-
10-
Enable react in your ``webpack.config.js``:
11-
12-
.. code-block:: javascript
6+
.. code-block:: diff
137
148
// webpack.config.js
159
// ...
1610
1711
Encore
1812
// ...
19-
.enableReactPreset()
13+
+ .enableReactPreset()
2014
;
2115
22-
That's it! Your ``.js`` and ``.jsx`` files will now be transformed through
23-
``babel-preset-react``.
16+
17+
Then restart Encore. When you do, it will give you a command you can run to
18+
install any missing dependencies. After running that command and restarting
19+
Encore, you're done!
20+
21+
Your ``.js`` and ``.jsx`` files will now be transformed through ``babel-preset-react``.
2422

2523
.. _`babel-preset-react`: https://babeljs.io/docs/plugins/preset-react/

0 commit comments

Comments
 (0)