Skip to content

Commit 6662b3c

Browse files
committed
WIP
1 parent 85a6758 commit 6662b3c

3 files changed

Lines changed: 115 additions & 3 deletions

File tree

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

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,94 @@ const HOT_CSS_ROUTE = `${HOT_CSS_BASE_PATH}/${HOT_CSS_FILE_NAME}`;
2525
const HOT_CSS_MAP_ROUTE = `${HOT_CSS_BASE_PATH}/${HOT_CSS_MAP_FILE_NAME}`;
2626
const HOT_CSS_EVENTS_ROUTE = `${HOT_CSS_BASE_PATH}/events`;
2727

28+
function appendSourceMapComment(cssContent, mapFileName) {
29+
const normalizedCss = String(cssContent || '').replace(/\/\*# sourceMappingURL=.*?\*\//g, '').trimEnd();
30+
const normalizedMapFile = String(mapFileName || '').trim();
31+
32+
if (normalizedMapFile === '') {
33+
return normalizedCss;
34+
}
35+
36+
return `${normalizedCss}\n/*# sourceMappingURL=${normalizedMapFile} */\n`;
37+
}
38+
39+
function normalizeSourceMapForDisplay(mapContent, projectRootPath) {
40+
if (typeof mapContent !== 'string' || mapContent.trim() === '') {
41+
return '';
42+
}
43+
44+
let parsedMap;
45+
try {
46+
parsedMap = JSON.parse(mapContent);
47+
} catch (_error) {
48+
return mapContent;
49+
}
50+
51+
if (!parsedMap || typeof parsedMap !== 'object' || !Array.isArray(parsedMap.sources)) {
52+
return mapContent;
53+
}
54+
55+
parsedMap.sources = parsedMap.sources.map((source) => normalizeSourceMapSource(source, projectRootPath));
56+
57+
if (typeof parsedMap.sourceRoot === 'string' && parsedMap.sourceRoot.startsWith('file://')) {
58+
parsedMap.sourceRoot = '';
59+
}
60+
61+
return JSON.stringify(parsedMap);
62+
}
63+
64+
function normalizeSourceMapSource(source, projectRootPath) {
65+
if (typeof source !== 'string' || source === '') {
66+
return source;
67+
}
68+
69+
const projectRoot = path.resolve(projectRootPath);
70+
const projectRootPrefix = `${projectRoot}${path.sep}`;
71+
72+
if (source.startsWith('file://')) {
73+
try {
74+
const absolutePath = path.resolve(fileURLToPath(source));
75+
const normalizedAbsolute = absolutePath.replace(/\\/g, '/');
76+
77+
if (normalizedAbsolute.startsWith('/var/www/html/')) {
78+
return toRootAbsoluteDisplayPath(normalizedAbsolute.slice('/var/www/html/'.length));
79+
}
80+
81+
if (absolutePath === projectRoot || absolutePath.startsWith(projectRootPrefix)) {
82+
return toRootAbsoluteDisplayPath(path.relative(projectRoot, absolutePath).replace(/\\/g, '/'));
83+
}
84+
85+
return normalizedAbsolute;
86+
} catch (_error) {
87+
return source;
88+
}
89+
}
90+
91+
if (path.isAbsolute(source)) {
92+
const absolutePath = path.resolve(source);
93+
if (absolutePath === projectRoot || absolutePath.startsWith(projectRootPrefix)) {
94+
return toRootAbsoluteDisplayPath(path.relative(projectRoot, absolutePath).replace(/\\/g, '/'));
95+
}
96+
}
97+
98+
return toRootAbsoluteDisplayPath(source);
99+
}
100+
101+
function toRootAbsoluteDisplayPath(pathValue) {
102+
const normalizedPath = String(pathValue || '').replace(/\\/g, '/');
103+
if (
104+
normalizedPath === ''
105+
|| normalizedPath.startsWith('/')
106+
|| normalizedPath.startsWith('./')
107+
|| normalizedPath.startsWith('../')
108+
|| /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(normalizedPath)
109+
) {
110+
return normalizedPath;
111+
}
112+
113+
return `/${normalizedPath}`;
114+
}
115+
28116
function readJsonFile(filePath, fallbackValue = null) {
29117
try {
30118
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
@@ -485,10 +573,16 @@ function createScssSidecar(projectRoot) {
485573
const result = await compileSass(compileEntryPath);
486574

487575
await fs.promises.mkdir(path.dirname(cssOutputPath), { recursive: true });
488-
await fs.promises.writeFile(cssOutputPath, result.css || '', 'utf8');
576+
const normalizedMap = normalizeSourceMapForDisplay(result.map || '', rootPath);
577+
578+
let cssOutput = result.css || '';
579+
if (scssSourceMapEnabled && normalizedMap !== '') {
580+
cssOutput = appendSourceMapComment(cssOutput, HOT_CSS_MAP_FILE_NAME);
581+
}
582+
await fs.promises.writeFile(cssOutputPath, cssOutput, 'utf8');
489583

490-
if (scssSourceMapEnabled && typeof result.map === 'string' && result.map !== '') {
491-
await fs.promises.writeFile(cssMapOutputPath, result.map, 'utf8');
584+
if (scssSourceMapEnabled && normalizedMap !== '') {
585+
await fs.promises.writeFile(cssMapOutputPath, normalizedMap, 'utf8');
492586
} else if (fs.existsSync(cssMapOutputPath)) {
493587
await fs.promises.unlink(cssMapOutputPath);
494588
}

bin/storefront-hot-proxy/start-hot-reload.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const { createProxyMiddleware } = storefrontRequire('http-proxy-middleware');
2121

2222
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
2323
process.env.MODE = process.env.MODE || 'hot';
24+
process.noDeprecation = true;
2425

2526
const proxyPort = Number(process.env.STOREFRONT_PROXY_PORT) || 9998;
2627
const assetPort = Number(process.env.STOREFRONT_ASSETS_PORT) || 9999;

src/Command/WatchCommand.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,12 @@ private function buildHotEnvironment(
414414
'SHOPWARE_STOREFRONT_SCSS_SOURCE_MAP',
415415
$scssEngine === 'sass-cli' ? '1' : '0'
416416
);
417+
$nodeOptions = $this->appendNodeOption($this->env('NODE_OPTIONS', ''), '--no-deprecation');
417418

418419
$environment = [
419420
'PROJECT_ROOT' => $projectRoot,
420421
'NODE_ENV' => $this->env('NODE_ENV', 'development'),
422+
'NODE_OPTIONS' => $nodeOptions,
421423
'MODE' => $this->env('MODE', 'hot'),
422424
'NPM_CONFIG_FUND' => 'false',
423425
'NPM_CONFIG_AUDIT' => 'false',
@@ -573,6 +575,21 @@ private function env(string $key, string $default): string
573575
return (string) $value;
574576
}
575577

578+
private function appendNodeOption(string $options, string $option): string
579+
{
580+
$normalized = trim($options);
581+
if ($normalized === '') {
582+
return $option;
583+
}
584+
585+
$parts = preg_split('/\s+/', $normalized);
586+
if (\is_array($parts) && \in_array($option, $parts, true)) {
587+
return $normalized;
588+
}
589+
590+
return $normalized . ' ' . $option;
591+
}
592+
576593
private function resolveScssEngine(): string
577594
{
578595
$envValue = strtolower($this->env('SHOPWARE_STOREFRONT_SCSS_ENGINE', ''));

0 commit comments

Comments
 (0)