-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Various Encore updates #8084
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Various Encore updates #8084
Changes from 9 commits
3d9905e
0ff1c3c
62dd63e
8b0a22a
a099bff
807b83f
a0982ec
f5c22a6
f8461d3
15e816e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
Advanced Webpack Config | ||
======================= | ||
|
||
Quite simply, Encore generates the Webpack configuration that's used in your | ||
``webpack.config.js`` file. Encore doesn't support adding all of Webpack's | ||
`configuration options`_, because many can be easy added on your own. | ||
|
||
For example, suppose you need to set `Webpack's watchOptions`_ setting. To do that, | ||
modify the config it after fetching the it from Encore: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
.. code-block:: javascript | ||
|
||
// webpack.config.js | ||
|
||
var Encore = require('@symfony/webpack-encore'); | ||
|
||
// ... all Encore config here | ||
|
||
// fetch the config, then modify it! | ||
var config = Encore.getWebpackConfig(); | ||
config.watchOptions = { poll: true, ignored: /node_modules/ }; | ||
|
||
// other examples: add an alias or extension | ||
// config.resolve.alias.local = path.resolve(__dirname, './resources/src'); | ||
// config.resolve.extensions.push('json'); | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra blank line here |
||
// export the final config | ||
module.exports = config; | ||
|
||
But be careful not to accidentally override any config from Encore: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is easy to say, but hard to accomplish :) I guess most of configs are handled internally by Webpack so, how can I know if I'm unawarely overriding a setting? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, this is tricky. For example, suppose today Encore doesn't ship with any There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe link to one of the deepExtend implementations (like the one from underscore.js) and use it instead? So that config is merged, if appropriate? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's probably a good idea - would you be willing to open a small PR for that? |
||
|
||
.. code-block:: javascript | ||
|
||
// webpack.config.js | ||
// ... | ||
|
||
// GOOD - this modifies the config.resolve.extensions array | ||
// config.resolve.extensions.push('json'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would uncomment the code line. It would be more readable as it will be syntax-highlighted (same below) |
||
|
||
// BAD - this replaces any extensions added by Encore | ||
// config.resolve.extensions = ['json']; | ||
|
||
.. _`configuration options`: https://webpack.js.org/configuration/ | ||
.. _`Webpack's watchOptions`: https://webpack.js.org/configuration/watch/#watchoptions |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
Adding Custom Loaders & Plugins | ||
=============================== | ||
|
||
Adding Custom Loaders | ||
--------------------- | ||
|
||
Encore already comes with a variety of different loaders out of the box, | ||
but if there is a specific loader that you want to use that is not currently supported, you | ||
can add your own loader through the ``addLoader`` function. | ||
The ``addLoader`` takes any valid webpack rules config. | ||
|
||
If, for example, you want to add the `handlebars-loader`_, call ``addLoader`` with | ||
your loader config | ||
|
||
.. code-block:: javascript | ||
|
||
Encore | ||
// ... | ||
.addLoader({ test: /\.handlebars$/, loader: 'handlebars-loader' }) | ||
; | ||
|
||
Since the loader config accepts any valid Webpack rules object, you can pass any | ||
additional information your need for the loader | ||
|
||
.. code-block:: javascript | ||
|
||
Encore | ||
// ... | ||
.addLoader({ | ||
test: /\.handlebars$/, | ||
loader: 'handlebars-loader', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indentation looks weird here |
||
query: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be |
||
helperDirs: [ | ||
__dirname + '/helpers1', | ||
__dirname + '/helpers2', | ||
], | ||
partialDirs: [ | ||
path.join(__dirname, 'templates', 'partials') | ||
] | ||
} | ||
}) | ||
; | ||
|
||
Adding Custom Plugins | ||
--------------------- | ||
|
||
Encore uses a variety of different `plugins`_ internally. But, you can add your own | ||
via the ``addPlugin()`` method. For example, if you use `Moment.js`_, you might want | ||
to use the `IgnorePlugin`_ (see `moment/moment#2373`_): | ||
|
||
.. code-block:: diff | ||
|
||
// webpack.config.js | ||
Encore | ||
// ... | ||
|
||
+ .addPlugin(new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)) | ||
; | ||
|
||
.. _`handlebars-loader`: https://github.com/pcardune/handlebars-loader | ||
.. _`plugins`: https://webpack.js.org/plugins/ | ||
.. _`Moment.js`: https://momentjs.com/ | ||
.. _`IgnorePlugin`: https://webpack.js.org/plugins/ignore-plugin/ | ||
.. _`moment/moment#2373`: https://github.com/moment/moment/issues/2373 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
FAQ and Common Issues | ||
===================== | ||
|
||
My App Lives under a Subdirectory | ||
--------------------------------- | ||
|
||
If your app does not live at the root of your web server (i.e. it lives under a subdirectory, | ||
like ``/myAppSubdir``), you just need to configure that when calling ``Encore.setPublicPrefix()``: | ||
|
||
.. code-block:: diff | ||
|
||
// webpack.config.js | ||
Encore | ||
// ... | ||
|
||
.setOutputPath('web/build/') | ||
|
||
- .setPublicPath('/build') | ||
+ // this is your *true* public path | ||
+ .setPublicPath('/myAppSubdir/build') | ||
|
||
+ // this is now needed so that your manifest.json keys are still `build/foo.js` | ||
+ // i.e. you won't need to change anything in your Symfony app | ||
+ config.setManifestKeyPrefix('build') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
; | ||
|
||
If you're :ref:`processing your assets through manifest.json <load-manifest-files>`, | ||
you're done! The ``manifest.json`` file will now include the subdirectory in the | ||
final paths: | ||
|
||
.. code-block:: json | ||
|
||
{ | ||
"build/app.js": "/myAppSubdir/build/app.123abc.js", | ||
"build/dashboard.css": "/myAppSubdir/build/dashboard.a4bf2d.css" | ||
} | ||
|
||
"jQuery is not defined" or "$ is not defined" | ||
--------------------------------------------- | ||
|
||
This error happens when your code (or some library that you are using) expects ``$`` | ||
or ``jQuery`` to be a global variable. But, when you use Webpack and ``require('jquery')``, | ||
no global variables are set. | ||
|
||
The fix depends on if the error is happening in your code or inside some third-party | ||
code that you're using. See :doc:`/frontend/encore/legacy-apps` for the fix. | ||
|
||
Uncaught ReferenceError: webpackJsonp is not defined | ||
---------------------------------------------------- | ||
|
||
If you get this error, it's probably because you've just added a :doc:`shared entry </frontend/encore/shared-entry>` | ||
but you *forgot* to add a ``script`` tag for the new ``manifest.js`` file. See the | ||
information about the :ref:`script tags <encore-shared-entry-script>` in that section. | ||
|
||
This dependency was not found: some-module in ./path/to/file.js | ||
--------------------------------------------------------------- | ||
|
||
Usually, after you install a package via yarn, you can require / import it to use | ||
it. For example, after running ``yarn add respond.js``, you try to require that module: | ||
|
||
.. code-block:: javascript | ||
|
||
require('respond.js'); | ||
|
||
But, instead of working, you see an error: | ||
|
||
This dependency was not found: | ||
|
||
* respond.js in ./app/Resources/assets/js/app.js | ||
|
||
Typically, a package will "advertise" its "main" file by adding a ``main`` key to | ||
its ``package.json``. But sometimes, old libraries won't have this. Instead, you'll | ||
need to specifically require the file you need. In this case, the file you should | ||
use is located at ``node_modules/respond.js/dest/respond.src.js``. You can require | ||
this via: | ||
|
||
.. code-block:: javascript | ||
|
||
// require a non-minified file whenever possible | ||
require('respond.js/dest/respond.src.js'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,21 @@ | ||
jQuery and Legacy Applications | ||
============================== | ||
|
||
Inside Webpack, when you require a module, it does *not* (usually) set a global variable. | ||
Instead, it just returns a value: | ||
|
||
.. code-block:: javascript | ||
|
||
// this loads jquery, but does *not* set a global $ or jQuery variable | ||
const $ = require('jquery'); | ||
|
||
In practice, this will cause problems with some outside libraries that *rely* on | ||
jQuery to be global. It will be a problem if some of *your* JavaScript isn't being | ||
processed through Webpack (e.g. you have some JavaScript in your templates). | ||
|
||
Using Libraries that Expect jQuery to be Global | ||
----------------------------------------------- | ||
|
||
Some legacy JavaScript applications use programming practices that don't play | ||
well with the new practices promoted by Webpack. The most common of these | ||
problems is using code (e.g. jQuery plugins) that assume that jQuery is already | ||
|
@@ -27,7 +42,7 @@ So, when working with legacy applications, you may need to add the following to | |
+ .autoProvidejQuery() | ||
; | ||
|
||
Internally, this ``autoProvidejQuery()`` method uses the ``autoProvideVariables()`` | ||
Internally, this ``autoProvidejQuery()`` method calls the ``autoProvideVariables()`` | ||
method from Encore. In practice, it's equivalent to doing: | ||
|
||
.. code-block:: javascript | ||
|
@@ -38,16 +53,33 @@ method from Encore. In practice, it's equivalent to doing: | |
.autoProvideVariables({ | ||
$: 'jquery', | ||
jQuery: 'jquery' | ||
'window.jQuery': 'jquery', | ||
}) | ||
// ... | ||
; | ||
|
||
Accessing jQuery from outside of Webpack JavaScript Files | ||
--------------------------------------------------------- | ||
|
||
If you also need to provide access to ``$`` and ``jQuery`` variables outside of | ||
JavaScript files processed by Webpack, you must create the global variables | ||
yourself in some file loaded before the legacy JavaScript code. For example, you | ||
can define a ``common.js`` file processed by Webpack and loaded in every page | ||
with the following content: | ||
JavaScript files processed by Webpack (e.g. JavaScript that still lives in your | ||
templates), you need to manually set these as global variables in some JavaScript | ||
file that is loaded before your legacy code. | ||
|
||
For example, you could define a ``common.js`` file that's processed by Webpack and | ||
loaded on every page with the following content: | ||
|
||
.. code-block:: javascript | ||
|
||
window.$ = window.jQuery = require('jquery'); | ||
// require jQuery normally | ||
const $ = require('jquery'); | ||
|
||
// create global $ and jQuery variables | ||
global.$ = global.jQuery = $; | ||
|
||
.. tip:: | ||
|
||
The ``global`` variable is a special way of setting things pn the ``window`` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
variable. In a web context, using ``global`` and ``window`` are equivalent, | ||
except that ``window.jQuery`` won't work when using ``autoProvidejQuery()``. | ||
In other words, use ``global``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
easy -> easily ?