-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathmixin.core.js
93 lines (79 loc) · 2.25 KB
/
mixin.core.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
const { Mixin } = require('hops-mixin');
const {
internal: { createWebpackMiddleware },
} = require('hops-webpack');
function exists(path) {
try {
require.resolve(path);
return true;
} catch (e) {
return false;
}
}
function tryResolve(name) {
try {
return require.resolve(name);
} catch (e) {
return '';
}
}
class GraphQLMixin extends Mixin {
configureServer(rootApp, middleware, mode) {
if (
!exists(this.config.graphqlMockSchemaFile) ||
process.env.NODE_ENV === 'production'
) {
return;
}
middleware.initial.push({
path: this.config.graphqlMockServerPath,
handler: createWebpackMiddleware(
['graphql-mock-server', 'node'],
mode === 'develop',
this
),
});
}
configureBuild(webpackConfig, loaderConfigs, target) {
const { allLoaderConfigs } = loaderConfigs;
if (
!allLoaderConfigs.find(({ loader }) => loader === 'graphql-tag/loader')
) {
allLoaderConfigs.splice(allLoaderConfigs.length - 1, 0, {
test: /\.(graphql|gql)$/,
loader: 'graphql-tag/loader',
});
}
webpackConfig.externals.push('encoding');
if (process.env.NODE_ENV === 'production') {
return;
}
if (exists(this.config.graphqlMockSchemaFile)) {
webpackConfig.resolve.alias['hops-apollo-mock-server/schema'] =
require.resolve(this.config.graphqlMockSchemaFile);
}
if (exists(this.config.graphqlMockContextExtenderFile)) {
webpackConfig.resolve.alias['hops-apollo-mock-server/context-extender'] =
require.resolve(this.config.graphqlMockContextExtenderFile);
}
if (target === 'graphql-mock-server') {
webpackConfig.externals.push(
{
'apollo-server-express': tryResolve('apollo-server-express'),
express: tryResolve('express'),
graphql: tryResolve('graphql'),
},
'graphql-tools',
/^@graphql-tools\/.+$/
);
webpackConfig.output.filename = 'hops-graphql-mock-server.js';
webpackConfig.resolve.alias['hops/entrypoint'] = require.resolve(
'./lib/mock-server-middleware'
);
}
}
diagnose({ detectDuplicatePackages }) {
detectDuplicatePackages('graphql');
}
}
module.exports = GraphQLMixin;