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
21 changes: 21 additions & 0 deletions docs/loaders.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ $assets = (new EncoreEntrypointsLoader())
Chunks from an entrypoint are registered in the correct order with
dependencies.

Call `loadFromArray()` when the application has already decoded and validated
the manifest, for example after filtering invalid optional entrypoints:

```php
$assets = (new EncoreEntrypointsLoader())
->withDirectoryUrl('https://example.com/theme/build/')
->loadFromArray([
'entrypoints' => [
'theme' => ['css' => ['css/app.css'], 'js' => ['js/app.js']],
],
], __DIR__ . '/build/entrypoints.json');
```

The second argument supplies the manifest path used to resolve asset paths. It
does not trigger a file read and the manifest need not exist. The caller must
validate the input shape and apply any application-specific path restrictions.
The loader preserves the file loader's handles, chunk dependencies, URL mapping
and version-discovery settings. Existing file-access and inline-content policies
still apply when the assets are consumed. Consumers do not need to subclass the
loader or call its protected parsing method.

## Array Loader

```php
Expand Down
14 changes: 14 additions & 0 deletions src/Loader/EncoreEntrypointsLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@
*/
class EncoreEntrypointsLoader extends AbstractWebpackLoader implements LoaderInterface
{
/**
* Load an already decoded and validated Encore manifest without reading it again.
* The resource path anchors relative asset paths; the manifest need not exist.
* File access and inline-content policies still apply when assets are consumed.
*
* @param array{entrypoints?: array<string, array{css?: list<string>, js?: list<string>}>} $data
* @return array<Asset>
*/
#[\NoDiscard]
public function loadFromArray(array $data, string $resource): array
{
return $this->parseData($data, $resource);
}

/**
* {@inheritDoc}
*/
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/Unit/Asset/ScriptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public function testWithTranslation(): void

/**
* @test
* @dataProvider provideLocalized
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provideLocalized')]
public function testWithLocalize(string $objectName, $objectValue, $expected): void
{
$script = new Script('handle', 'script.js');
Expand Down Expand Up @@ -229,8 +229,8 @@ static function (): string {

/**
* @test
* @dataProvider provideAssetsFile
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provideAssetsFile')]
public function testDependencyExtractionPlugin(
string $scriptFile,
string $depsFileName,
Expand Down Expand Up @@ -366,8 +366,8 @@ public function testDependencyExtractionPluginWithDependencies(): void

/**
* @test
* @dataProvider provideVersions
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provideVersions')]
public function testDependencyExtractionPluginWithVersion(
?string $withVersion,
string $dependencyExtractionPluginVersion,
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/Unit/Asset/StyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function testStyleLoadingModeCanReturnToBlocking(): void
static::assertSame([], $testee->filters());
}

/** @dataProvider provideCssVars */
#[\PHPUnit\Framework\Attributes\DataProvider('provideCssVars')]
public function testWithCssVars(string $element, array $cssVars, array $expected): void
{
$testee = new Style('handle', 'foo.css');
Expand Down
14 changes: 4 additions & 10 deletions tests/phpunit/Unit/AssetFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ public function testCreateLocation(): void

/**
* @test
*
* @dataProvider provideDependencies
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provideDependencies')]
public function testDependencies($input, array $expected): void
{
$asset = AssetFactory::create(
Expand Down Expand Up @@ -252,9 +251,8 @@ public function testCreateMultipleLocations(): void

/**
* @test
*
* @dataProvider provideInvalidConfig
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provideInvalidConfig')]
public function testInvalidConfig(array $config, string $expectedExceptionType): void
{
\Brain\Monkey\Functions\when('esc_html')->returnArg();
Expand Down Expand Up @@ -414,9 +412,7 @@ public static function provideInvalidConfig(): \Generator
];
}

/**
* @dataProvider provideConfigWithTranslation
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provideConfigWithTranslation')]
public function testCreateWithTranslation(array $config, array $expected): void
{
/** @var Script $asset */
Expand Down Expand Up @@ -474,9 +470,7 @@ public static function provideConfigWithTranslation(): array
];
}

/**
* @dataProvider provideConfigWithLocalize
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provideConfigWithLocalize')]
public function testCreateWithLocalize(array $config, array $expected): void
{
/** @var Script $asset */
Expand Down
3 changes: 1 addition & 2 deletions tests/phpunit/Unit/Loader/AbstractWebpackLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ public function load($filePath): array

/**
* @test
*
* @dataProvider provideAssetLocations
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provideAssetLocations')]
public function testResolveLocations(string $inputFile, int $expectedLocation): void
{
$loader = new class extends AbstractWebpackLoader {
Expand Down
34 changes: 34 additions & 0 deletions tests/phpunit/Unit/Loader/EncoreEntrypointsLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,40 @@ public function testLoadWithDependencies(): void
static::assertSame(['theme', 'theme-1'], $asset->dependencies());
}

public function testLoadFromArrayPreservesPathsAndChunkDependenciesWithoutReadingManifest(): void
{
$loader = (new EncoreEntrypointsLoader())->withDirectoryUrl('https://example.com/build/');
$data = [
'entrypoints' => [
'theme' => [
'css' => ['css/theme.css'],
'js' => ['js/runtime.js', 'js/theme.js'],
],
],
];
$assets = $loader->loadFromArray($data, '/theme/build/missing-entrypoints.json');

static::assertCount(3, $assets);
static::assertInstanceOf(Style::class, $assets[0]);
static::assertInstanceOf(Script::class, $assets[1]);
static::assertSame('https://example.com/build/css/theme.css', $assets[0]->url());
static::assertSame('/theme/build/css/theme.css', $assets[0]->filePath());
static::assertSame('/theme/build/js/theme.js', $assets[2]->filePath());
static::assertSame('https://example.com/build/js/theme.js', $assets[2]->url());
static::assertSame('theme-1', $assets[2]->handle());
static::assertSame(['theme'], $assets[2]->dependencies());
static::assertSame([], $loader->loadFromArray([], '/theme/build/entrypoints.json'));
}

public function testLoadFromArrayMatchesFileLoader(): void
{
$data = ['entrypoints' => ['theme' => ['js' => ['./theme.js']]]];
$file = $this->mockEntrypointsFile($data);
$loader = new EncoreEntrypointsLoader();

static::assertEquals($loader->load($file), $loader->loadFromArray($data, $file));
}

private function mockEntrypointsFile(array $json): string
{
return vfsStream::newFile('entrypoints.json')
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/Unit/Loader/WebpackManifestLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public function setUp(): void

/**
* @test
* @dataProvider provideManifest
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provideManifest')]
public function testLoadFromManifest(
string $json,
string $expectedHandle,
Expand Down Expand Up @@ -130,8 +130,8 @@ public function testLoadFromManifestNotSupportedTypes(): void

/**
* @test
* @dataProvider provideManifestWithAlternativeUrl
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provideManifestWithAlternativeUrl')]
public function testLoadFromManifestWithAlternativeUrl(
string $json,
string $alternativeUrl,
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/Unit/Util/AssetHookResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public function testResolveBackend(): void

/**
* @test
* @dataProvider provideLastHook
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provideLastHook')]
public function testResolveLastHook(ContextStub $context, $expected): void
{
$hookResolver = new AssetHookResolver($context);
Expand Down
Loading