@@ -12,6 +12,7 @@ const esbuild = require('esbuild');
12
12
const command = process . argv [ 2 ] ;
13
13
const entryPoint = process . argv [ 3 ] ;
14
14
const outdir = process . argv [ 4 ] ;
15
+ const publicPath = process . argv [ 5 ] || '/' ;
15
16
16
17
if ( ! command || ! entryPoint || ! outdir ) {
17
18
throw new Error ( 'missing args' ) ;
@@ -32,6 +33,10 @@ function insertCollectedStyles(result) {
32
33
if ( stylesheets . length === 0 ) return ;
33
34
if ( stylesheets . length > 1 ) throw new Error ( 'expected at most one generated stylesheet' ) ;
34
35
36
+ const href = publicPath === '/' ?
37
+ path . relative ( outdir , stylesheet ) :
38
+ `/app/${ path . relative ( outdir , stylesheet ) } ` ;
39
+
35
40
const htmls = Object . keys ( result . metafile . outputs ) . filter ( o => o . endsWith ( '.html' ) ) ;
36
41
const html = htmls [ 0 ] ;
37
42
if ( htmls . length !== 1 ) throw new Error ( 'expected exactly one generated html' ) ;
@@ -40,15 +45,15 @@ function insertCollectedStyles(result) {
40
45
const htmlText = fs . readFileSync ( html , 'utf-8' ) ;
41
46
if ( ! htmlText . includes ( needle ) ) throw new Error ( `expected ${ needle } in html` ) ;
42
47
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 } " />` ) ;
45
49
fs . writeFileSync ( html , newHtmlText ) ;
46
50
}
47
51
48
52
/**
49
53
* @param {esbuild.BuildResult } result
50
54
*/
51
55
function fixScriptSrc ( result ) {
56
+ if ( publicPath === '/' ) return ;
52
57
if ( ! result . metafile ) throw new Error ( 'expected metafile' ) ;
53
58
54
59
const htmls = Object . keys ( result . metafile . outputs ) . filter ( o => o . endsWith ( '.html' ) ) ;
@@ -81,7 +86,10 @@ async function main() {
81
86
'.woff' : 'file' ,
82
87
'.woff2' : 'file' ,
83
88
} ,
84
- publicPath : '/app' ,
89
+ define : {
90
+ 'process.env.VIEWER_ORIGIN' : JSON . stringify ( process . env . VIEWER_ORIGIN || '' ) ,
91
+ } ,
92
+ publicPath,
85
93
bundle : true ,
86
94
outdir,
87
95
minify : true ,
0 commit comments