You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
minor #115 Add Encore.configureRuntimeEnvironment() method to the public API (Lyrkan)
This PR was squashed before being merged into the master branch (closes#115).
Discussion
----------
Add Encore.configureRuntimeEnvironment() method to the public API
This PR fixes#109 by adding an `Encore.configureRuntimeEnvironment` and an `Encore.clearRuntimeEnvironment` method to the public API.
---
Basically, until now if you tried requiring `@symfony/webpack-encore` without using `./node_modules/.bin/encore`, it would throw an `Are you trying to require index.js directly?` error.
This prevented retrieving the Webpack configuration object easily, which can be needed in some cases (e.g. if you use [karma-webpack](https://github.com/webpack-contrib/karma-webpack#usage)).
With these changes, requiring `index.js` doesn't create a `WebpackConfig` instance anymore if the runtime environment isn't available, which removes the need of having a check at that time.
In order to deal with methods of the public API that need the `WebpackConfig` to be available, a [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) of the API is exported instead of the real object. This proxy only allows calls to `configureRuntimeEnvironment` and `clearRuntimeEnvironment` if the `WebpackConfig` isn't initialized, or act as a passthrough if it is.
This also fixes#55 since it allows to add a `try { ... } catch { ... }` quite easily around all the methods of the API.
The `Encore.clearRuntimeEnvironment()` (that nullifies `runtimeConfig` and `webpackConfig`) was needed to write tests but since I doubt it'll really be useful outside of that case let me know if you see another way to do it.
---
**Examples:**
```js
const Encore = require('@symfony/webpack-encore');
Encore
.setOutputPath('build/')
.setPublicPath('/')
.addEntry('main', './src/index.ts');
```
If executed directly would result in the following error:
```
Error: Encore.setOutputPath() cannot be called yet because the runtime environment doesn't appear to be configured. Try calling Encore.configureRuntimeEnvironment() first.
```
Whereas the following would work:
```js
const Encore = require('@symfony/webpack-encore');
Encore
.configureRuntimeEnvironment('dev-server')
.setOutputPath('build/')
.setPublicPath('/')
.addEntry('main', './src/index.ts');
```
It is also possible to pass the same options that are available using the usual CLI tool using the second argument of `configureRuntimeEnvironment` and camel-cased names:
```js
const Encore = require('@symfony/webpack-encore');
Encore
.configureRuntimeEnvironment('dev-server', {
keepPublicPath: true,
https: true,
})
.setOutputPath('build/')
.setPublicPath('/')
.addEntry('main', './src/index.ts');
```
Commits
-------
f760e52 Remove some tests related to the public API proxy and improve some texts/comments
ce6e7d3 Add Encore.configureRuntimeEnvironment() and Encore.clearRuntimeEnvironment() methods
thrownewError(`Encore.${prop}() 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.`);
590
+
}
591
+
592
+
// Either a safe method has been called or the webpackConfig
593
+
// object is already available. In this case act as a passthrough.
0 commit comments