UNPKG

3.27 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.validatePackageName = validatePackageName;
7exports.default = void 0;
8
9var _assert = _interopRequireDefault(require("assert"));
10
11function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
13// Reasoning behind this validation:
14// https://github.com/parcel-bundler/parcel/issues/3397#issuecomment-521353931
15function validatePackageName(pkg, pluginType, key) {
16 // $FlowFixMe
17 if (!pkg) {
18 return;
19 }
20
21 (0, _assert.default)(typeof pkg === 'string', `"${key}" must be a string`);
22
23 if (pkg.startsWith('@parcel')) {
24 (0, _assert.default)(pkg.replace(/^@parcel\//, '').startsWith(`${pluginType}-`), `Official parcel ${pluginType} packages must be named according to "@parcel/${pluginType}-{name}"`);
25 } else if (pkg.startsWith('@')) {
26 let [scope, name] = pkg.split('/');
27 (0, _assert.default)(name.startsWith(`parcel-${pluginType}-`), `Scoped parcel ${pluginType} packages must be named according to "${scope}/parcel-${pluginType}-{name}"`);
28 } else {
29 (0, _assert.default)(pkg.startsWith(`parcel-${pluginType}-`), `Parcel ${pluginType} packages must be named according to "parcel-${pluginType}-{name}"`);
30 }
31}
32
33const validatePluginName = (pluginType, key) => {
34 return val => {
35 // allow plugin spread...
36 if (val === '...') return;
37
38 try {
39 validatePackageName(val, pluginType, key);
40 } catch (e) {
41 return e.message;
42 }
43 };
44};
45
46const validateExtends = val => {
47 // allow relative paths...
48 if (val.startsWith('.')) return;
49
50 try {
51 validatePackageName(val, 'config', 'extends');
52 } catch (e) {
53 return e.message;
54 }
55};
56
57const pipelineSchema = (pluginType, key) => {
58 return {
59 type: 'array',
60 items: {
61 type: 'string',
62 __validate: validatePluginName(pluginType, key)
63 }
64 };
65};
66
67const mapPipelineSchema = (pluginType, key) => {
68 return {
69 type: 'object',
70 properties: {},
71 additionalProperties: pipelineSchema(pluginType, key)
72 };
73};
74
75const mapStringSchema = (pluginType, key) => {
76 return {
77 type: 'object',
78 properties: {},
79 additionalProperties: {
80 type: 'string',
81 __validate: validatePluginName(pluginType, key)
82 }
83 };
84};
85
86var _default = {
87 type: 'object',
88 properties: {
89 extends: {
90 oneOf: [{
91 type: 'string',
92 __validate: validateExtends
93 }, {
94 type: 'array',
95 items: {
96 type: 'string',
97 __validate: validateExtends
98 }
99 }]
100 },
101 bundler: {
102 type: 'string',
103 __validate: validatePluginName('bundler', 'bundler')
104 },
105 resolvers: pipelineSchema('resolver', 'resolvers'),
106 transforms: mapPipelineSchema('transformer', 'transforms'),
107 validators: mapPipelineSchema('validator', 'validators'),
108 namers: pipelineSchema('namer', 'namers'),
109 packagers: mapStringSchema('packager', 'packagers'),
110 optimizers: mapPipelineSchema('optimizer', 'optimizers'),
111 reporters: pipelineSchema('reporter', 'reporters'),
112 runtimes: mapPipelineSchema('runtime', 'runtimes'),
113 filePath: {
114 type: 'string'
115 },
116 resolveFrom: {
117 type: 'string'
118 }
119 },
120 additionalProperties: false
121};
122exports.default = _default;
\No newline at end of file