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 asset BundleManager & re-organize the asset helper classes #1164

Merged
merged 51 commits into from
Jul 23, 2024

Conversation

jaxwilko
Copy link
Member

@jaxwilko jaxwilko commented Jul 17, 2024

This PR adds a new BundleManager class which is responsible for managing the different "asset bundles" available in Winter. Bundles will automatically be offered by the vite:create & mix:create scaffolding commands as option flags.

This allows us to:

  • Have a single entity responsible for managing versions
  • Allow the user to configure their own bundles / overwrite the existing bundles if desired
  • Manage the update process better by reducing magic numbers in the system

I've also cleaned up some of the install logic and added some user feedback similar to create:plugin when running {$compiler}:create.

In addition to the above, this PR removes the majority of the fixtures for Mix/Vite configs and now lets subscribers modify the base config on the fly.

\System\Classes\Asset\BundleManager::instance()->registerCallback(function ($manager) {
    // Register a bundle (using a compiler as a key and having an array as a value means that the array 
    // will be squashed into the bundle when that compiler is being called, else the array will be discarded):
    $manager->registerBundle('jaxjs', [
        'jaxjs' => 'v1.0.0',
        'mix' => [
            'mix-jaxjs-adapter' => 'v1.2.0'
        ],
        'vite' => [
            'vite-jaxjs-adapter' => 'v1.6.0'
        ],
    ]);
    
    // Register a setup handler (is executed on install to allow for bundles to generate their own config
    // files / prep the plugin)
    $manager->registerSetupHandler('jaxjs', function (string $packagePath, string $packageType) {
        $this->writeFile(
            $packagePath . '/jax.js',
            File::get(__DIR__ . '/../fixtures/jax.js')
        );
    });

    // Register a scaffold handler (is executed during stub/config generation), `contentType`
    // can be: vite, mix, js, css.
    $manager->registerScaffoldHandler('jaxjs', function (string $contents, string $contentType) {
        return match ($contentType) {
            'mix' => $contents . PHP_EOL . <<<JAVASCRIPT
            console.log('jaxjs installed');
            JAVASCRIPT,
             'vite' => $contents . PHP_EOL . '// example',
            'css' => $content . PHP_EOL . $this->getFixture('css/jaxjs.css.fixture'),
            'js' => $this->getFixture('js/jaxjs.fixture')
            default => $contents
        };
    });
]);

Once the configs have been set, either via a config file, or from config calls in a plugin, the following can be ran in the CLI:

./artisan vite:create Acme.Plugin --jaxjs
# Or
./artisan mix:create Acme.Plugin --jaxjs

Plugins can call these methods to register handlers to modify either stub or config generation, or do additional tasks when the {$compiler}:create command is ran.

NOTE: setupHandlers & scaffoldHandlers are executed within the context of AssetCreate this means they have access to the internal properties of the class such as assetType (mix or vite) and have access to methods such as writeFile() & getFixture().

@bennothommo
Copy link
Member

@jaxwilko you might consider putting the "groups" of packages in that same class and call it NodePackages. Then the commands could simply call the group in. You could even make it extensible - ie. allow plugins to define packages if the plugins themselves provide front-end functionality?

@jaxwilko
Copy link
Member Author

@jaxwilko you might consider putting the "groups" of packages in that same class and call it NodePackages. Then the commands could simply call the group in. You could even make it extensible - ie. allow plugins to define packages if the plugins themselves provide front-end functionality?

Thy will be done

@jaxwilko jaxwilko requested a review from LukeTowers July 18, 2024 08:32
@jaxwilko jaxwilko marked this pull request as ready for review July 18, 2024 08:32
@LukeTowers LukeTowers added maintenance PRs that fix bugs, are translation changes or make only minor changes Status: Testing Needed labels Jul 21, 2024
@LukeTowers LukeTowers added this to the 1.2.7 milestone Jul 21, 2024
modules/system/classes/asset/BundleManager.php Outdated Show resolved Hide resolved
modules/system/classes/asset/BundleManager.php Outdated Show resolved Hide resolved
modules/system/console/asset/AssetCreate.php Outdated Show resolved Hide resolved
@LukeTowers LukeTowers requested a review from bennothommo July 22, 2024 04:24
@jaxwilko
Copy link
Member Author

Docs for new BundleManager features have been added here: wintercms/docs#203

@LukeTowers LukeTowers merged commit fb719f4 into develop Jul 23, 2024
11 checks passed
@LukeTowers LukeTowers deleted the wip/asset-version-manager branch July 23, 2024 04:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance PRs that fix bugs, are translation changes or make only minor changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants