0

I try to use RxJs with Require.JS on Html page (without Angular or WebPack), this is my test

    <script src="http://requirejs.org/docs/release/2.3.6/comments/require.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/7.8.1/rxjs.umd.min.js"></script>
    <script type="text/javascript">
    require.config({
      paths: {
          "rxjs": "https://cdnjs.cloudflare.com/ajax/libs/rxjs/7.8.1/rxjs.umd.min.js"
      }
    });
    require (['rxjs'], (x) => x.of(1,2,3,4,5).subscribe(console.log));
    //-- before this line all working fine

    var rxjs_1 = require (['rxjs'], (x) => x);
    rxjs_1.of(6,7,8,9,10).subscribe(console.log);
    ....
    </script>

First way working fine, I see on console 1,2,3,4,5, but I don't want rewrite something my huge code and want to receive reference to RxJs in variable rxjs_1 and than simple working with variable rxjs_1.
Something lacks in my code, maybe await or other functions from rxjs_1 obj, current code get me in rxjs_1 variable only ref to 'function localRequire(deps, callback, errback)' instead reference to RxJs and Observable. And, of course, OF() in rxjs_1 is undefined.
What lack in this code?

7
  • Why can't you just paste your code into the function? Commented Feb 1, 2024 at 19:44
  • @Konrad, because my code is result of Typescript compilation on Node.js. I don't want manually changing this code, I search minimum changing as possible, without manual rewriting thousands line of code, debugged on Node.js, My current result of TS compilcation started from this lines var rxjs_1 and I want to a couple code line before existing code. Commented Feb 1, 2024 at 19:48
  • If you compile typescript anyway why just not bundle rxjs with it? Commented Feb 1, 2024 at 19:51
  • @Konrad, with webpack? Why I need use WebPack instead one missing AWAIT ? I need only reference to RxJs from Require.JS. Of course, there is way to use Angular or Webpack. But Require.JS is simple loader of any 3-rd party library, why I can avoid this simple way? Need only understand where is missing await or () and add one code line above result of compilation. Commented Feb 1, 2024 at 19:55
  • 1
    1. You can bundle modules using typescript 2. requirejs doesn't return a promise so you can't await. You can do something like const rxjs = await new Promise(r => require (['rxjs'], r));, but you can only use await in an async function Commented Feb 1, 2024 at 20:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.