@@ -6,23 +6,16 @@ import { createExecutor } from '../../utils/create-executor';
66import { toPosixPath } from '../../utils/path-resolver' ;
77import { readFileText , writeFileText , ensureDir } from '../../utils/file-operations' ;
88import { copyDirectory } from '../copy-files/copy-files.impl' ;
9+ import {
10+ DATA_URI_SCSS_REGEX ,
11+ encodeDataUriForCssUrl ,
12+ } from '../../utils/scss-data-uri' ;
913import { ScssAssembleExecutorSchema } from './schema' ;
1014
11- const DATA_URI_REGEX = / d a t a - u r i \( (?: ' ( i m a g e \/ s v g \+ x m l ; c h a r s e t = U T F - 8 ) ' , \s ) ? [ ' " ] ? ( [ ^ ) ' " ] + ) [ ' " ] ? \) / g;
12-
1315const SCSS_EXTENSIONS = new Set ( [ '.scss' , '.css' ] ) ;
1416
15- function encodeSvg ( buffer : Buffer , svgEncoding ?: string ) : string {
16- const encoding = svgEncoding ?? 'image/svg+xml;charset=UTF-8' ;
17- return `"data:${ encoding } ,${ encodeURIComponent ( buffer . toString ( ) ) } "` ;
18- }
19-
20- function encodeImage ( buffer : Buffer , ext : string ) : string {
21- return `"data:image/${ ext } ;base64,${ buffer . toString ( 'base64' ) } "` ;
22- }
23-
2417async function inlineDataUri ( content : string , scssRoot : string ) : Promise < string > {
25- const matches = [ ...content . matchAll ( DATA_URI_REGEX ) ] ;
18+ const matches = [ ...content . matchAll ( DATA_URI_SCSS_REGEX ) ] ;
2619 if ( matches . length === 0 ) return content ;
2720
2821 const replacements = new Map < string , string > ( ) ;
@@ -35,15 +28,13 @@ async function inlineDataUri(content: string, scssRoot: string): Promise<string>
3528 const svgEncoding = match [ 1 ] ;
3629 const fileName = match [ 2 ] ;
3730 const filePath = path . resolve ( scssRoot , fileName ) ;
38- const ext = path . extname ( filePath ) . slice ( 1 ) ;
3931 const buffer = await fs . readFile ( filePath ) ;
40- const escapedString =
41- ext === 'svg' ? encodeSvg ( buffer , svgEncoding ) : encodeImage ( buffer , ext ) ;
32+ const escapedString = encodeDataUriForCssUrl ( buffer , filePath , svgEncoding ) ;
4233 replacements . set ( matchStr , `url(${ escapedString } )` ) ;
4334 } ) ,
4435 ) ;
4536
46- return content . replace ( DATA_URI_REGEX , ( match ) => replacements . get ( match ) ?? match ) ;
37+ return content . replace ( DATA_URI_SCSS_REGEX , ( match ) => replacements . get ( match ) ?? match ) ;
4738}
4839
4940async function copyScssWithInlineDataUri (
0 commit comments