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:
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
Reproduction
Unit tests that check this would be:
Fix
To fix this issue, the RegEx that searches the require function should be changed to be like this:
Find the tests and the proposed fix in this pull-request: https://github.com/originjs/vite-plugins/pull/26