-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
options.ts
639 lines (617 loc) · 18 KB
/
options.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
import { defineStore } from 'pinia';
import type { Ref } from 'vue';
import { computed, ref, shallowRef, watchEffect } from 'vue';
import type { RollupOptions } from '../../../src/rollup/types';
import { useModules } from './modules';
import { useRollupOutput } from './rollupOutput';
interface BaseOptionType<T> {
available: Ref<boolean | undefined>;
defaultValue: T | undefined;
name: string;
required: Ref<boolean>;
type: string;
value: Ref<T | undefined>;
}
interface OptionTypeSelect<T> extends BaseOptionType<T> {
options: Ref<T[]>;
type: 'select';
}
interface OptionTypeString extends BaseOptionType<string> {
placeholder: string | null;
type: 'string';
}
interface OptionTypeStringMapping extends BaseOptionType<Record<string, string>> {
keys: Ref<string[]>;
type: 'string-mapping';
}
interface OptionTypeNumber extends BaseOptionType<number> {
placeholder: string | null;
type: 'number';
}
type OptionType =
| OptionTypeSelect<unknown>
| OptionTypeString
| OptionTypeStringMapping
| OptionTypeNumber;
const mapOptions = {
number({ defaultValue, name, placeholder, required, value }: OptionTypeNumber) {
return {
name,
placeholder,
removable: !required.value,
type: 'number',
value: value.value === undefined ? defaultValue : value.value
} as const;
},
select({ defaultValue, name, options, required, value }: OptionTypeSelect<unknown>) {
return {
name,
options: options.value,
removable: !required.value,
type: 'select',
value:
value.value === undefined || !options.value.includes(value.value)
? defaultValue
: value.value
} as const;
},
string({ defaultValue, name, placeholder, required, value }: OptionTypeString) {
return {
name,
placeholder,
removable: !required.value,
type: 'string',
value: value.value === undefined ? defaultValue : value.value
} as const;
},
'string-mapping'({ defaultValue, keys, name, required, value }: OptionTypeStringMapping) {
return {
keys: keys.value,
name,
removable: !required.value,
type: 'string-mapping',
value: value.value === undefined ? defaultValue : value.value
} as const;
}
};
type Option = ReturnType<(typeof mapOptions)[keyof typeof mapOptions]>;
const alwaysTrue = computed(() => true);
const alwaysFalse = computed(() => false);
const allFormats = ['es', 'amd', 'cjs', 'iife', 'umd', 'system'];
const codeSplittingFormats = ['es', 'amd', 'cjs', 'system'];
const amdFormats = new Set(['amd', 'umd']);
const iifeFormats = new Set(['iife', 'umd']);
const interopFormats = new Set(['amd', 'cjs', 'iife', 'umd']);
const isOptionShown = ({ required, available, value }: OptionType) =>
available.value && (required.value || value.value !== undefined);
export const useOptions = defineStore('options2', () => {
const rollupOutputStore = useRollupOutput();
const modulesStore = useModules();
const outputHasMultipleChunks = computed(() => rollupOutputStore.output?.output.length > 1);
const isAmdFormat = computed(() => {
const { value } = optionOutputFormat.value;
return value != null && amdFormats.has(value);
});
const isIifeFormat = computed(() => {
const { value } = optionOutputFormat.value;
return value != null && iifeFormats.has(value);
});
const isInteropFormat = computed(() => {
const { value } = optionOutputFormat.value;
return value != null && interopFormats.has(value);
});
const externalImports = computed(() => rollupOutputStore.output?.externalImports || []);
const isTreeshakeEnabled = computed(() =>
[undefined, true].includes(optionTreeshake.value.value as any)
);
const optionContext = getString({
defaultValue: 'undefined',
name: 'context'
});
const optionOutputAmdAutoId = getBoolean({
available: () => isAmdFormat.value,
name: 'output.amd.autoId'
});
const optionOutputAmdBasePath = getString({
available: optionOutputAmdAutoId.value,
name: 'output.amd.basePath'
});
const optionOutputAmdDefine = getString({
available: isAmdFormat,
defaultValue: 'define',
name: 'output.amd.define'
});
const optionOutputAmdForceJsExtensionForImports = getBoolean({
available: isAmdFormat,
name: 'output.amd.forceJsExtensionForImports'
});
const optionOutputAmdId = getString({
available: () =>
isAmdFormat.value && !outputHasMultipleChunks.value && !optionOutputAmdAutoId.value.value,
name: 'output.amd.id',
placeholder: 'leave blank for anonymous module'
});
const optionOutputBanner = getString({
name: 'output.banner'
});
const optionOutputChunkFileNames = getString({
available: alwaysTrue,
defaultValue: '[name]-[hash].js',
name: 'output.chunkFileNames'
});
const optionOutputCompact = getBoolean({
name: 'output.compact'
});
const optionOutputDynamicImportInCjs = getBoolean({
available: () => optionOutputFormat.value.value === 'cjs',
defaultValue: true,
name: 'output.dynamicImportInCjs'
});
const optionOutputEntryFileNames = getString({
available: alwaysTrue,
defaultValue: '[name].js',
name: 'output.entryFileNames'
});
const optionOutputEsModule = getSelect({
available: isInteropFormat,
defaultValue: 'if-default-prop',
name: 'output.esModule',
options: () => [false, true, 'if-default-prop']
});
const optionOutputExperimentalMinChunkSize = getNumber({
defaultValue: 0,
name: 'output.experimentalMinChunkSize'
});
const optionOutputExports = getSelect({
available: isInteropFormat,
defaultValue: 'auto',
name: 'output.exports',
options: () => ['auto', 'default', 'named', 'none']
});
const optionOutputExtend = getBoolean({
available: isIifeFormat,
name: 'output.extend'
});
const optionOutputExternalLiveBindings = getBoolean({
available: isInteropFormat,
defaultValue: true,
name: 'output.externalLiveBindings'
});
const optionOutputExternalImportAssertions = getBoolean({
available: () => optionOutputFormat.value.value === 'es',
defaultValue: true,
name: 'output.externalImportAssertions'
});
const optionOutputFreeze = getBoolean({
defaultValue: true,
name: 'output.freeze'
});
const optionOutputFooter = getString({
name: 'output.footer'
});
const optionOutputFormat = getSelect({
defaultValue: 'es',
name: 'output.format',
options: () =>
optionOutputPreserveModules.value.value === true || outputHasMultipleChunks.value
? codeSplittingFormats
: allFormats,
required: () => true
});
const optionOutputGeneratedCodeArrowFunctions = getBoolean({
name: 'output.generatedCode.arrowFunctions'
});
const optionOutputGeneratedCodeConstBindings = getBoolean({
name: 'output.generatedCode.constBindings'
});
const optionOutputGeneratedCodeObjectShorthand = getBoolean({
name: 'output.generatedCode.objectShorthand'
});
const optionOutputGeneratedCodePreset = getSelect({
defaultValue: null,
name: 'output.generatedCode.preset',
options: () => [null, 'es5', 'es2015']
});
const optionOutputGeneratedCodeReservedNamesAsProperties = getBoolean({
defaultValue: true,
name: 'output.generatedCode.reservedNamesAsProps'
});
const optionOutputGeneratedCodeSymbols = getBoolean({
name: 'output.generatedCode.symbols'
});
const optionOutputGlobals = getStringMapping({
available: () => isIifeFormat.value && externalImports.value.length > 0,
keys: externalImports,
name: 'output.globals',
required: () => true
});
const optionOutputHoistTransitiveImports = getBoolean({
available: alwaysTrue,
defaultValue: true,
name: 'output.hoistTransitiveImports'
});
const optionOutputIndent = getBoolean({
available: () => ['amd', 'iife', 'umd', 'system'].includes(optionOutputFormat.value.value!),
defaultValue: true,
name: 'output.indent'
});
const optionOutputInlineDynamicImports = getBoolean({
available: () => {
const modules = modulesStore.modules;
if (!modules) return false;
let entryPoints = 0;
for (const { isEntry } of modules) {
if (isEntry) {
entryPoints++;
if (entryPoints > 1) {
return false;
}
}
}
return true;
},
name: 'output.inlineDynamicImports'
});
const optionOutputInterop = getSelect({
available: () => {
const { value } = optionOutputFormat.value;
return value != null && interopFormats.has(value);
},
defaultValue: 'default',
name: 'output.interop',
options: () => ['compat', 'auto', 'esModule', 'default', 'defaultOnly']
});
const optionOutputIntro = getString({
name: 'output.intro'
});
const optionOutputMinifyInternalExports = getBoolean({
available: alwaysTrue,
name: 'output.minifyInternalExports'
});
const optionOutputNoConflict = getBoolean({
available: () => optionOutputFormat.value.value === 'umd',
name: 'output.noConflice'
});
const optionOutputName = getString({
available: isIifeFormat,
defaultValue: 'myBundle',
name: 'output.name',
required: () => rollupOutputStore.output.output[0]?.exports.length > 0
});
const optionOutputOutro = getString({
name: 'output.outro'
});
const optionOutputPaths = getStringMapping({
available: () => externalImports.value.length > 0 && optionOutputFormat.value.value !== 'iife',
keys: externalImports,
name: 'output.paths'
});
const optionOutputPreserveModules = getBoolean({
available: () => {
const { value } = optionOutputFormat.value;
return value == null || codeSplittingFormats.includes(value);
},
name: 'output.preserveModules'
});
const optionOutputPreserveModulesRoot = getString({
available: optionOutputPreserveModules.value,
name: 'output.preserveModulesRoot'
});
const optionOutputSourcemap = getBoolean({
name: 'output.sourcemap'
});
const optionOutputSanitizeFileName = getBoolean({
available: alwaysTrue,
defaultValue: true,
name: 'output.sanitizeFileName'
});
const optionOutputSourcemapBaseUrl = getString({
available: optionOutputSourcemap.value,
name: 'output.sourcemapBaseUrl'
});
const optionOutputSourcemapExcludeSources = getBoolean({
available: optionOutputSourcemap.value,
name: 'output.sourcemapExcludeSources'
});
const optionOutputStrict = getBoolean({
available: () =>
optionOutputFormat.value.value !== undefined && optionOutputFormat.value.value !== 'es',
defaultValue: true,
name: 'output.strict'
});
const optionOutputValidate = getBoolean({
name: 'output.validate'
});
const optionPreserveEntrySignatures = getSelect({
available: alwaysTrue,
defaultValue: 'exports-only',
name: 'preserveEntrySignatures',
options: () => ['strict', 'allow-extension', 'exports-only', false]
});
const optionOutputSystemNullSetters = getBoolean({
available: () => optionOutputFormat.value.value === 'system',
defaultValue: true,
name: 'output.systemNullSetters'
});
const optionShimMissingExports = getBoolean({
defaultValue: false,
name: 'shimMissingExports'
});
const optionTreeshake = getSelect({
defaultValue: true,
name: 'treeshake',
options: () => [false, true, 'smallest', 'safest', 'recommended']
});
const optionTreeshakeAnnotations = getBoolean({
available: isTreeshakeEnabled,
defaultValue: true,
name: 'treeshake.annotations'
});
const optionTreeshakeCorrectVariableValueBeforeDeclaration = getBoolean({
available: isTreeshakeEnabled,
defaultValue: false,
name: 'treeshake.correctVarValueBeforeDeclaration'
});
const optionTreeshakeModuleSideEffects = getSelect({
available: isTreeshakeEnabled,
defaultValue: true,
name: 'treeshake.moduleSideEffects',
options: () => [false, true, 'no-external']
});
const optionTreeshakePreset = getSelect({
available: isTreeshakeEnabled,
defaultValue: null,
name: 'treeshake.preset',
options: () => [null, 'smallest', 'safest', 'recommended']
});
const optionTreeshakePropertyReadSideEffects = getSelect({
available: isTreeshakeEnabled,
defaultValue: true,
name: 'treeshake.propertyReadSideEffects',
options: () => [false, true, 'always']
});
const optionTreeshakeTryCatchDeoptimization = getBoolean({
available: isTreeshakeEnabled,
defaultValue: true,
name: 'treeshake.tryCatchDeoptimization'
});
const optionTreeshakeUnknownGlobalSideEffects = getBoolean({
available: isTreeshakeEnabled,
defaultValue: true,
name: 'treeshake.unknownGlobalSideEffects'
});
const optionList: OptionType[] = [
optionContext,
optionOutputAmdAutoId,
optionOutputAmdBasePath,
optionOutputAmdDefine,
optionOutputAmdForceJsExtensionForImports,
optionOutputAmdId,
optionOutputBanner,
optionOutputChunkFileNames,
optionOutputCompact,
optionOutputDynamicImportInCjs,
optionOutputEntryFileNames,
optionOutputEsModule,
optionOutputExperimentalMinChunkSize,
optionOutputExports,
optionOutputExtend,
optionOutputExternalLiveBindings,
optionOutputExternalImportAssertions,
optionOutputFreeze,
optionOutputFooter,
optionOutputFormat,
optionOutputGeneratedCodeArrowFunctions,
optionOutputGeneratedCodeConstBindings,
optionOutputGeneratedCodeObjectShorthand,
optionOutputGeneratedCodePreset,
optionOutputGeneratedCodeReservedNamesAsProperties,
optionOutputGeneratedCodeSymbols,
optionOutputGlobals,
optionOutputHoistTransitiveImports,
optionOutputIndent,
optionOutputInlineDynamicImports,
optionOutputInterop,
optionOutputIntro,
optionOutputMinifyInternalExports,
optionOutputNoConflict,
optionOutputName,
optionOutputOutro,
optionOutputPaths,
optionOutputPreserveModules,
optionOutputPreserveModulesRoot,
optionOutputSourcemap,
optionOutputSanitizeFileName,
optionOutputSourcemapBaseUrl,
optionOutputSourcemapExcludeSources,
optionOutputStrict,
optionOutputValidate,
optionPreserveEntrySignatures,
optionOutputSystemNullSetters,
optionShimMissingExports,
optionTreeshake,
optionTreeshakeAnnotations,
optionTreeshakeCorrectVariableValueBeforeDeclaration,
optionTreeshakeModuleSideEffects,
optionTreeshakePreset,
optionTreeshakePropertyReadSideEffects,
optionTreeshakeTryCatchDeoptimization,
optionTreeshakeUnknownGlobalSideEffects
];
const options = computed<Option[]>(() =>
optionList
.filter(isOptionShown)
.map((option: OptionType) => mapOptions[option.type](option as any))
);
return {
additionalAvailableOptions: computed<string[]>(() =>
optionList
.filter(option => option.available.value && !isOptionShown(option))
.map(({ name }) => name)
),
addOption(optionName: string) {
const option = optionList.find(({ name }) => name === optionName)!;
option.value.value = option.defaultValue;
},
options,
optionsObject: getOptionsObject(options),
set(name: string, value: any) {
optionList.find(option => option.name === name)!.value.value = value;
},
setAll(options: RollupOptions) {
for (const { name, value } of optionList) {
const path = name.split('.');
let key: string | undefined;
let subOptions: any = options;
while ((key = path.shift())) {
subOptions = subOptions?.[key];
}
value.value = name === 'treeshake' && typeof subOptions === 'object' ? true : subOptions;
}
}
};
});
function getBoolean({
available,
defaultValue,
name
}: {
available?: (() => boolean) | Ref<boolean | undefined>;
defaultValue?: boolean;
name: string;
}): OptionTypeSelect<boolean> {
return getSelect({
available,
defaultValue: defaultValue || false,
name,
options: () => [false, true]
});
}
function getSelect<T>({
available,
defaultValue,
name,
options,
required
}: {
available?: (() => boolean) | Ref<boolean | undefined>;
defaultValue: T;
name: string;
options: () => T[];
required?: () => boolean;
}): OptionTypeSelect<T> {
return {
available: typeof available === 'function' ? computed(available) : available || alwaysTrue,
defaultValue,
name,
options: computed(options),
required: required ? computed(required) : alwaysFalse,
type: 'select',
value: ref(undefined)
};
}
function getString({
available,
defaultValue,
name,
placeholder,
required
}: {
available?: (() => boolean) | Ref<boolean | undefined>;
defaultValue?: string;
name: string;
placeholder?: string;
required?: () => boolean;
}): OptionTypeString {
return {
available: typeof available === 'function' ? computed(available) : available || alwaysTrue,
defaultValue: defaultValue ?? '',
name,
placeholder: placeholder || null,
required: required ? computed(required) : alwaysFalse,
type: 'string',
value: ref(undefined)
};
}
function getStringMapping({
available,
keys,
name,
required
}: {
available?: () => boolean;
keys: Ref<string[]>;
name: string;
placeholder?: string;
required?: () => boolean;
}): OptionTypeStringMapping {
return {
available: available ? computed(available) : alwaysTrue,
defaultValue: {},
keys,
name,
required: required ? computed(required) : alwaysFalse,
type: 'string-mapping',
value: shallowRef(undefined)
};
}
function getNumber({
available,
defaultValue,
name,
placeholder,
required
}: {
available?: (() => boolean) | Ref<boolean | undefined>;
defaultValue?: number;
name: string;
placeholder?: string;
required?: () => boolean;
}): OptionTypeNumber {
return {
available: typeof available === 'function' ? computed(available) : available || alwaysTrue,
defaultValue: defaultValue ?? 0,
name,
placeholder: placeholder || null,
required: required ? computed(required) : alwaysFalse,
type: 'number',
value: ref(undefined)
};
}
function getOptionsObject(options: Ref<Option[]>): Ref<RollupOptions> {
let previousOptions = options.value.filter(({ value }) => value !== undefined);
const result = shallowRef<RollupOptions>({});
watchEffect(
() => {
const filteredOptions = options.value.filter(({ value }) => value !== undefined);
if (
filteredOptions.length === previousOptions.length &&
filteredOptions.every(
({ name, value }, index) =>
name === previousOptions[index].name && value === previousOptions[index].value
)
) {
return;
}
previousOptions = filteredOptions;
const object = {};
for (const { name, value } of filteredOptions) {
const path = name.split('.');
const valueKey = path.pop()!;
let key: string | undefined;
let subOptions: any = object;
while ((key = path.shift())) {
// Special logic to handle treeshake option
if (subOptions[key] === true) {
subOptions[key] = {};
}
subOptions = subOptions[key] ??= {};
}
subOptions[valueKey] = value;
}
result.value = object;
},
{ flush: 'post' }
);
return result;
}