@@ -13,7 +13,9 @@ const {
1313
1414const HOT_CSS_BASE_PATH = '/_sidworks_hot' ;
1515const HOT_CSS_FILE_NAME = 'sidworks-hot.css' ;
16+ const HOT_CSS_MAP_FILE_NAME = `${ HOT_CSS_FILE_NAME } .map` ;
1617const HOT_CSS_ROUTE = `${ HOT_CSS_BASE_PATH } /${ HOT_CSS_FILE_NAME } ` ;
18+ const HOT_CSS_MAP_ROUTE = `${ HOT_CSS_BASE_PATH } /${ HOT_CSS_MAP_FILE_NAME } ` ;
1719const HOT_CSS_EVENTS_ROUTE = `${ HOT_CSS_BASE_PATH } /events` ;
1820
1921const sassDeprecations = [ 'import' , 'global-builtin' , 'color-functions' , 'slash-div' , 'legacy-js-api' ] ;
@@ -149,6 +151,8 @@ function createScssSidecar(projectRoot) {
149151 const themeConfigPath = path . resolve ( rootPath , 'files/theme-config/index.json' ) ;
150152 const fallbackThemeVariablesPath = path . resolve ( rootPath , 'var/theme-variables.scss' ) ;
151153 const cssOutputPath = path . resolve ( generatedEntryDirectoryPath , HOT_CSS_FILE_NAME ) ;
154+ const cssMapOutputPath = path . resolve ( generatedEntryDirectoryPath , HOT_CSS_MAP_FILE_NAME ) ;
155+ const scssSourceMapEnabled = asString ( process . env . SHOPWARE_STOREFRONT_SCSS_SOURCE_MAP , '1' ) === '1' ;
152156
153157 const state = {
154158 subscribers : new Set ( ) ,
@@ -560,6 +564,11 @@ function createScssSidecar(projectRoot) {
560564 recursive : true ,
561565 } ) ;
562566 await fs . promises . writeFile ( cssOutputPath , result . css || '' , 'utf8' ) ;
567+ if ( scssSourceMapEnabled && typeof result . map === 'string' && result . map !== '' ) {
568+ await fs . promises . writeFile ( cssMapOutputPath , result . map , 'utf8' ) ;
569+ } else if ( fs . existsSync ( cssMapOutputPath ) ) {
570+ await fs . promises . unlink ( cssMapOutputPath ) ;
571+ }
563572
564573 state . version = Date . now ( ) ;
565574 updateWatchSet ( result . loadedFiles , compileEntryPath ) ;
@@ -588,7 +597,8 @@ function createScssSidecar(projectRoot) {
588597 state . sassImplementation . render ( {
589598 file : entryPath ,
590599 outFile : cssOutputPath ,
591- sourceMap : false ,
600+ sourceMap : scssSourceMapEnabled ,
601+ sourceMapContents : scssSourceMapEnabled ,
592602 outputStyle : 'expanded' ,
593603 quietDeps : true ,
594604 includePaths : [
@@ -625,6 +635,7 @@ function createScssSidecar(projectRoot) {
625635
626636 resolve ( {
627637 css : result . css . toString ( ) ,
638+ map : result . map ? result . map . toString ( ) : '' ,
628639 loadedFiles : result . stats && Array . isArray ( result . stats . includedFiles )
629640 ? result . stats . includedFiles
630641 : [ ] ,
@@ -714,6 +725,24 @@ function createScssSidecar(projectRoot) {
714725 return true ;
715726 }
716727
728+ if ( requestPath === HOT_CSS_MAP_ROUTE ) {
729+ if ( ! fs . existsSync ( cssMapOutputPath ) ) {
730+ res . writeHead ( 404 , {
731+ 'Content-Type' : 'text/plain' ,
732+ } ) ;
733+ res . end ( 'SCSS sidecar source map not ready' ) ;
734+ return true ;
735+ }
736+
737+ res . writeHead ( 200 , {
738+ 'Content-Type' : 'application/json; charset=utf-8' ,
739+ 'Cache-Control' : 'no-cache, no-store, must-revalidate' ,
740+ 'Access-Control-Allow-Origin' : '*' ,
741+ } ) ;
742+ fs . createReadStream ( cssMapOutputPath ) . pipe ( res ) ;
743+ return true ;
744+ }
745+
717746 return false ;
718747 }
719748
0 commit comments