Skip to content

Commit d88601c

Browse files
Support additional load paths (#37)
* Support additional load paths * Add an empty commit to rebuild CI * [docs]: Rewrite header in capitalized style * Add an empty commit to rebuild CI again --------- Co-authored-by: bocharsky-bw <[email protected]>
1 parent aa47da8 commit d88601c

File tree

7 files changed

+75
-2
lines changed

7 files changed

+75
-2
lines changed

doc/index.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ You can configure most of the `Dart Sass CLI options <https://sass-lang.com/docu
205205
# Emit a @charset or BOM for CSS with non-ASCII characters. Defaults to true in Dart Sass.
206206
# charset: true
207207
208+
# Register additional load paths. Defaults to empty array.
209+
# load_path: []
210+
208211
# Wether to generate source maps. Defaults to true when "kernel.debug" is true.
209212
# source_map: true
210213
@@ -236,3 +239,24 @@ This bundle already installed for you the right binary. However, if you already
236239
237240
symfonycasts_sass:
238241
binary: 'node_modules/.bin/sass'
242+
243+
Register Additional Load Paths
244+
------------------------------
245+
246+
You can provide additional `load paths <https://sass-lang.com/documentation/at-rules/use/#load-paths>`_ to resolve modules with the ``load_path`` option.
247+
248+
For example, an alternative way to use Bootstrap would be to register the vendor path:
249+
250+
.. code-block:: yaml
251+
252+
# config/packages/symfonycasts_sass.yaml
253+
symfonycasts_sass:
254+
sass_options:
255+
load_path:
256+
- '%kernel.project_dir%/vendor/bootstrap/scss'
257+
258+
And then import bootstrap from ``app.scss`` with:
259+
260+
.. code-block:: scss
261+
262+
@import 'bootstrap';

src/DependencyInjection/SymfonycastsSassExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ public function getConfigTreeBuilder(): TreeBuilder
112112
->info('Embed source map contents in CSS.')
113113
->defaultValue('%kernel.debug%')
114114
->end()
115+
->arrayNode('load_path')
116+
->info('Additional load paths')
117+
->scalarPrototype()
118+
->end()
119+
->end()
115120
->booleanNode('quiet')
116121
->info('Don\'t print warnings.')
117122
->end()

src/SassBuilder.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class SassBuilder
2525
'--style' => 'expanded', // Output style. [expanded (default), compressed]
2626
'--[no-]charset' => null, // Emit a @charset or BOM for CSS with non-ASCII characters.
2727
'--[no-]error-css' => null, // Emit a CSS file when an error occurs.
28+
'--load-path' => null, // Additional load paths
2829
// Source Maps
2930
'--[no-]source-map' => true, // Whether to generate source maps. (defaults to on)
3031
'--[no-]embed-sources' => null, // Embed source file contents in source maps.
@@ -123,7 +124,7 @@ public function getScssCssTargets(): array
123124
}
124125

125126
/**
126-
* @param array<string, bool|string> $options
127+
* @param array<string, bool|array|string> $options
127128
*
128129
* @return list<string>
129130
*/
@@ -147,6 +148,13 @@ public function getBuildOptions(array $options = []): array
147148
$buildOptions[] = $option.'='.$value;
148149
continue;
149150
}
151+
// --load-path
152+
if (\is_array($value)) {
153+
foreach ($value as $item) {
154+
$buildOptions[] = $option.'='.$item;
155+
}
156+
continue;
157+
}
150158
// --update
151159
// --watch
152160
if ($value) {
@@ -182,7 +190,7 @@ private function createBinary(): SassBinary
182190
*
183191
* Options are converted from PHP option names to CLI option names.
184192
*
185-
* @param array<string, bool|string> $options
193+
* @param array<string, bool|array|string> $options
186194
*
187195
* @see getOptionMap()
188196
*/

tests/ConfigurationTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function testSassOptionsAreSet(): void
6666
'embed_sources' => true,
6767
'embed_source_map' => true,
6868
'error_css' => true,
69+
'load_path' => ['foo'],
6970
'quiet' => true,
7071
'quiet_deps' => true,
7172
'stop_on_error' => true,
@@ -82,6 +83,7 @@ public function testSassOptionsAreSet(): void
8283
'embed_sources' => false,
8384
'embed_source_map' => false,
8485
'error_css' => false,
86+
'load_path' => [],
8587
'quiet' => false,
8688
'quiet_deps' => false,
8789
'stop_on_error' => false,
@@ -102,6 +104,7 @@ public function testSassOptionsAreNullable(): void
102104
'embed_sources' => null,
103105
'embed_source_map' => null,
104106
'error_css' => null,
107+
'load_path' => null,
105108
'quiet' => null,
106109
'quiet_deps' => null,
107110
'stop_on_error' => null,

tests/SassBuilderTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,22 @@ public function testEmbedSources(): void
9797
$this->assertStringContainsString('color:%20$color;', $result);
9898
}
9999

100+
public function testLoadPaths(): void
101+
{
102+
$builder = $this->createBuilder('app_using_external.scss', [
103+
'load_path' => [
104+
__DIR__.'/fixtures/external',
105+
],
106+
]);
107+
108+
$process = $builder->runBuild(false);
109+
$process->wait();
110+
111+
$this->assertTrue($process->isSuccessful(), $process->getOutput());
112+
$this->assertFileExists(__DIR__.'/fixtures/assets/dist/app_using_external.output.css');
113+
$this->assertStringContainsString('color: red;', file_get_contents(__DIR__.'/fixtures/assets/dist/app_using_external.output.css'));
114+
}
115+
100116
public function testSassOptions(): void
101117
{
102118
$builder = new SassBuilder(
@@ -253,5 +269,16 @@ public static function provideSassPhpOptions()
253269
'--no-trace',
254270
],
255271
];
272+
yield 'Array options are expanded' => [
273+
[
274+
'style' => null,
275+
'load_path' => ['foo', 'bar'],
276+
'source_map' => null,
277+
],
278+
[
279+
'--load-path=foo',
280+
'--load-path=bar',
281+
],
282+
];
256283
}
257284
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@use "partial";
2+
3+
p {
4+
color: partial.$color;
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$color: red;

0 commit comments

Comments
 (0)