diff --git a/Classes/Asset/TagRenderer.php b/Classes/Asset/TagRenderer.php index 178dea4..55af87b 100644 --- a/Classes/Asset/TagRenderer.php +++ b/Classes/Asset/TagRenderer.php @@ -168,6 +168,9 @@ public function renderWebpackLinkTags(LinkTag $linkTag): void 'excludeFromConcatenation' => true, 'splitChar' => '|', 'inline' => false, + 'tagAttributes' => [], + 'integrity' => '', + 'crossorigin' => '', ], $parameters); $attributes = array_values($attributes); diff --git a/Classes/Integration/PageRendererHooks.php b/Classes/Integration/PageRendererHooks.php new file mode 100644 index 0000000..08664a5 --- /dev/null +++ b/Classes/Integration/PageRendererHooks.php @@ -0,0 +1,114 @@ + $jsFile) { + if (! $this->isEncoreEntryName($jsFile['file'])) { + continue; + } + + $buildAndEntryName = $this->createBuildAndEntryName($jsFile['file']); + $buildName = EntrypointLookupInterface::DEFAULT_BUILD; + + if (2 === count($buildAndEntryName)) { + [$buildName, $entryName] = $buildAndEntryName; + } else { + $entryName = $buildAndEntryName[0]; + } + + $position = ($jsFile['section'] ?? '') === self::PART_FOOTER ? TagRendererInterface::POSITION_FOOTER : ''; + + unset($params[$includeType][$key], $jsFile['file'], $jsFile['section'], $jsFile['integrity']); + + $scriptTag = new ScriptTag($entryName, $position, $buildName, $pageRenderer, $jsFile, true, $isLibrary); + + $this->tagRenderer->renderWebpackScriptTags($scriptTag); + } + } + + // Add CSS-Files by entryNames + foreach (TagRendererInterface::ALLOWED_CSS_POSITIONS as $includeType) { + if (! isset($params[$includeType])) { + continue; + } + + foreach ($params[$includeType] as $key => $cssFile) { + if (! $this->isEncoreEntryName($cssFile['file'])) { + continue; + } + $buildAndEntryName = $this->createBuildAndEntryName($cssFile['file']); + $buildName = EntrypointLookupInterface::DEFAULT_BUILD; + + if (2 === count($buildAndEntryName)) { + [$buildName, $entryName] = $buildAndEntryName; + } else { + $entryName = $buildAndEntryName[0]; + } + + unset($params[$includeType][$key], $cssFile['file'], $cssFile['rel']); + + $linkTag = new LinkTag($entryName, 'all', $buildName, $pageRenderer, $cssFile); + $this->tagRenderer->renderWebpackLinkTags($linkTag); + } + } + } + + private function isEncoreEntryName(string $file): bool + { + return \str_starts_with($file, self::ENCORE_PREFIX); + } + + private function removePrefix(string $file): string + { + return str_replace(self::ENCORE_PREFIX, '', $file); + } + + private function createBuildAndEntryName(string $file): array + { + return GeneralUtility::trimExplode(':', $this->removePrefix($file), true, 2); + } +} diff --git a/README.md b/README.md index 495649f..0e4d5fe 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,31 @@ If you have defined multiple entries you can define the desired entryName in the ``` -Note the prefix typo3_encore: This is important in order to render the files correctly. + +Alternatively you can also include the files via TypoScript + +```php +page.includeCSS { + # Pattern PKG:typo3_encore:entryName + app = PKG:typo3_encore:app + # If you want to ensure that this file is loaded first uncomment the next line + # app.forceOnTop = 1 +} + +page.includeJS { + # Pattern PKG:typo3_encore:entryName + app = PKG:typo3_encore:app + # If you want to ensure that this file is loaded first uncomment the next line + # app.forceOnTop = 1 +} + +page.includeJSFooter { + # Pattern PKG:typo3_encore:entryName + app = PKG:typo3_encore:app +} +``` + +Note the prefix PKG:typo3_encore: This is important in order to render the files correctly. You can then use all other known settings to include your files. You don“t have to care about including it only once. This will not happen during one request cycle unless you want to. @@ -143,6 +167,15 @@ This way of using the AssetViewHelper is similar to the `asset` function used in } ``` + Finally, you can specify which build to use: + + ```php + page.includeCSS { + # Pattern PKG:typo3_encore:buildName:entryName + app = PKG:typo3_encore:firstBuild:app + } + ``` + ```html shouldBeCalledOnce(); $this->assetRegistry->registerFile(Argument::any())->shouldBeCalledOnce(); @@ -180,7 +183,10 @@ public function testRenderWebpackLinkTagsWithDefaultBuildWithoutAssetRegistratio '', true, '|', - false + false, + [], + '', + '' )->shouldBeCalledOnce(); $this->assetRegistry->registerFile(Argument::any())->shouldNotBeCalled(); diff --git a/ext_localconf.php b/ext_localconf.php index 0dd2352..bee3b8b 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -1,5 +1,8 @@ renderPreProcess'; + $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['tcaDatabaseRecord'][\Ssch\Typo3Encore\Form\FormDataProvider\RichtextEncoreConfiguration::class] = [ 'depends' => [\TYPO3\CMS\Backend\Form\FormDataProvider\TcaText::class], ];