Skip to content

Commit 9f0686c

Browse files
committed
feat: Add the ability to disable dependency extraction
Add a `dependencyExtractionEnabled` property to `Script` constructor that will disable automatic dependency extraction if set to `false`
1 parent 9bf2a42 commit 9f0686c

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

src/Script.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ class Script extends BaseAsset implements Asset, DataAwareAsset, FilterAwareAsse
3535
'path' => null,
3636
];
3737

38+
protected bool $dependencyExtractionEnabled = false;
39+
40+
public function __construct(
41+
string $handle,
42+
string $url,
43+
int $location = Asset::FRONTEND | Asset::ACTIVATE,
44+
bool $dependencyExtractionEnabled = true
45+
) {
46+
47+
parent::__construct($handle, $url, $location);
48+
$this->dependencyExtractionEnabled = $dependencyExtractionEnabled;
49+
}
50+
3851
/**
3952
* @return array<string, mixed>
4053
*/
@@ -199,7 +212,7 @@ public function useDependencyExtractionPlugin(): Script
199212
*/
200213
public function version(): ?string
201214
{
202-
$this->resolveDependencyExtractionPlugin();
215+
$this->dependencyExtractionEnabled and $this->resolveDependencyExtractionPlugin();
203216

204217
return parent::version();
205218
}
@@ -209,7 +222,7 @@ public function version(): ?string
209222
*/
210223
public function dependencies(): array
211224
{
212-
$this->resolveDependencyExtractionPlugin();
225+
$this->dependencyExtractionEnabled and $this->resolveDependencyExtractionPlugin();
213226

214227
return parent::dependencies();
215228
}

tests/phpunit/Unit/Asset/ScriptTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,4 +404,59 @@ public function provideVersions(): \Generator
404404
'1.0',
405405
];
406406
}
407+
408+
/**
409+
* @test
410+
*/
411+
public function testDependencyExtractionCanBeDisabled(): void
412+
{
413+
$expectedDependencies = ['foo', 'bar', 'baz'];
414+
$expectedVersion = '1.0';
415+
416+
vfsStream::newFile('script.asset.json')
417+
->withContent(
418+
json_encode(
419+
[
420+
'dependencies' => $expectedDependencies,
421+
'version' => $expectedVersion,
422+
]
423+
)
424+
)
425+
->at($this->root);
426+
427+
$expectedFile = vfsStream::newFile('script.js')->at($this->root);
428+
429+
$testee = new Script('script', $expectedFile->url(), Asset::FRONTEND, false);
430+
$testee->withFilePath($expectedFile->url());
431+
432+
// Dependencies should not be loaded from the .asset.json file
433+
static::assertEmpty($testee->dependencies());
434+
435+
// Version is still autodiscovered from file modification time, not from .asset.json
436+
// To verify it's not from .asset.json, we check it's not the expected version
437+
static::assertNotEquals($expectedVersion, $testee->version());
438+
}
439+
440+
/**
441+
* @test
442+
*/
443+
public function testDependencyExtractionEnabledByDefault(): void
444+
{
445+
$expectedDependencies = ['foo', 'bar', 'baz'];
446+
447+
vfsStream::newFile('script.asset.json')
448+
->withContent(json_encode(['dependencies' => $expectedDependencies]))
449+
->at($this->root);
450+
451+
$expectedFile = vfsStream::newFile('script.js')->at($this->root);
452+
453+
$testee = new Script('script', $expectedFile->url());
454+
$testee->withFilePath($expectedFile->url());
455+
456+
// Should load dependencies by default
457+
static::assertEqualsCanonicalizing(
458+
$expectedDependencies,
459+
$testee->dependencies()
460+
);
461+
}
407462
}

0 commit comments

Comments
 (0)