forked from plone/volto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addon-registry.js
374 lines (334 loc) · 11.5 KB
/
addon-registry.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
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
/* eslint no-console: 0 */
const glob = require('glob').sync;
const path = require('path');
const fs = require('fs');
const { map } = require('lodash');
const { DepGraph } = require('dependency-graph');
function getPackageBasePath(base) {
while (!fs.existsSync(`${base}/package.json`)) {
base = path.join(base, '../');
}
return path.resolve(base);
}
function fromEntries(pairs) {
const res = {};
pairs.forEach((p) => {
res[p[0]] = p[1];
});
return res;
}
function buildDependencyGraph(addons, extractDependency) {
// getAddonsLoaderChain
const graph = new DepGraph({ circular: true });
graph.addNode('@package');
const seen = ['@package'];
const stack = [['@package', addons]];
while (stack.length > 0) {
const [pkgName, addons] = stack.shift();
if (!graph.hasNode(pkgName)) {
graph.addNode(pkgName, []);
}
if (!seen.includes(pkgName)) {
stack.push([pkgName, extractDependency(pkgName)]);
seen.push(pkgName);
}
addons.forEach((loaderString) => {
const [name, extra] = loaderString.split(':');
if (!graph.hasNode(name)) {
graph.addNode(name, []);
}
const data = graph.getNodeData(name) || [];
if (extra) {
extra.split(',').forEach((funcName) => {
if (!data.includes(funcName)) data.push(funcName);
});
}
graph.setNodeData(name, data);
graph.addDependency(pkgName, name);
if (!seen.includes(name)) {
stack.push([name, extractDependency(name)]);
}
});
}
return graph;
}
/**
* Given an addons loader string, it generates an addons loader string with
* a resolved chain of dependencies
*/
function getAddonsLoaderChain(graph) {
return graph.dependenciesOf('@package').map((name) => {
const extras = graph.getNodeData(name) || [].join(',');
return extras.length ? `${name}:${extras}` : name;
});
}
/**
* A registry to discover and publish information about the structure of Volto
* projects and their addons.
*
* The registry builds information about both addons and other development
* packages, structured as an object with the following information:
*
* - name
* - isPublishedPackage (just for info, to distinguish addons from other Javascript development packages)
* - modulePath (the path that would be resolved from Javascript package name)
* - packageJson (the path to the addon's package.json file)
* - extraConfigLoaders (names for extra functions to be loaded from the addon
* for configuration purposes)
* - razzleExtender (the path to the addon's razzle.extend.js path, to allow
* addons to customize the webpack configuration)
*
*/
class AddonConfigurationRegistry {
constructor(projectRootPath) {
const packageJson = (this.packageJson = require(path.join(
projectRootPath,
'package.json',
)));
this.projectRootPath = projectRootPath;
this.voltoPath =
packageJson.name === '@plone/volto'
? `${projectRootPath}`
: `${projectRootPath}/node_modules/@plone/volto`;
this.addonNames = (packageJson.addons || []).map((s) => s.split(':')[0]);
this.packages = {};
this.customizations = new Map();
this.initDevelopmentPackages();
this.initPublishedPackages();
this.initTestingPackages();
this.dependencyGraph = buildDependencyGraph(
packageJson.addons || [],
(name) => {
this.initPublishedPackage(name);
return this.packages[name].addons || [];
},
);
this.initRazzleExtenders();
}
/**
* Use jsconfig.js to get paths for "development" packages/addons (coming from mrs.developer.json)
* Not all of these packages have to be Volto addons.
*/
initDevelopmentPackages() {
if (fs.existsSync(`${this.projectRootPath}/jsconfig.json`)) {
const jsConfig = require(`${this.projectRootPath}/jsconfig`)
.compilerOptions;
const pathsConfig = jsConfig.paths;
Object.keys(pathsConfig).forEach((name) => {
if (!this.addonNames.includes(name)) this.addonNames.push(name);
const packagePath = `${this.projectRootPath}/${jsConfig.baseUrl}/${pathsConfig[name][0]}`;
const packageJsonPath = `${getPackageBasePath(
packagePath,
)}/package.json`;
const pkg = {
modulePath: packagePath,
packageJson: packageJsonPath,
isPublishedPackage: false,
name,
addons: require(packageJsonPath).addons || [],
};
this.packages[name] = Object.assign(this.packages[name] || {}, pkg);
});
}
}
/**
* Add path to the "src" of npm-released packages. These packages can
* release their source code in src, or transpile. The "main" of their
* package.json needs to point to the module that exports `applyConfig` as
* default.
*/
initPublishedPackages() {
this.addonNames.forEach(this.initPublishedPackage.bind(this));
}
initPublishedPackage(name) {
if (!Object.keys(this.packages).includes(name)) {
if (!this.addonNames.includes(name)) this.addonNames.push(name);
const resolved = require.resolve(name, { paths: [this.projectRootPath] });
const basePath = getPackageBasePath(resolved);
const packageJson = path.join(basePath, 'package.json');
const pkg = require(packageJson);
const main = pkg.main || 'src/index.js';
const modulePath = path.dirname(require.resolve(`${basePath}/${main}`));
this.packages[name] = {
name,
isPublishedPackage: true,
modulePath,
packageJson,
addons: pkg.addons || [],
};
}
}
initTestingPackages() {
if (process.env.RAZZLE_TESTING_ADDONS) {
process.env.RAZZLE_TESTING_ADDONS.split(',').forEach(
this.initTestingPackage.bind(this),
);
}
}
initTestingPackage(name) {
const normalizedAddonName = name.split(':')[0];
const testingPackagePath = `${this.projectRootPath}/packages/${normalizedAddonName}/src`;
if (fs.existsSync(testingPackagePath)) {
const basePath = getPackageBasePath(testingPackagePath);
const packageJson = path.join(basePath, 'package.json');
if (!this.addonNames.includes(normalizedAddonName))
this.addonNames.push(normalizedAddonName);
const pkg = {
modulePath: testingPackagePath,
packageJson: packageJson,
isPublishedPackage: false,
name: normalizedAddonName,
addons: require(packageJson).addons || [],
};
this.packages[normalizedAddonName] = Object.assign(
this.packages[normalizedAddonName] || {},
pkg,
);
}
}
/**
* Allow addons to provide razzle.config extenders. These extenders
* modules (named razzle.extend.js) need to provide two functions:
* `plugins(defaultPlugins) => plugins` and
* `modify(...) => config`
*/
initRazzleExtenders() {
this.getAddons().forEach((addon) => {
const base = path.dirname(addon.packageJson);
const razzlePath = path.resolve(`${base}/razzle.extend.js`);
if (fs.existsSync(razzlePath)) {
addon.razzleExtender = razzlePath;
}
});
}
/**
* Returns the addon records, respects order from package.json:addons
*/
getAddons() {
return this.dependencyGraph
.dependenciesOf('@package')
.map((name) => this.packages[name]);
}
getAddonExtenders() {
return this.getAddons()
.map((o) => o.razzleExtender)
.filter((e) => e);
}
/**
* Returns a mapping name:diskpath to be uses in webpack's resolve aliases
*/
getResolveAliases() {
const pairs = Object.keys(this.packages).map((o) => [
o,
this.packages[o].modulePath,
]);
return fromEntries(pairs);
}
/**
* Retrieves a mapping (to be used in resolve aliases) of module name to
* filestype paths, to be used as customization paths.
*
* There are two styles of customizations: "classic style" and "addons style"
*
* In the classic style, the customization folders are only used to customize
* Volto itself, so inside it we'll find a mirror of the Volto project
* structure.
*
* In the "addons style" customization folder, we allow customization of
* addons along with Volto customization, so we need separate folders. By
* convention, we use a folder called "volto" inside customizations folder
* and separate folder for each addon, identified by its addon (package) name.
*/
getCustomizationPaths(packageJson, rootPath) {
const aliases = {};
let { customizationPaths } = packageJson;
if (!customizationPaths) {
customizationPaths = ['src/customizations'];
}
customizationPaths.forEach((customizationPath) => {
customizationPath = customizationPath.endsWith('/')
? customizationPath.slice(0, customizationPath.length - 1)
: customizationPath;
const base = path.join(rootPath, customizationPath);
const reg = [];
// All registered addon packages (in jsconfig.json and package.json:addons) can be customized
Object.values(this.packages).forEach((addon) => {
const { name, modulePath } = addon;
if (fs.existsSync(path.join(base, name))) {
reg.push({
customPath: path.join(base, name),
sourcePath: modulePath,
name,
});
}
});
reg.push(
fs.existsSync(path.join(base, 'volto'))
? {
// new style (addons) customizations
customPath: path.join(base, 'volto'),
sourcePath: `${this.voltoPath}/src/`,
name: '@plone/volto',
}
: {
// old style, customizations directly in folder
customPath: base,
sourcePath: `${this.voltoPath}/src/`,
name: '@plone/volto',
},
);
reg.forEach(({ customPath, name, sourcePath }) => {
map(
glob(`${customPath}/**/*.*(svg|png|jpg|jpeg|gif|ico|less|js|jsx)`),
(filename) => {
const targetPath = filename.replace(customPath, sourcePath);
if (fs.existsSync(targetPath)) {
aliases[
filename.replace(customPath, name).replace(/\.(js|jsx)$/, '')
] = path.resolve(filename);
} else {
console.log(
`The file ${filename} doesn't exist in the ${name} (${targetPath}), unable to customize.`,
);
}
},
);
});
});
return aliases;
}
getProjectCustomizationPaths() {
return this.getCustomizationPaths(this.packageJson, this.projectRootPath);
}
/**
* Allow addons to customize Volto and other addons.
*
* We follow the same logic for overriding as the main `package.json:addons`.
* First declared addon gets overridden by subsequent declared addons. That
* means: customization in volto-addonA will potentially be rewritten by
* customizations in volto-addonB if the declaration in package.json is like:
* `addons:volto-addonA,volto-addonB`
*/
getAddonCustomizationPaths() {
let aliases = {};
this.getAddons().forEach((addon) => {
aliases = {
...aliases,
...this.getCustomizationPaths(
require(addon.packageJson),
getPackageBasePath(addon.modulePath),
),
};
});
return aliases;
}
/**
* Returns an agregated, dependency-resolved list of addon loader strings
*/
getAddonDependencies() {
return getAddonsLoaderChain(this.dependencyGraph);
}
}
module.exports = AddonConfigurationRegistry;
module.exports.getAddonsLoaderChain = getAddonsLoaderChain;
module.exports.buildDependencyGraph = buildDependencyGraph;