1 | const fs = require('fs');
|
2 | const path = require('path');
|
3 | const pkgRoot = require('./package-root');
|
4 |
|
5 | const defaultEnv = 'web';
|
6 | const defaultBrowsers = ['>1%', 'last 2 versions', 'Firefox ESR', 'not ie < 12', 'not ie_mob < 12'];
|
7 | const pkg = pkgRoot();
|
8 | let enact = pkg.meta.enact || {};
|
9 |
|
10 | function gentlyParse(file) {
|
11 | try {
|
12 | return JSON.parse(fs.readFileSync(file, {encoding: 'utf8'}));
|
13 | } catch (e) {
|
14 | return undefined;
|
15 | }
|
16 | }
|
17 |
|
18 | function 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 |
|
29 | function 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 |
|
36 | function 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 |
|
43 | const config = {
|
44 |
|
45 | context: pkg.path,
|
46 |
|
47 | name: pkg.meta.name,
|
48 | applyEnactMeta: function(meta) {
|
49 | enact = Object.assign(enact, meta);
|
50 |
|
51 |
|
52 | config.isomorphic = enact.isomorphic;
|
53 |
|
54 | config.template = enact.template;
|
55 |
|
56 | config.title = enact.title;
|
57 |
|
58 | config.externalStartup = enact.externalStartup;
|
59 |
|
60 | config.nodeBuiltins = enact.nodeBuiltins;
|
61 |
|
62 |
|
63 | config.node = typeof enact.node !== 'object' && enact.node;
|
64 |
|
65 | config.deep = enact.deep;
|
66 |
|
67 | config.proxy = enact.proxy || pkg.meta.proxy;
|
68 |
|
69 | config.theme = enact.theme;
|
70 |
|
71 |
|
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 |
|
81 | config.ri = enact.ri || {
|
82 | baseSize: config.screenTypes.reduce((r, s) => (s.base && s.pxPerRem) || r, null)
|
83 | };
|
84 |
|
85 |
|
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 |
|
95 |
|
96 |
|
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 |
|
105 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
163 | config.applyEnactMeta();
|
164 |
|
165 | module.exports = config;
|