Notalib / nativescript-webview-ext

Nativescript plugin with extended WebView functionality
Apache License 2.0
76 stars 37 forks source link

Loading a webpack or parcel app from "dist" folder #54

Closed minskmaz closed 4 years ago

minskmaz commented 4 years ago

From the documentation as well as the sample app it's difficult to ascertain how one would load and call the build/dist files of a web app. I've added the index.html and all the supporting js files from the dist using the following code. As it stands none of the javascript I've loaded executes. What step(s) am I missing? Thanks in advance! (PS. the dom query is to get the latest name of the app.js file which changes with each new dist generation)

function webviewLoaded(args){
  console.log('/// webviewLoaded ///', args)
  const dom = parser.parseFromString(indxHtmlFile.readTextSync());
  var dist_js_str = dom.getElementsByTagName('script')[0].getAttribute('src');
  var dist_js_bytes = "this should change";
  var dist_js_path;
  webview = args.object;
  webview.on('hello', (e)=>{
    console.log("MESSAGE FROM WEBVIEWEXT", e)
  })
  webview.on(WebViewExt.loadFinishedEvent, (args) => {
    console.log("/// loadFinishedEvent ///")
    webview = args.object
    const js_files = www_folder_entities.filter(function (e) {
      if(e._extension === '.js'){
        webview.registerLocalResource(e._name, e._path)
        if(e._path.includes(dist_js_str)){
          if(fileExists(e._path)){
            var dist_js_file = www_folder.getFile(e._path)
            dist_js_bytes = dist_js_file.readTextSync()
          }
        }
        return {resourceName:e._name, filepath:e._path};
      }
    })
    webview.loadJavaScriptFiles(js_files)
    // this works
    //webview.autoExecuteJavaScript("window.nsWebViewBridge.emit('hello', {'hello':'world'})")
    // this does not
    //console.log(dist_js_bytes)
    webview.autoExecuteJavaScript(dist_js_bytes)
  })
  webview.registerLocalResource('index_html', indxHtmlPath)
  webview.loadUrl(indxHtmlPath)
minskmaz commented 4 years ago

I solved this issue. The problem was with the line var dist_js_file = www_folder.getFile(e._path) - e._path doesn't get the file - www_folder.getFile(e._name) does - so I was passing the fileExists test but receiving 0 bytes from readTextSync()