Skip to content

Commit bf649ab

Browse files
committed
Documenting copyFiles() and a few small things
1 parent 6833bb6 commit bf649ab

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

frontend.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Adding more Features
4848
* :doc:`PostCSS and autoprefixing </frontend/encore/postcss>`
4949
* :doc:`Enabling React.js </frontend/encore/reactjs>`
5050
* :doc:`Enabling Vue.js (vue-loader) </frontend/encore/vuejs>`
51+
* :doc:`/frontend/encore/copy-files`
5152
* :doc:`Configuring Babel </frontend/encore/babel>`
5253
* :doc:`Source maps </frontend/encore/sourcemaps>`
5354
* :doc:`Enabling TypeScript (ts-loader) </frontend/encore/typescript>`

frontend/encore/copy-files.rst

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
Copying & Referencing Images
2+
============================
3+
4+
Need to reference a static file - like the path to an image for an ``img`` tag?
5+
That can be tricky if you store your assets outside of the public document root.
6+
Fortunately, depending on your situation, there is a solution!
7+
8+
Referencing Images from Inside a Webpacked JavaScript File
9+
----------------------------------------------------------
10+
11+
To reference an image tag from inside a JavaScript file, *require* the file:
12+
13+
.. code-block:: javascript
14+
15+
// assets/js/app.js
16+
17+
// returns the final, public path to this file
18+
// path is relative to this file - e.g. assets/images/logo.png
19+
const logoPath = require('../images/logo.png');
20+
21+
var html = `<img src="${logoPath}">`;
22+
23+
When you ``require`` (or ``import``) an image file, Webpack copies it into your
24+
output directory and returns the final, *public* path to that file.
25+
26+
Referencing Image files from a Template
27+
---------------------------------------
28+
29+
To reference an image file from outside of a JavaScript file that's processed by
30+
Webpack - like a template - you can use the ``copyFiles()`` method to copy those
31+
files into your final output directory.
32+
33+
.. code-block:: diff
34+
35+
// webpack.config.js
36+
37+
Encore
38+
// ...
39+
.setOutputPath('web/build/')
40+
41+
+ .copyFiles({
42+
+ from: './assets/images',
43+
+
44+
+ // optional target path, relative to the output dir
45+
+ //to: 'images/[path][name].[ext]',
46+
+
47+
+ // only copy files matching this pattern
48+
+ //pattern: /\.(png|jpg|jpeg)$/
49+
+ })
50+
51+
This will copy all files from ``assets/images`` into ``web/build`` (the output
52+
path). If you have :doc:`versioning enabled <versioning>`, the copied files will
53+
include a hash based on their content.
54+
55+
To render inside Twig, use the ``asset()`` function:
56+
57+
{# assets/images/logo.png was copied to web/build/logo.png #}
58+
<img src="{{ asset('build/logo.png') }}"
59+
60+
{# assets/images/subdir/logo.png was copied to web/build/subdir/logo.png #}
61+
<img src="{{ asset('build/subdir/logo.png') }}"
62+
63+
Make sure you've enabled the :ref:`json_manifest_path <load-manifest-files>` option,
64+
which tells the ``asset()`` function to read the final paths from the ``manifest.json``
65+
file. If you're not sure what path argument to pass to the ``asset()`` function,
66+
find the file in ``manifest.json`` and use the *key* as the argument.

frontend/encore/faq.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,11 @@ this via:
129129
// require a non-minified file whenever possible
130130
require('respond.js/dest/respond.src.js');
131131
132+
I need to execute Babel on a third-party Module
133+
-----------------------------------------------
134+
135+
For performance, Encore does not process libraries inside ``node_modules/`` through
136+
Babel. But, you can change that via the ``configureBabel()`` method. See
137+
:doc:`/frontend/encore/babel` for details.
138+
132139
.. _`rsync`: https://rsync.samba.org/

frontend/encore/simple-example.rst

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,32 @@ you need a feature, Encore will tell you what you need to install. Run:
316316
Your app now supports Sass. Encore also supports LESS and Stylus. See
317317
:doc:`/frontend/encore/css-preprocessors`.
318318

319+
Compiling Only a CSS File
320+
-------------------------
321+
322+
.. caution::
323+
324+
Using ``addStyleEntry()`` is supported, but not recommended. A better option
325+
is to use follow the pattern above: use ``addEntry()`` to point to a JavaScript
326+
file, then require the CSS needed from inside of that.
327+
328+
If you want to only compile a CSS file, that's possible via ``addStyleEntry()``:
329+
330+
.. code-block:: javascript
331+
332+
// webpack/config.js
333+
Encore
334+
// ...
335+
336+
.addStyleEntry('some_page', './assets/css/some_page.css')
337+
;
338+
339+
This will output a new ``some_page.css``.
340+
319341
Keep Going!
320342
-----------
321343

322-
Go back to the :ref:`List of Encore Articles <encore-toc>` to learn more and add new features.
344+
Encore support many more features! For a full list of what you can do, see
345+
`Encore's index.js file`_. Or, go back to :ref:`list of Encore articles <encore-toc>`.
346+
347+
.. _`Encore's index.js file`: https://github.com/symfony/webpack-encore/blob/master/index.js

0 commit comments

Comments
 (0)