kentcdodds / babel-plugin-codegen

💥 Generate code at build-time
https://www.npmjs.com/package/babel-plugin-codegen
MIT License
348 stars 30 forks source link

codegen seems to be running before other plugins regardless of order? #1

Closed ghost closed 7 years ago

ghost commented 7 years ago

In short, I'm having this issue:

// @codegen

import fs from 'fs';

the above throws unexpected token import, if I remove the // @codegen line it works fine. My babel plugins are:

 ['transform-es2015-modules-commonjs', 'transform-flow-comments', 'codegen']

I'd expect that codegen would get code transformed from transform-es2015-modules-commonjs and then execute that, but it doesn't seem to be the case. Thoughts?

kentcdodds commented 7 years ago

Yeah, let me fix that...

Congrats on being issue #1! :tada:

kentcdodds commented 7 years ago

Could you try the latest version and let me know if that fixes your issue? Thanks!

ghost commented 7 years ago

thanks for the quick reply, same thing though, using 1.1.3:

~
❯ echo "// @codegen\nimport fs from 'fs';" >> foo.js

~
❯ babel --plugins=transform-es2015-modules-commonjs,codegen foo.js
SyntaxError: foo.js: Unexpected token import
undefined

~
❯ babel --plugins=transform-es2015-modules-commonjs foo.js
'use strict';

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
kentcdodds commented 7 years ago

Hmm.... Weird! Could you add a test case in the tests? That would help us figure out what's going on...

ghost commented 7 years ago

I don't see any tests? nps (not sure what that is?) throws an err saying no config is found. And side-note, yarn is telling me babel-register conflicts in dependencies vs devDependencies :)

kentcdodds commented 7 years ago

Ah yes, if you could remove babel-register from devDependencies that'd be awesome.

The tests are here. Probably best to just put it here at the bottom. Something like:

'pragma on a file can handle ES6 imports': {
  skip: true, // until we get this test passing
  code: `
    // codegen
    import fs from 'fs'
  `
}

That make sense?

kentcdodds commented 7 years ago

Oh, and you can run the tests in watch mode with npm start test.watch

linonetwo commented 6 years ago

I get

SyntaxError: Unexpected token, expected ";" (2:84)
      1 | /* @babel/template */;
    > 2 | {lastNameClose: ({ lastNameClose }) => new Date(lastNameClose),lastPervoteBucketFill: ({ lastPervoteBucketFill }) => new Date(lastPervoteBucketFill)}
        |                                                                                    ^

Trying to do so:

export const GlobalStatus = {
  ...codegen`module.exports = '{' + ['lastNameClose', 'lastPervoteBucketFill'].map(field =>
    \`\${field}: ({ \${field} }) => new Date(\${field})\`
  ).join(',') + '}'`,
};
// to
export const GlobalStatus = {
  ...{
    lastNameClose: ({ lastNameClose }) => new Date(lastNameClose),
    lastPervoteBucketFill: ({ lastPervoteBucketFill }) => new Date(lastPervoteBucketFill),
  },
};
kentcdodds commented 6 years ago

babel-plugin-codegen no longer runs your codegen-source code through babel. It must be written in code that's supported natively in the version of node you're running.