Skip to content

Commit 33b57f4

Browse files
committed
Merge branch 'v1-develop' into v1
2 parents 412879a + 2f000fa commit 33b57f4

File tree

7 files changed

+2320
-698
lines changed

7 files changed

+2320
-698
lines changed

.php-cs-fixer.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
<?php
22

3-
$finder = PhpCsFixer\Finder::create()
3+
use PhpCsFixer\Finder as PhpCsFixerFinder;
4+
use PhpCsFixer\Config as PhpCsFixerConfig;
5+
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
6+
7+
$finder = PhpCsFixerFinder::create()
48
->in(__DIR__)
59
->exclude('vendor')
610
->exclude('vendor-bin')
711
;
812

9-
$config = new PhpCsFixer\Config();
13+
$config = new PhpCsFixerConfig();
1014
return $config
15+
->setParallelConfig(ParallelConfigFactory::detect())
1116
->setRules([
1217
'@PER-CS' => true,
1318
'binary_operator_spaces' => ['default' => 'at_least_single_space', 'operators' => ['=>' => 'align']],
@@ -19,6 +24,7 @@
1924
'phpdoc_align' => ['align' => 'left'],
2025
'phpdoc_separation' => ['skip_unlisted_annotations' => true],
2126
'self_accessor' => true,
27+
'trailing_comma_in_multiline' => ['after_heredoc' => true, 'elements' => ['arrays']], // remove this line when we drop support for PHP < 8.0
2228
'visibility_required' => ['elements' => ['property', 'method']], // removed 'const' since we still support PHP 7.0 for now
2329
])
2430
->setFinder($finder)

Css/PreProcessor/Adapter/Less/Processor.php

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Baldwin\LessJsCompiler\Css\PreProcessor\Adapter\Less;
44

5+
use Magento\Framework\App\Config\ScopeConfigInterface;
56
use Magento\Framework\App\ProductMetadataInterface;
67
use Magento\Framework\Css\PreProcessor\File\Temporary;
78
use Magento\Framework\Exception\LocalizedException;
@@ -25,6 +26,7 @@ class Processor implements ContentProcessorInterface
2526
private $productMetadata;
2627
private $filesystem;
2728
private $directoryList;
29+
private $scopeConfig;
2830

2931
public function __construct(
3032
LoggerInterface $logger,
@@ -33,7 +35,8 @@ public function __construct(
3335
ShellInterface $shell,
3436
ProductMetadataInterface $productMetadata,
3537
Filesystem $filesystem,
36-
DirectoryList $directoryList
38+
DirectoryList $directoryList,
39+
ScopeConfigInterface $scopeConfig
3740
) {
3841
$this->logger = $logger;
3942
$this->assetSource = $assetSource;
@@ -42,6 +45,7 @@ public function __construct(
4245
$this->productMetadata = $productMetadata;
4346
$this->filesystem = $filesystem;
4447
$this->directoryList = $directoryList;
48+
$this->scopeConfig = $scopeConfig;
4549
}
4650

4751
/**
@@ -89,24 +93,28 @@ public function processContent(AssetFile $asset)
8993
*/
9094
protected function compileFile($filePath)
9195
{
92-
$nodeCmdArgs = $this->getNodeArgsAsString();
93-
$lessCmdArgs = $this->getCompilerArgsAsString();
94-
$cmd = "%s $nodeCmdArgs %s $lessCmdArgs %s";
96+
$nodeCmdArgs = $this->getNodeArgsAsArray();
97+
$lessCmdArgs = $this->getCompilerArgsAsArray();
98+
99+
$cmd = '%s ' . str_repeat(' %s', count($nodeCmdArgs)) . ' %s' . str_repeat(' %s', count($lessCmdArgs)) . ' %s';
100+
$arguments = [];
101+
$arguments[] = $this->getPathToNodeBinary();
102+
$arguments = array_merge($arguments, $nodeCmdArgs);
103+
$arguments[] = $this->getPathToLessCompiler();
104+
$arguments = array_merge($arguments, $lessCmdArgs);
105+
$arguments[] = $filePath;
95106

96107
// to log or not to log, that's the question
97108
// also, it would be better to use the logger in the Shell class,
98109
// since that one will contain the exact correct command, and not this sprintf version
110+
// $logArguments = array_map('escapeshellarg', $arguments);
99111
// $this->logger->debug('Less compilation command: `'
100-
// . sprintf($cmd, $this->getPathToNodeBinary(), $this->getPathToLessCompiler(), $filePath)
112+
// . sprintf($cmd, ...$logArguments)
101113
// . '`');
102114

103115
return $this->shell->execute(
104116
$cmd,
105-
[
106-
$this->getPathToNodeBinary(),
107-
$this->getPathToLessCompiler(),
108-
$filePath,
109-
]
117+
$arguments
110118
);
111119
}
112120

@@ -117,9 +125,23 @@ protected function compileFile($filePath)
117125
*/
118126
protected function getCompilerArgsAsString()
119127
{
120-
$args = ['--no-color']; // for example: --no-ie-compat, --no-js, --compress, ...
128+
$args = $this->getConfigValueFromPath('dev/less_js_compiler/less_arguments');
129+
if ($args === null) {
130+
// default supplied args
131+
$args = '--no-color'; // for example: --ie-compat --compress --math="always", ...
132+
}
121133

122-
return implode(' ', $args);
134+
return $args;
135+
}
136+
137+
/**
138+
* Get all arguments which will be used in the cli call to the lessc compiler
139+
*
140+
* @return array<string>
141+
*/
142+
protected function getCompilerArgsAsArray()
143+
{
144+
return explode(' ', $this->getCompilerArgsAsString());
123145
}
124146

125147
/**
@@ -154,9 +176,23 @@ protected function getPathToLessCompiler()
154176
*/
155177
protected function getNodeArgsAsString()
156178
{
157-
$args = ['--no-deprecation']; // squelch warnings about deprecated modules being used
179+
$args = $this->getConfigValueFromPath('dev/less_js_compiler/node_arguments');
180+
if ($args === null) {
181+
// default supplied args
182+
$args = '--no-deprecation'; // squelch warnings about deprecated modules being used
183+
}
158184

159-
return implode(' ', $args);
185+
return $args;
186+
}
187+
188+
/**
189+
* Get all arguments which will be used in the cli call with the nodejs binary
190+
*
191+
* @return array<string>
192+
*/
193+
protected function getNodeArgsAsArray()
194+
{
195+
return explode(' ', $this->getNodeArgsAsString());
160196
}
161197

162198
/**
@@ -207,4 +243,19 @@ protected function outputErrorMessage($errorMessage, AssetFile $file)
207243

208244
$this->logger->critical($errorMessage);
209245
}
246+
247+
/**
248+
* @param string $path
249+
*
250+
* @return ?string
251+
*/
252+
private function getConfigValueFromPath($path)
253+
{
254+
$value = $this->scopeConfig->getValue($path);
255+
if (is_string($value)) {
256+
return $value;
257+
}
258+
259+
return null;
260+
}
210261
}

README.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ You'll also need [composer](https://getcomposer.org/) to add this module to your
2323
First, we recommend you to install the less compiler itself, and save it into your `package.json` file as a production dependency:
2424

2525
```sh
26-
npm install --save less@1.7.5
26+
npm install --save less@3.13.1
2727
```
2828

2929
> Watch out: from Magento 2.1 onwards, the `package.json` file is being renamed to `package.json.sample` to enable you to have your own nodejs dependencies without Magento overwriting this every time with its own version each time you update Magento. So if you use Magento >= 2.1 make sure you copy the `package.json.sample` file to `package.json` before running the above command if you want to work with Magento's `grunt` setup.
@@ -53,18 +53,39 @@ bin/magento setup:upgrade
5353

5454
As the last step, in your deploy scripts, make sure you call `npm install --production`. You should execute this somewhere between `composer install` and `bin/magento setup:static-content:deploy`
5555

56+
## Configuration
57+
58+
You have the opportunity to configure the arguments we send to the `lessc` and `node` commands.
59+
The defaults are:
60+
61+
- `lessc --no-color`
62+
- `node --no-deprecation`
63+
64+
If you want to override the default arguments, you can do this by modifying your `app/etc/config.php` or `app/etc/env.php` file and adding something like this:
65+
66+
```php
67+
'system' => [
68+
'default' => [
69+
'dev' => [
70+
'less_js_compiler' => [
71+
'less_arguments' => '--no-color --ie-compat',
72+
'node_arguments' => '--no-deprecation --no-warnings',
73+
]
74+
]
75+
]
76+
]
77+
```
78+
5679
## Debugging less compilation errors
5780

5881
When your `.less` files have a syntax error or contain something which doesn't allow it to compile properly, please have a look at the `var/log/system.log` file, it will contain the error what causes the problem.
5982

6083
## Remarks
6184

6285
1. Installing this module will effectively replace the default less compiler from Magento2. If you want to go back to the default less compiler, you need to disable this module or uninstall it.
63-
2. We strongly recommend you to install less.js version 1.7.5, and not the very latest version (which is 2.7.1 at the time). Magento's built-in less.php library is based on less.js version 1.x and isn't compatible with 2.x. This means Magento has only tested their less files with a less compiler which is compatible with version 1.x of less. If you want to use version 2.x (which you certainly can), be aware that there might be subtle changes in the resulting css.
64-
Also: if you use `grunt` or `gulp` in your frontend workflow, you are probably also using less.js version 1.7.5, so using this module makes sure what you see on the server is 100% exactly the same as on your developer machine.
65-
3. This module expects the less compiler to exist in `{MAGENTO_ROOT_DIR}/node_modules/.bin/lessc`, this is a hard coded path which is being used in the module. The compiler will end up there if you follow the installation steps above, but if for some reason you prefer to install your nodejs modules someplace else, then this module won't work. If somebody actually has this problem and has an idea how to make this path configurable (preferably without getting it from the database), please let me know!
66-
4. The default less processor in Magento 2 passes an option to the less compiler, which says it should [compress the resulting css file](https://github.com/magento/magento2/blob/6a40b41f6281c7d405cd78029d6becab1d837c87/lib/internal/Magento/Framework/Css/PreProcessor/Adapter/Less/Processor.php#L73). In this module, we have chosen not to do so, as we believe this isn't a task to be executed while compiling less files. It should be done further down the line, like for example during the minification phase. If someone disagrees with this, please let me know, I'm open to discussion about this.
67-
5. This module was tested against Magento versions 2.0.7, 2.1.0 - 2.1.9 and 2.2.0
86+
2. This module expects the less compiler to exist in `{MAGENTO_ROOT_DIR}/node_modules/.bin/lessc`, this is a hard coded path which is being used in the module. The compiler will end up there if you follow the installation steps above, but if for some reason you prefer to install your nodejs modules someplace else, then this module won't work. If somebody actually has this problem and wants us to make this configurable, please let me know!
87+
3. The default less processor in Magento 2 passes an option to the less compiler, which says it should [compress the resulting css file](https://github.com/magento/magento2/blob/6a40b41f6281c7d405cd78029d6becab1d837c87/lib/internal/Magento/Framework/Css/PreProcessor/Adapter/Less/Processor.php#L73). In this module, we have chosen not to do so, as we believe this isn't a task to be executed while compiling less files. It should be done further down the line, like for example during the minification phase. If someone disagrees with this, please let me know, I'm open to discussion about this.
88+
4. This module was tested against Magento versions 2.0.7, 2.1.x, 2.2.x, 2.3.x, 2.4.x
6889

6990
## Benchmarks
7091

0 commit comments

Comments
 (0)