mirror of https://github.com/webpack/webpack.git
refactor: code
This commit is contained in:
parent
658853a3bc
commit
ac7f48fdc5
|
@ -22,65 +22,45 @@ const inputRx = /^(?:((?:[A-Z]:)?[/\\].*?))?(?::(.+?))?$/i;
|
|||
* @property {string=} query
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {string | null | undefined} input input string
|
||||
* @param {string} context the context directory
|
||||
* @returns {BrowserslistHandlerConfig} config
|
||||
*/
|
||||
const parse = (input, context) => {
|
||||
// browserslist
|
||||
if (!input) {
|
||||
return {};
|
||||
}
|
||||
|
||||
// browserslist:path-to-config
|
||||
// browserslist:path-to-config:env
|
||||
if (path.isAbsolute(input)) {
|
||||
const [, configPath, env] = inputRx.exec(input) || [];
|
||||
|
||||
return { configPath, env };
|
||||
}
|
||||
|
||||
const config = browserslist.findConfig(context);
|
||||
|
||||
// browserslist:query
|
||||
// browserslist:env
|
||||
// When we have a configuration file and value after `:` it can be env or query
|
||||
if (config) {
|
||||
return { env: input, query: input };
|
||||
}
|
||||
|
||||
return { query: input };
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string | null | undefined} input input string
|
||||
* @param {string} context the context directory
|
||||
* @returns {string[] | undefined} selected browsers
|
||||
*/
|
||||
const load = (input, context) => {
|
||||
const { configPath, env, query } = parse(input, context);
|
||||
// browserslist:path-to-config
|
||||
// browserslist:path-to-config:env
|
||||
if (input && path.isAbsolute(input)) {
|
||||
const [, configPath, env] = inputRx.exec(input) || [];
|
||||
|
||||
// TODO refactor me after resolve - https://github.com/browserslist/browserslist/issues/906
|
||||
// Try to apply query firstly,
|
||||
if (query) {
|
||||
try {
|
||||
return browserslist(query);
|
||||
} catch (_err) {
|
||||
// Nothing
|
||||
}
|
||||
}
|
||||
|
||||
// if a path to a config is specified then load it, else find a nearest config
|
||||
const config = configPath
|
||||
? browserslist.loadConfig({
|
||||
const config = browserslist.loadConfig({
|
||||
config: configPath,
|
||||
env
|
||||
})
|
||||
: browserslist.loadConfig({ path: context, env });
|
||||
});
|
||||
|
||||
if (!config) return;
|
||||
return browserslist(config, { env, throwOnMissing: true });
|
||||
return browserslist(config, { env });
|
||||
}
|
||||
|
||||
const env = input || undefined;
|
||||
|
||||
// browserslist:env
|
||||
const config = browserslist.loadConfig({
|
||||
path: context,
|
||||
env
|
||||
});
|
||||
|
||||
if (config) {
|
||||
const result = browserslist(config, { env });
|
||||
|
||||
if (result.length > 0) {
|
||||
return browserslist(config, { env });
|
||||
}
|
||||
}
|
||||
|
||||
// browserslist:query
|
||||
if (env) {
|
||||
return browserslist(env);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -16,7 +16,7 @@ const getBrowserslistTargetHandler = memoize(() =>
|
|||
* @returns {string} default target
|
||||
*/
|
||||
const getDefaultTarget = (context) => {
|
||||
const browsers = getBrowserslistTargetHandler().load(null, context);
|
||||
const browsers = getBrowserslistTargetHandler().load(undefined, context);
|
||||
return browsers ? "browserslist" : "web";
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue