Skip to content

Commit

Permalink
fix: allow require in comments (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
flyfishzy committed Aug 10, 2021
1 parent 85d1457 commit 609869a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
18 changes: 18 additions & 0 deletions packages/vite-plugin-commonjs/__tests__/transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ test('transform require', () => {
expect(result.code).toMatch(/import \* as .+ from \'@\/page\/login';/);
});

test('require in comments', () => {
//singleline comments
let code = ` const a=0; // the hook will be setup by require("react").`
let result = transformRequire(code, 'main.ts');
expect(result.code).toMatch(/const a=0;/);

code = `//hello
const a=0;
// the hook will be setup by require("react").`
result = transformRequire(code, 'main.ts');
expect(result.code).toMatch(/const a=0;/);

//multiline comments
code = ` /* the hook will be setup by \n require("react").\n */`
result = transformRequire(code, 'main.ts');
expect(result.code).toMatch(`/* */`);
});

test('isCommonJS', () => {
expect(isCommonJS(`module.exports = {}`)).toBeTruthy();
expect(isCommonJS(`exports = { hello: false }`)).toBeTruthy();
Expand Down
2 changes: 1 addition & 1 deletion packages/vite-plugin-commonjs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@originjs/vite-plugin-commonjs",
"version": "1.0.0-beta7",
"version": "1.0.0-beta8",
"description": "A vite plugin that support commonjs to esm in vite",
"scripts": {
"build": "tsc -p tsconfig.json"
Expand Down
20 changes: 17 additions & 3 deletions packages/vite-plugin-commonjs/src/lib.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
const commonJSRegex: RegExp = /\b(module\.exports|exports\.\w+|exports\s*=\s*)/;
const requireRegex: RegExp = /_{0,2}require\s*\(\s*(["'].*["'])\s*\)/g;
const requireRegex: RegExp = /_{0,2}require\s*\(\s*(["'].*?["'])\s*\)/g;
const IMPORT_STRING_PREFIX: String = "__require_for_vite";
const multilineCommentsRegex = /\/\*(.|[\r\n])*?\*\//gm
const singleCommentsRegex = /\/\/.*/g

export interface TransformRequireResult {
code: string;
replaced: boolean;
}

export function transformRequire(code: string, id: string):TransformRequireResult {
export function transformRequire(code: string, id: string): TransformRequireResult {
let replaced = false;
// skip if has no require
if (!/require/.test(code)) {
return {
replaced,
code
}
}
// empty multiline comments
code = code.replace(multilineCommentsRegex, '/* */');
// remove singleline comments
code = code.replace(singleCommentsRegex, ' ');

const requireMatches = code.matchAll(requireRegex);
let importsString = "";
let packageName = "";
let replaced = false;
for (let item of requireMatches) {
if (!isString(item[1])) {
console.warn(`Not supported dynamic import, file:${id}`);
Expand Down

0 comments on commit 609869a

Please sign in to comment.