Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

360
Visualizações
How to Convert TypeScript ES6 into ES5 using Babel

I am building a component library and would like to be able to use this in other places.

I have the following component at src/components/List/List.tsx

import React from 'react'
import styled from '@mui/material/styles/styled'

import MuiList, { ListProps as MuiListProps } from '@mui/material/List'

const ListStyled = styled(MuiList)`
  text-decoration: none;
  cursor: pointer;
`

export const List: React.FC<MuiListProps> = ({ children, ...props }) => {
  return <ListStyled {...props}>{children}</ListStyled>
}

export default List

When running:

rm -rf dist && NODE_ENV=production babel src/components --out-dir dist --copy-files --ignore __tests__,__snapshots__,__mocks__,test.tsx,stories.js

The compiled version is as dist/List/List.tsx:

import React from 'react'
import styled from '@mui/material/styles/styled'

import MuiList, { ListProps as MuiListProps } from '@mui/material/List'

const ListStyled = styled(MuiList)`
  text-decoration: none;
  cursor: pointer;
`

export const List: React.FC<MuiListProps> = ({ children, ...props }) => {
  return <ListStyled {...props}>{children}</ListStyled>
}

export default List

The file is identical. I would like to see dist/List/List.js ideally for my packages to use. But I realize, maybe with TS it cannot work this way, as the interfaces, etc will not be used anymore? So logically I believe I would need dist/List/List.js accompanied with dist/List/List.d.ts? If so, how can I do this?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The problem is that babel does not recognize .tsx file extensions by default. The --copy-files flag copies files that are not compiled - this is currently why your file is being copied.

You can add an extensions flag with .tsx included --extensions '.tsx' and it will attempt to compile them.

I was able to compile your file using babel src/components/List.tsx --out-dir dist --extensions '.tsx' --presets @babel/preset-typescript

Example output:

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.List = void 0;

var _react = _interopRequireDefault(require("react"));

var _styled = _interopRequireDefault(require("@mui/material/styles/styled"));

var _List = _interopRequireDefault(require("@mui/material/List"));

var _excluded = ["children"];

var _templateObject;

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }

var ListStyled = (0, _styled.default)(_List.default)(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n  text-decoration: none;\n  cursor: pointer;\n"])));

var List = function List(_ref) {
  var children = _ref.children,
      props = _objectWithoutProperties(_ref, _excluded);

  return <ListStyled {...props}>{children}</ListStyled>;
};

exports.List = List;
var _default = List;
exports.default = _default;
about 4 years ago · Juan Pablo Isaza Relatório

0

Actually, for creating a library or a package for your project you should use rollup alongside implementing the typescript configurations.

I experienced it and lost many hours to configure the base but totally my friend nominated TSDX, Zero-config CLI for TypeScript package development, which contains:

  1. CJS
  2. ESM
  3. UMD
  4. Treeshaking
  5. Live Playground
  6. Dev/Prod Builds
  7. Zero-config
  8. Project Templates
  9. TypeScript
  10. Rollup
  11. Jest
  12. Prettier
  13. ESLint

It's created by Jared Palmer, this TSDX package is and CLI for creating what you want, even you can add your extra configurations. it has complete documentations too and a very active community. All the configs of TypeScript, Babel, building are fully configured in the best optimum case.

You can create a new package by this command:

npx tsdx create [your lib name]

And then move your codes into src folder, even the exporting types doesn't need any configs, just export the types in the root export file of src folder like below:

// index.ts or index.tsx // the root index file in src folder
import type { OneComponentProps } from '../OneComponent';

export type { OneComponentProps };
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda