Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,11 @@
"replace": false,
"merge-dev": false
}
},
"config": {
"allow-plugins": {
"composer/installers": true,
"wikimedia/composer-merge-plugin": true
}
}
}
2 changes: 1 addition & 1 deletion modules/system/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ protected function registerAssetBundles()
});

MixAssets::registerCallback(function ($mix) {
$mix->registerPackage('snowboard', '~/modules/system/assets/js/snowboard');
$mix->registerPackage('snowboard', '~/modules/system/assets/js/snowboard/winter.mix.js');
});
}

Expand Down

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion modules/system/assets/js/snowboard/build/snowboard.base.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

137 changes: 108 additions & 29 deletions modules/system/classes/MixAssets.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php namespace System\Classes;

use File;
use ApplicationException;
use Config;
use SystemException;
use Cms\Classes\Theme;
use Winter\Storm\Support\Str;
use System\Classes\PluginManager;
use Winter\Storm\Filesystem\PathResolver;

/**
Expand All @@ -26,6 +29,13 @@ class MixAssets
*/
protected $packageJson = 'package.json';

/**
* The filename that stores the Laravel Mix configuration
*
* @var string
*/
protected $mixJs = 'winter.mix.js';

/**
* A list of packages registered for mixing.
*
Expand Down Expand Up @@ -59,7 +69,7 @@ public function init()
* public function registerMixPackages()
* {
* return [
* 'package-name-1' => 'winter-mix.js',
* 'package-name-1' => 'winter.mix.js',
* 'package-name-2' => 'assets/js/build.js',
* ];
* }
Expand All @@ -72,26 +82,69 @@ public function init()
}

foreach ($packageArray as $name => $package) {
$this->registerPackage($name, PluginManager::instance()->getPluginPath($pluginCode), $package);
$this->registerPackage($name, PluginManager::instance()->getPluginPath($pluginCode) . '/' . $package);
}
}
}

// Allow current theme to define mix assets
$theme = Theme::getActiveTheme();
// 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('mix', []);

if (!is_null($theme)) {
$mix = $theme->getConfigValue('mix', []);
if (count($mix)) {
foreach ($mix as $name => $file) {
$this->registerPackage($name, $theme->getPath() . '/' . $file);
}
}
}
}

$packagePaths = [];

// Search modules for Mix packages to autoregister
foreach ($enabledModules as $module) {
$module = strtolower($module);
$path = base_path("modules/$module") . '/' . $this->mixJs;
if (File::exists($path)) {
$packagePaths["module-$module"] = $path;
}
}

if (count($mix)) {
foreach ($mix as $name => $file) {
$path = PathResolver::resolve($theme->getPath() . '/' . $file);
$pinfo = pathinfo($path);
// Search plugins for Mix packages to autoregister
$plugins = PluginManager::instance()->getPlugins();
foreach ($plugins as $plugin) {
$path = $plugin->getPluginPath() . '/' . $this->mixJs;
if (File::exists($path)) {
$packagePaths[$plugin->getPluginIdentifier()] = $path;
}
}

$this->registerPackage($name, $pinfo['dirname'], $pinfo['basename']);
// Search themes for Mix packages to autoregister
if (in_array('Cms', $enabledModules)) {
$themes = Theme::all();
foreach ($themes as $theme) {
$path = $theme->getPath() . '/' . $this->mixJs;
if (File::exists($path)) {
$packagePaths["theme-" . $theme->getId()] = $path;
}
}
}

// Register the autodiscovered Mix packages
foreach ($packagePaths as $package => $path) {
try {
$this->registerPackage($package, $path);
} catch (SystemException $e) {
// Either the package name or the mixJs path have already been registered, skip.
continue;
}
}
}

/**
Expand Down Expand Up @@ -135,6 +188,7 @@ public function getPackageCount()
*/
public function getPackages()
{
ksort($this->packages);
return $this->packages;
}

Expand All @@ -146,40 +200,65 @@ public function getPackages()
* 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 MixAssets class will look for a `package.json` file for Node dependencies, and a `
* By default, the MixAssets class will look for a `package.json` file for Node dependencies, and a `winter.mix.js`
* file for the Laravel Mix configuration
*
* @param string $name
* @param string $path
* @param string $packageJson
* @param string $mixJson
* @param string $name The name of the package being registered
* @param string $path The path to the Mix JS configuration file. If there is a related package.json file then it is
* required to be present in the same directory as the winter.mix.js file
* @return void
*/
public function registerPackage($name, $path, $mixJs = 'winter-mix.js')
public function registerPackage($name, $path)
{
// Require JS file for $mixJs
// 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() . '/');
$mixJs = $pinfo['basename'];

// Require $mixJs to be a JS file
$extension = File::extension($mixJs);
if ($extension !== 'js') {
throw new ApplicationException(
throw new SystemException(
sprintf('The mix configuration for package "%s" must be a JavaScript file ending with .js', $name)
);
}

$path = rtrim(File::symbolizePath($path), '/\\');
if (!File::exists($path . DIRECTORY_SEPARATOR . $this->packageJson)) {
throw new ApplicationException(
sprintf('Missing file "%s" in path "%s" for package "%s"', $this->packageJson, $path, $name)
// Check that the package path exists
if (!File::exists($path)) {
throw new SystemException(
sprintf('Cannot register "%s" as a Mix package; the "%s" path does not exist.', $name, $path)
);
}
if (!File::exists($path . DIRECTORY_SEPARATOR . $mixJs)) {
throw new ApplicationException(
sprintf('Missing file "%s" in path "%s" for package "%s"', $mixJs, $path, $name)

// 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 Mix package; it has already been registered at %s.', $name, $this->packages[$name]['mix'])
);
}

$package = "$path/{$this->packageJson}";
$mix = "$path/$mixJs";

// Check for any existing package that already registers the given Mix path
foreach ($this->packages as $packageName => $config) {
if ($config['mix'] === $mix) {
throw new SystemException(
sprintf('Cannot register "%s" (%s) as a Mix package; it has already been registered as %s.', $name, $mix, $packageName)
);
}
}

// Register the package
$this->packages[$name] = [
'path' => $path,
'package' => $path . DIRECTORY_SEPARATOR . $this->packageJson,
'mix' => $path . DIRECTORY_SEPARATOR . $mixJs,
'package' => $package,
'mix' => $mix,
];
}
}
1 change: 1 addition & 0 deletions modules/system/classes/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ public function bootPlugin($plugin)
public function getPluginPath($id)
{
$classId = $this->getIdentifier($id);
$classId = $this->normalizeIdentifier($classId);
if (!isset($this->pathMap[$classId])) {
return null;
}
Expand Down
Loading