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

Require a pug template #15

Open
Gooseware opened this issue Oct 3, 2017 · 2 comments
Open

Require a pug template #15

Gooseware opened this issue Oct 3, 2017 · 2 comments

Comments

@Gooseware
Copy link

Gooseware commented Oct 3, 2017

Hi,
I am wondering if it would be possible to require a pug template. Using a tagged string literal is great but I would love to keep the templates separate. Unfortunately I don't know if I am able to convert a string into a string literal or if there is a global pug function which I can use on a string that I require in using raw-loader in webpack.

Thank you for any assistance on this.

@ForbesLindesay
Copy link
Member

This is not currently supported. It would require an update to https://github.com/pugjs/babel-plugin-transform-react-pug/blob/master/src/index.js to handle a call to pug.compileFile by resolving the file name, reading the file, then running a slight variation on:

          const ast = parsePug(src);
          const context = Context.create(this.file, path, interpolationRef);
          const transformed = ast.nodes.map(node => visitExpression(node, context));
          const expression = transformed.length === 1 ? transformed[0] : t.arrayExpression(transformed);

          context.variablesToDeclare.forEach(id => {
            path.scope.push({kind: 'let', id});
          });

          path.replaceWith(expression);

This is not trivial, and not on my current road map, but if someone wants to take a stab at it, I'll do my best to review the pull request.

@bluewings
Copy link

@Gooseware
You can use pug-as-jsx-loader.
The loader supports inline templates and can store the pug files separately.

And if you use create-react-app, you can use pug-as-jsx-loader with cra-rewired without npm run eject.

https://github.com/bluewings/cra-rewired/tree/master/examples/pug-as-jsx-loader

App.pug

.App
  header.App-header
    img.App-logo(src='{logo}', alt='logo')
    p
      | Edit 
      code src/App.js
      |  and save to reload.
    a.App-link(href='https://reactjs.org', target='_blank', rel='noopener noreferrer')
      | Learn React

App.js

import template from './App.pug';

class App extends Component {
  render() {
    return template({
      // variables
      logo,
    });
  }
}

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

No branches or pull requests

4 participants