* Allow to specify module for web program

This commit is contained in:
Michaël Van Canneyt 2022-03-05 16:24:38 +01:00
parent 7485ac827a
commit 67a95d4f7f
3 changed files with 45 additions and 13 deletions

View File

@ -204,4 +204,17 @@ object WebBrowserProjectOptionsForm: TWebBrowserProjectOptionsForm
TabOrder = 13
TextHint = 'Name of your webassembly file'
end
object CBUseModule: TCheckBox
AnchorSideLeft.Control = CBCreateHTML
AnchorSideTop.Control = CBServerURL
AnchorSideTop.Side = asrBottom
Left = 6
Height = 23
Top = 391
Width = 293
BorderSpacing.Top = 6
Caption = 'Create a javascript module instead of a script'
OnChange = CBUseHTTPServerChange
TabOrder = 14
end
end

View File

@ -16,6 +16,7 @@ type
BPHelpOptions: TButtonPanel;
CBCreateHTML: TCheckBox;
CBUseBrowserApp: TCheckBox;
CBUseModule: TCheckBox;
CBUseWASI: TCheckBox;
CBUseBrowserConsole: TCheckBox;
CBUseHTTPServer: TCheckBox;
@ -52,6 +53,7 @@ type
property UseRunOnReady : Boolean Index 6 read GetB Write SetB;
property ShowUncaughtExceptions : Boolean Index 7 read GetB Write SetB;
property UseWASI : Boolean Index 8 read GetB Write SetB;
property UseModule : Boolean Index 9 read GetB Write SetB;
Property ServerPort : Word Read GetServerPort Write SetServerPort;
Property URL : String Read GetURL Write SetURL;
Property WasmProgramURL : String Read GetWasmProgramURL Write SetWasmProgramURL;
@ -148,6 +150,7 @@ begin
6 : Result:=CBRunOnReady.Checked;
7 : Result:=cbShowUncaughtExceptions.Checked;
8 : Result:=cbUseWASI.Checked;
9 : Result:=cbUseModule.Checked;
else
Result:=False;
end;
@ -195,6 +198,7 @@ begin
6 : CBRunOnReady.Checked:=Avalue;
7 : cbShowUncaughtExceptions.Checked:=aValue;
8 : cbUseWASI.Checked:=aValue;
9 : cbUseModule.Checked:=aValue;
end;
end;

View File

@ -33,7 +33,8 @@ type
baoStartServer, // Start simple server
baoUseURL, // Use this URL to run/show project in browser
baoShowException, // let RTL show uncaught exceptions
baoUseWASI // Use WASI browser app object
baoUseWASI, // Use WASI browser app object
baoUseModule // include as module as opposed to regular script
);
TBrowserApplicationOptions = set of TBrowserApplicationOption;
@ -783,6 +784,7 @@ begin
StartHTTPServer:=CO(baoStartServer);
UseRunOnReady:=CO(baoRunOnReady);
UseWASI:=CO(baoUseWASI);
UseModule:=CO(baoUseModule);
ShowUncaughtExceptions:=CO(baoShowException);
// We allocate the new port in all cases.
ServerPort:=GetNextPort;
@ -801,6 +803,7 @@ begin
SO(UseRunOnReady,baoRunOnReady);
SO(ShowUncaughtExceptions,baoShowException);
SO(UseWASI,baoUseWASI);
SO(UseModule,baoUseModule);
SO(StartHTTPServer,baoStartServer);
Self.ProjectPort:=ServerPort;
SO(UseURL,baoUseURL);
@ -851,12 +854,10 @@ Const
+' <meta http-equiv="Content-type" content="text/html; charset=utf-8">'+LineEnding
+' <meta name="viewport" content="width=device-width, initial-scale=1">'+LineEnding
+' <title>Project1</title>'+LineEnding
+' <script src="%s"></script>'+LineEnding
+' <script %s src="%s"></script>'+LineEnding
+'</head>'+LineEnding
+'<body>'+LineEnding
+' <script>'+LineEnding
+' %s'+LineEnding
+' </script>'+LineEnding
+' %s'+LineEnding
+'</body>'+LineEnding
+'</html>'+LineEnding;
@ -865,7 +866,7 @@ Const
Var
HTMLFile : TLazProjectFile;
HTMLSource : String;
RunScript,Content : String;
ScriptType, RunScript,Content : String;
begin
HTMLFile:=AProject.CreateProjectFile('project1.html');
@ -873,17 +874,29 @@ begin
AProject.CustomData.Values[PJSProjectHTMLFile]:=HTMLFile.Filename;
AProject.AddFile(HTMLFile,false);
Content:='';
ScriptType:='';
RunScript:='';
if baoUseBrowserConsole in Options then
Content:=ConsoleDiv;
if baoShowException in Options then
Runscript:='rtl.showUncaughtExceptions=true;'+LineEnding+' '
if baoUseModule in Options then
begin
ScriptType:='type="module"';
end
else
RunScript:='';
if baoRunOnReady in Options then
RunScript:=Runscript+'window.addEventListener("load", rtl.run);'+LineEnding
else
RunScript:=Runscript+'rtl.run();'+LineEnding;
HTMLSource:=Format(TemplateHTMLSource,[aFileName,RunScript,Content]);
begin
if baoShowException in Options then
Runscript:='rtl.showUncaughtExceptions=true;'+LineEnding+' '
else
RunScript:='';
if baoRunOnReady in Options then
RunScript:=Runscript+'window.addEventListener("load", rtl.run);'+LineEnding
else
RunScript:=Runscript+'rtl.run();'+LineEnding;
RunScript:=' <script>'+LineEnding
+RunScript
+' </script>'+LineEnding
end;
HTMLSource:=Format(TemplateHTMLSource,[ScriptType,aFileName,RunScript,Content]);
HTMLFile.SetSourceText(HTMLSource);
Result:=HTMLFile;
end;
@ -991,6 +1004,8 @@ begin
CompOpts:=AProject.LazCompilerOptions;
SetDefaultWebCompileOptions(CompOpts);
CompOpts.TargetFilename:='project1';
if baoUseModule in Options then
CompOpts.TargetOS:='module';
SetDefaultWebRunParams(AProject.RunParameters.GetOrCreate('Default'));
AProject.MainFile.SetSourceText(CreateProjectSource,true);
AProject.CustomData.Values[PJSProject]:='1';