@@ -12,6 +12,7 @@ const esbuild = require('esbuild');
1212const command = process . argv [ 2 ] ;
1313const entryPoint = process . argv [ 3 ] ;
1414const outdir = process . argv [ 4 ] ;
15+ const publicPath = process . argv [ 5 ] || '/' ;
1516
1617if ( ! 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