Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ghost opened this issue Aug 18, 2017 · 9 comments
Closed

Comments

@ghost
Copy link

ghost commented Aug 18, 2017

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
Copy link
Owner

Yeah, let me fix that...

Congrats on being issue #1! 🎉

kentcdodds pushed a commit to kentcdodds/babel-plugin-preval that referenced this issue Aug 18, 2017
@kentcdodds
Copy link
Owner

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

@ghost
Copy link
Author

ghost commented Aug 18, 2017

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
Copy link
Owner

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

@ghost
Copy link
Author

ghost commented Aug 18, 2017

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
Copy link
Owner

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
Copy link
Owner

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

@linonetwo
Copy link

linonetwo commented Jun 29, 2018

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
Copy link
Owner

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.

markmabery added a commit to markmabery/babel-plugin-preval that referenced this issue Mar 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants