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

Commonjs: Transform of “require” must only be applied to global function named exactly “require” #25

Open
MartinKolbAtWork opened this issue Feb 16, 2022 · 0 comments
Labels
good first issue Good for newcomers has pr

Comments

@MartinKolbAtWork
Copy link
Contributor

MartinKolbAtWork commented Feb 16, 2022

The commonjs plugin should transform only calls to a global function called “require”. Calls to functions containing the term ”require” (e.g. “myrequire”) and calling “require” on an object (e.g. “myObj.require”) must not be transformed.

Versions

  • originjs: 1.0.3

Reproduction

Unit tests that check this would be:

test('require function on object', () => {
  let code = `myObj.require("react");`
  let result = transformRequire(code, 'main.ts');
  expect(result.code).toMatch(`myObj.require("react");`);
});

test('require as part of function name', () => {
  let code = `myrequire("react");`
  let result = transformRequire(code, 'main.ts');
  expect(result.code).toMatch(`myrequire("react");`);
});

Fix

To fix this issue, the RegEx that searches the require function should be changed to be like this:

const requireRegex: RegExp = /(?<!\.)\b_{0,2}require\s*\(\s*(["'].*?["'])\s*\)/g;

Find the tests and the proposed fix in this pull-request: #26

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers has pr
Projects
None yet
Development

No branches or pull requests

2 participants