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

Please, don't use dynamic require imports #525

Open
Razzwan opened this issue Nov 30, 2020 · 3 comments
Open

Please, don't use dynamic require imports #525

Razzwan opened this issue Nov 30, 2020 · 3 comments

Comments

@Razzwan
Copy link

Razzwan commented Nov 30, 2020

Instead of such syntax:

const partOfLibName = 'part1';
const myLib = require(partOfLibName + 'LibName');

Please, use such one:

cosnt myLib1 = require('part1LibName');
cosnt myLib2 = require('part2LibName');

// and later
let res;
if (/* some condition here */) {
  res = myLib1.method();
} else {
    res = myLib2.method();
}

Or with dynamic imports:

cosnt myLib1 = import('part1LibName');
cosnt myLib2 = import('part2LibName');

// and later
let res;
if (/* some condition here */) {
  myLib1.then(res => {
    // user res here
  });
} else {
  myLib2.then(res => {
    // user res here
  });
}
@roienatan
Copy link

roienatan commented Nov 30, 2020

Also you can use the new JavaScript 2020 native dynamic import with await, something like this:

let module = await import('/modules/my-module.js');

This can be written inside an if statement.

@dOrgJelli
Copy link
Member

Hey @Razzwan, I'm assuming you're referring to these two usages:

contractJson = require(path.resolve(`${customAbisLocation}/${standAlone.name}.json`))

and
let abi = require(`${abiPath}`)
if (abi.rootVersion) {
const abiName = abiPath.substring(abiPath.lastIndexOf('/') + 1)
const abiFolder = path.dirname(abiPath)
const rootPath = path.join(
abiFolder, `../${abi.rootVersion}/${abiName}`
)
abi = require(`./${rootPath}`)

Both of these source files come from this repo: https://github.com/daostack/migration

They're copied into the DAOcreator here: https://github.com/dOrgTech/DAOcreator/blob/master/packages/lib/scripts/prepArc.js

The reason this manual copying is done is to:

  • reducing ABI bloat
  • removing needless dependencies that cause build problems

If you'd like this code changed, I'd suggest making an issue here: https://github.com/daostack/migration

@orenyodfat
Copy link

ok. will try to update migration daostack/migration#373

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

4 participants