UNPKG

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