bundlewatch
Version:
Keep watch of your bundle size
113 lines (89 loc) • 3.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _readPkgUp = require('read-pkg-up');
var _readPkgUp2 = _interopRequireDefault(_readPkgUp);
var _ValidationError = require('../app/errors/ValidationError');
var _ValidationError2 = _interopRequireDefault(_ValidationError);
var _logger = require('../logger');
var _logger2 = _interopRequireDefault(_logger);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const readConfigFile = configFilePath => {
try {
return _fs2.default.readFileSync(configFilePath, 'utf8');
} catch (error) {
_logger2.default.error(`Exception while trying to read JSON config file`, error);
return null;
}
};
const getConfigFileJson = configFilePath => {
const configFileContents = readConfigFile(configFilePath);
if (!configFileContents) {
throw new _ValidationError2.default(`Could not find JSON config file: ${configFilePath}}`);
}
try {
return JSON.parse(configFileContents);
} catch (error) {
_logger2.default.error(`Exception while parsing JSON config`, error);
throw new _ValidationError2.default(`Could not parse JSON config file: ${configFilePath}`);
}
};
const getConfigFileJS = configFilePath => {
const projectDir = _path2.default.resolve(_fs2.default.realpathSync(process.cwd()));
const fullPath = `${projectDir}/${configFilePath}`;
try {
return require(fullPath); // eslint-disable-line global-require
} catch (error) {
_logger2.default.error(`Exception while loading JS config`, error);
throw new _ValidationError2.default(`Exception while loading JS config: ${fullPath}`);
}
};
const getConfigFileContents = configFilePath => {
if (configFilePath.endsWith('.js')) {
return getConfigFileJS(configFilePath);
}
return getConfigFileJson(configFilePath);
};
const determineConfig = cliOptions => {
const pkgJson = _readPkgUp2.default.sync().pkg;
let pkgJsonbundlewatch = pkgJson.bundlewatch;
if (cliOptions.args && cliOptions.args.length > 0) {
if (pkgJsonbundlewatch) {
_logger2.default.warn(`CLI files supplied, config in package.json will be ignored`);
}
if (cliOptions.configFilePath) {
_logger2.default.warn(`CLI files supplied, configFilePath will be ignored (this must be used on its own)`);
}
const files = cliOptions.args.map(filePathGlob => {
return {
path: filePathGlob,
maxSize: cliOptions.maxSize
};
});
return {
files,
defaultCompression: cliOptions.compression || 'gzip'
};
}
if (cliOptions.config) {
if (pkgJsonbundlewatch) {
_logger2.default.warn(`configFilePath supplied, config in package.json will be ignored`);
}
return getConfigFileContents(cliOptions.config);
}
if (pkgJsonbundlewatch) {
if (Array.isArray(pkgJsonbundlewatch)) {
return {
files: pkgJsonbundlewatch
};
}
return pkgJsonbundlewatch;
}
return {};
};
exports.default = determineConfig;