File tree Expand file tree Collapse file tree
packages/vite-plugin-commonjs Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -28,6 +28,24 @@ test('transform require', () => {
2828 expect ( result . code ) . toMatch ( / i m p o r t \* a s .+ f r o m \' @ \/ p a g e \/ l o g i n ' ; / ) ;
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 ( / c o n s t 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 ( / c o n s t 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+
3149test ( 'isCommonJS' , ( ) => {
3250 expect ( isCommonJS ( `module.exports = {}` ) ) . toBeTruthy ( ) ;
3351 expect ( isCommonJS ( `exports = { hello: false }` ) ) . toBeTruthy ( ) ;
Original file line number Diff line number Diff line change 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"
Original file line number Diff line number Diff line change 11const commonJSRegex : RegExp = / \b ( m o d u l e \. e x p o r t s | e x p o r t s \. \w + | e x p o r t s \s * = \s * ) / ;
2- const requireRegex : RegExp = / _ { 0 , 2 } r e q u i r e \s * \( \s * ( [ " ' ] .* [ " ' ] ) \s * \) / g;
2+ const requireRegex : RegExp = / _ { 0 , 2 } r e q u i r e \s * \( \s * ( [ " ' ] .* ? [ " ' ] ) \s * \) / g;
33const IMPORT_STRING_PREFIX : String = "__require_for_vite" ;
4+ const multilineCommentsRegex = / \/ \* ( .| [ \r \n ] ) * ?\* \/ / gm
5+ const singleCommentsRegex = / \/ \/ .* / g
46
57export 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 ( ! / r e q u i r e / . 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 } ` ) ;
You can’t perform that action at this time.
0 commit comments