带有打字稿的 babel

编程入门 行业动态 更新时间:2024-10-03 14:19:11

带有打字稿的 <a href=https://www.elefans.com/category/jswz/34/1770408.html style=babel"/>

带有打字稿的 babel

所以,我看到了很多类似的issues,但大多数都是指构建代码,而这个实际上是一个CLI脚本。

我的命令是:

node_modules/.bin/babel-node -x .js,.jsx,.ts,.tsx scripts/database/index.ts generate

如果它从

node_modules
调用一些代码(
React Native
与精确相关)它会抛出错误。

我试过

type: module
但它导致了更严重的错误。

babel.config.js:

module.exports = {
  presets: [
    'module:metro-react-native-babel-preset',
    '@babel/preset-flow',
  ],
  plugins: [
    ['@babel/plugin-proposal-decorators', { legacy: true }],
    ['@babel/plugin-transform-flow-strip-types'],
    [
      require.resolve('babel-plugin-module-resolver'),
      {
        root: ['.'],
        extensions: [
          '.ios.js',
          '.android.js',
          '.js',
          '.ts',
          '.tsx',
          '.json',
          // '.png',
        ],
        alias: {
          app: ['./app'],
          'test/*': ['test/'],
          '@components': './app/components',
        },
      },
    ],
  ],
  sourceMaps: true,
};

tsconfig.json:

{
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "emitDecoratorMetadata": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "isolatedModules": true,
    "jsx": "react",
    "lib": ["ES2016"],
    "module": "commonjs",
    "moduleResolution": "node",
    "noEmit": true,
    "resolveJsonModule": true,
    "strict": true,
    "target": "esnext",
    "baseUrl": "./",
    "paths": {
      "app/*": ["app/*"],
      "tests": ["tests/*"],
    },
    // ensure ignores node_modules
    "skipLibCheck": true,
    "preserveSymlinks": true,
    "typeRoots": ["./node_modules/@types"],
    "types": [
      "node",
      "@wdio/types",
      "webdriverio/async",
      // "@wdio/jasmine-framework",
      // "expect-webdriverio/jasmine",
      "jest"
    ]
  },
  // "include": [
  //   "src/*",
  //   "tests/*",
  // ],
  "exclude": [
    "node_modules",
    "babel.config.js",
    "metro.config.js",
    "jest.config.js",
    // isnores special cases
    "**/modules/**",
    "node_modules/react-native/**",
    "node_modules/@react-navigation/**",
  ]
}
回答如下:

删除

"type": "module"

安装

@babel/register

在您的第一行代码中,添加这一行

require('@babel/register')({ extensions: ['.js', '.jsx', '.ts', '.tsx'] })

npm 脚本

"start": "babel-node -x js,.jsx,.ts,.tsx -- src/app.ts"

更新:

原来你可以跳过整个

@babel/register
的事情,你缺少的是你命令中的
--

更多推荐

带有打字稿的 babel

本文发布于:2024-05-31 05:24:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1771322.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:babel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!