2
votes

I want to use babel runtime in a big/complex nodejs app. I don't want to use the babel require hook because the app is big and when I have tried to use it I get the following error:

RangeError: Maximum call stack size exceeded

And I only want to transpile a few JS files, at least for now.

The babel docs are a bit cryptic for the runtime support. After installing babel-runtime, they provide:

require("babel").transform("code", { optional: ["runtime"] });

Where does that code get included? And is "code" truly just a string? I have tried to add that to my main app.js file (express 3 app). Unfortunately, that doesn't work.

1
Could you elaborate? Does that error happen when you build? When you run your code?loganfsmyth
It happens when the app starts.rob_hicks
Maybe I'm confused about what your question is. You should just add runtime to the list of transformations you run on your code. If you are compiling via the CLI, you'd do --optional runtime, if you're using something else, you'd need to pass the options to that. The .transform example is if you were programmatically using the Babel API to compile your file, you wouldn't call it from your own code, just build scripts. How are you building now?loganfsmyth

1 Answers

0
votes

I cannot totally understand your questions, but I think I can answer part of it.

As explained in the babel api, transform() function takes a string that is supposed to be source code to be transpiled, and returns an object including three properties:

  • code the code generated
  • map the source map for the code
  • ast the syntax tree

This means, if you want to transpile your code in a folder, for each file you want to transpile, you should read the file with fs utility, give it to transform() function, and write the value of the code property in the object returned, to your output folder.

To simplify the step to read files, you could use the function transformFile provided by babel.

As for the problem you mention with your express app, I cannot help, unless you provide more information.