UNPKG

6.51 kBJavaScriptView Raw
1const fs = require('fs');
2const path = require('path');
3const pkgRoot = require('./package-root');
4
5const defaultEnv = 'web';
6const defaultBrowsers = ['>1%', 'last 2 versions', 'Firefox ESR', 'not ie < 12', 'not ie_mob < 12'];
7const pkg = pkgRoot();
8let enact = pkg.meta.enact || {};
9
10function gentlyParse(file) {
11 try {
12 return JSON.parse(fs.readFileSync(file, {encoding: 'utf8'}));
13 } catch (e) {
14 return undefined;
15 }
16}
17
18function parseBL(file) {
19 try {
20 return fs
21 .readFileSync(file, {encoding: 'utf8'})
22 .split(/[\r\n]+/)
23 .filter(t => t.trim() && t.charAt(0) !== '#');
24 } catch (e) {
25 return undefined;
26 }
27}
28
29function screenTypes(theme) {
30 const decorator = theme.charAt(0).toUpperCase() + theme.slice(1) + 'Decorator';
31 const scoped = path.join('node_modules', '@enact', theme, decorator, 'screenTypes.json');
32 const basic = path.join('node_modules', theme, decorator, 'screenTypes.json');
33 return fs.existsSync(scoped) ? scoped : fs.existsSync(basic) ? basic : null;
34}
35
36function fontGenerator(theme) {
37 const decorator = theme.charAt(0).toUpperCase() + theme.slice(1) + 'Decorator';
38 const scoped = path.join('node_modules', '@enact', theme, decorator, 'fontGenerator.js');
39 const basic = path.join('node_modules', theme, decorator, 'fontGenerator.js');
40 return fs.existsSync(scoped) ? scoped : fs.existsSync(basic) ? basic : null;
41}
42
43const config = {
44 // Project base directory
45 context: pkg.path,
46 // Project name
47 name: pkg.meta.name,
48 applyEnactMeta: function(meta) {
49 enact = Object.assign(enact, meta);
50
51 // Optional alternate entrypoint for isomorphic builds.
52 config.isomorphic = enact.isomorphic;
53 // Optional filepath to an alternate HTML template for html-webpack-plugin.
54 config.template = enact.template;
55 // Optional <title></title> value for HTML
56 config.title = enact.title;
57 // Optional flag whether to externalize the prerender startup js
58 config.externalStartup = enact.externalStartup;
59 // Optional webpack node configuration value (see https://webpack.js.org/configuration/node/).
60 config.nodeBuiltins = enact.nodeBuiltins;
61 // Optional property to specify a version of NodeJS to target required polyfills.
62 // True or 'current' will use active version of Node, otherwise will use a specified version number.
63 config.node = typeof enact.node !== 'object' && enact.node;
64 // Optional window condition(s) that indicate deeplinking and invalidate HTML prerender.
65 config.deep = enact.deep;
66 // Proxy target to use within the http-proxy-middleware during serving.
67 config.proxy = enact.proxy || pkg.meta.proxy;
68 // Optional theme preset for theme-specific settings (see below).
69 config.theme = enact.theme;
70
71 // Resolve array of screenType configurations. When not found, falls back to any theme preset or moonstone.
72 config.screenTypes =
73 (Array.isArray(enact.screenTypes) && enact.screenTypes) ||
74 (typeof enact.screenTypes === 'string' &&
75 (gentlyParse(path.join(pkg.path, enact.screenTypes)) ||
76 gentlyParse(path.join(pkg.path, 'node_modules', enact.screenTypes)))) ||
77 gentlyParse(screenTypes(enact.theme || 'moonstone')) ||
78 [];
79
80 // Resolve the resolution independence settings from explicit settings or the resolved screenTypes definitions.
81 config.ri = enact.ri || {
82 baseSize: config.screenTypes.reduce((r, s) => (s.base && s.pxPerRem) || r, null)
83 };
84
85 // Resolved filepath to fontGenerator. When not found, falls back to any theme preset or moonstone.
86 config.fontGenerator =
87 (typeof enact.screenTypes === 'string' &&
88 [
89 path.join(pkg.path, enact.fontGenerator),
90 path.join(pkg.path, 'node_modules', enact.fontGenerator)
91 ].find(fs.existsSync)) ||
92 fontGenerator(enact.theme || 'moonstone');
93
94 // Override theme's accent LESS variable value if desired. Private option; may be removed in future.
95 // When used, creates a LESS variable override map, overriding '@moon-accent' and/or '@<theme>-accent'
96 // values with the specified override. This allows a simple way to alter Enact spotlight color.
97 config.accent =
98 enact.accent &&
99 Object.assign(
100 {'moon-accent': enact.accent},
101 enact.theme && enact.theme !== 'moonstone' && {[enact.theme + '-accent']: enact.accent}
102 );
103
104 // Handle dynamic resolving of targets for both browserlist format and webpack target string format.
105 // Temporary support for parsing BROWSERSLIST env var. Will be supported out-of-the-box in Babel 7 in all forms.
106 const browserslist =
107 (process.env['BROWSERSLIST'] && process.env['BROWSERSLIST'].split(/\s*,\s*/)) ||
108 pkg.meta.browserlist ||
109 parseBL(path.join(pkg.path, '.browserslistrc')) ||
110 parseBL(path.join(pkg.path, 'browserslist')) ||
111 (Array.isArray(enact.target) && enact.target);
112 if (browserslist) {
113 // Standard browserslist format (https://github.com/ai/browserslist)
114 if (browserslist.find(b => !b.startsWith('not') && b.indexOf('Electron') > -1)) {
115 config.environment = enact.environment || 'electron-main';
116 } else {
117 config.environment = enact.environment || defaultEnv;
118 }
119 config.browsers = browserslist;
120 } else if (typeof enact.target === 'string' || enact.environment) {
121 // Optional webpack target value (see https://webpack.js.org/configuration/target/).
122 config.environment = enact.environment || enact.target || defaultEnv;
123 switch (config.environment) {
124 case 'atom':
125 case 'electron':
126 case 'electron-main':
127 case 'electron-renderer': {
128 const versionMap = require('electron-to-chromium/versions');
129 const lastFour = Object.keys(versionMap)
130 .sort((a, b) => parseInt(versionMap[a]) - parseInt(versionMap[b]))
131 .slice(-4)
132 .map(v => 'Electron ' + v);
133 try {
134 // Attempt to detect current-used Electron version
135 const electron = JSON.parse(
136 fs.readFileSync(path.join(pkg.path, 'node_modules', 'electron', 'package.json'), {
137 encoding: 'utf8'
138 })
139 );
140 const label = (electron.version + '').replace(/^(\d+\.\d+).*$/, '$1');
141 config.browsers = versionMap[label] ? ['Electron ' + label] : lastFour;
142 } catch (e) {
143 // Fallback to last 4 releases of Electron.
144 config.browsers = lastFour;
145 }
146 break;
147 }
148 case 'node':
149 config.node = config.node || true;
150 delete config.browsers;
151 delete config.nodeBuiltins;
152 break;
153 default:
154 config.browsers = defaultBrowsers;
155 }
156 } else {
157 config.environment = defaultEnv;
158 config.browsers = defaultBrowsers;
159 }
160 }
161};
162
163config.applyEnactMeta();
164
165module.exports = config;