Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config-chain.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.buildPresetChain = buildPresetChain;
  6. exports.buildRootChain = buildRootChain;
  7. exports.buildPresetChainWalker = void 0;
  8. function _path() {
  9. const data = require("path");
  10. _path = function () {
  11. return data;
  12. };
  13. return data;
  14. }
  15. function _debug() {
  16. const data = require("debug");
  17. _debug = function () {
  18. return data;
  19. };
  20. return data;
  21. }
  22. var _options = require("./validation/options");
  23. var _patternToRegex = require("./pattern-to-regex");
  24. var _printer = require("./printer");
  25. var _files = require("./files");
  26. var _caching = require("./caching");
  27. var _configDescriptors = require("./config-descriptors");
  28. const debug = _debug()("babel:config:config-chain");
  29. function* buildPresetChain(arg, context) {
  30. const chain = yield* buildPresetChainWalker(arg, context);
  31. if (!chain) return null;
  32. return {
  33. plugins: dedupDescriptors(chain.plugins),
  34. presets: dedupDescriptors(chain.presets),
  35. options: chain.options.map(o => normalizeOptions(o)),
  36. files: new Set()
  37. };
  38. }
  39. const buildPresetChainWalker = makeChainWalker({
  40. root: preset => loadPresetDescriptors(preset),
  41. env: (preset, envName) => loadPresetEnvDescriptors(preset)(envName),
  42. overrides: (preset, index) => loadPresetOverridesDescriptors(preset)(index),
  43. overridesEnv: (preset, index, envName) => loadPresetOverridesEnvDescriptors(preset)(index)(envName),
  44. createLogger: () => () => {}
  45. });
  46. exports.buildPresetChainWalker = buildPresetChainWalker;
  47. const loadPresetDescriptors = (0, _caching.makeWeakCacheSync)(preset => buildRootDescriptors(preset, preset.alias, _configDescriptors.createUncachedDescriptors));
  48. const loadPresetEnvDescriptors = (0, _caching.makeWeakCacheSync)(preset => (0, _caching.makeStrongCacheSync)(envName => buildEnvDescriptors(preset, preset.alias, _configDescriptors.createUncachedDescriptors, envName)));
  49. const loadPresetOverridesDescriptors = (0, _caching.makeWeakCacheSync)(preset => (0, _caching.makeStrongCacheSync)(index => buildOverrideDescriptors(preset, preset.alias, _configDescriptors.createUncachedDescriptors, index)));
  50. const loadPresetOverridesEnvDescriptors = (0, _caching.makeWeakCacheSync)(preset => (0, _caching.makeStrongCacheSync)(index => (0, _caching.makeStrongCacheSync)(envName => buildOverrideEnvDescriptors(preset, preset.alias, _configDescriptors.createUncachedDescriptors, index, envName))));
  51. function* buildRootChain(opts, context) {
  52. let configReport, babelRcReport;
  53. const programmaticLogger = new _printer.ConfigPrinter();
  54. const programmaticChain = yield* loadProgrammaticChain({
  55. options: opts,
  56. dirname: context.cwd
  57. }, context, undefined, programmaticLogger);
  58. if (!programmaticChain) return null;
  59. const programmaticReport = yield* programmaticLogger.output();
  60. let configFile;
  61. if (typeof opts.configFile === "string") {
  62. configFile = yield* (0, _files.loadConfig)(opts.configFile, context.cwd, context.envName, context.caller);
  63. } else if (opts.configFile !== false) {
  64. configFile = yield* (0, _files.findRootConfig)(context.root, context.envName, context.caller);
  65. }
  66. let {
  67. babelrc,
  68. babelrcRoots
  69. } = opts;
  70. let babelrcRootsDirectory = context.cwd;
  71. const configFileChain = emptyChain();
  72. const configFileLogger = new _printer.ConfigPrinter();
  73. if (configFile) {
  74. const validatedFile = validateConfigFile(configFile);
  75. const result = yield* loadFileChain(validatedFile, context, undefined, configFileLogger);
  76. if (!result) return null;
  77. configReport = yield* configFileLogger.output();
  78. if (babelrc === undefined) {
  79. babelrc = validatedFile.options.babelrc;
  80. }
  81. if (babelrcRoots === undefined) {
  82. babelrcRootsDirectory = validatedFile.dirname;
  83. babelrcRoots = validatedFile.options.babelrcRoots;
  84. }
  85. mergeChain(configFileChain, result);
  86. }
  87. let ignoreFile, babelrcFile;
  88. let isIgnored = false;
  89. const fileChain = emptyChain();
  90. if ((babelrc === true || babelrc === undefined) && typeof context.filename === "string") {
  91. const pkgData = yield* (0, _files.findPackageData)(context.filename);
  92. if (pkgData && babelrcLoadEnabled(context, pkgData, babelrcRoots, babelrcRootsDirectory)) {
  93. ({
  94. ignore: ignoreFile,
  95. config: babelrcFile
  96. } = yield* (0, _files.findRelativeConfig)(pkgData, context.envName, context.caller));
  97. if (ignoreFile) {
  98. fileChain.files.add(ignoreFile.filepath);
  99. }
  100. if (ignoreFile && shouldIgnore(context, ignoreFile.ignore, null, ignoreFile.dirname)) {
  101. isIgnored = true;
  102. }
  103. if (babelrcFile && !isIgnored) {
  104. const validatedFile = validateBabelrcFile(babelrcFile);
  105. const babelrcLogger = new _printer.ConfigPrinter();
  106. const result = yield* loadFileChain(validatedFile, context, undefined, babelrcLogger);
  107. if (!result) {
  108. isIgnored = true;
  109. } else {
  110. babelRcReport = yield* babelrcLogger.output();
  111. mergeChain(fileChain, result);
  112. }
  113. }
  114. if (babelrcFile && isIgnored) {
  115. fileChain.files.add(babelrcFile.filepath);
  116. }
  117. }
  118. }
  119. if (context.showConfig) {
  120. console.log(`Babel configs on "${context.filename}" (ascending priority):\n` + [configReport, babelRcReport, programmaticReport].filter(x => !!x).join("\n\n") + "\n-----End Babel configs-----");
  121. }
  122. const chain = mergeChain(mergeChain(mergeChain(emptyChain(), configFileChain), fileChain), programmaticChain);
  123. return {
  124. plugins: isIgnored ? [] : dedupDescriptors(chain.plugins),
  125. presets: isIgnored ? [] : dedupDescriptors(chain.presets),
  126. options: isIgnored ? [] : chain.options.map(o => normalizeOptions(o)),
  127. fileHandling: isIgnored ? "ignored" : "transpile",
  128. ignore: ignoreFile || undefined,
  129. babelrc: babelrcFile || undefined,
  130. config: configFile || undefined,
  131. files: chain.files
  132. };
  133. }
  134. function babelrcLoadEnabled(context, pkgData, babelrcRoots, babelrcRootsDirectory) {
  135. if (typeof babelrcRoots === "boolean") return babelrcRoots;
  136. const absoluteRoot = context.root;
  137. if (babelrcRoots === undefined) {
  138. return pkgData.directories.indexOf(absoluteRoot) !== -1;
  139. }
  140. let babelrcPatterns = babelrcRoots;
  141. if (!Array.isArray(babelrcPatterns)) {
  142. babelrcPatterns = [babelrcPatterns];
  143. }
  144. babelrcPatterns = babelrcPatterns.map(pat => {
  145. return typeof pat === "string" ? _path().resolve(babelrcRootsDirectory, pat) : pat;
  146. });
  147. if (babelrcPatterns.length === 1 && babelrcPatterns[0] === absoluteRoot) {
  148. return pkgData.directories.indexOf(absoluteRoot) !== -1;
  149. }
  150. return babelrcPatterns.some(pat => {
  151. if (typeof pat === "string") {
  152. pat = (0, _patternToRegex.default)(pat, babelrcRootsDirectory);
  153. }
  154. return pkgData.directories.some(directory => {
  155. return matchPattern(pat, babelrcRootsDirectory, directory, context);
  156. });
  157. });
  158. }
  159. const validateConfigFile = (0, _caching.makeWeakCacheSync)(file => ({
  160. filepath: file.filepath,
  161. dirname: file.dirname,
  162. options: (0, _options.validate)("configfile", file.options)
  163. }));
  164. const validateBabelrcFile = (0, _caching.makeWeakCacheSync)(file => ({
  165. filepath: file.filepath,
  166. dirname: file.dirname,
  167. options: (0, _options.validate)("babelrcfile", file.options)
  168. }));
  169. const validateExtendFile = (0, _caching.makeWeakCacheSync)(file => ({
  170. filepath: file.filepath,
  171. dirname: file.dirname,
  172. options: (0, _options.validate)("extendsfile", file.options)
  173. }));
  174. const loadProgrammaticChain = makeChainWalker({
  175. root: input => buildRootDescriptors(input, "base", _configDescriptors.createCachedDescriptors),
  176. env: (input, envName) => buildEnvDescriptors(input, "base", _configDescriptors.createCachedDescriptors, envName),
  177. overrides: (input, index) => buildOverrideDescriptors(input, "base", _configDescriptors.createCachedDescriptors, index),
  178. overridesEnv: (input, index, envName) => buildOverrideEnvDescriptors(input, "base", _configDescriptors.createCachedDescriptors, index, envName),
  179. createLogger: (input, context, baseLogger) => buildProgrammaticLogger(input, context, baseLogger)
  180. });
  181. const loadFileChainWalker = makeChainWalker({
  182. root: file => loadFileDescriptors(file),
  183. env: (file, envName) => loadFileEnvDescriptors(file)(envName),
  184. overrides: (file, index) => loadFileOverridesDescriptors(file)(index),
  185. overridesEnv: (file, index, envName) => loadFileOverridesEnvDescriptors(file)(index)(envName),
  186. createLogger: (file, context, baseLogger) => buildFileLogger(file.filepath, context, baseLogger)
  187. });
  188. function* loadFileChain(input, context, files, baseLogger) {
  189. const chain = yield* loadFileChainWalker(input, context, files, baseLogger);
  190. if (chain) {
  191. chain.files.add(input.filepath);
  192. }
  193. return chain;
  194. }
  195. const loadFileDescriptors = (0, _caching.makeWeakCacheSync)(file => buildRootDescriptors(file, file.filepath, _configDescriptors.createUncachedDescriptors));
  196. const loadFileEnvDescriptors = (0, _caching.makeWeakCacheSync)(file => (0, _caching.makeStrongCacheSync)(envName => buildEnvDescriptors(file, file.filepath, _configDescriptors.createUncachedDescriptors, envName)));
  197. const loadFileOverridesDescriptors = (0, _caching.makeWeakCacheSync)(file => (0, _caching.makeStrongCacheSync)(index => buildOverrideDescriptors(file, file.filepath, _configDescriptors.createUncachedDescriptors, index)));
  198. const loadFileOverridesEnvDescriptors = (0, _caching.makeWeakCacheSync)(file => (0, _caching.makeStrongCacheSync)(index => (0, _caching.makeStrongCacheSync)(envName => buildOverrideEnvDescriptors(file, file.filepath, _configDescriptors.createUncachedDescriptors, index, envName))));
  199. function buildFileLogger(filepath, context, baseLogger) {
  200. if (!baseLogger) {
  201. return () => {};
  202. }
  203. return baseLogger.configure(context.showConfig, _printer.ChainFormatter.Config, {
  204. filepath
  205. });
  206. }
  207. function buildRootDescriptors({
  208. dirname,
  209. options
  210. }, alias, descriptors) {
  211. return descriptors(dirname, options, alias);
  212. }
  213. function buildProgrammaticLogger(_, context, baseLogger) {
  214. var _context$caller;
  215. if (!baseLogger) {
  216. return () => {};
  217. }
  218. return baseLogger.configure(context.showConfig, _printer.ChainFormatter.Programmatic, {
  219. callerName: (_context$caller = context.caller) == null ? void 0 : _context$caller.name
  220. });
  221. }
  222. function buildEnvDescriptors({
  223. dirname,
  224. options
  225. }, alias, descriptors, envName) {
  226. const opts = options.env && options.env[envName];
  227. return opts ? descriptors(dirname, opts, `${alias}.env["${envName}"]`) : null;
  228. }
  229. function buildOverrideDescriptors({
  230. dirname,
  231. options
  232. }, alias, descriptors, index) {
  233. const opts = options.overrides && options.overrides[index];
  234. if (!opts) throw new Error("Assertion failure - missing override");
  235. return descriptors(dirname, opts, `${alias}.overrides[${index}]`);
  236. }
  237. function buildOverrideEnvDescriptors({
  238. dirname,
  239. options
  240. }, alias, descriptors, index, envName) {
  241. const override = options.overrides && options.overrides[index];
  242. if (!override) throw new Error("Assertion failure - missing override");
  243. const opts = override.env && override.env[envName];
  244. return opts ? descriptors(dirname, opts, `${alias}.overrides[${index}].env["${envName}"]`) : null;
  245. }
  246. function makeChainWalker({
  247. root,
  248. env,
  249. overrides,
  250. overridesEnv,
  251. createLogger
  252. }) {
  253. return function* (input, context, files = new Set(), baseLogger) {
  254. const {
  255. dirname
  256. } = input;
  257. const flattenedConfigs = [];
  258. const rootOpts = root(input);
  259. if (configIsApplicable(rootOpts, dirname, context)) {
  260. flattenedConfigs.push({
  261. config: rootOpts,
  262. envName: undefined,
  263. index: undefined
  264. });
  265. const envOpts = env(input, context.envName);
  266. if (envOpts && configIsApplicable(envOpts, dirname, context)) {
  267. flattenedConfigs.push({
  268. config: envOpts,
  269. envName: context.envName,
  270. index: undefined
  271. });
  272. }
  273. (rootOpts.options.overrides || []).forEach((_, index) => {
  274. const overrideOps = overrides(input, index);
  275. if (configIsApplicable(overrideOps, dirname, context)) {
  276. flattenedConfigs.push({
  277. config: overrideOps,
  278. index,
  279. envName: undefined
  280. });
  281. const overrideEnvOpts = overridesEnv(input, index, context.envName);
  282. if (overrideEnvOpts && configIsApplicable(overrideEnvOpts, dirname, context)) {
  283. flattenedConfigs.push({
  284. config: overrideEnvOpts,
  285. index,
  286. envName: context.envName
  287. });
  288. }
  289. }
  290. });
  291. }
  292. if (flattenedConfigs.some(({
  293. config: {
  294. options: {
  295. ignore,
  296. only
  297. }
  298. }
  299. }) => shouldIgnore(context, ignore, only, dirname))) {
  300. return null;
  301. }
  302. const chain = emptyChain();
  303. const logger = createLogger(input, context, baseLogger);
  304. for (const {
  305. config,
  306. index,
  307. envName
  308. } of flattenedConfigs) {
  309. if (!(yield* mergeExtendsChain(chain, config.options, dirname, context, files, baseLogger))) {
  310. return null;
  311. }
  312. logger(config, index, envName);
  313. yield* mergeChainOpts(chain, config);
  314. }
  315. return chain;
  316. };
  317. }
  318. function* mergeExtendsChain(chain, opts, dirname, context, files, baseLogger) {
  319. if (opts.extends === undefined) return true;
  320. const file = yield* (0, _files.loadConfig)(opts.extends, dirname, context.envName, context.caller);
  321. if (files.has(file)) {
  322. throw new Error(`Configuration cycle detected loading ${file.filepath}.\n` + `File already loaded following the config chain:\n` + Array.from(files, file => ` - ${file.filepath}`).join("\n"));
  323. }
  324. files.add(file);
  325. const fileChain = yield* loadFileChain(validateExtendFile(file), context, files, baseLogger);
  326. files.delete(file);
  327. if (!fileChain) return false;
  328. mergeChain(chain, fileChain);
  329. return true;
  330. }
  331. function mergeChain(target, source) {
  332. target.options.push(...source.options);
  333. target.plugins.push(...source.plugins);
  334. target.presets.push(...source.presets);
  335. for (const file of source.files) {
  336. target.files.add(file);
  337. }
  338. return target;
  339. }
  340. function* mergeChainOpts(target, {
  341. options,
  342. plugins,
  343. presets
  344. }) {
  345. target.options.push(options);
  346. target.plugins.push(...(yield* plugins()));
  347. target.presets.push(...(yield* presets()));
  348. return target;
  349. }
  350. function emptyChain() {
  351. return {
  352. options: [],
  353. presets: [],
  354. plugins: [],
  355. files: new Set()
  356. };
  357. }
  358. function normalizeOptions(opts) {
  359. const options = Object.assign({}, opts);
  360. delete options.extends;
  361. delete options.env;
  362. delete options.overrides;
  363. delete options.plugins;
  364. delete options.presets;
  365. delete options.passPerPreset;
  366. delete options.ignore;
  367. delete options.only;
  368. delete options.test;
  369. delete options.include;
  370. delete options.exclude;
  371. if (Object.prototype.hasOwnProperty.call(options, "sourceMap")) {
  372. options.sourceMaps = options.sourceMap;
  373. delete options.sourceMap;
  374. }
  375. return options;
  376. }
  377. function dedupDescriptors(items) {
  378. const map = new Map();
  379. const descriptors = [];
  380. for (const item of items) {
  381. if (typeof item.value === "function") {
  382. const fnKey = item.value;
  383. let nameMap = map.get(fnKey);
  384. if (!nameMap) {
  385. nameMap = new Map();
  386. map.set(fnKey, nameMap);
  387. }
  388. let desc = nameMap.get(item.name);
  389. if (!desc) {
  390. desc = {
  391. value: item
  392. };
  393. descriptors.push(desc);
  394. if (!item.ownPass) nameMap.set(item.name, desc);
  395. } else {
  396. desc.value = item;
  397. }
  398. } else {
  399. descriptors.push({
  400. value: item
  401. });
  402. }
  403. }
  404. return descriptors.reduce((acc, desc) => {
  405. acc.push(desc.value);
  406. return acc;
  407. }, []);
  408. }
  409. function configIsApplicable({
  410. options
  411. }, dirname, context) {
  412. return (options.test === undefined || configFieldIsApplicable(context, options.test, dirname)) && (options.include === undefined || configFieldIsApplicable(context, options.include, dirname)) && (options.exclude === undefined || !configFieldIsApplicable(context, options.exclude, dirname));
  413. }
  414. function configFieldIsApplicable(context, test, dirname) {
  415. const patterns = Array.isArray(test) ? test : [test];
  416. return matchesPatterns(context, patterns, dirname);
  417. }
  418. function shouldIgnore(context, ignore, only, dirname) {
  419. if (ignore && matchesPatterns(context, ignore, dirname)) {
  420. var _context$filename;
  421. const message = `No config is applied to "${(_context$filename = context.filename) != null ? _context$filename : "(unknown)"}" because it matches one of \`ignore: ${JSON.stringify(ignore)}\` from "${dirname}"`;
  422. debug(message);
  423. if (context.showConfig) {
  424. console.log(message);
  425. }
  426. return true;
  427. }
  428. if (only && !matchesPatterns(context, only, dirname)) {
  429. var _context$filename2;
  430. const message = `No config is applied to "${(_context$filename2 = context.filename) != null ? _context$filename2 : "(unknown)"}" because it fails to match one of \`only: ${JSON.stringify(only)}\` from "${dirname}"`;
  431. debug(message);
  432. if (context.showConfig) {
  433. console.log(message);
  434. }
  435. return true;
  436. }
  437. return false;
  438. }
  439. function matchesPatterns(context, patterns, dirname) {
  440. return patterns.some(pattern => matchPattern(pattern, dirname, context.filename, context));
  441. }
  442. function matchPattern(pattern, dirname, pathToTest, context) {
  443. if (typeof pattern === "function") {
  444. return !!pattern(pathToTest, {
  445. dirname,
  446. envName: context.envName,
  447. caller: context.caller
  448. });
  449. }
  450. if (typeof pathToTest !== "string") {
  451. throw new Error(`Configuration contains string/RegExp pattern, but no filename was passed to Babel`);
  452. }
  453. if (typeof pattern === "string") {
  454. pattern = (0, _patternToRegex.default)(pattern, dirname);
  455. }
  456. return pattern.test(pathToTest);
  457. }