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
|
* @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 | null | undefined} input input string
|
||||||
* @param {string} context the context directory
|
* @param {string} context the context directory
|
||||||
* @returns {string[] | undefined} selected browsers
|
* @returns {string[] | undefined} selected browsers
|
||||||
*/
|
*/
|
||||||
const load = (input, context) => {
|
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
|
const config = browserslist.loadConfig({
|
||||||
// Try to apply query firstly,
|
config: configPath,
|
||||||
if (query) {
|
env
|
||||||
try {
|
});
|
||||||
return browserslist(query);
|
|
||||||
} catch (_err) {
|
return browserslist(config, { env });
|
||||||
// Nothing
|
}
|
||||||
|
|
||||||
|
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 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if a path to a config is specified then load it, else find a nearest config
|
// browserslist:query
|
||||||
const config = configPath
|
if (env) {
|
||||||
? browserslist.loadConfig({
|
return browserslist(env);
|
||||||
config: configPath,
|
}
|
||||||
env
|
|
||||||
})
|
|
||||||
: browserslist.loadConfig({ path: context, env });
|
|
||||||
|
|
||||||
if (!config) return;
|
|
||||||
return browserslist(config, { env, throwOnMissing: true });
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -16,7 +16,7 @@ const getBrowserslistTargetHandler = memoize(() =>
|
||||||
* @returns {string} default target
|
* @returns {string} default target
|
||||||
*/
|
*/
|
||||||
const getDefaultTarget = (context) => {
|
const getDefaultTarget = (context) => {
|
||||||
const browsers = getBrowserslistTargetHandler().load(null, context);
|
const browsers = getBrowserslistTargetHandler().load(undefined, context);
|
||||||
return browsers ? "browserslist" : "web";
|
return browsers ? "browserslist" : "web";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue