|
1 | 1 | const loaderUtils = require('loader-utils'); |
2 | 2 | const fs = require('fs'); |
3 | 3 | const path = require('path'); |
4 | | - |
5 | | -// Collect groups [url(] [ |'|"]optional './|../', file part and end of url |
6 | | -const urlMatcher = /(url\()(\'|\")?(\.\/|\.\.\/)(\S*)(\2\))/g; |
| 4 | +const { rewriteCssUrls } = require('./theme-loader-utils'); |
7 | 5 |
|
8 | 6 | /** |
9 | 7 | * This custom loader handles rewriting urls for the application theme css files. |
@@ -31,75 +29,6 @@ module.exports = function (source, map) { |
31 | 29 |
|
32 | 30 | logger.log("Using '", themeFolder, "' for the application theme base folder."); |
33 | 31 |
|
34 | | - source = source.replace(urlMatcher, function (match, url, quoteMark, replace, fileUrl, endString) { |
35 | | - let absolutePath = path.resolve(handledResourceFolder, replace, fileUrl); |
36 | | - if ( |
37 | | - (fs.existsSync(absolutePath) && absolutePath.startsWith(themeFolder)) || |
38 | | - assetsContains(fileUrl, themeFolder, logger) |
39 | | - ) { |
40 | | - // Adding ./ will skip css-loader, which should be done for asset files |
41 | | - const skipLoader = fs.existsSync(absolutePath) && absolutePath.startsWith(themeFolder) ? '' : './'; |
42 | | - const frontendThemeFolder = skipLoader + 'themes/' + path.basename(themeFolder); |
43 | | - logger.debug( |
44 | | - 'Updating url for file', |
45 | | - "'" + replace + fileUrl + "'", |
46 | | - 'to use', |
47 | | - "'" + frontendThemeFolder + '/' + fileUrl + "'" |
48 | | - ); |
49 | | - const pathResolved = absolutePath.substring(themeFolder.length).replace(/\\/g, '/'); |
50 | | - |
51 | | - // keep the url the same except replace the ./ or ../ to themes/[themeFolder] |
52 | | - if (quoteMark) { |
53 | | - return url + quoteMark + frontendThemeFolder + pathResolved + endString; |
54 | | - } |
55 | | - return url + frontendThemeFolder + pathResolved + endString; |
56 | | - } else if (options.devMode) { |
57 | | - logger.log("No rewrite for '", match, "' as the file was not found."); |
58 | | - } |
59 | | - return match; |
60 | | - }); |
61 | | - |
| 32 | + source = rewriteCssUrls(source, handledResourceFolder, themeFolder, logger, options); |
62 | 33 | this.callback(null, source, map); |
63 | 34 | }; |
64 | | - |
65 | | -function assetsContains(fileUrl, themeFolder, logger) { |
66 | | - const themeProperties = getThemeProperties(themeFolder); |
67 | | - if (!themeProperties) { |
68 | | - logger.debug('No theme properties found.'); |
69 | | - return false; |
70 | | - } |
71 | | - const assets = themeProperties['assets']; |
72 | | - if (!assets) { |
73 | | - logger.debug('No defined assets in theme properties'); |
74 | | - return false; |
75 | | - } |
76 | | - // Go through each asset module |
77 | | - for (let module of Object.keys(assets)) { |
78 | | - const copyRules = assets[module]; |
79 | | - // Go through each copy rule |
80 | | - for (let copyRule of Object.keys(copyRules)) { |
81 | | - // if file starts with copyRule target check if file with path after copy target can be found |
82 | | - if (fileUrl.startsWith(copyRules[copyRule])) { |
83 | | - const targetFile = fileUrl.replace(copyRules[copyRule], ''); |
84 | | - const files = require('glob').sync(path.resolve('node_modules/', module, copyRule), { nodir: true }); |
85 | | - |
86 | | - for (let file of files) { |
87 | | - if (file.endsWith(targetFile)) return true; |
88 | | - } |
89 | | - } |
90 | | - } |
91 | | - } |
92 | | - return false; |
93 | | -} |
94 | | - |
95 | | -function getThemeProperties(themeFolder) { |
96 | | - const themePropertyFile = path.resolve(themeFolder, 'theme.json'); |
97 | | - if (!fs.existsSync(themePropertyFile)) { |
98 | | - return {}; |
99 | | - } |
100 | | - const themePropertyFileAsString = fs.readFileSync(themePropertyFile); |
101 | | - if (themePropertyFileAsString.length === 0) { |
102 | | - return {}; |
103 | | - } |
104 | | - return JSON.parse(themePropertyFileAsString); |
105 | | -} |
0 commit comments