1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const fs = require('fs');
|
13 | const path = require('path');
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | module.exports = function validateConfig(
|
37 | dir /*: string */,
|
38 | silent /*: boolean */ = false
|
39 | ) /*: FusionRC */ {
|
40 | const configPath = path.join(dir, '.fusionrc.js');
|
41 | let config;
|
42 | if (fs.existsSync(configPath)) {
|
43 |
|
44 | config = require(configPath);
|
45 | if (!isValid(config, silent)) {
|
46 | throw new Error('.fusionrc.js is invalid');
|
47 | }
|
48 | } else {
|
49 | config = {};
|
50 | }
|
51 | return config;
|
52 | };
|
53 |
|
54 | function isValid(config, silent) {
|
55 | if (!(typeof config === 'object' && config !== null)) {
|
56 | throw new Error('.fusionrc.js must export an object');
|
57 | }
|
58 |
|
59 | if (
|
60 | !Object.keys(config).every(key =>
|
61 | [
|
62 | 'babel',
|
63 | 'splitChunks',
|
64 | 'modernBuildOnly',
|
65 | 'defaultImportSideEffects',
|
66 | 'assumeNoImportSideEffects',
|
67 | 'experimentalCompile',
|
68 | 'experimentalTransformTest',
|
69 | 'experimentalBundleTest',
|
70 | 'nodeBuiltins',
|
71 | 'jest',
|
72 | 'brotli',
|
73 | 'zopfli',
|
74 | 'gzip',
|
75 | ].includes(key)
|
76 | )
|
77 | ) {
|
78 | if (config.experimentalSideEffectsTest) {
|
79 | throw new Error(
|
80 | `experimentalSideEffectsTest has been removed. Use assumeNoImportSideEffects instead.`
|
81 | );
|
82 | }
|
83 | throw new Error(`Invalid property in .fusionrc.js`);
|
84 | }
|
85 |
|
86 | if (config.experimentalCompile && config.experimentalTransformTest) {
|
87 | throw new Error(
|
88 | `Cannot use both experimentalCompile and experimentalTransformTest in .fusionrc.js`
|
89 | );
|
90 | }
|
91 |
|
92 | if (config.experimentalCompile && config.experimentalBundleTest) {
|
93 | throw new Error(
|
94 | `Cannot use both experimentalCompile and experimentalBundleTest in .fusionrc.js`
|
95 | );
|
96 | }
|
97 |
|
98 | if (config.experimentalCompile) {
|
99 | if (!silent) {
|
100 | console.log(
|
101 | 'WARNING: experimentalCompile is deprecated. Use experimentalTransformTest instead.'
|
102 | );
|
103 | }
|
104 |
|
105 | config.experimentalTransformTest = (file, defaults) => {
|
106 | return 'all';
|
107 | };
|
108 | delete config.experimentalCompile;
|
109 | }
|
110 |
|
111 | if (
|
112 | config.babel &&
|
113 | !Object.keys(config.babel).every(el => ['plugins', 'presets'].includes(el))
|
114 | ) {
|
115 | throw new Error(
|
116 | `Only "plugins" and "presets" are supported in fusionrc.js babel config`
|
117 | );
|
118 | }
|
119 |
|
120 | if (
|
121 | !(
|
122 | config.assumeNoImportSideEffects === false ||
|
123 | config.assumeNoImportSideEffects === true ||
|
124 | config.assumeNoImportSideEffects === void 0
|
125 | )
|
126 | ) {
|
127 | throw new Error(
|
128 | 'assumeNoImportSideEffects must be true, false, or undefined in fusionrc.js config'
|
129 | );
|
130 | }
|
131 |
|
132 | if (
|
133 | !(
|
134 | config.zopfli === false ||
|
135 | config.zopfli === true ||
|
136 | config.zopfli === void 0
|
137 | )
|
138 | ) {
|
139 | throw new Error('zopfli must be true, false, or undefined in fusionrc.js');
|
140 | }
|
141 |
|
142 | if (config.zopfli === false || config.zopfli === true) {
|
143 | console.warn('`zopfli` option has been deprecated. Use `gzip` instead');
|
144 | }
|
145 |
|
146 | if (
|
147 | !(config.gzip === false || config.gzip === true || config.gzip === void 0)
|
148 | ) {
|
149 | throw new Error('gzip must be true, false, or undefined in fusionrc.js');
|
150 | }
|
151 |
|
152 | if (
|
153 | !(
|
154 | config.brotli === false ||
|
155 | config.brotli === true ||
|
156 | config.brotli === void 0
|
157 | )
|
158 | ) {
|
159 | throw new Error('brotli must be true, false, or undefined in fusionrc.js');
|
160 | }
|
161 |
|
162 | if (
|
163 | !(
|
164 | config.defaultImportSideEffects === void 0 ||
|
165 | config.defaultImportSideEffects === true ||
|
166 | config.defaultImportSideEffects === false ||
|
167 | (Array.isArray(config.defaultImportSideEffects) &&
|
168 | config.defaultImportSideEffects.every(item => typeof item === 'string'))
|
169 | )
|
170 | ) {
|
171 | throw new Error(
|
172 | 'defaultImportSideEffects must be true, false, or an array of strings in fusionrc.js'
|
173 | );
|
174 | }
|
175 |
|
176 | if (
|
177 | config.defaultImportSideEffects !== void 0 &&
|
178 | config.assumeNoImportSideEffects !== void 0
|
179 | ) {
|
180 | throw new Error(
|
181 | `Cannot use both defaultImportSideEffects and assumeNoImportSideEffects in .fusionrc.js`
|
182 | );
|
183 | }
|
184 |
|
185 | return true;
|
186 | }
|