Skip to content

Commit da1fc6a

Browse files
authored
Merge pull request #35 from marmelab/fix_build
[RFR] Fix webpack build
2 parents acaf7c0 + 0258f61 commit da1fc6a

File tree

8 files changed

+79
-63
lines changed

8 files changed

+79
-63
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -419,14 +419,14 @@ Add a `script` tag referencing the library:
419419
<script src="../lib/json-graphql-server.min.js"></script>
420420
```
421421

422-
It will expose the `GraphQLClientServer` as a global object:
422+
It will expose the `JsonGraphqlServer` as a global object:
423423

424424
```html
425425
<script type="text/javascript">
426426
window.addEventListener('load', function() {
427427
const data = [...];
428428
429-
const server = GraphQLClientServer({
429+
const server = JsonGraphqlServer({
430430
data,
431431
url: 'http://localhost:3000/graphql'
432432
});
@@ -458,11 +458,11 @@ npm install json-graphql-server
458458
```
459459

460460
```js
461-
import GraphQLClientServer from 'json-graphql-server';
461+
import JsonGraphqlServer from 'json-graphql-server';
462462

463463
const data = [...];
464464

465-
const server = GraphQLClientServer({
465+
const server = JsonGraphqlServer({
466466
data,
467467
url: 'http://localhost:3000/graphql'
468468
});
@@ -489,10 +489,10 @@ xhr.send(body);
489489

490490
```js
491491
import fetchMock from 'fetch-mock';
492-
import GraphQLClientServer from 'json-graphql-server';
492+
import JsonGraphqlServer from 'json-graphql-server';
493493

494494
const data = [...];
495-
const server = GraphQLClientServer({ data });
495+
const server = JsonGraphqlServer({ data });
496496

497497
fetchMock.post('http://localhost:3000/graphql', server.getHandler());
498498

bin/json-graphql-server.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
#!/usr/bin/env node
22
require('reify');
3-
const path = require('path');
4-
const express = require('express');
5-
const cors = require('cors');
6-
const JsonGraphqlServer = require('../src/');
7-
8-
// fixme the build fails without those
9-
global.window = false;
10-
global.document = false;
11-
global.navigator = false;
3+
var path = require('path');
4+
var express = require('express');
5+
var cors = require('cors');
6+
var JsonGraphqlServer = require('../lib/json-graphql-server.node.min').default;
127

138
var dataFilePath = process.argv.length > 2 ? process.argv[2] : './data.json';
149
var data = require(path.join(process.cwd(), dataFilePath));
1510
var PORT = 3000;
1611
var app = express();
1712

1813
app.use(cors());
19-
app.use('/', JsonGraphqlServer.jsonGraphqlExpress(data));
14+
app.use('/', JsonGraphqlServer(data));
2015
app.listen(PORT);
2116
var msg = `GraphQL server running with your data at http://localhost:${PORT}/`;
2217
console.log(msg); // eslint-disable-line no-console
18+
19+
process.on('unhandledRejection', (reason, p) => {
20+
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
21+
});

example/index.html

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<!doctype html>
22
<html lang="en">
33
<head>
4-
<meta charset="UTF-8">
4+
<meta charset="utf-8" />
55
</head>
66
<body>
77
<button id="button">Load posts</button>
8-
<script src="../lib/json-graphql-server.min.js"></script>
8+
<script src="../lib/json-graphql-server.client.min.js"></script>
99
<script type="text/javascript">
1010
window.addEventListener('load', function() {
1111
const data = {
@@ -35,29 +35,28 @@
3535
],
3636
};
3737

38-
try {
39-
const server = GraphQLClientServer({ data, url: "http://localhost:3000/graphql" });
40-
server.start();
41-
} catch(error) {
42-
console.error({error});
43-
}
44-
45-
window.document.getElementById('button').addEventListener('click', function () {
46-
const xhr = new XMLHttpRequest();
47-
xhr.open("POST", "http://localhost:3000/graphql", true);
48-
xhr.setRequestHeader("Content-Type", "application/json");
49-
xhr.setRequestHeader("Accept", "application/json");
50-
xhr.onerror = function(error) {
51-
console.error(error);
52-
}
53-
xhr.onload = function() {
54-
const result = JSON.parse(xhr.responseText);
55-
console.log('data returned:', result);
56-
alert('Found ' + result.data.allPosts.length + ' posts');
57-
}
58-
const body = JSON.stringify({ query: 'query allPosts { allPosts { id } }' });
59-
xhr.send(body);
38+
const server = JsonGraphqlServer({
39+
data,
40+
url: 'http://localhost:3000/graphql'
6041
});
42+
43+
server.start();
44+
});
45+
window.document.getElementById('button').addEventListener('click', function () {
46+
const xhr = new XMLHttpRequest();
47+
xhr.open("POST", "http://localhost:3000/graphql", true);
48+
xhr.setRequestHeader("Content-Type", "application/json");
49+
xhr.setRequestHeader("Accept", "application/json");
50+
xhr.onerror = function(error) {
51+
console.error(error);
52+
}
53+
xhr.onload = function() {
54+
const result = JSON.parse(xhr.responseText);
55+
console.log('data returned:', result);
56+
alert('Found ' + result.data.allPosts.length + ' posts');
57+
}
58+
const body = JSON.stringify({ query: 'query allPosts { allPosts { id } }' });
59+
xhr.send(body);
6160
});
6261
</script>
6362
</body>

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"name": "json-graphql-server",
33
"version": "1.0.2",
4-
"main": "src/index",
4+
"main": "lib/json-graphql-server.node.min.js",
5+
"browser": "lib/json-graphql-server.client.min.js",
56
"repository": "git@github.com:marmelab/json-graphql-server.git",
67
"authors": [
78
"François Zaninotto",
@@ -62,8 +63,7 @@
6263
"graphql-type-json": "~0.1.4",
6364
"inflection": "~1.12.0",
6465
"lodash.merge": "~4.6.0",
65-
"reify": "~0.12.0",
66-
"xhr-mock": "2.0.2"
66+
"reify": "~0.12.0"
6767
},
6868
"bin": {
6969
"json-graphql-server": "bin/json-graphql-server.js"

src/client.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import GraphQLClientServer from './graphQLClientServer';
2+
3+
if (typeof window !== 'undefined') {
4+
window.JsonGraphqlServer = GraphQLClientServer;
5+
}
6+
7+
export default GraphQLClientServer;

src/index.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/node.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import jsonGraphqlExpress from './jsonGraphqlExpress';
2+
3+
export default jsonGraphqlExpress;

webpack.config.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,13 @@ if (process.env.NODE_ENV === 'production') {
1818
'process.env.NODE_ENV': JSON.stringify('production'),
1919
})
2020
);
21-
outputFile = libraryName + '.min.js';
21+
outputFile = target => `${libraryName}.${target}.min.js`;
2222
} else {
23-
outputFile = libraryName + '.js';
23+
outputFile = target => `${libraryName}.${target}.js`;
2424
}
2525

26-
const config = {
27-
entry: __dirname + '/src/index.js',
26+
const defaultConfig = {
2827
devtool: 'source-map',
29-
output: {
30-
path: __dirname + '/lib',
31-
filename: outputFile,
32-
library: libraryName,
33-
libraryTarget: 'umd',
34-
umdNamedDefine: true,
35-
},
3628
module: {
3729
rules: [
3830
{
@@ -49,4 +41,28 @@ const config = {
4941
plugins,
5042
};
5143

52-
module.exports = config;
44+
const serverConfig = Object.assign({}, defaultConfig, {
45+
target: 'node',
46+
entry: __dirname + '/src/node.js',
47+
output: {
48+
path: path.resolve(__dirname, 'lib'),
49+
filename: outputFile('node'),
50+
library: libraryName,
51+
libraryTarget: 'umd',
52+
umdNamedDefine: true,
53+
}
54+
});
55+
56+
const clientConfig = Object.assign({}, defaultConfig, {
57+
target: 'web',
58+
entry: __dirname + '/src/client.js',
59+
output: {
60+
path: path.resolve(__dirname, 'lib'),
61+
filename: outputFile('client'),
62+
library: libraryName,
63+
libraryTarget: 'umd',
64+
umdNamedDefine: true,
65+
}
66+
});
67+
68+
module.exports = [serverConfig, clientConfig];

0 commit comments

Comments
 (0)