Skip to content

Commit 609869a

Browse files
committed
fix: allow require in comments (#6)
1 parent 85d1457 commit 609869a

3 files changed

Lines changed: 36 additions & 4 deletions

File tree

packages/vite-plugin-commonjs/__tests__/transform.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ test('transform require', () => {
2828
expect(result.code).toMatch(/import \* as .+ from \'@\/page\/login';/);
2929
});
3030

31+
test('require in comments', () => {
32+
//singleline comments
33+
let code = ` const a=0; // the hook will be setup by require("react").`
34+
let result = transformRequire(code, 'main.ts');
35+
expect(result.code).toMatch(/const a=0;/);
36+
37+
code = `//hello
38+
const a=0;
39+
// the hook will be setup by require("react").`
40+
result = transformRequire(code, 'main.ts');
41+
expect(result.code).toMatch(/const a=0;/);
42+
43+
//multiline comments
44+
code = ` /* the hook will be setup by \n require("react").\n */`
45+
result = transformRequire(code, 'main.ts');
46+
expect(result.code).toMatch(`/* */`);
47+
});
48+
3149
test('isCommonJS', () => {
3250
expect(isCommonJS(`module.exports = {}`)).toBeTruthy();
3351
expect(isCommonJS(`exports = { hello: false }`)).toBeTruthy();

packages/vite-plugin-commonjs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@originjs/vite-plugin-commonjs",
3-
"version": "1.0.0-beta7",
3+
"version": "1.0.0-beta8",
44
"description": "A vite plugin that support commonjs to esm in vite",
55
"scripts": {
66
"build": "tsc -p tsconfig.json"

packages/vite-plugin-commonjs/src/lib.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
const commonJSRegex: RegExp = /\b(module\.exports|exports\.\w+|exports\s*=\s*)/;
2-
const requireRegex: RegExp = /_{0,2}require\s*\(\s*(["'].*["'])\s*\)/g;
2+
const requireRegex: RegExp = /_{0,2}require\s*\(\s*(["'].*?["'])\s*\)/g;
33
const IMPORT_STRING_PREFIX: String = "__require_for_vite";
4+
const multilineCommentsRegex = /\/\*(.|[\r\n])*?\*\//gm
5+
const singleCommentsRegex = /\/\/.*/g
46

57
export interface TransformRequireResult {
68
code: string;
79
replaced: boolean;
810
}
911

10-
export function transformRequire(code: string, id: string):TransformRequireResult {
12+
export function transformRequire(code: string, id: string): TransformRequireResult {
13+
let replaced = false;
14+
// skip if has no require
15+
if (!/require/.test(code)) {
16+
return {
17+
replaced,
18+
code
19+
}
20+
}
21+
// empty multiline comments
22+
code = code.replace(multilineCommentsRegex, '/* */');
23+
// remove singleline comments
24+
code = code.replace(singleCommentsRegex, ' ');
25+
1126
const requireMatches = code.matchAll(requireRegex);
1227
let importsString = "";
1328
let packageName = "";
14-
let replaced = false;
1529
for (let item of requireMatches) {
1630
if (!isString(item[1])) {
1731
console.warn(`Not supported dynamic import, file:${id}`);

0 commit comments

Comments
 (0)