@@ -34,8 +34,10 @@ module.exports = function (source, map) {
3434
3535 source = source . replace ( urlMatcher , function ( match , url , quoteMark , replace , fileUrl , endString ) {
3636 let absolutePath = path . resolve ( handledResourceFolder , replace , fileUrl ) ;
37- if ( fs . existsSync ( absolutePath ) && absolutePath . startsWith ( themeFolder ) ) {
38- const frontendThemeFolder = "themes/" + path . basename ( themeFolder ) ;
37+ if ( ( fs . existsSync ( absolutePath ) && absolutePath . startsWith ( themeFolder ) ) || assetsContains ( fileUrl , themeFolder , logger ) ) {
38+ // Adding ./ will skip css-loader, which should be done for asset files
39+ const skipLoader = ( fs . existsSync ( absolutePath ) && absolutePath . startsWith ( themeFolder ) ) ? "" : "./" ;
40+ const frontendThemeFolder = skipLoader + "themes/" + path . basename ( themeFolder ) ;
3941 logger . debug ( "Updating url for file" , "'" + replace + fileUrl + "'" , "to use" , "'" + frontendThemeFolder + "/" + fileUrl + "'" ) ;
4042 const pathResolved = absolutePath . substring ( themeFolder . length ) . replace ( / \\ / g, '/' ) ;
4143
@@ -52,3 +54,46 @@ module.exports = function (source, map) {
5254
5355 this . callback ( null , source , map ) ;
5456}
57+
58+ function assetsContains ( fileUrl , themeFolder , logger ) {
59+ const themeProperties = getThemeProperties ( themeFolder ) ;
60+ if ( ! themeProperties ) {
61+ logger . debug ( "No theme properties found." ) ;
62+ return false ;
63+ }
64+ const assets = themeProperties [ 'assets' ] ;
65+ if ( ! assets ) {
66+ logger . debug ( "No defined assets in theme properties" ) ;
67+ return false ;
68+ }
69+ // Go through each asset module
70+ for ( let module of Object . keys ( assets ) ) {
71+ const copyRules = assets [ module ] ;
72+ // Go through each copy rule
73+ for ( let copyRule of Object . keys ( copyRules ) ) {
74+ // if file starts with copyRule target check if file with path after copy target can be found
75+ if ( fileUrl . startsWith ( copyRules [ copyRule ] ) ) {
76+ const targetFile = fileUrl . replace ( copyRules [ copyRule ] , "" ) ;
77+ const files = require ( 'glob' ) . sync ( path . resolve ( 'node_modules/' , module , copyRule ) , { nodir : true } ) ;
78+
79+ for ( let file of files ) {
80+ if ( file . endsWith ( targetFile ) )
81+ return true ;
82+ }
83+ }
84+ }
85+ }
86+ return false ;
87+ }
88+
89+ function getThemeProperties ( themeFolder ) {
90+ const themePropertyFile = path . resolve ( themeFolder , 'theme.json' ) ;
91+ if ( ! fs . existsSync ( themePropertyFile ) ) {
92+ return { } ;
93+ }
94+ const themePropertyFileAsString = fs . readFileSync ( themePropertyFile ) ;
95+ if ( themePropertyFileAsString . length === 0 ) {
96+ return { } ;
97+ }
98+ return JSON . parse ( themePropertyFileAsString ) ;
99+ }
0 commit comments