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

How does one use crank without a bundler? #124

Closed
Perrishnikov opened this issue Jun 4, 2020 · 5 comments
Closed

How does one use crank without a bundler? #124

Perrishnikov opened this issue Jun 4, 2020 · 5 comments

Comments

@Perrishnikov
Copy link

Perrishnikov commented Jun 4, 2020

How does one use crank without a bundler? Following the installation instructions as-is, did not work. "Uncaught SyntaxError: Unexpected token '<'"

@brainkim
Copy link
Member

brainkim commented Jun 5, 2020

Hello! I’m sorry to hear you’re having trouble. I think I need some more information about your situation. Which instructions are you referring to? Also, if you’re using JSX, you necessarily need a transpilation step of some kind. Do you have a minimal example that I can look at?

@Perrishnikov
Copy link
Author

Thanks for following up. I was following the Getting Started Installation instructions. Below is the code.

//index.html
<!DOCTYPE html>
<html>
  <head>
    <title>Crank.js Test</title>
  </head>

  <body>
    <div id="app"></div>
    <script type="module" src="index.js"></script>
  </body>
</html>

//index.js
/** @jsx createElement */
import { createElement } from "https://unpkg.com/@bikeshaving/crank@0.1.6/esm/index.js";
// import { createElement } from "/web_modules/@bikeshaving/crank.js";
// import { Renderer } from '/web_modules/@bikeshaving/crank.js';
import { Renderer } from 'https://unpkg.com/@bikeshaving/crank@0.1.6/esm/index.js';

function Greeting({ name = "World" }) {
  return (
    <div>Hello {name}</div>
  );
}

Renderer.render(<Greeting />, document.body);

@brainkim
Copy link
Member

brainkim commented Jun 5, 2020

Hmmm, so as far as I can tell, the problem is that you’re using JSX in a vanilla JavaScript file. You need to either transpile it with something like babel, typescript or sucrase, or use an alternative syntax. You can, for instance, use createElement directly:

function Greeting({ name = "World" }) {
  return createElement("div", null, "Hello ", name);
}

Or you can use something like htm:

import { createElement } from "https://unpkg.com/@bikeshaving/crank@0.1.6/index.js";
import { Renderer } from 'https://unpkg.com/@bikeshaving/crank@0.1.6/dom.js';
import htm from 'https://unpkg.com/htm?module';
const html = htm.bind(createElement);
function Greeting({ name = "World" }) {
  return html`<div> Hello ${name}</div>`;
}

I like transpilation, but these options are available to you. Also check out the docs from this PR for more information.

Let me know if this makes sense!

@Perrishnikov
Copy link
Author

Thanks for your explanation.

@brainkim
Copy link
Member

brainkim commented Jul 2, 2020

Closing for housekeeping purposes. If you still need help feel free to reopen the issue!

@brainkim brainkim closed this as completed Jul 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants