-
Notifications
You must be signed in to change notification settings - Fork 12
/
bundle-template.js
61 lines (53 loc) · 2.95 KB
/
bundle-template.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
import path from 'path';
import * as t from 'babel-types';
import template from 'babel-template';
import generate from 'babel-generator';
import {parse} from 'babylon';
function buildImport(replacements) {
const modulePath = replacements.TYPE_MODULE_PATH.value;
return template(`import TYPE_NAME_IDENTIFIER from "./${modulePath}";`, {sourceType: 'module'})(replacements);
}
const buildDeclaration = template('const BUNDLE_MODULE_NAME = {types: {}};');
const buildModuleAssignment = template('BUNDLE_MODULE_NAME.types[TYPE_NAME] = TYPE_NAME_IDENTIFIER;');
const buildRootLevelAssignment = template('BUNDLE_MODULE_NAME.PROPERTY_NAME = PROPERTY_VALUE;');
const buildExport = template('export default recursivelyFreezeObject(BUNDLE_MODULE_NAME);', {sourceType: 'module'});
export default function bundleTemplate({queryTypeName, mutationTypeName, subscriptionTypeName}, types, bundleModuleName) {
const BUNDLE_MODULE_NAME = t.identifier(bundleModuleName);
const QUERY_TYPE_NAME = queryTypeName ? t.stringLiteral(queryTypeName) : t.nullLiteral();
const MUTATION_TYPE_NAME = mutationTypeName ? t.stringLiteral(mutationTypeName) : t.nullLiteral();
const SUBSCRIPTION_TYPE_NAME = subscriptionTypeName ? t.stringLiteral(subscriptionTypeName) : t.nullLiteral();
const typeConfigs = types.map((type) => {
return {
TYPE_NAME: t.stringLiteral(type.name),
TYPE_NAME_IDENTIFIER: t.identifier(type.name),
TYPE_MODULE_PATH: t.stringLiteral(path.join(type.path.replace(/\.js$/, ''))),
BUNDLE_MODULE_NAME
};
});
const imports = typeConfigs.map((typeConfig) => buildImport(typeConfig));
const declaration = buildDeclaration({BUNDLE_MODULE_NAME});
const assignments = typeConfigs.map((typeConfig) => buildModuleAssignment(typeConfig));
const queryTypeAssignment = buildRootLevelAssignment({BUNDLE_MODULE_NAME, PROPERTY_NAME: t.identifier('queryType'), PROPERTY_VALUE: QUERY_TYPE_NAME});
const mutationTypeAssignment = buildRootLevelAssignment({BUNDLE_MODULE_NAME, PROPERTY_NAME: t.identifier('mutationType'), PROPERTY_VALUE: MUTATION_TYPE_NAME});
const subscriptionTypeAssignment = buildRootLevelAssignment({BUNDLE_MODULE_NAME, PROPERTY_NAME: t.identifier('subscriptionType'), PROPERTY_VALUE: SUBSCRIPTION_TYPE_NAME});
const moduleExport = buildExport({BUNDLE_MODULE_NAME});
return parse(`
${imports.map((ast) => generate(ast).code).join('\n')}
${generate(declaration).code}
${assignments.map((ast) => generate(ast).code).join('\n')}
${generate(queryTypeAssignment).code}
${generate(mutationTypeAssignment).code}
${generate(subscriptionTypeAssignment).code}
function recursivelyFreezeObject(structure) {
Object.getOwnPropertyNames(structure).forEach((key) => {
const value = structure[key];
if (value && typeof value === 'object') {
recursivelyFreezeObject(value);
}
});
Object.freeze(structure);
return structure;
}
${generate(moduleExport).code}
`, {sourceType: 'module'});
}