forked from styleguidist/react-styleguidist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyleguide.loader.js
104 lines (85 loc) · 2.93 KB
/
styleguide.loader.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
var fs = require('fs');
var path = require('path');
var glob = require('glob');
var prettyjson = require('prettyjson');
var _ = require('lodash');
var config = require('../src/utils/config');
function processComponent(filepath) {
// If component name can’t be detected in runtime use filename or folder name (if file name is 'index')
var filename = path.basename(filepath).replace(/\.\w+$/, '');
var nameFallbak = filename === 'index' ? path.basename(path.dirname(filepath)) : filename;
return '{' + [
'filepath: ' + JSON.stringify(filepath),
'nameFallbak: ' + JSON.stringify(nameFallbak),
'pathLine: ' + JSON.stringify(config.getComponentPathLine(path.relative(config.configDir, filepath))),
'module: ' + requireIt(filepath),
'props: ' + requireIt('!!props!' + filepath),
'examples: ' + getExamples(filepath, nameFallbak)
].join(',') + '}';
}
function getExamples(filepath, nameFallbak) {
var examplesFile = config.getExampleFilename(filepath);
if (hasExamples(filepath)) {
return requireIt('examples!' + examplesFile);
}
if (config.defaultExample) {
return requireIt('examples?componentName=' + nameFallbak + '!' + config.defaultExample);
}
return null;
}
function hasExamples(filepath) {
var examplesFile = config.getExampleFilename(filepath);
return !!fs.existsSync(examplesFile);
}
function requireIt(filepath) {
return 'require(' + JSON.stringify(filepath) + ')';
}
function processComponentsSource(components, config) {
if (!components) return null;
var componentSources;
if (typeof components === 'function') {
componentSources = components();
}
else {
componentSources = glob.sync(path.resolve(config.configDir, components));
}
if (config.verbose) {
console.log();
console.log('Loading components:');
console.log(prettyjson.render(componentSources));
console.log();
}
if (config.skipComponentsWithoutExample) {
componentSources = _.filter(componentSources, hasExamples);
}
return '[' + componentSources.map(processComponent).join(',') + ']';
}
function processSection(section, config) {
return '{' + [
'name: ' + JSON.stringify(section.name),
'content: ' + (section.content ? requireIt('examples!' + section.content) : null),
'components: ' + processComponentsSource(section.components, config),
'sections: ' + processSectionsList(section.sections, config)
].join(',') + '}';
}
function processSectionsList(sections, config) {
if (!sections) return null;
return '[' +
sections.map(function(section) { return processSection(section, config); }).join(',') +
']';
}
module.exports = function() {};
module.exports.pitch = function() {
this.cacheable && this.cacheable();
var simplifiedConfig = _.pick(config, [
'title',
'highlightTheme'
]);
return [
'module.exports = {',
' config: ' + JSON.stringify(simplifiedConfig) + ',',
' components: ' + processComponentsSource(config.components, config) + ',',
' sections: ' + processSectionsList(config.sections, config),
'};'
].join('\n');
};