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
发布评论