How to inject typescript into webview as string of javascript?

330 views Asked by At

I just started working with react-native-webview. I added a vs-code extension called es6-string-javascript, which has alleviated some of the hassle. But to get the benefits of linting and type checking, I was wondering:

  1. Is there an inline utility for transforming typescript into javascript?
  2. What would be the correct way to stringify the result? I have tried using toString() on javascript functions, but it doesn't seem to be working.

So what I want is something that can do this:

my-injection-code.ts

const myTSMethod = /* ...a method defined defined in typescript  */
const myTSMethodAsJS = tsToJsTransformer(myTSMethod); // IS THERE A LIBRARY THAT DOES THIS?
const myMethodAsStringOfJS = myTSMethodAsJS.toString(); // WHAT IS THE PROPER WAY TO STRINGIFY?

const injectionCode = `
  ${myMethodAsStringOfJS}

  const messageEventListenerFn = (e) => {
    // Some code that calls myMethodAsStringOfJS
  };
  document.addEventListener('message', (e) => messageEventListenerFn(e));
`;

export { injectionCode };

MyWebviewScreen.tsx

import React, { useRef } from 'react';
import WebView, { WebViewMessageEvent } from 'react-native-webview';
import { injectionCode } from './path/to/my-injection-code';

const MyWebviewScreen = ({}: Props) => {
    const webviewRef = useRef<WebView>(null);

    const handlePress = () => webview?.injectJavaScript(injectionCode);  // <-- maybe use like this

    return (
      <>
      <Button onPress={handlePress} />
      <WebView
          ref={webviewRef}
          injectedJavaScript={injectionCode} // <-- ...alternatively
          source={{ html: /* injectionCode in HTML */) }} <-- ...alternatively
          injectedJavaScriptBeforeContentLoaded={injectionCode} // <-- ...alternatively
          ...
      />
      </>
    );
};

export default MyWebviewScreen;
1

There are 1 answers

3
Jeet Makwana On

To transform TypeScript code into JavaScript code inline, you can use a tool called ts-transpile-module. It allows you to transpile TypeScript code to JavaScript code programmatically.

First, install the package by running the following command in your project directory:

import { transpileModule } from 'ts-transpile-module';
const myTSMethod = // a method defined in TypeScript ;
const transpiledCode = transpileModule(myTSMethod, {
  compilerOptions: {
   // Specify any necessary compiler options here
  },
});

const myTSMethodAsJS = transpiledCode.outputText;
const myMethodAsStringOfJS = myTSMethodAsJS.toString();

const injectionCode = ` ${myMethodAsStringOfJS}

const messageEventListenerFn = (e) => {
  // Some code that calls myMethodAsStringOfJS
};
document.addEventListener('message', (e) => messageEventListenerFn(e));`;

export { injectionCode };

Note that transpileModule accepts a TypeScript code snippet as the first argument and an optional compilerOptions object as the second argument. You can specify any necessary TypeScript compiler options within the compilerOptions object.

Regarding stringifying JavaScript functions, using the toString() method should work fine. However, keep in mind that when you stringify a function, it won't include the function's closure. If your TypeScript code relies on variables or functions defined outside of myTSMethod, you'll need to make sure those are available in the context where you execute the stringified JavaScript code.