-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
131 lines (104 loc) · 3.3 KB
/
index.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
// TODO -
// initialize an idyll instance
// - write out a string that represents the
// - parsed version, passing AST into the idyll-document
// - only include the necessary components
// const {getOptions} = require('loader-utils')
const compile = require('idyll-compiler');
const Idyll = require('idyll');
const { dirname } = require('path');
const { paramCase } = require('change-case');
const {
getNodesByType,
getProperty,
filterNodes,
getType
} = require('idyll-ast');
const getComponentNodes = ast => {
const ignoreTypes = new Set(['var', 'data', 'meta', 'derived']);
let filter = filterNodes(ast, node => {
if (node.type === 'textnode') {
return false;
}
return !ignoreTypes.has(getType(node).toLowerCase());
});
return filter;
};
const getDataNodes = ast => {
const nodes = getNodesByType(ast, 'data');
return nodes.map(node => {
return {
node,
name: getProperty(node, 'name'),
source: getProperty(node, 'source'),
async: getProperty(node, 'async')
};
});
};
const loader = async function (content) {
const callback = this.async()
// const options = Object.assign({}, getOptions(this), {
// filepath: this.resourcePath
// });
const options = {};
const workingDir = dirname(this.resourcePath);
const idyll = Idyll({
inputFile: this.resourcePath,
output: workingDir + '/build/',
componentFolder: workingDir + '/components/',
dataFolder: workingDir + '/data',
layout: 'centered',
theme: 'default'
});
const resolvers = idyll.getResolvers();
const { ComponentResolver } = idyll.getResolverConstructors();
const paths = idyll.getPaths();
const opts = idyll.getOptions();
let ast;
try {
ast = await compile(content);
} catch (err) {
return callback(err);
}
resolvers.set('components', new ComponentResolver(opts, paths));
let nameArray = [];
getComponentNodes(ast).forEach(node => {
if (['var', 'derived', 'data'].indexOf(node.type) > -1) {
nameArray.push(node.type);
} else {
// console.log('checking component names - ', node);
nameArray.push(node.name.split('.')[0]);
}
});
const uniqueComponents = Array.from(new Set(nameArray));
const components = uniqueComponents.reduce((acc, name) => {
let resolved = resolvers.get('components').resolve(name);
if (resolved) acc[paramCase(name)] = resolved;
return acc;
}, {});
const data = getDataNodes(ast).reduce(
(acc, { name, source, async }) => {
let { resolvedName, data } = resolvers
.get('data')
.resolve(name, source, async);
acc[resolvedName] = data;
return acc;
},
{}
);
const code = `
import React from 'react'
import IdyllDocument from 'idyll-document'
var ast = ${JSON.stringify(ast)};
var components = { ${Object.keys(components).map(k => { return `"${k}": require("${components[k]}")` }).join(', ')} };
var datasets = { ${Object.keys(data).map(k => { return `"${k}": require("${data[k]}")` }).join(', ')} };
var opts = ${JSON.stringify(options)};
var layout = opts.layout;
var theme = opts.theme;
var context = () => {};
export default function() {
return <IdyllDocument ast={ast} components={components} datasets={datasets} context={context} layout={layout} theme={theme} />;
}`
return callback(null, code)
}
module.exports = loader