kentcdodds / babel-plugin-macros

🎣 Allows you to build simple compile-time libraries
https://npm.im/babel-plugin-macros
MIT License
2.62k stars 135 forks source link

ReferenceError: `macro name` is not defined #98

Closed Albert-Gao closed 5 years ago

Albert-Gao commented 5 years ago
  "dependencies": {
    "react": "^16.7.0",
    "react-dom": "^16.7.0",
    "react-scripts": "2.1.2"
  },
  1. create-react-app macro

  2. Open this macro project.

  3. Create a file convert.macro.js

    
    // @ts-ignore
    const { createMacro } = require("babel-plugin-macros");

module.exports = createMacro(convert);

function convert({ references, state, babel }) { console.log(references); }


4. Add something to `app.js`
```javascript
import convert from "./convert.macro";

convert();
  1. yarn start

  2. In the browser, see the error:

×
ReferenceError: convert is not defined
Module../src/App.js
src/App.js:6
  3 | import "./App.css";
  4 | import convert from "./convert.macro";
  5 | 
> 6 | convert();
  7 | 
  8 | class App extends Component {
  9 |   render() {
View compiled
__webpack_require__
/Users/albertgao/codes/temp/tempm/webpack/bootstrap:782
  779 | };
  780 | 
  781 | // Execute the module function
> 782 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
      | ^  783 | 
  784 | // Flag the module as loaded
  785 | module.l = true;
kentcdodds commented 5 years ago

Hi @Albert-Gao You need to make sure that your macro manipulates the AST in a way that produces valid code. Check out the resources in the author docs. Good luck!

bohdanbirdie commented 5 years ago

@kentcdodds this might be a bug I'm able to do import { macro } from "./somename.macro" But import macro from "./somename.macro" doesn't work

Even in my macros file, I have export default createMacro(myMacro);