UNPKG

3.27 kBJavaScriptView Raw
1const DEBUG = process.env.NODE_ENV !== "production"
2
3const { data, string, number } = require("@algebraic/type");
4const { List } = require("@algebraic/collections");
5const PluginConfiguration = require("@isomorphic/plugin/configuration");
6const Product = require("./product");
7
8// FIXME: These lists should probaby be OrderedSets.
9const Configuration = data `Build.Configuration` (
10 root => string,
11 cache => string,
12 concurrency => number,
13 products => List(Product),
14 pluginConfigurations => List(PluginConfiguration) );
15
16
17Configuration.parse = (function ()
18{
19 const { resolve, basename, extname } = require("path");
20 const flatMap = (f, array) => [].concat(...array.map(f));
21 const dedupe = array => Array.from(new Set(array));
22
23 // FIXME: This is legacy, we need to be able to define destinations in the
24 // configuration. This currently ONLY works for javascript files.
25 const FIXME_toDestination = (destination, entrypoint) =>
26 `${destination}/${basename(entrypoint, extname(entrypoint))}.bundle.js`;
27
28 return function parseConfiguration(options)
29 {
30 const cpus = require("os").cpus().length;
31 const concurrency =
32 options.concurrency === void(0) ?
33 cpus : options.concurrency;
34
35 if (DEBUG && options.concurrency > cpus)
36 console.warn(
37 `WARNING: concurrency of ${concurrency} chosen, but machine only ` +
38 `has ${cpus} cpus. This usually results in degraded performance.`);
39
40 // FIXME: There can be overlapping entrypoints.
41 // FIXME: Explicit Products aren't checked for existence.
42 const destination = options.entrypoints && resolve(options.destination);
43 const entrypointProducts = List(Product)
44 (dedupe(flatMap(glob, options.entrypoints || []))
45 .map(entrypoint => Product(
46 {
47 entrypoint,
48 destination: FIXME_toDestination(destination, entrypoint)
49 })));
50 const explicitProducts =
51 List(Product)(options.products || []).map(Product);
52 const products = entrypointProducts.concat(explicitProducts);
53
54 if (products.size <= 0)
55 return Error(`No products passed in.`);
56
57 const root = resolve(options.root);
58 const pluginConfigurations =
59 List(PluginConfiguration)(options.plugins || [])
60 .map(options => PluginConfiguration.parse(root, options));
61
62 return Configuration(
63 {
64 root,
65 cache: resolve(options.cache),
66 concurrency,
67 products,
68 pluginConfigurations
69 });
70 }
71})();
72
73module.exports = Configuration;
74
75const glob = (function ()
76{
77 const fastGlob = require("fast-glob");
78
79 return function glob(pattern)
80 {
81 const results = fastGlob(pattern);
82
83 if (DEBUG && result.length === 0)
84 console.warn(
85 `WARNING: The pattern "${pattern}" resulted in no files. ` +
86 `Perhaps there is a typo in your pattern or you are running ` +
87 `this from the wrong working directory.`);
88
89 return results;
90 }
91})();
92
\No newline at end of file