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

Error using transform-define #1292

Closed
sedubois opened this issue Feb 26, 2017 · 4 comments
Closed

Error using transform-define #1292

sedubois opened this issue Feb 26, 2017 · 4 comments

Comments

@sedubois
Copy link
Contributor

sedubois commented Feb 26, 2017

An example of using transform-define for universal configuration is provided, but I have a weird error, which I narrowed down below (run it with babel-node):

// package.json
{
  "dependencies": {
    "babel-plugin-transform-define": "^1.2.0"
  },
  "babel": {
    "plugins": [
      [
        "transform-define",
        "./config.js"
      ]
    ]
  }
}
// config.js
module.exports = { KEY: 'value' };
// index.js
const config = require('./config');
console.log(config.KEY);

Somehow next manages to load my app based on the above principle just fine, but the console.log throws when I run the server myself (I'm trying to add integration tests):

TypeError: /Users/sdubois/Documents/relate-debug/index.js: Property property of MemberExpression expected node to be of a type ["Identifier"] but instead got "StringLiteral"
    at validate (/usr/local/lib/node_modules/babel-cli/node_modules/babel-types/lib/definitions/index.js:109:13)
    at Object.validate (/usr/local/lib/node_modules/babel-cli/node_modules/babel-types/lib/definitions/core.js:412:50)
    at Object.validate (/usr/local/lib/node_modules/babel-cli/node_modules/babel-types/lib/index.js:505:9)
    at NodePath._replaceWith (/usr/local/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/path/replacement.js:176:7)
    at NodePath.replaceWith (/usr/local/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/path/replacement.js:160:8)
    at replaceAndEvaluateNode (/Users/sdubois/Documents/relate-debug/node_modules/babel-plugin-transform-define/lib/index.js:111:12)
    at processNode (/Users/sdubois/Documents/relate-debug/node_modules/babel-plugin-transform-define/lib/index.js:148:5)
    at PluginPass.Identifier (/Users/sdubois/Documents/relate-debug/node_modules/babel-plugin-transform-define/lib/index.js:23:9)
    at newFn (/usr/local/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/visitors.js:276:21)
    at NodePath._call (/usr/local/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/path/context.js:76:18)
@sedubois
Copy link
Contributor Author

Strangely enough, it works when I change to console.log(config['KEY'])).

@timneutkens
Copy link
Member

// index.js = your custom server?
If you run it with babel-node it will transpile all occurences of KEY which might cause what you're seeing?

@sedubois
Copy link
Contributor Author

OK it's because my custom server is normally simply started with node server, which doesn't do any babel stuff, but in my jest tests, the require('../../server') I'm using there is transformed by babel.

I'll try to adapt the babel config not to load that plugin in test env.

It's not trivial to get all the universal config right...

@sedubois
Copy link
Contributor Author

@tim how can I set up my babel config, so that it doesn't transpile a certain folder (server/)?

@lock lock bot locked as resolved and limited conversation to collaborators May 12, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants