@@ -2,6 +2,8 @@ import fs from 'fs';
22import path from 'path' ;
33
44import stripJSONComments from 'strip-json-comments' ;
5+ import isUTF8 from 'is-utf8' ;
6+ import stripBom from 'strip-bom' ;
57
68import { getNormalizedConfig } from '../configLoader' ;
79
@@ -17,7 +19,7 @@ export default getConfigContent;
1719function readConfigContent ( configPath ) {
1820 const parsedPath = path . parse ( configPath )
1921 const isRcFile = parsedPath . ext !== '.js' && parsedPath . ext !== '.json' ;
20- const jsonString = fs . readFileSync ( configPath , 'utf-8' ) ;
22+ const jsonString = readConfigFileContent ( configPath ) ;
2123 const parse = isRcFile ?
2224 ( contents ) => JSON . parse ( stripJSONComments ( contents ) ) :
2325 ( contents ) => JSON . parse ( contents ) ;
@@ -61,3 +63,20 @@ function getConfigContent (configPath, baseDirectory) {
6163 const content = readConfigContent ( resolvedPath ) ;
6264 return getNormalizedConfig ( configBasename , content ) ;
6365} ;
66+
67+ /**
68+ * Read proper content from config file.
69+ * If the chartset of the config file is not utf-8, one error will be thrown.
70+ * @param {String } configPath
71+ * @return {String }
72+ */
73+ function readConfigFileContent ( configPath ) {
74+
75+ let rawBufContent = fs . readFileSync ( configPath ) ;
76+
77+ if ( ! isUTF8 ( rawBufContent ) ) {
78+ throw new Error ( `The config file at "${ configPath } " contains invalid charset, expect utf8` ) ;
79+ }
80+
81+ return stripBom ( rawBufContent . toString ( "utf8" ) ) ;
82+ }
0 commit comments