如何使用Babel将TypeScript ES6转换为ES5



我正在构建一个组件库,希望能够在其他地方使用它。

我在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

运行时:

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

编译版本为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

该文件完全相同。我希望看到dist/List/List.js非常适合我的软件包使用。但我意识到,也许TS不能以这种方式工作,因为接口等将不再使用?所以从逻辑上讲,我认为我需要dist/List/List.jsdist/List/List.d.ts?如果是,我该怎么做?

问题是babel默认情况下无法识别.tsx文件扩展名。--copy-files标志复制未编译的文件——这就是当前复制文件的原因。

您可以添加包含.tsx--extensions '.tsx'的扩展标志,它将尝试编译它们。

我能够使用babel src/components/List.tsx --out-dir dist --extensions '.tsx' --presets @babel/preset-typescript编译您的文件

示例输出:

"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;

实际上,为了为项目创建库或包,您应该在实现typescript配置的同时使用rollup

我经历了它,花了很多小时来配置基础,但我的朋友完全提名了TSDX,用于TypeScript包开发的Zero-config CLI,其中包含:

  1. CJS
  2. ESM
  3. UMD
  4. 盗树
  5. 现场游乐场
  6. 开发/生产构建
  7. 零配置
  8. 项目模板
  9. TypeScript
  10. 汇总
  11. Jest
  12. Prettier
  13. ESLint

它是由Jared Palmer创建的,这个TSDX包和CLI用于创建您想要的内容,甚至您可以添加额外的配置。它也有完整的文档和一个非常活跃的社区。TypeScript、Babel、building的所有配置都在最佳情况下进行了充分配置。

您可以通过以下命令创建一个新包:

npx tsdx create [your lib name]

然后将代码移到src文件夹中,即使导出类型也不需要任何配置,只需导出src文件夹根导出文件中的类型,如下所示:

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

最新更新