UNPKG

6.55 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _micromatch = require("micromatch");
9
10var _path = require("path");
11
12var _loadParcelPlugin = _interopRequireDefault(require("./loadParcelPlugin"));
13
14function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
16function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
17
18class ParcelConfig {
19 constructor(config, packageManager) {
20 _defineProperty(this, "packageManager", void 0);
21
22 _defineProperty(this, "filePath", void 0);
23
24 _defineProperty(this, "resolvers", void 0);
25
26 _defineProperty(this, "transforms", void 0);
27
28 _defineProperty(this, "bundler", void 0);
29
30 _defineProperty(this, "namers", void 0);
31
32 _defineProperty(this, "runtimes", void 0);
33
34 _defineProperty(this, "packagers", void 0);
35
36 _defineProperty(this, "validators", void 0);
37
38 _defineProperty(this, "optimizers", void 0);
39
40 _defineProperty(this, "reporters", void 0);
41
42 _defineProperty(this, "pluginCache", void 0);
43
44 this.packageManager = packageManager;
45 this.filePath = config.filePath;
46 this.resolvers = config.resolvers || [];
47 this.transforms = config.transforms || {};
48 this.runtimes = config.runtimes || {};
49 this.bundler = config.bundler || '';
50 this.namers = config.namers || [];
51 this.packagers = config.packagers || {};
52 this.optimizers = config.optimizers || {};
53 this.reporters = config.reporters || [];
54 this.validators = config.validators || {};
55 this.pluginCache = new Map();
56 }
57
58 static deserialize(serialized) {
59 return new ParcelConfig(serialized.config, serialized.packageManager);
60 }
61
62 getConfig() {
63 return {
64 filePath: this.filePath,
65 resolvers: this.resolvers,
66 transforms: this.transforms,
67 validators: this.validators,
68 runtimes: this.runtimes,
69 bundler: this.bundler,
70 namers: this.namers,
71 packagers: this.packagers,
72 optimizers: this.optimizers,
73 reporters: this.reporters
74 };
75 }
76
77 serialize() {
78 return {
79 $$raw: false,
80 packageManager: this.packageManager,
81 config: this.getConfig()
82 };
83 }
84
85 loadPlugin(pluginName) {
86 let plugin = this.pluginCache.get(pluginName);
87
88 if (plugin) {
89 return plugin;
90 }
91
92 plugin = (0, _loadParcelPlugin.default)(this.packageManager, pluginName, this.filePath);
93 this.pluginCache.set(pluginName, plugin);
94 return plugin;
95 }
96
97 loadPlugins(plugins) {
98 return Promise.all(plugins.map(async pluginName => {
99 return {
100 name: pluginName,
101 plugin: await this.loadPlugin(pluginName)
102 };
103 }));
104 }
105
106 getResolverNames() {
107 if (this.resolvers.length === 0) {
108 throw new Error('No resolver plugins specified in .parcelrc config');
109 }
110
111 return this.resolvers;
112 }
113
114 getResolvers() {
115 return this.loadPlugins(this.getResolverNames());
116 }
117
118 getValidatorNames(filePath) {
119 let validators = this.matchGlobMapPipelines(filePath, this.validators) || [];
120 return validators;
121 }
122
123 getTransformerNames(filePath, pipeline) {
124 let transformers = this.matchGlobMapPipelines(filePath, this.transforms, pipeline);
125
126 if (!transformers || transformers.length === 0) {
127 throw new Error(`No transformers found for "${filePath}".`);
128 }
129
130 return transformers;
131 }
132
133 getValidators(filePath) {
134 let names = this.getValidatorNames(filePath);
135 return this.loadPlugins(names);
136 }
137
138 getNamedPipelines() {
139 return Object.keys(this.transforms).filter(glob => glob.includes(':')).map(glob => glob.split(':')[0]);
140 }
141
142 getTransformers(filePath, pipeline) {
143 return this.loadPlugins(this.getTransformerNames(filePath, pipeline));
144 }
145
146 getBundler() {
147 if (!this.bundler) {
148 throw new Error('No bundler specified in .parcelrc config');
149 }
150
151 return this.loadPlugin(this.bundler);
152 }
153
154 getNamers() {
155 if (this.namers.length === 0) {
156 throw new Error('No namer plugins specified in .parcelrc config');
157 }
158
159 return this.loadPlugins(this.namers);
160 }
161
162 getRuntimes(context) {
163 let runtimes = this.runtimes[context];
164
165 if (!runtimes) {
166 return Promise.resolve([]);
167 }
168
169 return this.loadPlugins(runtimes);
170 }
171
172 getPackagerName(filePath) {
173 let packagerName = this.matchGlobMap(filePath, this.packagers);
174
175 if (!packagerName) {
176 throw new Error(`No packager found for "${filePath}".`);
177 }
178
179 return packagerName;
180 }
181
182 async getPackager(filePath) {
183 let packagerName = this.getPackagerName(filePath);
184 return {
185 name: packagerName,
186 plugin: await this.loadPlugin(packagerName)
187 };
188 }
189
190 getOptimizerNames(filePath, pipeline) {
191 var _this$matchGlobMapPip;
192
193 return (_this$matchGlobMapPip = this.matchGlobMapPipelines(filePath, this.optimizers, pipeline)) !== null && _this$matchGlobMapPip !== void 0 ? _this$matchGlobMapPip : [];
194 }
195
196 getOptimizers(filePath, pipeline) {
197 let optimizers = this.getOptimizerNames(filePath, pipeline);
198
199 if (optimizers.length === 0) {
200 return Promise.resolve([]);
201 }
202
203 return this.loadPlugins(optimizers);
204 }
205
206 getReporters() {
207 return this.loadPlugins(this.reporters);
208 }
209
210 isGlobMatch(filePath, pattern, pipeline) {
211 let prefix = pipeline ? `${pipeline}:` : '';
212 return (0, _micromatch.isMatch)(prefix + filePath, pattern) || (0, _micromatch.isMatch)(prefix + (0, _path.basename)(filePath), pattern);
213 }
214
215 matchGlobMap(filePath, globMap) {
216 for (let pattern in globMap) {
217 if (this.isGlobMatch(filePath, pattern)) {
218 return globMap[pattern];
219 }
220 }
221
222 return null;
223 }
224
225 matchGlobMapPipelines(filePath, globMap, pipeline) {
226 let matches = [];
227
228 for (let pattern in globMap) {
229 if (this.isGlobMatch(filePath, pattern, pipeline)) {
230 matches.push(globMap[pattern]);
231 }
232 }
233
234 let flatten = () => {
235 let pipeline = matches.shift() || [];
236 let spreadIndex = pipeline.indexOf('...');
237
238 if (spreadIndex >= 0) {
239 pipeline = [...pipeline.slice(0, spreadIndex), ...flatten(), ...pipeline.slice(spreadIndex + 1)];
240 }
241
242 if (pipeline.includes('...')) {
243 throw new Error('Only one spread parameter can be included in a config pipeline');
244 }
245
246 return pipeline;
247 };
248
249 let res = flatten();
250 return res;
251 }
252
253}
254
255exports.default = ParcelConfig;
\No newline at end of file