Skip to content

Commit

Permalink
Allow spread (...) to be used with "require"
Browse files Browse the repository at this point in the history
Fixes issue mentioned here:
originjs#26 (comment)
  • Loading branch information
MartinKolbAtWork committed Jun 26, 2023
1 parent 568f5b7 commit 533feeb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/vite-plugin-commonjs/__tests__/transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ test('require as part of function name', () => {
expect(result.code).toMatch(`myrequire("react");`);
});

test('require in object spread', () => {
let code = `const messagesDE = {...require('./i18n/de')}`
let result = transformRequire(code, 'main.ts');
expect(result.code).not.toMatch(code);
});

test('require in comments', () => {
//singleline comments
let code = ` const a=0; // the hook will be setup by require("react").`
Expand Down
2 changes: 1 addition & 1 deletion packages/vite-plugin-commonjs/src/lib.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const commonJSRegex: RegExp = /\b(module\.exports|exports\.\w+|exports\s*=\s*|exports\s*\[.*\]\s*=\s*)/;
const requireRegex: RegExp = /(?<!\.)\b_{0,2}require\s*\(\s*(["'`].*?["'`])\s*\)/g;
const requireRegex: RegExp = /(?<![^\.]\.)\b_{0,2}require\s*\(\s*(["'`].*?["'`])\s*\)/g;
const IMPORT_STRING_PREFIX: String = "__require_for_vite";
const multilineCommentsRegex = /\/\*(.|[\r\n])*?\*\//gm
const singleCommentsRegex = /([^\:])\/\/.*/g
Expand Down

0 comments on commit 533feeb

Please sign in to comment.