Skip to content

Commit fb335a8

Browse files
committed
feature #10441 [Encore] Add a section about the configureRuntimeEnvironment method (Lyrkan, weaverryan)
This PR was merged into the 3.4 branch. Discussion ---------- [Encore] Add a section about the configureRuntimeEnvironment method This PR adds some info about the `configureRuntimeEnvironment(...)` method of Encore. I wasn't sure where to put that section, it's not an "advanced config" per-se, but it didn't feel right to add it to the FAQ either... but maybe that's just me? **Feature PR:** symfony/webpack-encore#115 **Closes:** symfony/webpack-encore#233 Commits ------- 24b3370 minor tweak 653ccff [Encore] Add a section about the configureRuntimeEnvironment method
2 parents 0950d8d + 24b3370 commit fb335a8

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

frontend/encore/advanced-config.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,53 @@ prefer to build configs separately, pass the ``--config-name`` option:
9595
9696
$ yarn encore dev --config-name firstConfig
9797
98+
Generating a Webpack Configuration Object without using the Command-Line Interface
99+
----------------------------------------------------------------------------------
100+
101+
Ordinarily you would use your ``webpack.config.js`` file by calling Encore
102+
from the command-line interface. But sometimes, having access to the generated
103+
Webpack configuration can be required by tools that don't use Encore (for
104+
instance a test-runner such as `Karma`_).
105+
106+
The problem is that if you try generating that Webpack configuration object
107+
without using the ``encore`` command you will encounter the following error:
108+
109+
.. code-block:: text
110+
111+
Error: Encore.setOutputPath() cannot be called yet because the runtime environment doesn't appear to be configured. Make sure you're using the encore executable or call Encore.configureRuntimeEnvironment() first if you're purposely not calling Encore directly.
112+
113+
The reason behind that message is that Encore needs to know a few thing before
114+
being able to create a configuration object, the most important one being what
115+
the target environment is.
116+
117+
To solve this issue you can use ``configureRuntimeEnvironment``. This method
118+
must be called from a JavaScript file **before** requiring ``webpack.config.js``.
119+
120+
For instance:
121+
122+
.. code-block:: javascript
123+
124+
const Encore = require('@symfony/webpack-encore');
125+
126+
// Set the runtime environment
127+
Encore.configureRuntimeEnvironment('dev');
128+
129+
// Retrieve the Webpack configuration object
130+
const webpackConfig = require('./webpack.config');
131+
132+
If needed, you can also pass to that method all the options that you would
133+
normally use from the command-line interface:
134+
135+
.. code-block:: javascript
136+
137+
Encore.configureRuntimeEnvironment('dev-server', {
138+
// Same options you would use with the
139+
// CLI utility, with their name in camelCase.
140+
https: true,
141+
keepPublicPath: true,
142+
});
143+
98144
.. _`configuration options`: https://webpack.js.org/configuration/
99145
.. _`Webpack's watchOptions`: https://webpack.js.org/configuration/watch/#watchoptions
100146
.. _`array of configurations`: https://github.com/webpack/docs/wiki/configuration#multiple-configurations
147+
.. _`Karma`: https://karma-runner.github.io

0 commit comments

Comments
 (0)