GithubHelp home page GithubHelp logo

Comments (4)

JoeStanton avatar JoeStanton commented on August 20, 2024

Fixed in 4de2cf5

from babel-transform-js-to-jsx.

bryce-larson avatar bryce-larson commented on August 20, 2024

Do you have docs or a test that shows how this works?

from babel-transform-js-to-jsx.

JoeStanton avatar JoeStanton commented on August 20, 2024

I use this project with a project specific wrapper, you can see towards the end we pass in some options to the babel transform including the component list:

#!/usr/bin/env node
var fs = require("fs");
var path = require("path");

var parseArgs = require('minimist');
var argv = parseArgs(process.argv.slice(2), { default: { format: true } });
var filename = argv._[0];

require("babel-register")({ only: /babel-js-to-jsx/ });
var livescript = require("livescript");
var esformatter = require("esformatter");

if (!filename) {
  console.error("Usage: js-to-jsx <filename>");
  console.error("Use the --no-format flag to disable esformatter");
  process.exit(1);
}

var original = fs.readFileSync(filename, 'utf8');

var components = fs.readFileSync(path.resolve(__dirname, "../components.txt"), "utf8").split("\n");
var es5 = livescript.compile(original, { header: false, bare: true });
var es6 = transpileToES6(es5, components).replace(/\'/g, '"');

if (!argv.format) {
  console.log(es6);
  process.exit(0);
}

var formatted = esformatter.format(es6, {
  "plugins": [
    "esformatter-jsx",
    "esformatter-quotes"
  ],
  "quotes": {
    "type": "double"
  },
  "jsx": {
    "formatJSX": true,
    "attrsOnSameLineAsTag": true,
    "maxAttrsOnTag": 1,
    "firstAttributeOnSameLine": false,
    "spaceInJSXExpressionContainers": " ",
    "alignWithFirstAttribute": true,
    "htmlOptions": {
      "brace_style": "collapse",
      "indent_char": " ",
      "indent_size": 2,
      "max_preserve_newlines": 2,
      "preserve_newlines": true
    }
  }
});

console.log(formatted);

function transpileToES6(es5, components) {
  return require("babel-core").transform(es5, {
    plugins: [
      ["babel-js-to-jsx", { components: components }],
      "babel-js-to-jsx/es6/arrow-functions",
      "babel-js-to-jsx/es6/modules",
      "babel-js-to-jsx/es6/remove-dom-shim",
      "babel-js-to-jsx/es6/unhoist-variables"
    ]
  }).code;
}

from babel-transform-js-to-jsx.

bryce-larson avatar bryce-larson commented on August 20, 2024

Thank you!

from babel-transform-js-to-jsx.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.