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

Remove automatic import of React #2

Open
ralmidani opened this issue Sep 5, 2017 · 2 comments
Open

Remove automatic import of React #2

ralmidani opened this issue Sep 5, 2017 · 2 comments

Comments

@ralmidani
Copy link

I plan to use Vue rather than React for my own real-world projects. Mithril, Preact, and Inferno also support JSX. While React is the most popular, shouldn't we avoid making assumptions about which library a dev is using?

@helixbass
Copy link
Owner

@ralmidani yup I figured making disabling or specifying the auto-import a configuration option would be a good choice. I'll take a look at that today

@helixbass
Copy link
Owner

@ralmidani I added a jsxFramework compiler option (and corresponding --jsx-framework command-line option) to specify which framework's conventions it should use when compiling JSX. So far added react (default), reactNative, and vue as options; released as [email protected] if you want to test it out in an actual Vue project

So to compile for Vue, you could do eg on the command line:

coffee --jsx-framework vue -p tmp.coffee

or if you're passing a config options object into a build tool, set jsxFramework: 'vue'. I updated coffee-jsxy-loader to accept jsxFramework option (released as [email protected]) so if you're using Webpack, you might use loader config:

  test: /\.coffee$/,
  loader: [
    'babel-loader',
    {
      loader: 'coffee-jsxy-loader',
      options: {
        jsxFramework: 'vue',
      },
    },
  ],

Looking at Vue, the differences I saw as far as how JSX should be compiled were:

  • no import is required to use JSX (since createElement() helper gets injected)
  • class shorthands should compile to class attribute, not className
  • dynamic classname syntax shouldn't import/use classnames helper, it should just pass dynamic class names as array value of class attribute. So eg .(class1, class2: maybe) compiles to <div class={[class1, {class2: maybe}]}></div>
  • style attribute is ok with an array value (like React Native), so .[...] style shorthand (not yet documented) should compile to array value (rather than _extends())

For now, see the test in this PR (or ask) if you have questions about how these syntaxes are working for Vue. I'll add documentation for how to configure the jsxFramework option, what effects it has on the compiled JSX, and document the new .[...] style shorthand

This PR should be a good example if you're interested in how to add a config/command-line option to the compiler. And yes we should support any frameworks that use JSX if possible, I haven't looked at the others you mentioned (Mithril, Preact, Inferno) - are you interested in seeing if you can add the relevant jsxFramework config options to support them (and maybe see if there are other aspects of JSX compilation that they need to control for any of the other frameworks)? Otherwise I can look at their docs

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