-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
103 lines (82 loc) · 3.03 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
/* jshint node: true */
'use strict';
var Funnel = require('broccoli-funnel');
var cashaySchema = require('broccoli-cashay-schema');
var esTranspiler = require('broccoli-babel-transpiler');
var merge = require('broccoli-merge-trees');
var path = require('path');
var WebpackWriter = require('broccoli-webpack');
module.exports = {
name: 'ember-cashay',
isDevelopingAddon: function() {
return false;
},
included: function(app) {
// see: https://github.com/ember-cli/ember-cli/issues/3718
if (typeof(app.import) !== 'function' && app.app) {
app = app.app;
}
app.import('vendor/cashay.amd.js');
this.app = app;
// Get build config from ember-cli-build.js
this.addonBuildConfig = app.options['ember-cashay'] || {};
// Get addon config from the usual config/environment.js
this.addonConfig = app.project.config(app.env)['ember-cashay'] || {};
this._super.included.apply(this, arguments);
var rootDirectory;
// Allow the build config to override the root directory
// e.g. "tests/dummy/"
if (this.addonBuildConfig['rootDirectory']) {
rootDirectory = path.join(app.project.root, this.addonBuildConfig['rootDirectory']);
} else {
rootDirectory = app.project.root;
}
// Get the directory of the user-provided graphql schema
// DEFAULT: graphql-server
this.graphqlDir = this.addonConfig['schema-directory'];
this.graphqlDir = path.join(rootDirectory, this.graphqlDir);
// Get the (app prefixed) output dir for the client-safe schema
// DEFAULT: app/graphql-client
this.clientOutputDir = this.addonConfig['clientOutputDir'] || 'graphql-client';
// Get the (app prefixed) output dir for the server schema (non-prod only)
// DEFAULT: app/graphql-server
this.serverOutputDir = this.addonConfig['serverOutputDir'] || 'graphql-server';
},
treeForApp: function(appTree) {
var trees = [ appTree ];
// Add the server schema (non-prod)
// TODO: Make the check for `enabled` respect explicit `false`
if (this.app.env !== 'production' || this.addonConfig['copy-server-schema']) {
var serverTree = new Funnel(this.graphqlDir, {
destDir: this.serverOutputDir
});
trees.push(serverTree);
}
// Add the client-safe schema
var clientTree = cashaySchema(esTranspiler(this.graphqlDir), {
cashay: require('cashay'),
graphql: require('graphql'),
clientSchemaPath: path.join(this.clientOutputDir, 'schema.js')
});
trees.push(clientTree);
return merge(trees);
},
treeForVendor: function(tree) {
const cashayPath = path.dirname(require.resolve('cashay'));
const cashayTree = new WebpackWriter([ cashayPath ], {
entry: './index.js',
output: {
library: 'cashay',
libraryTarget: 'amd',
filename: 'cashay.amd.js',
}
});
if (!tree) {
return this._super.treeForVendor.call(this, cashayTree);
}
const trees = merge([cashayTree, tree], {
overwrite: true
});
return this._super.treeForVendor.call(this, trees);
}
};