-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ParcelConfig.schema.js
138 lines (127 loc) Β· 3.56 KB
/
ParcelConfig.schema.js
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
// @flow strict-local
import type {PackageName} from '@parcel/types';
import type {SchemaEntity} from '@parcel/utils';
import assert from 'assert';
// Reasoning behind this validation:
// https://github.com/parcel-bundler/parcel/issues/3397#issuecomment-521353931
export function validatePackageName(
pkg: ?PackageName,
pluginType: string,
key: string,
) {
// $FlowFixMe
if (!pkg) {
return;
}
assert(typeof pkg === 'string', `"${key}" must be a string`);
if (pkg.startsWith('@parcel')) {
assert(
pkg.replace(/^@parcel\//, '').startsWith(`${pluginType}-`),
`Official parcel ${pluginType} packages must be named according to "@parcel/${pluginType}-{name}"`,
);
} else if (pkg.startsWith('@')) {
let [scope, name] = pkg.split('/');
assert(
name.startsWith(`parcel-${pluginType}-`) ||
name === `parcel-${pluginType}`,
`Scoped parcel ${pluginType} packages must be named according to "${scope}/parcel-${pluginType}[-{name}]"`,
);
} else {
assert(
pkg.startsWith(`parcel-${pluginType}-`),
`Parcel ${pluginType} packages must be named according to "parcel-${pluginType}-{name}"`,
);
}
}
const validatePluginName = (pluginType: string, key: string) => {
return (val: string) => {
// allow plugin spread...
if (val === '...') return;
try {
validatePackageName(val, pluginType, key);
} catch (e) {
return e.message;
}
};
};
const validateExtends = (val: string): void => {
// allow relative paths...
if (val.startsWith('.')) return;
try {
validatePackageName(val, 'config', 'extends');
} catch (e) {
return e.message;
}
};
const pipelineSchema = (pluginType: string, key: string): SchemaEntity => {
return {
type: 'array',
items: {
type: 'string',
__validate: validatePluginName(pluginType, key),
},
};
};
const mapPipelineSchema = (pluginType: string, key: string): SchemaEntity => {
return {
type: 'object',
properties: {},
additionalProperties: pipelineSchema(pluginType, key),
};
};
const mapStringSchema = (pluginType: string, key: string): SchemaEntity => {
return {
type: 'object',
properties: {},
additionalProperties: {
type: 'string',
__validate: validatePluginName(pluginType, key),
},
};
};
export default {
type: 'object',
properties: {
$schema: {
type: 'string',
},
extends: {
oneOf: [
{
type: 'string',
__validate: validateExtends,
},
{
type: 'array',
items: {
type: 'string',
__validate: validateExtends,
},
},
],
},
bundler: {
type: 'string',
__validate: (validatePluginName('bundler', 'bundler'): string => void),
},
resolvers: (pipelineSchema('resolver', 'resolvers'): SchemaEntity),
transformers: (mapPipelineSchema(
'transformer',
'transformers',
): SchemaEntity),
validators: (mapPipelineSchema('validator', 'validators'): SchemaEntity),
namers: (pipelineSchema('namer', 'namers'): SchemaEntity),
packagers: (mapStringSchema('packager', 'packagers'): SchemaEntity),
optimizers: (mapPipelineSchema('optimizer', 'optimizers'): SchemaEntity),
compressors: (mapPipelineSchema('compressor', 'compressors'): SchemaEntity),
reporters: (pipelineSchema('reporter', 'reporters'): SchemaEntity),
runtimes: (pipelineSchema('runtime', 'runtimes'): SchemaEntity),
filePath: {
type: 'string',
},
resolveFrom: {
type: 'string',
},
},
additionalProperties: false,
};