Skip to content

Commit 93435bc

Browse files
committed
Make source maps default
1 parent a1dc3ba commit 93435bc

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

bin/storefront-hot-proxy/scss-sidecar.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ const {
1313

1414
const HOT_CSS_BASE_PATH = '/_sidworks_hot';
1515
const HOT_CSS_FILE_NAME = 'sidworks-hot.css';
16+
const HOT_CSS_MAP_FILE_NAME = `${HOT_CSS_FILE_NAME}.map`;
1617
const 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}`;
1719
const HOT_CSS_EVENTS_ROUTE = `${HOT_CSS_BASE_PATH}/events`;
1820

1921
const 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

src/Command/WatchCommand.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,10 @@ private function buildHotEnvironment(
434434
: 'narrow';
435435

436436
$useSassEmbedded = $this->env('SHOPWARE_STOREFRONT_USE_SASS_EMBEDDED', '1');
437+
$scssSourceMapEnabled = $this->env(
438+
'SHOPWARE_STOREFRONT_SCSS_SOURCE_MAP',
439+
$scssEngine === 'sass-cli' ? '1' : '0'
440+
);
437441

438442
$environment = [
439443
'PROJECT_ROOT' => $projectRoot,
@@ -448,7 +452,7 @@ private function buildHotEnvironment(
448452
'SHOPWARE_STOREFRONT_HOT_CORE_ONLY' => $disableJs ? '1' : '0',
449453
'SHOPWARE_STOREFRONT_TWIG_WATCH_MODE' => $twigWatchMode,
450454
'SHOPWARE_STOREFRONT_JS_SOURCE_MAP' => '0',
451-
'SHOPWARE_STOREFRONT_SCSS_SOURCE_MAP' => '0',
455+
'SHOPWARE_STOREFRONT_SCSS_SOURCE_MAP' => $scssSourceMapEnabled,
452456
'SHOPWARE_STOREFRONT_SCSS_ENGINE' => $scssEngine,
453457
'SHOPWARE_STOREFRONT_SKIP_POSTCSS' => '1',
454458
'SHOPWARE_STOREFRONT_SASS_SILENCE_DEPRECATIONS' => '1',

0 commit comments

Comments
 (0)