-
-
Notifications
You must be signed in to change notification settings - Fork 191
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
Integrate Vite & Rework Mix #1141
Merged
Merged
Changes from 15 commits
Commits
Show all changes
63 commits
Select commit
Hold shift + click to select a range
eb3573f
Initial wip
jaxwilko 989f1cf
Switched implementation to use PackageJson
jaxwilko 5743fbb
Added fix for packages being resolved as an object in json
jaxwilko d6a15ce
Added vite function to twig
jaxwilko c847c1a
Added vite commands, renamed old commands and added abstraction
jaxwilko c373d9a
Added vite config stubs
jaxwilko ea92c49
Added laravel plugin to install list
jaxwilko 0eaa431
Added fix for eslint parsing fixtures
jaxwilko 5d53b4d
Added fix for undefined keys
jaxwilko 96f1bc6
Revert workflow change
jaxwilko bf96f7f
Added comment and return type
jaxwilko 1e4ff21
Added comments and small tweaks
jaxwilko 08cca0c
Apply suggestions from code review
jaxwilko e35a1a0
Added common aliases
jaxwilko 914de7d
Merge branch 'wip/vite-mix-v2' of github.com:wintercms/winter into wi…
jaxwilko eacd3c2
Remove duplicated type information
LukeTowers eb95c3c
Apply suggestions from code review
LukeTowers 1500557
Added reordered params and script handling for PackageJson
jaxwilko 6b2b30e
Added fix for getPackage returning incorrect package config
jaxwilko 4a2151e
Added fix for checking package registered
jaxwilko 5aa3248
Added fixes for mix:compile with incorrect config paths
jaxwilko 97576c6
Added Vite unit test
jaxwilko 3a98a96
Added eslint ignores
jaxwilko 8a696dc
Added fix to enable passing package name to vite twig function
jaxwilko ec7680b
Added default to .mjs file to fix module conflict
jaxwilko 0eb0a99
Updated fixtures and added css stubs
jaxwilko 3f5bbca
Added support for setting up default stub files
jaxwilko bf954fc
Added postcss config when setting up tailwind config
jaxwilko c3f8099
Apply suggestions from code review
jaxwilko d55b517
Added helper for generating vite tags
jaxwilko 0568b54
Merge
jaxwilko d46a830
Added config tests
jaxwilko 5f50119
Added fix for namespace
jaxwilko d82978a
Added refactor to use artisan helper
jaxwilko 11ca249
Merge branch 'develop' into wip/vite-mix-v2
jaxwilko 647f285
Added fix for unset position
jaxwilko 744245b
Added support for --disable-tty
jaxwilko 8a18c9b
Added support for --disable-tty to vite commands
jaxwilko 2c3f3a5
Added tests
jaxwilko e5e2cc1
Cleaned up test with artisan command
jaxwilko 8bd7043
Added confirmation for overriding configured package
jaxwilko c708a01
Added blocks to vite config refresh paths
jaxwilko d02926c
Added extra tests and fixes for PackageJson
jaxwilko ba8cadc
Added metapackage list and added js stubs
jaxwilko 78de514
Added js/css inputs as default for all configs
jaxwilko 3c5d45a
Added compile on exit for vite watch
jaxwilko c55bb79
Added export-ignore for package.json
jaxwilko e2a8da0
Added fix for mixed case package names coming from PluginManager
jaxwilko 25d3c43
Added addVite() method to AssetMaker
jaxwilko 49962d6
Added fix so mix & vite stub paths match
jaxwilko f5626be
Added fix for incorrect signature in npm:run
jaxwilko 41eaffb
Added vite asset render to twig scripts tag
jaxwilko fd27f1b
Update modules/system/classes/CompilableAssets.php
LukeTowers be4e50f
Update modules/system/classes/PackageJson.php
LukeTowers f55d171
Update modules/system/console/asset/AssetConfig.php
LukeTowers c3fadc0
Added registration method name generation
jaxwilko 6aa24cc
Merge branch 'wip/vite-mix-v2' of github.com:wintercms/winter into wi…
jaxwilko 1d9257e
Update modules/cms/twig/Extension.php
jaxwilko 2905f62
Swapped exception
jaxwilko 6409346
Added fix for incorrect error message
jaxwilko 287dae5
Swapped exception
jaxwilko 72f1e29
Removed auto populate of config on install without config
jaxwilko 9b252d6
Merge branch 'develop' into wip/vite-mix-v2
jaxwilko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,307 @@ | ||
<?php namespace System\Classes; | ||
LukeTowers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
use Cms\Classes\Theme; | ||
use System\Classes\PluginManager; | ||
use Winter\Storm\Exception\SystemException; | ||
use Winter\Storm\Filesystem\PathResolver; | ||
use Winter\Storm\Support\Facades\Config; | ||
use Winter\Storm\Support\Facades\File; | ||
use Winter\Storm\Support\Str; | ||
|
||
/** | ||
* Compilable assets using for Node.js compilation and processing. | ||
* | ||
* This works similar to the `System\Classes\CombineAssets` class in that it allows modules, plugins and themes to | ||
* register configurations that will be passed on to Node.js based compilers for processing. | ||
* | ||
* @package winter\wn-system-module | ||
* @author Ben Thomson <git@alfreido.com>, Jack Wilkinson <me@jackwilky.com> | ||
jaxwilko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @author Winter CMS | ||
jaxwilko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
class CompilableAssets | ||
{ | ||
use \Winter\Storm\Support\Traits\Singleton; | ||
|
||
/** | ||
* The filename that stores the package definition. | ||
*/ | ||
protected string $packageJson = 'package.json'; | ||
|
||
/** | ||
* List of package types and registration methods | ||
jaxwilko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
protected array $compilableConfigs = [ | ||
'mix' => [ | ||
'registrationMethod' => 'registerMixPackages', | ||
'configFile' => 'winter.mix.js' | ||
], | ||
'vite' => [ | ||
'registrationMethod' => 'registerVitePackages', | ||
jaxwilko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'configFile' => 'vite.config.js' | ||
] | ||
]; | ||
|
||
/** | ||
* A list of packages registered for mixing. | ||
LukeTowers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
protected array $packages = []; | ||
|
||
/** | ||
* Registered callbacks. | ||
*/ | ||
protected static array $callbacks = []; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public function init(): void | ||
{ | ||
$packagePaths = []; | ||
|
||
/* | ||
* Get packages registered in plugins. | ||
* | ||
* In the Plugin.php file for your plugin, you can define the `registerMixPackages` or `registerVitePackages` | ||
* method and return an array, with the name of the package being the key, and the build config path - relative | ||
* to the plugin directory - as the value. | ||
* | ||
* Example: | ||
* | ||
* public function registerMixPackages() | ||
LukeTowers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* { | ||
* return [ | ||
* 'package-name-1' => 'winter.mix.js', | ||
* 'package-name-2' => 'assets/js/build.js', | ||
* ]; | ||
* } | ||
* | ||
* public function registerVitePackages() | ||
LukeTowers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* { | ||
* return [ | ||
* 'package-name-1' => 'vite.config.js', | ||
* 'package-name-2' => 'assets/js/build.js', | ||
* ]; | ||
* } | ||
*/ | ||
foreach ($this->compilableConfigs as $type => $config) { | ||
$packages = PluginManager::instance()->getRegistrationMethodValues($config['registrationMethod']); | ||
if (count($packages)) { | ||
foreach ($packages as $pluginCode => $packageArray) { | ||
if (!is_array($packageArray)) { | ||
continue; | ||
} | ||
|
||
foreach ($packageArray as $name => $package) { | ||
$this->registerPackage( | ||
$name, | ||
PluginManager::instance()->getPluginPath($pluginCode) . '/' . $package, | ||
$type | ||
); | ||
} | ||
} | ||
} | ||
|
||
// Get the currently enabled modules | ||
$enabledModules = Config::get('cms.loadModules', []); | ||
|
||
if (in_array('Cms', $enabledModules)) { | ||
// Allow current theme to define mix assets | ||
$theme = Theme::getActiveTheme(); | ||
|
||
if (!is_null($theme)) { | ||
$mix = $theme->getConfigValue($type, []); | ||
|
||
if (count($mix)) { | ||
foreach ($mix as $name => $file) { | ||
$this->registerPackage($name, $theme->getPath() . '/' . $file, $type); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Search modules for compilable packages to autoregister | ||
foreach ($enabledModules as $module) { | ||
$module = strtolower($module); | ||
$path = base_path('modules' . DIRECTORY_SEPARATOR . $module) . DIRECTORY_SEPARATOR . $config['configFile']; | ||
if (File::exists($path)) { | ||
$packagePaths[$type]["module-$module"] = $path; | ||
} | ||
} | ||
|
||
// Search plugins for compilable packages to autoregister | ||
$plugins = PluginManager::instance()->getPlugins(); | ||
foreach ($plugins as $plugin) { | ||
$path = $plugin->getPluginPath() . '/' . $config['configFile']; | ||
if (File::exists($path)) { | ||
$packagePaths[$type][$plugin->getPluginIdentifier()] = $path; | ||
} | ||
} | ||
|
||
// Search themes for compilable packages to autoregister | ||
if (in_array('Cms', $enabledModules)) { | ||
$themes = Theme::all(); | ||
foreach ($themes as $theme) { | ||
$path = $theme->getPath() . '/' . $config['configFile']; | ||
if (File::exists($path)) { | ||
$packagePaths[$type]["theme-" . $theme->getId()] = $path; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Register the autodiscovered compilable packages | ||
foreach ($packagePaths as $type => $packages) { | ||
foreach ($packages as $package => $path) { | ||
try { | ||
$this->registerPackage($package, $path, $type); | ||
} catch (SystemException $e) { | ||
// Either the package name or the config file path have already been registered, skip. | ||
continue; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Register a compilable config, this allows the | ||
* @param string $name | ||
* @param array $config | ||
* @return void | ||
*/ | ||
public function registerCompilable(string $name, array $config): void | ||
{ | ||
$this->compilableConfigs[$name] = $config; | ||
} | ||
|
||
/** | ||
* Registers a callback for processing. | ||
*/ | ||
public static function registerCallback(callable $callback): void | ||
{ | ||
static::$callbacks[] = $callback; | ||
} | ||
|
||
/** | ||
* Calls the deferred callbacks. | ||
*/ | ||
public function fireCallbacks(): void | ||
{ | ||
// Call callbacks | ||
foreach (static::$callbacks as $callback) { | ||
$callback($this); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the count of packages registered. | ||
*/ | ||
public function getPackageCount(): int | ||
{ | ||
return array_sum(array_map(fn ($packages) => count($packages), ...$this->packages)); | ||
} | ||
|
||
/** | ||
* Returns all packages registered. | ||
*/ | ||
public function getPackages(bool $includeIgnored = false, string $type = 'mix'): array | ||
LukeTowers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
$packages = $this->packages[$type] ?? []; | ||
|
||
ksort($packages); | ||
|
||
if (!$includeIgnored) { | ||
return array_filter($packages, function ($package) { | ||
return !($package['ignored'] ?? false); | ||
}); | ||
} | ||
|
||
return $packages; | ||
} | ||
|
||
/** | ||
* Registers an entity as a package for compilation. | ||
* | ||
* Entities can include plugins, components, themes, modules and much more. | ||
* | ||
* The name of the package is an alias that can be used to reference this package in other methods within this | ||
* class. | ||
* | ||
* By default, the CompilableAssets class will look for a `package.json` file for Node dependencies, and a config | ||
jaxwilko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* file for the compilable configuration | ||
* | ||
* @param string $name The name of the package being registered | ||
* @param string $path The path to the compilable JS configuration file. If there is a related package.json file | ||
* then it is required to be present in the same directory as the config file | ||
* @param string $type The type of compilable | ||
* @throws SystemException | ||
*/ | ||
public function registerPackage(string $name, string $path, string $type = 'mix'): void | ||
{ | ||
// Symbolize the path | ||
$path = File::symbolizePath($path); | ||
|
||
// Normalize the arguments | ||
$name = strtolower($name); | ||
$resolvedPath = PathResolver::resolve($path); | ||
$pinfo = pathinfo($resolvedPath); | ||
$path = Str::after($pinfo['dirname'], base_path() . DIRECTORY_SEPARATOR); | ||
$configFile = $pinfo['basename']; | ||
|
||
// Require $configFile to be a JS file | ||
$extension = File::extension($configFile); | ||
if ($extension !== 'js') { | ||
throw new SystemException( | ||
sprintf('Compilable configuration for package "%s" must be a JavaScript file ending with .js', $name) | ||
); | ||
} | ||
|
||
// Check that the package path exists | ||
if (!File::exists($path)) { | ||
throw new SystemException( | ||
sprintf('Cannot register "%s" as a compilable package; the "%s" path does not exist.', $name, $path) | ||
); | ||
} | ||
|
||
// Check for any existing packages already registered under the provided name | ||
if (isset($this->packages[$name])) { | ||
throw new SystemException(sprintf( | ||
'Cannot register "%s" as a compilable package; it has already been registered at %s.', | ||
$name, | ||
$this->packages[$name]['config'] | ||
)); | ||
} | ||
|
||
$package = "$path/{$this->packageJson}"; | ||
$config = $path . DIRECTORY_SEPARATOR . $configFile; | ||
|
||
// Check for any existing package that already registers the given compilable config path | ||
foreach ($this->packages[$type] ?? [] as $packageName => $settings) { | ||
if ($settings['config'] === $config) { | ||
throw new SystemException(sprintf( | ||
'Cannot register "%s" (%s) as a compilable package; it has already been registered as %s.', | ||
$name, | ||
$config, | ||
$packageName | ||
)); | ||
} | ||
} | ||
|
||
// Register the package | ||
$this->packages[$type][$name] = [ | ||
'path' => $path, | ||
'package' => $package, | ||
'config' => $config, | ||
'ignored' => $this->isPackageIgnored($path), | ||
]; | ||
} | ||
|
||
/** | ||
* Check if the provided package is ignored. | ||
*/ | ||
protected function isPackageIgnored(string $packagePath): bool | ||
{ | ||
// Load the main package.json for the project | ||
$packageJson = new PackageJson(base_path($this->packageJson)); | ||
return $packageJson->hasIgnoredPackage($packagePath); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to double-up @return doc with return type definition, unless you want to specify some additional content for the return doc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll go through and remove all of them where relevant :)