codemix / babel-plugin-typecheck

Static and runtime type checking for JavaScript in the form of a Babel plugin.
MIT License
886 stars 44 forks source link

Doesn't work with babel require hook #47

Closed aaronshaf closed 8 years ago

aaronshaf commented 8 years ago

I have the following in foo.js:

require('babel-core/register')({
  'plugins': ['typecheck']
})

require('./bar')

In bar.js:

export default function () {}

Error:

TypeError: template is not a function
    at createChecks (/Users/me/workspace/hello-world/node_modules/babel-plugin-typecheck/lib/index.js:322:15)
    at exports.default (/Users/me/workspace/hello-world/node_modules/babel-plugin-typecheck/lib/index.js:27:16)
    at Function.memoisePluginContainer (/Users/me/workspace/hello-world/node_modules/babel-core/lib/transformation/file/plugin-manager.js:77:23)
    at PluginManager.add (/Users/me/workspace/hello-world/node_modules/babel-core/lib/transformation/file/plugin-manager.js:209:30)
    at File.buildTransformers (/Users/me/workspace/hello-world/node_modules/babel-core/lib/transformation/file/index.js:237:21)
    at new File (/Users/me/workspace/hello-world/node_modules/babel-core/lib/transformation/file/index.js:139:10)
    at Pipeline.transform (/Users/me/workspace/hello-world/node_modules/babel-core/lib/transformation/pipeline.js:164:16)
    at Object.transformFileSync (/Users/me/workspace/hello-world/node_modules/babel-core/lib/api/node.js:137:37)
    at compile (/Users/me/workspace/hello-world/node_modules/babel-core/lib/api/register/node.js:132:20)
    at normalLoader (/Users/me/workspace/hello-world/node_modules/babel-core/lib/api/register/node.js:199:14)
phpnode commented 8 years ago

@aaronshaf which version of babel are you using?

aaronshaf commented 8 years ago

6.0.15

phpnode commented 8 years ago

Can't replicate this with babel 6.1.x but I notice that you didn't include the commonjs modules transformer (or the es2015 preset) in your config so it should have failed for different reasons. Leads me to suspect you have a .babelrc file which might be overriding something, if so please post the contents here. Will reopen if you can provide a more detailed repro.

sgodwin424 commented 8 years ago

I am having this same issue. I have app.js:

require('babel-core/register')();
require('./bar.js');

And in bar.js:

let a: string = 'test';

With .babelrc:

{
    "plugins": [
        "typecheck"
    ],
    "presets": [
        "es2015"
    ]
}

When I run node app, I get the following error:

C:\Users\Scott\Documents\GitHub\test\node_modules\babel-core\lib\transformation\file\index.js:540
      throw err;
      ^

SyntaxError: C:/Users/Scott/Documents/GitHub/test/bar.js: Unexpected token (1:5)
←[0m> 1 | let a←[1m:←[22m string ←[1m=←[22m ←[31m'test'←[39m←[1m;←[22m
    |      ^←[0m
    at Parser.pp.raise (C:\Users\Scott\Documents\GitHub\test\node_modules\babylon\lib\parser\location.js:22:13)
    at Parser.pp.unexpected (C:\Users\Scott\Documents\GitHub\test\node_modules\babylon\lib\parser\util.js:91:8)
    at Parser.pp.semicolon (C:\Users\Scott\Documents\GitHub\test\node_modules\babylon\lib\parser\util.js:78:38)
    at Parser.pp.parseVarStatement (C:\Users\Scott\Documents\GitHub\test\node_modules\babylon\lib\parser\statement.js:405:8)
    at Parser.pp.parseStatement (C:\Users\Scott\Documents\GitHub\test\node_modules\babylon\lib\parser\statement.js:114:19)
    at Parser.pp.parseBlockBody (C:\Users\Scott\Documents\GitHub\test\node_modules\babylon\lib\parser\statement.js:527:25)
    at Parser.pp.parseTopLevel (C:\Users\Scott\Documents\GitHub\test\node_modules\babylon\lib\parser\statement.js:29:8)
    at Parser.parse (C:\Users\Scott\Documents\GitHub\test\node_modules\babylon\lib\parser\index.js:96:17)
    at Object.parse (C:\Users\Scott\Documents\GitHub\test\node_modules\babylon\lib\index.js:44:50)
    at File.parse (C:\Users\Scott\Documents\GitHub\test\node_modules\babel-core\lib\transformation\file\index.js:472:24)

If I remove the : string portion, it runs fine. If I also just do babel app.js bar.js --out-dir dist, I get the output:

app.js -> dist\app.js
SyntaxError: bar.js: Unexpected token (1:8)
> 1 | let name: string = 'test';
    |         ^

global babel-cli@6.2.0 (babel-core@6.2.1) babel-core@6.2.1 typecheck@3.0.2

I feel like I am missing something obvious here, but I cannot get even a basic example to run with the plugin.

phpnode commented 8 years ago

@sgodwin424 please try adding babel-plugin-syntax-flow to your list of plugins or babel-preset-react to your list of presets then try again.

sgodwin424 commented 8 years ago

Thank you. Adding babel-preset-react or babel-plugin-syntax-flow and babel-transform-flow-strip-types seems to work. I was under the impression that this plugin was standalone as the installation does not seem to suggest otherwise, but I suppose it makes sense that it would need other flow plugins.

Maybe this should be added to the installation section to avoid these issues? I have been wanting to use flowtype, but they do not support Windows yet, so when I found this plugin, I was quite thrilled to be able to use it in part. This is also the reason why I did not have the other flow plugins.

phpnode commented 8 years ago

@sgodwin424 there's a weird issue with plugin ordering in babel6 which makes it impossible to specify that this plugin depends on those others automatically. I've added a note to the README in #56