babel-plugin-rewire
babel-plugin-rewire copied to clipboard
Error with `babel/preset-typescript`
I have been trying to use this plugin alongside the TypeScript preset from babel. For the most part, it works fine. However, when I import something that is only a type it blows up!
I am importing
import { Reducer } from 'redux';
This causes the error
TypeError: Property typeName of TSTypeReference expected node to be of a type ["TSEntityName"] but instead got "CallExpression"
at Object.validate (/Users/ross/Code/atlas-web-and-tv/node_modules/@babel/types/lib/definitions/utils.js:128:13)
at Object.validate (/Users/ross/Code/atlas-web-and-tv/node_modules/@babel/types/lib/validators/validate.js:17:9)
at NodePath._replaceWith (/Users/ross/Code/atlas-web-and-tv/node_modules/@babel/traverse/lib/path/replacement.js:194:9)
at NodePath.replaceWith (/Users/ross/Code/atlas-web-and-tv/node_modules/@babel/traverse/lib/path/replacement.js:178:8)
at RewireState.Identifier (/Users/ross/Code/atlas-web-and-tv/node_modules/babel-plugin-rewire/lib/babel-plugin-rewire.js:77:12)
at NodePath._call (/Users/ross/Code/atlas-web-and-tv/node_modules/@babel/traverse/lib/path/context.js:53:20)
at NodePath.call (/Users/ross/Code/atlas-web-and-tv/node_modules/@babel/traverse/lib/path/context.js:40:17)
at NodePath.visit (/Users/ross/Code/atlas-web-and-tv/node_modules/@babel/traverse/lib/path/context.js:88:12)
at TraversalContext.visitQueue (/Users/ross/Code/atlas-web-and-tv/node_modules/@babel/traverse/lib/context.js:118:16)
at TraversalContext.visitSingle (/Users/ross/Code/atlas-web-and-tv/node_modules/@babel/traverse/lib/context.js:90:19)
at TraversalContext.visit (/Users/ross/Code/atlas-web-and-tv/node_modules/@babel/traverse/lib/context.js:146:19)
at Function.traverse.node (/Users/ross/Code/atlas-web-and-tv/node_modules/@babel/traverse/lib/index.js:94:17)
at NodePath.visit (/Users/ross/Code/atlas-web-and-tv/node_modules/@babel/traverse/lib/path/context.js:95:18)
at TraversalContext.visitQueue (/Users/ross/Code/atlas-web-and-tv/node_modules/@babel/traverse/lib/context.js:118:16)
at TraversalContext.visitSingle (/Users/ross/Code/atlas-web-and-tv/node_modules/@babel/traverse/lib/context.js:90:19)
...
Any help would be appreciated! In the meantime I will keep digging.
This seemed to fix it for me so raised a PR https://github.com/speedskater/babel-plugin-rewire/pull/219
I actually got the current rewire plugin to finally work with my Typescript setup in testing env. I use nyc
to run ava
. Before, I specified all of the required modules via Ava's require
config. I required ts-node/register
, @babel/register
and @babel/polyfill
. Still I got those errors.
So, I moved all of those into nyc
's own require
configuration. And voila, it works. I would like to get rid of ts-node
completely, but that seems to be the only thing fixing the problem for me.
This is the nyc config:
{
"extension": [
".js",
".ts"
],
"require": [
"ts-node/register",
"./ava_babel_register",
"@babel/polyfill"
]
}
And this is the npm script to run ava:
"test": "nyc ava"
I am facing this same issue but running with mocha, I have a file with all my requires, it looks like this
mocha-bootload.js
require('@babel/polyfill');
require('@babel/register')({
extensions: ['.js', '.jsx', '.ts', '.tsx'],
plugins: [
'babel-plugin-rewire',
'@babel/proposal-class-properties',
'@babel/proposal-object-rest-spread',
],
});
require('chai')
.use(require('chai-as-promised'))
.use(require('sinon-chai'));
Then in my build script I run mocha --require mocha-bootload
.
I would really appreciate some help with this!
thx @dmiranda2791 for your example, I'd used mocha too. And this is my configs (may helps someone else) There are both js & ts files on my project.
package.json
"@babel/register": "^7.0.0",
"mocha": "^5.2.0",
"ts-mocha": "^2.0.0",
babel-register.js
require('@babel/register')({
extensions: ['.js', '.ts'],
});
test command
npx ts-mocha -r ./babel-register.js test