Skip to content

Adding ifProd() and ifDev() methods #900

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

Conversation

weaverryan
Copy link
Member

Hi!

This occurred to me when I was trying to add Encore.enableBuildCache() to the official recipe. I was thinking that it might be best to enable it except when doing a production build. Currently, that's only possible by breaking the chaining:

Encore
    // .alotOfCalls()
;

if (!Encore.isProduction()) {
    Encore.enableBuildCache({
        config: [__filename],
    })
}

We have always handled making this easier on a method-by-method basis, which is still probably nice in many cases (e.g. ..enableVersioning(Encore.isProduction()) reads really nice. But these new methods make it possible to keep chaining with anything else we don't support in this way:

Encore
    // .alotOfCalls()

    .ifDev((Encore) => Encore.enableBuildCache({ config: [__filename] }))
;

Cheers!

@Kocal
Copy link
Member

Kocal commented Jan 28, 2021

Nice idea!

What about ifDevServer, since we have isDevServer?

@weaverryan
Copy link
Member Author

What about ifDevServer, since we have isDevServer?

I thought about that... and this does introduce some inconsistency. The new method isDev() is true if you are isDev() OR isDevServer()... which I think is just much more useful. We could name the new method ifNotProduction()... but that might be less clear.

@Kocal
Copy link
Member

Kocal commented Jan 28, 2021

Hum alright, I see your point!

@Kocal
Copy link
Member

Kocal commented Jan 28, 2021

Also, I've been thinking about this for a long time but, WDYT about a more generic method like when, that would be usable like this:

Encore
  .when(true, Encore => Encore.foo())
  .when(() => true, Encore => Encore.bar())
  .when(Encore => Encore.isDev(), Encore => Encore.baz())

A usecase I have on many projects at work is to add BundleAnalyzerPlugin plugin when argument --analyze is passed:

// Before

// ...
if (process.argv.includes('--analyze')) {
  Encore.addPlugin(new BundleAnalyzerPlugin({ analyzerHost: '0.0.0.0' }));
}

// After
Encore
  // ...
  .when(process.argv.includes('--analyze'), Encore => Encore.addPlugin(new BundleAnalyzerPlugin({ analyzerHost: '0.0.0.0' }));

@Lyrkan
Copy link
Collaborator

Lyrkan commented Feb 12, 2021

Also, I've been thinking about this for a long time but, WDYT about a more generic method like when, that would be usable like this:

I really like that idea.

nicolas-grekas added a commit to symfony/symfony that referenced this pull request Feb 22, 2021
…s to vary by env (nicolas-grekas)

This PR was merged into the 5.3-dev branch.

Discussion
----------

[FrameworkBundle] allow container/routing configurators to vary by env

| Q             | A
| ------------- | ---
| Branch?       | 5.x
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | #40215
| License       | MIT
| Doc PR        | -

Inspired by symfony/webpack-encore#900 and by a chat on Slack with @weaverryan

This aims at allowing conditional configuration, which would allow merging config files in one.

Using the PHP-DSL:
```php
$container
    ->when(env: 'prod')
    ->services()
        ->set(Foo::class)
    //...
```

In Yaml:
```yaml
framework:
    secret: '%env(APP_SECRET)%'

when@dev:
    services:
        App\FooForDev: ~

when@test:
    framework:
        test: true
        session:
            storage_factory_id: session.storage.mock_file
```

In XML (omitting namespaces):
```xml
<when env="test">
	<framework test="true">
		<!-- ... -->
	</framework>
</when>

```

A similar syntax is also provided for routes, with support for annotations:
`@Route(env="prod")` defines a route that is enabled only on the "prod" env.

Commits
-------

108375b [FrameworkBundle] allow container/routing configurators to vary by env
symfony-splitter pushed a commit to symfony/dependency-injection that referenced this pull request Feb 22, 2021
…s to vary by env (nicolas-grekas)

This PR was merged into the 5.3-dev branch.

Discussion
----------

[FrameworkBundle] allow container/routing configurators to vary by env

| Q             | A
| ------------- | ---
| Branch?       | 5.x
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | #40215
| License       | MIT
| Doc PR        | -

Inspired by symfony/webpack-encore#900 and by a chat on Slack with @weaverryan

This aims at allowing conditional configuration, which would allow merging config files in one.

Using the PHP-DSL:
```php
$container
    ->when(env: 'prod')
    ->services()
        ->set(Foo::class)
    //...
```

In Yaml:
```yaml
framework:
    secret: '%env(APP_SECRET)%'

when@dev:
    services:
        App\FooForDev: ~

when@test:
    framework:
        test: true
        session:
            storage_factory_id: session.storage.mock_file
```

In XML (omitting namespaces):
```xml
<when env="test">
	<framework test="true">
		<!-- ... -->
	</framework>
</when>

```

A similar syntax is also provided for routes, with support for annotations:
`@Route(env="prod")` defines a route that is enabled only on the "prod" env.

Commits
-------

108375b068 [FrameworkBundle] allow container/routing configurators to vary by env
Kocal added a commit to Kocal/webpack-encore that referenced this pull request Apr 15, 2021
Replace symfony#900 by adding a more generic and flexible API than Encore.isDev()/Encore.isProd().

 Encore.when() takes two parameters:
- `condition`: can be a callback (where the current instance of Encore is passed as first parameter) or a boolean. If this results to `true, the parameter `callback` is called
- `callback`: executed when parameter `condition` results to `true`, it takes the current instance of Encore as first parameter
@Kocal Kocal mentioned this pull request Apr 15, 2021
weaverryan added a commit that referenced this pull request Apr 22, 2021
This PR was squashed before being merged into the main branch.

Discussion
----------

feat: add Encore.when()

Replace #900 by adding a more generic and flexible API than `Encore.isDev()`/`Encore.isProd()`, see #900 (comment).

`Encore.when()` takes two parameters:
- `condition`: can be a callback (where the current instance of Encore is passed as first parameter) or a boolean. If this results to `true`, the parameter `callback` is called
- `callback`: executed when parameter `condition` results to `true`, it takes the current instance of Encore as first parameter

WDYT? Thanks!

Commits
-------

8082c61 feat: add Encore.when()
@weaverryan weaverryan closed this Apr 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants