@@ -2,11 +2,12 @@ const path = require('path');
22const { getFileContentHash } = require ( './file' ) ;
33
44const JS_ASSET_KEYS = [ 'script' , 'editorScript' , 'viewScript' , 'viewScriptModule' , 'scriptModule' ] ;
5+ const CSS_ASSET_KEYS = [ 'style' , 'editorStyle' , 'viewStyle' ] ;
56
67/**
78 * Transform the asset path from `.ts or .tsx` to `.js`
89 *
9- * When a block.json file has a script or style property that points to a `.ts or .tsx` file,
10+ * When a block.json file has a script property that points to a `.ts or .tsx` file,
1011 * this function will transform the path to point to the `.js` file instead.
1112 *
1213 * @param {string|Array<string> } asset - The asset path to transform
@@ -27,6 +28,30 @@ function transformTSAsset(asset) {
2728 return Array . isArray ( asset ) ? asset . map ( replaceExtension ) : replaceExtension ( asset ) ;
2829}
2930
31+ /**
32+ * Transform the asset path from `.sass or .scss` to `.css`
33+ *
34+ * When a block.json file has a style property that points to a `.sass or .scss` file,
35+ * this function will transform the path to point to the `.css` file instead.
36+ *
37+ * @param {string|Array<string> } asset - The asset path to transform
38+ * @returns {string|Array<string> }
39+ */
40+ function transformSassAsset ( asset ) {
41+ function replaceExtension ( filePath ) {
42+ const isFilePath = filePath . startsWith ( 'file:' ) ;
43+ if ( ! isFilePath ) {
44+ return filePath ;
45+ }
46+
47+ // replace the `.sass or .scss` extension with `.css`
48+ const cssPath = filePath . replace ( / \. s [ a c ] s s $ / , '.css' ) ;
49+ return cssPath ;
50+ }
51+
52+ return Array . isArray ( asset ) ? asset . map ( replaceExtension ) : replaceExtension ( asset ) ;
53+ }
54+
3055const transformBlockJson = ( content , absoluteFilename ) => {
3156 const rawMetadata = content . toString ( ) ;
3257 if ( rawMetadata === '' ) {
@@ -70,6 +95,12 @@ const transformBlockJson = (content, absoluteFilename) => {
7095 }
7196 } ) ;
7297
98+ CSS_ASSET_KEYS . forEach ( ( key ) => {
99+ if ( metadata [ key ] ) {
100+ newMetadata [ key ] = transformSassAsset ( metadata [ key ] ) ;
101+ }
102+ } ) ;
103+
73104 return JSON . stringify ( newMetadata , null , 2 ) ;
74105} ;
75106
0 commit comments