kentcdodds / kentcdodds/babel-plugin-codegen

Import lots of variables in a single line of code

Open
#10 2 comments 1 reaction 0 assignees View on GitHub
help wanted
Dominant language
TypeScript
Stars
347
Forks
27
PR merge metrics
No merged PRs in 30d

Description

- "codegen.macro": "^1.0.0",
- node -v: v9.11.1
- yarn -v: 1.3.2

I'm generating variables so that my colleague who doesn't know how to code can write simple code now.

[💖this part has no problem]
```javascript
// ./memeTemplate/variables.js
const codegen = require('codegen.macro');

// preload formulas variables, and generate more variables, so my colleague can import them easily
codegen`
const fs = require('fs');

const formulasJSON = JSON.parse(fs.readFileSync(require.resolve('../formulas.json'), 'utf8'));
const variableNames = [
...Object.keys(formulasJSON),
...Object.keys(formulasJSON).map(name => '上期' + name),
...Object.keys(formulasJSON).map(name => '上上期' + name),
];

module.exports = variableNames
.map(
variableName =>
'export var ' + variableName.replace(/[()、:()]/g, '') + ' = loader => loader.load("' + variableName + '")'
)
.join(';');
`;

```

[Here are problems]
```js
// ./templates.js
// try to automatiously import variables
codegen`
'const { ' + Object.keys(require('./memeTemplate/variables')).join(',') + '} = require("./memeTemplate/variables");'
`;

// using the automatiously imported variable
`
偿债能力分析
2017年流动资产合计为${流动资产合计}
`
```
Now it will show an error ```./src/templates.js
Module build failed: MacroError: The macro you imported from "undefined" is being executed outside the context of compilation with babel-plugin-macros. This indicates that you don't have the babel plugin "babel-plugin-macros" configured correctly. Please see the documentation for how to configure babel-plugin-macros properly```

I had tried ```console.log(
'const { ' + Object.keys(require('./memeTemplate/variables')).join(',') + '} = require("./memeTemplate/variables");'
);```, it logs the correct require statement:

```js
const { 净资产收益率,平均净资产, ...hugeAmountOfVariables } = require("./memeTemplate/variables");
```

And if I just import them statisticly:
```js
import {
销售利润率,
流动比率,
...lotsToImport,
} from './memeTemplate/variables';
```
It works correctly.

So I can't use codegen to load code generated by codegen?
Or I can't use codegen to generate import statement?

Contributor guide

Open the contributing guide

Research direction

Start with ./templates.js and ./memeTemplate/variables.js, then reproduce the MacroError using the shown codegen.macro examples and the reported Babel setup. Determine whether nested code generation or generated import statements are supported; done means the documented behavior is clear and the example either builds successfully or reports the limitation accurately.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
build-system, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.