Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code transpilation is not performed for imported modules when prevaling with template literals #50

Closed
marzelin opened this issue Feb 25, 2018 · 1 comment · Fixed by #51

Comments

@marzelin
Copy link
Contributor

marzelin commented Feb 25, 2018

  • babel-plugin-preval version: ^1.6.3
  • node version: ^9
  • npm (or yarn) version: ^1

Great tool, thanks for it. There's one thing though: when the code to be prevaled in template tags requires a module, that module won't be transpiled by babel. For example, this code that prerenders a react element:

const markupForElement = preval`
   const render = require("react-dom/server").renderToStaticMarkup;
   const reactElement = require("./reactElement");
   module.exports = render(reactElement);
`

will fail with error:

Module build failed: .../reactElement.js:2
const reactElement = <div>Hello from React Element</div>;
                     ^

SyntaxError: Unexpected token <

even though react preset is added to babel configuration.

The simplest solution is to add require("babel-register"); at the top of a template literal. However, it would be nice if this package could handle this automatically.

Suggested solution:
Prepend transpiled with "require("babel-register");\n" before passing it to requireFromString at

const val = requireFromString(transpiled, filename)

@kentcdodds
Copy link
Owner

Sounds awesome 👍 Yes, I'd happily accept a PR that does this 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants