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

Import lots of variables in a single line of code #10

Open
linonetwo opened this issue Apr 25, 2018 · 2 comments
Open

Import lots of variables in a single line of code #10

linonetwo opened this issue Apr 25, 2018 · 2 comments

Comments

@linonetwo
Copy link

  • "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]

// ./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]

// ./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:

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

And if I just import them statisticly:

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?

@linonetwo
Copy link
Author

Will there be a solution to import lots of variables in a single line of codegen?

@linonetwo
Copy link
Author

linonetwo commented Apr 28, 2018

I think this is related to kentcdodds/babel-plugin-macros#48.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants