@@ -4,10 +4,10 @@ First Example
4
4
Imagine you have a simple project with one CSS and one JS file, organized into
5
5
an ``assets/ `` directory:
6
6
7
- * ``assets/js/main .js ``
8
- * ``assets/css/global .scss ``
7
+ * ``assets/js/app .js ``
8
+ * ``assets/css/app .scss ``
9
9
10
- With Encore, we can easily minify these files, pre-process ``global .scss ``
10
+ With Encore, we can easily minify these files, pre-process ``app .scss ``
11
11
through Sass and a *lot * more.
12
12
13
13
Configuring Encore/Webpack
@@ -22,20 +22,20 @@ Inside, use Encore to help generate your Webpack configuration.
22
22
var Encore = require (' @symfony/webpack-encore' );
23
23
24
24
Encore
25
- // directory where all compiled assets will be stored
25
+ // the project directory where all compiled assets will be stored
26
26
.setOutputPath (' web/build/' )
27
27
28
- // what's the public path to this directory (relative to your project's document root dir)
28
+ // the public path used by the web server to access the previous directory
29
29
.setPublicPath (' /build' )
30
30
31
31
// empty the outputPath dir before each build
32
32
.cleanupOutputBeforeBuild ()
33
33
34
- // will output as web/build/app.js
35
- .addEntry (' app' , ' ./assets/js/main .js' )
34
+ // will output as web/build/js/ app.js
35
+ .addEntry (' js/ app' , ' ./assets/js/app .js' )
36
36
37
- // will output as web/build/global .css
38
- .addStyleEntry (' global ' , ' ./assets/css/global .scss' )
37
+ // will output as web/build/css/app .css
38
+ .addStyleEntry (' css/app ' , ' ./assets/css/app .scss' )
39
39
40
40
// allow sass/scss files to be processed
41
41
.enableSassLoader ()
@@ -78,7 +78,7 @@ Actually, to use ``enableSassLoader()``, you'll need to install a few
78
78
more packages. But Encore will tell you *exactly * what you need.
79
79
80
80
After running one of these commands, you can now add ``script `` and ``link `` tags
81
- to the new, compiled assets (e.g. ``/build/global .css `` and ``/build/app.js ``).
81
+ to the new, compiled assets (e.g. ``/build/css/app .css `` and ``/build/js /app.js ``).
82
82
In Symfony, use the ``asset() `` helper:
83
83
84
84
.. code-block :: twig
@@ -88,11 +88,11 @@ In Symfony, use the ``asset()`` helper:
88
88
<html>
89
89
<head>
90
90
<!-- ... -->
91
- <link rel="stylesheet" href="{{ asset('build/global .css') }}">
91
+ <link rel="stylesheet" href="{{ asset('build/css/app .css') }}">
92
92
</head>
93
93
<body>
94
94
<!-- ... -->
95
- <script src="{{ asset('build/app.js') }}"></script>
95
+ <script src="{{ asset('build/js/ app.js') }}"></script>
96
96
</body>
97
97
</html>
98
98
@@ -139,29 +139,51 @@ may want also to :doc:`create a shared entry </frontend/encore/shared-entry>` fo
139
139
Requiring CSS Files from JavaScript
140
140
-----------------------------------
141
141
142
- Above, you created an entry called ``app `` that pointed to ``main .js ``:
142
+ Above, you created an entry called ``js/ app `` that pointed to ``app .js ``:
143
143
144
144
.. code-block :: javascript
145
145
146
146
Encore
147
147
// ...
148
- .addEntry (' app' , ' ./assets/js/main .js' )
148
+ .addEntry (' js/ app' , ' ./assets/js/app .js' )
149
149
;
150
150
151
- Once inside ``main .js ``, you can even require CSS files:
151
+ Once inside ``app .js ``, you can even require CSS files:
152
152
153
153
.. code-block :: javascript
154
154
155
- // assets/js/main .js
155
+ // assets/js/app .js
156
156
// ...
157
157
158
158
// a CSS file with the same name as the entry js will be output
159
- require (' ../css/main .scss' );
159
+ require (' ../css/app .scss' );
160
160
161
- Now, both an ``app.js `` **and ** an ``app.css `` file will be created. You'll need
162
- to add a link tag to the ``app.css `` file in your templates:
161
+ Now, both an ``app.js `` **and ** an ``app.css `` file will be created in the
162
+ ``build/js/ `` dir. You'll need to add a link tag to the ``app.css `` file in your
163
+ templates:
163
164
164
165
.. code-block :: diff
165
166
166
- <link rel="stylesheet" href="{{ asset('build/global.css') }}">
167
- + <link rel="stylesheet" href="{{ asset('build/app.css') }}">
167
+ <link rel="stylesheet" href="{{ asset('build/css/app.css') }}">
168
+ + <link rel="stylesheet" href="{{ asset('build/js/app.css') }}">
169
+
170
+ This article follows the traditional setup where you have just one main CSS file
171
+ and one main JavaScript file. In lots of modern JavaScript applications, it's
172
+ common to have one "entry" for each important section (homepage, blog, store, etc.)
173
+
174
+ In those application, it's better to just add JavaScript entries in the Webpack
175
+ configuration file and import the CSS files from the JavaScript entries, as
176
+ shown above:
177
+
178
+ .. code-block :: javascript
179
+
180
+ Encore
181
+ // ...
182
+ .addEntry (' homepage' , ' ./assets/js/homepage.js' )
183
+ .addEntry (' blog' , ' ./assets/js/blog.js' )
184
+ .addEntry (' store' , ' ./assets/js/store.js' )
185
+ ;
186
+
187
+ If those entries include CSS/Sass files (e.g. ``homepage.js `` requires
188
+ ``assets/css/homepage.scss ``), two files will be generated for each of them
189
+ (e.g. ``build/homepage.js `` and ``build/homepage.css ``).
0 commit comments