diff --git a/components/pas2js/frmpas2jsbrowserprojectoptions.lfm b/components/pas2js/frmpas2jsbrowserprojectoptions.lfm index 7403bb69d9..b083865c7b 100644 --- a/components/pas2js/frmpas2jsbrowserprojectoptions.lfm +++ b/components/pas2js/frmpas2jsbrowserprojectoptions.lfm @@ -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 diff --git a/components/pas2js/frmpas2jsbrowserprojectoptions.pp b/components/pas2js/frmpas2jsbrowserprojectoptions.pp index 2ff697996d..b491485f7c 100644 --- a/components/pas2js/frmpas2jsbrowserprojectoptions.pp +++ b/components/pas2js/frmpas2jsbrowserprojectoptions.pp @@ -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; diff --git a/components/pas2js/pjsdsgnregister.pas b/components/pas2js/pjsdsgnregister.pas index c6b3881ff6..6631db5149 100644 --- a/components/pas2js/pjsdsgnregister.pas +++ b/components/pas2js/pjsdsgnregister.pas @@ -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 +' '+LineEnding +' '+LineEnding +' Project1'+LineEnding - +' '+LineEnding + +' '+LineEnding +''+LineEnding +''+LineEnding - +' '+LineEnding +' %s'+LineEnding +''+LineEnding +''+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:=' '+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';