@@ -5,6 +5,7 @@ const os = require('os');
55const minimist = require ( 'minimist' ) ;
66
77const inlineCss = require ( './inline-css' ) ;
8+ const fileHelper = require ( './file-helper' ) ;
89const getMeta = require ( './get-meta' ) ;
910const config = require ( './config' ) ;
1011const version = require ( './version' ) ;
@@ -45,23 +46,40 @@ Version: \x1b[36m${newversion}\x1b[0m`);
4546 }
4647}
4748
49+ function getExistingFilePath ( filePath , normalizedFilePath , parentPath ) {
50+ if ( fs . existsSync ( filePath ) ) {
51+ return filePath ;
52+ }
53+
54+ if ( fs . existsSync ( normalizedFilePath ) ) {
55+ return normalizedFilePath ;
56+ }
57+
58+ throw new Error ( `${ parentPath } tries to import unreachable file ${ filePath } ` ) ;
59+ }
60+
4861function buildTree ( filePath , parentPath ) {
49- // Get file name
62+ // Get full file name
5063 if ( parentPath ) {
5164 filePath = path . join ( parentPath , '..' , filePath ) ;
5265 }
5366
54- if ( visited . includes ( filePath ) ) {
67+ const normalizedFilePath = fileHelper . normalizeFileName ( filePath ) ;
68+
69+ if ( visited . includes ( filePath ) || visited . includes ( normalizedFilePath ) ) {
70+ // file processed
5571 return ;
5672 }
5773
58- // Put file name into visited
59- visited . push ( filePath ) ;
74+ filePath = getExistingFilePath ( filePath , normalizedFilePath , parentPath ) ;
6075
6176 // Open file
62- const file = fs . readFileSync ( filePath ) . toString ( ) ;
6377 const importRegex = / ^ [ \t \r ] * i m p o r t .+ [ ' " ] ; $ / gm;
6478 const imports = [ ] ;
79+ const file = fs . readFileSync ( filePath ) . toString ( ) ;
80+
81+ // Put file name into visited
82+ visited . push ( filePath ) ;
6583
6684 // Get all imports
6785 let matches ;
@@ -75,7 +93,7 @@ function buildTree(filePath, parentPath) {
7593 } ) ;
7694
7795 if ( / \. c s s $ / g. test ( filePath ) ) {
78- css . push ( { file, filePath : filePath . split ( '\\' ) . join ( '/' ) } ) ;
96+ css . push ( { file, filePath : filePath . split ( '\\' ) . join ( '/' ) } ) ;
7997 } else {
8098 files . push ( {
8199 file : file
@@ -99,11 +117,13 @@ function getOutFile(addFilePathComments) {
99117 console . log ( '\x1b[33m%s\x1b[0m' , 'Concat js files' ) ;
100118
101119 files . forEach ( file => {
102- console . log ( `${ file . filePath . replace ( / ^ \. \/ / g, '' ) } ` ) ;
120+ const filePath = file . filePath . replace ( / ^ \. \/ / g, '' ) ;
121+
122+ console . log ( `${ filePath } ` ) ;
103123 out += os . EOL + os . EOL ;
104124
105125 if ( addFilePathComments ) {
106- out += `// ${ file . filePath . replace ( / ^ \. \/ / g , '' ) } ${ os . EOL } ` ;
126+ out += `// ${ filePath } ${ os . EOL } ` ;
107127 }
108128
109129 out += file . file ;
0 commit comments