Skip to content
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

Add automatic dependency management/externals generation to build process 🎉(fixes #1052) #1286

Merged
merged 16 commits into from
Jun 4, 2019

Conversation

nerrad
Copy link
Contributor

@nerrad nerrad commented Jun 4, 2019

Problem this Pull Request solves

See #1052 for background. This pull will make php dependency management for scripts and styles, and js webpack configuration for externals much easier (and less prone to human error).

Two primary benefits introduced in this pull:

Dependencies for scripts and styles are automatically registered php side.

With a few exceptions, you'll never have to touch php again for any current registered scripts as it's all handled automatically. The exceptions are:

  • Any script dependent on eventespresso-data-stores. Since this is not imported in implementations, it doesn't get picked up by the dependency extraction. So there may be a few cases where it may need added as a dependency manually. However, in most cases (since it, itself is dependent on wp.data) things should still get enqueued in the correct order.
  • Any style dependent on eventespresso-core-default-theme will need to have that dependency added manually because it is not imported in any implementations (I have some ideas for improving this in future iterations, but its tricky so I'll leave out of this pull for now)

Externals no longer need to be explicitly defined in the webpack config 🎉

Externals are picked up automatically via the dependency extraction webpack plugin (and the configuration for it). If any new packages are added they'll need to be updated in the configuration (I'll take care of doing this for the EDTR branch), but externals are automatically generated correctly for built files using the new webpack plugin.

To make all this possible, there were a number of corollary changes made in php files and js configuration including:

  • new methods (php side) for getting the dependencies from the json files.
  • making the chunk name strings and asset handle labels consistent (reduces the complexity of mapping).
  • some work on making the webpack configs more dry.
  • I also removed redux and react-redux from builds (making the vendor file much smaller) because we don't currently use them anywhere.

How has this been tested

  • Builds should run fine
  • js unit tests should pass

This impacts all current released code using built files so:

  • Verify plugin exit modal works as expected
  • Verify the EventAttendees block works as expected.

Checklist

Copy link
Member

@tn3rb tn3rb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AWESOME STUFF

LOVE IT!!!

one suggestion I have to make things a bit more DRY would be to replace all of the repeated blocks of code in EventEspresso\core\domain\services\assets\CoreAssetManager where the only thing changing is the asset handle.

For example, if we added a new method to EventEspresso\core\services\assets\AssetManager that was maybe something like:

public function addStandardJavascript(
        $handle,
        array $extra_dependencies = array(),
        $load_in_footer = true
    ) {
        $dependencies = ! empty($extra_dependencies) && is_array($extra_dependencies)
            ? array_merge(
                $this->registry->getJsDependencies(
                    $this->domain->assetNamespace(),
                    $handle
                ),
                $extra_dependencies
            )
            : $this->registry->getJsDependencies(
                $this->domain->assetNamespace(),
                $handle
            );
        return $this->addJavascript(
            $handle,
            $this->registry->getJsUrl($this->domain->assetNamespace(), $handle),
            $dependencies,
            $load_in_footer
        );
    }

then the following code in CoreAssetManager::loadCoreJs():

$this->addJavascript(
    CoreAssetManager::JS_HANDLE_JS_CORE,
    $this->registry->getJsUrl($this->domain->assetNamespace(), CoreAssetManager::JS_HANDLE_JS_CORE),
    $this->registry->getJsDependencies(
        $this->domain->assetNamespace(),
        CoreAssetManager::JS_HANDLE_JS_CORE
    )
)
->setHasInlineData();

$this->addJavascript(
    CoreAssetManager::JS_HANDLE_VENDOR,
    $this->registry->getJsUrl($this->domain->assetNamespace(), CoreAssetManager::JS_HANDLE_VENDOR),
    $this->registry->getJsDependencies(
        $this->domain->assetNamespace(),
        CoreAssetManager::JS_HANDLE_VENDOR
    )
);

$this->addJavascript(
    CoreAssetManager::JS_HANDLE_VALIDATORS,
    $this->registry->getJsUrl($this->domain->assetNamespace(), CoreAssetManager::JS_HANDLE_VALIDATORS),
    $this->registry->getJsDependencies(
        $this->domain->assetNamespace(),
        CoreAssetManager::JS_HANDLE_VALIDATORS
    )
)->setRequiresTranslation();

$this->addJavascript(
    CoreAssetManager::JS_HANDLE_HELPERS,
    $this->registry->getJsUrl($this->domain->assetNamespace(), CoreAssetManager::JS_HANDLE_HELPERS),
    $this->registry->getJsDependencies(
        $this->domain->assetNamespace(),
        CoreAssetManager::JS_HANDLE_HELPERS
    )
)->setRequiresTranslation();

could be changed to something like:

$this->addStandardJavascript(CoreAssetManager::JS_HANDLE_JS_CORE)->setHasInlineData();
$this->addStandardJavascript(CoreAssetManager::JS_HANDLE_VENDOR);
$this->addStandardJavascript(CoreAssetManager::JS_HANDLE_VALIDATORS)->setRequiresTranslation();
$this->addStandardJavascript(CoreAssetManager::JS_HANDLE_HELPERS)->setRequiresTranslation();

since the only thing changing in those blocks of code is the asset handle, and what is done to the asset after it is registered.

I couldn't come up with a better name for addStandardJavascript() (addAutoJavascript(), addJavascriptByHandle()???) but you might have a better idea.

Something similar could be done with the CSS assets

core/domain/services/assets/CoreAssetManager.php Outdated Show resolved Hide resolved
core/services/assets/Registry.php Outdated Show resolved Hide resolved
webpack.prod.js Outdated Show resolved Hide resolved
@tn3rb tn3rb removed their assignment Jun 4, 2019
@nerrad nerrad requested a review from tn3rb June 4, 2019 18:04
core/domain/services/assets/CoreAssetManager.php Outdated Show resolved Hide resolved
core/domain/services/assets/CoreAssetManager.php Outdated Show resolved Hide resolved
core/services/assets/BlockAssetManager.php Outdated Show resolved Hide resolved
* This is a known array of possible wp css handles that correspond to what may be exposed as dependencies in our
* build process. Currently the dependency export process in webpack does not consider css imports, so we derive
* them via the js dependencies (WP uses the same handle for both js and css). This is a list of known handles that
* are used for both js and css.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good description 👍

core/services/assets/Registry.php Outdated Show resolved Hide resolved
@tn3rb tn3rb removed their assignment Jun 4, 2019
@nerrad nerrad requested a review from tn3rb June 4, 2019 19:52
Copy link
Member

@tn3rb tn3rb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great!!!

@tn3rb tn3rb removed their assignment Jun 4, 2019
@nerrad nerrad assigned joshfeck and unassigned nerrad Jun 4, 2019
@nerrad
Copy link
Contributor Author

nerrad commented Jun 4, 2019

I gave this a priority of high because the longer this lingers the greater the chance of conflicts. Also, I'd like to merge this into the EDTR refactor work being done (which will already be a tricky merge).

@lorenzocaum
Copy link
Contributor

@nerrad

I tested the Event Attendees block and that worked:

Image 2019-06-04 at 4 09 21 PM

I also tested the exit modal with the skip option and then the yes option:

Image 2019-06-04 at 4 10 20 PM

@nerrad nerrad merged commit b2701ae into master Jun 4, 2019
@nerrad nerrad deleted the FET/add-automatic-dependency-management branch June 4, 2019 20:20
eeteamcodebase pushed a commit that referenced this pull request Jun 4, 2019
…atic dependency management/externals generation to build process 🎉(fixes #1052) (#1286)

* various changes/updates to webpack configs

* Add dependency extraction webpack plugin

* remove unused  dependencies for react-redux and redux

Neither of these packages are used so we can stop including them everywhere.  They can be added back in if we ever have need for them.

* dependency map extractor configuration for externals and external to script handle map.

* update all depedency registrations

* update build files (dev)

* add back in changes removed in a bad rebase

* make webpack config more dry.

* update build files (dev)

* remove unneccesary delete

* fix typo (copy pasta) in webpack.prod.js

* some more DRY work for asset manager

* relocate wp css handles for dependency generation

* add default return for condition where there isn’t a registered manifest file

* remove unnecessary Domain injection

* more implementations of asset helpers
"
Copy link

@sirreal sirreal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool to see this, hope it's working well 😁

@@ -0,0 +1,44 @@
const requestToExternal = ( request ) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😎

@nerrad
Copy link
Contributor Author

nerrad commented Jun 5, 2019

Very cool to see this, hope it's working well 😁

Hey! It's working very well! Thanks for creating the plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants