Skip to content

Commit cf04564

Browse files
authored
Merge pull request #91 from SupraSmooth/master
Allow Scoped SCSS Imports + Update phpscss to 1.4.0
2 parents a1436ee + 5ece06b commit cf04564

13 files changed

+99
-15
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"php": ">=7.1",
1414
"ext-dom": "*",
1515
"ext-libxml": "*",
16-
"scssphp/scssphp": "1.1.1",
16+
"scssphp/scssphp": "1.4.0",
1717
"squizlabs/php_codesniffer": "^3.3",
1818
"ramsey/uuid": "^3.8"
1919
},

composer.lock

Lines changed: 9 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ class Compiler
6666
*/
6767
protected $styleBuilder;
6868

69+
/**
70+
* @var string|null
71+
*/
72+
protected $relativePath = null;
73+
6974
/**
7075
* @var NodeHelper
7176
*/
@@ -133,6 +138,7 @@ public function __construct(DOMDocument $document, LoggerInterface $logger)
133138
{
134139
$this->builder = new TwigBuilder();
135140
$this->styleBuilder = new StyleBuilder();
141+
$this->relativePath = null;
136142
$this->nodeHelper = new NodeHelper();
137143
$this->document = $document;
138144
$this->logger = $logger;
@@ -159,6 +165,14 @@ public function setBanner($strings): void
159165
$this->banner = $strings;
160166
}
161167

168+
/**
169+
* @param string|null $path
170+
*/
171+
public function setRelativePath(?string $path): void
172+
{
173+
$this->relativePath = $path;
174+
}
175+
162176
/**
163177
* @throws ReflectionException
164178
* @throws Exception
@@ -216,7 +230,7 @@ public function convert(): string
216230
}
217231
foreach ($styleBlocks as $styleBlock) {
218232
/* @var DOMElement $styleBlock */
219-
$this->rawBlocks[] = $this->styleBuilder->compile($styleBlock);
233+
$this->rawBlocks[] = $this->styleBuilder->compile($styleBlock, $this->relativePath);
220234
}
221235
}
222236

src/Utils/StyleBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function setScssData(string $data): void
7070
$this->scssData = $data;
7171
}
7272

73-
public function compile(?DOMElement $styleElement): ?string
73+
public function compile(?DOMElement $styleElement, ?string $path = null): ?string
7474
{
7575
if (!$styleElement instanceof DOMElement
7676
|| ($styleElement->hasAttribute('scoped') && !($this->outputType & self::STYLE_SCOPED))
@@ -85,7 +85,7 @@ public function compile(?DOMElement $styleElement): ?string
8585
$this->scssCompiler = new ScssCompiler();
8686
}
8787
try {
88-
$style = $this->scssCompiler->compile($this->scssData . ' ' . $style);
88+
$style = $this->scssCompiler->compile($this->scssData . ' ' . $style, $path);
8989
} catch (CompilerException $e) {
9090
$style = "\n/* Warning: " . $e->getMessage() . " */\n";
9191
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Paneon\VueToTwig\Tests;
4+
5+
use Exception;
6+
use Paneon\VueToTwig\Utils\StyleBuilder;
7+
8+
class CompilerStyleBlockScopedImportTest extends AbstractTestCase
9+
{
10+
/**
11+
* @dataProvider dataProvider
12+
*
13+
* @param mixed $html
14+
* @param mixed $expected
15+
*
16+
* @throws Exception
17+
*/
18+
public function testStyleBlock($html, $expected)
19+
{
20+
$compiler = $this->createCompiler($html);
21+
$compiler->setStyleBlockOutputType(StyleBuilder::STYLE_ALL);
22+
$compiler->setRelativePath(__DIR__ . '/fixtures/style-block-scoped-import/style-block-scss-scoped-import.vue');
23+
24+
$actual = $compiler->convert();
25+
26+
$this->assertEqualHtml($expected, $actual);
27+
}
28+
29+
/**
30+
* @return array
31+
*/
32+
public function dataProvider()
33+
{
34+
return $this->loadFixturesFromDir('style-block-scoped-import');
35+
}
36+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$red: #FF0000;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
$red: #AA0000;
2+
$blue: #0000FF;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<style>.foo[data-v-7e73cf3ea81584bf814227e32d0e09f8] { color: #0000FF;}.foo__bar[data-v-7e73cf3ea81584bf814227e32d0e09f8] { color: #AA0000;}</style>
2+
<div data-v-7e73cf3ea81584bf814227e32d0e09f8 {{ dataScopedStyleAttribute|default('') }} class="{{ class|default('') }}" style="{{ style|default('') }}">
3+
<div class="foo__bar" data-v-7e73cf3ea81584bf814227e32d0e09f8>
4+
baz
5+
</div>
6+
</div>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<template>
2+
<div>
3+
<div class="foo__bar">
4+
baz
5+
</div>
6+
</div>
7+
</template>
8+
9+
<style lang="scss" scoped>
10+
@import "imports/import";
11+
@import "imports/subfolder/import2";
12+
.foo {
13+
color: $blue;
14+
&__bar {
15+
color: $red;
16+
}
17+
}
18+
</style>
19+
20+
<script>
21+
export default {
22+
}
23+
</script>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
<style>.grow-enter[data-v-de62703e2bdacde95c4bc73d69f14aa5], .grow-leave-to[data-v-de62703e2bdacde95c4bc73d69f14aa5] { width: 0 !important; }.grow-enter-active[data-v-de62703e2bdacde95c4bc73d69f14aa5], .grow-leave-active[data-v-de62703e2bdacde95c4bc73d69f14aa5] { transition: width 0.6s ease; }</style>
1+
<style>.grow-enter[data-v-de62703e2bdacde95c4bc73d69f14aa5], .grow-leave-to[data-v-de62703e2bdacde95c4bc73d69f14aa5] { width: 0 !important;}.grow-enter-active[data-v-de62703e2bdacde95c4bc73d69f14aa5], .grow-leave-active[data-v-de62703e2bdacde95c4bc73d69f14aa5] { transition: width 0.6s ease;}</style>
22
<div class="bar {{ class|default('') }}" data-v-de62703e2bdacde95c4bc73d69f14aa5 {{ dataScopedStyleAttribute|default('') }} style="{{ style|default('') }}"></div>

tests/fixtures/style-block-scoped/style-block-scss-scoped.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<style>.foo[data-v-bc2fd2697c25be635fabb11cba6f24b9] { color: red; } .foo__bar[data-v-bc2fd2697c25be635fabb11cba6f24b9] { color: blue; } .foo__baz[data-v-bc2fd2697c25be635fabb11cba6f24b9] { color: #00FF00; }</style>
1+
<style>.foo[data-v-bc2fd2697c25be635fabb11cba6f24b9] { color: red;}.foo__bar[data-v-bc2fd2697c25be635fabb11cba6f24b9] { color: blue;}.foo__baz[data-v-bc2fd2697c25be635fabb11cba6f24b9] { color: #00FF00;}</style>
22
<div data-v-bc2fd2697c25be635fabb11cba6f24b9 {{ dataScopedStyleAttribute|default('') }} class="{{ class|default('') }}" style="{{ style|default('') }}">
33
<div class="foo__bar" data-v-bc2fd2697c25be635fabb11cba6f24b9>
44
baz

tests/fixtures/style-block-scoped/style-blocks.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<style> .foo { color: red; }</style>
22
<style> .bar[data-v-d90c32720cdc50ff836bde94d71a2bb5] { color: blue; }</style>
3-
<style>.baz { color: #00FF00; }</style>
3+
<style>.baz { color: #00FF00;}</style>
44
<div data-v-d90c32720cdc50ff836bde94d71a2bb5 {{ dataScopedStyleAttribute|default('') }} class="{{ class|default('') }}" style="{{ style|default('') }}">
55
<div class="foo bar baz" data-v-d90c32720cdc50ff836bde94d71a2bb5>
66
42

tests/fixtures/style-block/style-block-scss.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<style>.foo { color: red; } .foo__bar { color: blue; } .foo__baz { color: #00FF00; }</style>
1+
<style>.foo { color: red;}.foo__bar { color: blue;}.foo__baz { color: #00FF00;}</style>
22
<div class="{{ class|default('') }}" style="{{ style|default('') }}">
33
<div class="foo">
44
foo

0 commit comments

Comments
 (0)