1010namespace Symfony \WebpackEncoreBundle \Twig ;
1111
1212use Psr \Container \ContainerInterface ;
13+ use Symfony \WebpackEncoreBundle \Asset \BuildFileLocator ;
1314use Symfony \WebpackEncoreBundle \Asset \EntrypointLookup ;
1415use Symfony \WebpackEncoreBundle \Asset \EntrypointLookupInterface ;
1516use Symfony \WebpackEncoreBundle \Asset \TagRenderer ;
@@ -30,6 +31,8 @@ public function getFunctions()
3031 return [
3132 new TwigFunction ('encore_entry_js_files ' , [$ this , 'getWebpackJsFiles ' ]),
3233 new TwigFunction ('encore_entry_css_files ' , [$ this , 'getWebpackCssFiles ' ]),
34+ new TwigFunction ('encore_entry_js_source ' , [$ this , 'getWebpackJsSource ' ]),
35+ new TwigFunction ('encore_entry_css_source ' , [$ this , 'getWebpackCssSource ' ]),
3336 new TwigFunction ('encore_entry_script_tags ' , [$ this , 'renderWebpackScriptTags ' ], ['is_safe ' => ['html ' ]]),
3437 new TwigFunction ('encore_entry_link_tags ' , [$ this , 'renderWebpackLinkTags ' ], ['is_safe ' => ['html ' ]]),
3538 new TwigFunction ('encore_disable_file_tracking ' , [$ this , 'disableReturnedFileTracking ' ]),
@@ -49,6 +52,22 @@ public function getWebpackCssFiles(string $entryName, string $entrypointName = '
4952 ->getCssFiles ($ entryName );
5053 }
5154
55+ public function getWebpackJsSource (string $ entryName , string $ entrypointName = '_default ' ): string
56+ {
57+ $ files = $ this ->getEntrypointLookup ($ entrypointName )
58+ ->getJavaScriptFiles ($ entryName );
59+
60+ return $ this ->concatenateFileSources ($ files );
61+ }
62+
63+ public function getWebpackCssSource (string $ entryName , string $ entrypointName = '_default ' ): string
64+ {
65+ $ files = $ this ->getEntrypointLookup ($ entrypointName )
66+ ->getCssFiles ($ entryName );
67+
68+ return $ this ->concatenateFileSources ($ files );
69+ }
70+
5271 public function renderWebpackScriptTags (string $ entryName , string $ packageName = null , string $ entrypointName = '_default ' ): string
5372 {
5473 return $ this ->getTagRenderer ()
@@ -82,6 +101,17 @@ private function changeReturnedFileTracking(bool $isEnabled, string $entrypointN
82101 $ lookup ->enableReturnedFileTracking ($ isEnabled );
83102 }
84103
104+ private function concatenateFileSources (array $ files ): string
105+ {
106+ $ locator = $ this ->getBuildFileLocator ();
107+ $ source = '' ;
108+ foreach ($ files as $ file ) {
109+ $ source .= file_get_contents ($ locator ->findFile ($ file ));
110+ }
111+
112+ return $ source ;
113+ }
114+
85115 private function getEntrypointLookup (string $ entrypointName ): EntrypointLookupInterface
86116 {
87117 return $ this ->container ->get ('webpack_encore.entrypoint_lookup_collection ' )
@@ -92,4 +122,9 @@ private function getTagRenderer(): TagRenderer
92122 {
93123 return $ this ->container ->get ('webpack_encore.tag_renderer ' );
94124 }
125+
126+ private function getBuildFileLocator (): BuildFileLocator
127+ {
128+ return $ this ->container ->get ('webpack_encore.build_file_locator ' );
129+ }
95130}
0 commit comments