Skip to content

Commit 08ca67c

Browse files
committed
WIP - re-organizing and updating the docs
1 parent c7dd577 commit 08ca67c

17 files changed

+726
-635
lines changed

README.rst

Lines changed: 15 additions & 635 deletions
Large diffs are not rendered by default.

docs/babel.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Configuring Babel
2+
=================
3+
4+
`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).
7+
8+
Need to extend the Babel configuration further? The easiest way is via
9+
``configureBabel()``:
10+
11+
.. code-block:: javascript
12+
13+
// webpack.config.js
14+
// ...
15+
16+
Encore
17+
// ...
18+
19+
// modify our default Babel configuration
20+
.configureBabel(function(babelConfig) {
21+
babelConfig.presets.push('es2017');
22+
})
23+
;
24+
25+
You can also create a standard ``.babelrc`` file at the root of your project.
26+
Just make sure to configure it with all the presets you need: as soon as a
27+
``.babelrc`` is present, Encore can no longer add *any* Babel configuration for
28+
you!
29+
30+
.. _`Babel`: http://babeljs.io/

docs/bootstrap.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Using Bootstrap CSS & JS
2+
========================
3+
4+
TODO - show how to bring in bootstrap JS and bootstrap-sass.
5+
6+
Here is an example from before to include... because I think it's really nice :).
7+
8+
// when no file path is defined (i.e. no file extension) Webpack loads the
9+
// given JavaScript module installed in node_modules/ dir (Webpack knows all
10+
// the specific files that must be loaded and in which order)
11+
require('bootstrap-star-rating');
12+
13+
// when a file path is given, but it doesn't start with '/' or './', the file
14+
// path is considered relative to node_modules/ dir
15+
require('bootstrap-star-rating/css/star-rating.css');
16+
17+
// when a file path is given and it starts with '/', './', or '../', it's considered
18+
// as the full file path for the asset (it can live outside the node_modules/ dir)
19+
require('../../../../../node_modules/bootstrap-star-rating/themes/krajee-svg/theme.css');

docs/cdn.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Using a CDN
2+
===========
3+
4+
Are you deploying to a CDN? That's awesome :) - and configuring
5+
Encore for that is easy. Once you've made sure that your built files
6+
are uploaded to the CDN, configure it in Encore:
7+
8+
.. code-block:: diff
9+
10+
// webpack.config.js
11+
// ...
12+
13+
Encore
14+
.setOutputPath('web/build/')
15+
// in dev mode, don't use the CDN
16+
.setPublicPath('/build');
17+
// ...
18+
;
19+
20+
+ if (Encore.isProduction()) {
21+
+ Encore.setPublicPath('https://my-cool-app.com.global.prod.fastly.net');
22+
+
23+
+ // guarantee that the keys in manifest.json are *still*
24+
+ // prefixed with build/
25+
+ // (e.g. "build/dashboard.js": "https://my-cool-app.com.global.prod.fastly.net/dashboard.js")
26+
+ Encore.setManifestKeyPrefix('build/');
27+
+ }
28+
29+
That's it! Internally, Webpack will now know to load assets from your CDN -
30+
e.g. ``https://my-cool-app.com.global.prod.fastly.net/dashboard.js``.
31+
32+
.. note::
33+
34+
It's still your responsibility to put your assets on the CDN - e.g. by
35+
uploading them or by using "origin pull", where your CDN pulls assets
36+
directly from your web server.
37+
38+
You *do* need to make sure that the ``script`` and ``link`` tags you include on your
39+
pages also uses the CDN. Fortunately, the ``manifest.json`` is automatically
40+
updated to point to the CDN. In Symfony, as long as you've configured
41+
:doc:`Asset Versioning </versioning>`_, you're done! The ``manifest.json``
42+
file includes the full CDN URL:
43+
44+
.. code-block:: js
45+
46+
{# Same code you had before and setting up the CDN #}
47+
<script src="{{ asset('build/dashboard.js') }}"></script>

docs/css-preprocessors.rst

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
CSS Preprocessors: SASS, LESS, etc
2+
==================================
3+
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
26+
be processed.
27+
28+
Using LESS
29+
----------
30+
31+
To use the LESS pre-processor, install the dependencies:
32+
33+
.. code-block:: terminal
34+
35+
yarn add --dev less-loader less
36+
37+
And enable it in ``webpack.config.js``:
38+
39+
.. code-block:: javascript
40+
41+
// webpack.config.js
42+
// ...
43+
44+
Encore
45+
// ...
46+
.enableLessLoader()
47+
;
48+
49+
That's it! All files ending in ``.less`` will be pre-processed.

docs/dev-server.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Using webpack-dev-server and HMR
2+
================================
3+
4+
While developing, instead of using ``encore dev --watch``, you can
5+
instead use the `webpack-dev-server`_:
6+
7+
.. code-block:: terminal
8+
9+
./node_modules/.bin/encore dev-server
10+
11+
.. note::
12+
13+
Hot module replacement is not currently supported.
14+
15+
This serves the built assets from a new server at ``http://localhost:8080``
16+
(it does not actually write any files to disk). This means your
17+
``script`` and ``link`` tags need to change to point to this.
18+
19+
If you've activated the :ref:`manifest.json versioning <load-manifest-files>`
20+
you're done: the paths in your templates will automatically point to the dev server.
21+
22+
.. _`webpack-dev-server`: https://webpack.js.org/configuration/dev-server/

docs/index.rst

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
Webpack Encore
2+
==============
3+
4+
`Webpack Encore`_ is a simpler way to integrate `Webpack`_ into your
5+
application. It *wraps* Webpack, giving you a clean & powerful API
6+
for bundling JavaScript modules, pre-processing CSS & JS and compiling
7+
and minifying assets. Encore gives you professional asset system
8+
that's a *delight* to use.
9+
10+
Encore is inspired by `Webpacker`_ and `Mix`_, but stays in the spirit of
11+
Webpack: using its features, concepts and naming conventions for a familiar
12+
feel. It aims to solve the most common Webpack use cases.
13+
14+
.. tip::
15+
16+
Encore is made by `Symfony`_ and works *beautifully* in Symfony applications.
17+
But it can easily be used in any application... in any language!
18+
19+
Documentation
20+
-------------
21+
22+
Getting Started
23+
...............
24+
25+
* :doc:`Installation </installation>`
26+
* :doc:`First Example </simple-example>`
27+
28+
Adding more Features
29+
....................
30+
31+
* :doc:`CSS Preprocessors: SASS, LESS, etc </css-preprocessors>`
32+
* :doc:`PostCSS and autoprefixing </postcss>`
33+
* :doc:`Enabling React.js </reactjs>`
34+
* :doc:`Configuring Babel </babel>`
35+
* :doc:`Sourcemaps </sourcemaps>`
36+
37+
Optimizing
38+
..........
39+
40+
* :doc:`Versioning and manifest.json </versioning>`
41+
* :doc:`Using A CDN </cdn>`
42+
* :doc:`Creating a "Shared" entry for re-used modules </shared-entry>`
43+
44+
Guides
45+
......
46+
47+
* :doc:`Using Bootstrap CSS & JS </bootstrap>`
48+
* :doc:`Creating Page-Specific CSS/JS </page-specific-assets>`
49+
* :doc:`jQuery and Legacy Applications </legacy-apps>`
50+
* :doc:`Passing Information from Twig to JavaScript </server-data>`
51+
* :doc:`webpack-dev-server and Hot Module Replacement (HMR) </dev-server>`
52+
53+
Full API
54+
........
55+
56+
* `Full API`_: https://github.com/symfony/webpack-encore/blob/master/index.js
57+
58+
.. _`Webpack Encore`: https://www.npmjs.com/package/@symfony/webpack-encore
59+
.. _`Webpack`: https://webpack.js.org/
60+
.. _`Webpacker`: https://github.com/rails/webpacker
61+
.. _`Mix`: https://laravel.com/docs/5.4/mix
62+
.. _`Symfony`: http://symfony.com/
63+
.. _`Full API`: https://github.com/symfony/webpack-encore/blob/master/index.js

docs/installation.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Installation
2+
============
3+
4+
First, make sure you `install Node.js`_ and also the `yarn package manager`_.
5+
6+
Then, install Encore into your project with yarn:
7+
8+
.. code-block:: terminal
9+
10+
$ yarn add @symfony/webpack-encore --dev
11+
12+
.. note::
13+
14+
If you want to use `npm`_ instead of `yarn`_, replace ``yarn add xxx --dev`` by
15+
``npm install xxx --save-dev``.
16+
17+
This command creates (or modifies) a ``package.json`` file and downloads
18+
dependencies into a ``node_modules`` directory. When using Yarn, a file called
19+
``yarn.lock`` is also created/updated. When using npm 5, a ``package-lock.json``
20+
file is created/updated.
21+
22+
.. tip::
23+
24+
You should commit ``package.json`` and ``yarn.lock`` (or ``package-lock.json``
25+
if using npm) to version control, but ignore ``node_modules``.
26+
27+
Next, create your ``webpack.config.js`` in :doc:`/simple-example`!
28+
29+
.. _`install Node.js`: https://nodejs.org/en/download/
30+
.. _`yarn package manager`: https://yarnpkg.com/lang/en/docs/install/
31+
.. _`npm`: https://www.npmjs.com/
32+
.. _`yarn`: https://yarnpkg.com/

docs/legacy-apps.rst

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
jQuery and Legacy Applications
2+
==============================
3+
4+
Some legacy JavaScript applications use programming practices that don't play
5+
well with the new practices promoted by Webpack. The most common of these
6+
problems is using code (e.g. jQuery plugins) that assume that jQuery is already
7+
available via the the ``$`` or ``jQuery`` global variables. If those variables
8+
are not defined, you'll get these errors:
9+
10+
.. code-block:: text
11+
12+
Uncaught ReferenceError: $ is not defined at [...]
13+
Uncaught ReferenceError: jQuery is not defined at [...]
14+
15+
Instead of rewriting everything, Encore allows for a different solution. Thanks
16+
to the ``autoProvidejQuery()`` method, whenever a JavaScript file uses the ``$``
17+
or ``jQuery`` variables, Webpack automatically requires ``jquery`` and creates
18+
those variables for you.
19+
20+
So, when working with legacy applications, you may need to add the following to
21+
``webpack.config.js``:
22+
23+
.. code-block:: diff
24+
25+
Encore
26+
// ...
27+
+ .autoProvidejQuery()
28+
;
29+
30+
Internally, this ``autoProvidejQuery()`` method uses the ``autoProvideVariables()``
31+
method from Encore. In practice, it's equivalent to doing:
32+
33+
.. code-block:: javascript
34+
35+
Encore
36+
// you can use this method to provide other common global variables,
37+
// such as '_' for the 'underscore' library
38+
.autoProvideVariables({
39+
$: 'jquery',
40+
jQuery: 'jquery'
41+
})
42+
// ...
43+
;
44+
45+
If you also need to provide access to ``$`` and ``jQuery`` variables outside of
46+
JavaScript files processed by Webpack, you must create the global variables
47+
yourself in some file loaded before the legacy JavaScript code. For example, you
48+
can define a ``common.js`` file processed by Webpack and loaded in every page
49+
with the following content:
50+
51+
.. code-block:: javascript
52+
53+
window.$ = window.jQuery = require('jquery');

docs/page-specific-assets.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Creating Page-Specific CSS/JS
2+
=============================
3+
4+
TODO

0 commit comments

Comments
 (0)