Skip to content

Commit 5963dcc

Browse files
authored
fix(viewer): use correct publicPath, replace process.env (#753)
1 parent 6e4585d commit 5963dcc

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

packages/server/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
"scripts": {
1414
"clean": "rm -rf ./dist ./storybook-static",
1515
"build": "npm run build:esbuild && npm run build:storybook",
16-
"build:esbuild": "node ../../scripts/build-app.js build ./src/ui/index.html ./dist",
17-
"build:watch": "node ../../scripts/build-app.js watch ./src/ui/index.html ./dist",
16+
"build:esbuild": "node ../../scripts/build-app.js build ./src/ui/index.html ./dist /app",
17+
"build:watch": "node ../../scripts/build-app.js watch ./src/ui/index.html ./dist /app",
1818
"build:source-map-explorer": "npm run clean && npm run build && ../../scripts/source-map-explorer.sh",
1919
"build:storybook": "build-storybook",
2020
"start:storybook": "start-storybook -p 6006"

scripts/build-app.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const esbuild = require('esbuild');
1212
const command = process.argv[2];
1313
const entryPoint = process.argv[3];
1414
const outdir = process.argv[4];
15+
const publicPath = process.argv[5] || '/';
1516

1617
if (!command || !entryPoint || !outdir) {
1718
throw new Error('missing args');
@@ -32,6 +33,10 @@ function insertCollectedStyles(result) {
3233
if (stylesheets.length === 0) return;
3334
if (stylesheets.length > 1) throw new Error('expected at most one generated stylesheet');
3435

36+
const href = publicPath === '/' ?
37+
path.relative(outdir, stylesheet) :
38+
`/app/${path.relative(outdir, stylesheet)}`;
39+
3540
const htmls = Object.keys(result.metafile.outputs).filter(o => o.endsWith('.html'));
3641
const html = htmls[0];
3742
if (htmls.length !== 1) throw new Error('expected exactly one generated html');
@@ -40,15 +45,15 @@ function insertCollectedStyles(result) {
4045
const htmlText = fs.readFileSync(html, 'utf-8');
4146
if (!htmlText.includes(needle)) throw new Error(`expected ${needle} in html`);
4247

43-
const newHtmlText =
44-
htmlText.replace(needle, `<link rel="stylesheet" href="/app/${path.relative(outdir, stylesheet)}" />`);
48+
const newHtmlText = htmlText.replace(needle, `<link rel="stylesheet" href="${href}" />`);
4549
fs.writeFileSync(html, newHtmlText);
4650
}
4751

4852
/**
4953
* @param {esbuild.BuildResult} result
5054
*/
5155
function fixScriptSrc(result) {
56+
if (publicPath === '/') return;
5257
if (!result.metafile) throw new Error('expected metafile');
5358

5459
const htmls = Object.keys(result.metafile.outputs).filter(o => o.endsWith('.html'));
@@ -81,7 +86,10 @@ async function main() {
8186
'.woff': 'file',
8287
'.woff2': 'file',
8388
},
84-
publicPath: '/app',
89+
define: {
90+
'process.env.VIEWER_ORIGIN': JSON.stringify(process.env.VIEWER_ORIGIN || ''),
91+
},
92+
publicPath,
8593
bundle: true,
8694
outdir,
8795
minify: true,

0 commit comments

Comments
 (0)