Skip to content

Commit d711c81

Browse files
committed
[RFR] Fix webpack build
- [x] One target for node - [x] Another for the browser
1 parent acaf7c0 commit d711c81

File tree

6 files changed

+43
-40
lines changed

6 files changed

+43
-40
lines changed

example/index.html

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
<!doctype html>
22
<html lang="en">
3-
<head>
4-
<meta charset="UTF-8">
5-
</head>
3+
<head></head>
64
<body>
75
<button id="button">Load posts</button>
8-
<script src="../lib/json-graphql-server.min.js"></script>
6+
<script src="../lib/json-graphql-server.client.min.js"></script>
97
<script type="text/javascript">
108
window.addEventListener('load', function() {
119
const data = {
@@ -35,29 +33,29 @@
3533
],
3634
};
3735

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);
36+
const server = GraphQLClientServer({
37+
data,
38+
url: 'http://localhost:3000/graphql'
6039
});
40+
41+
server.start();
42+
});
43+
window.document.getElementById('button').addEventListener('click', function () {
44+
const xhr = new XMLHttpRequest();
45+
xhr.responseType = 'json';
46+
xhr.open("POST", "http://localhost:3000/graphql", true);
47+
xhr.setRequestHeader("Content-Type", "application/json");
48+
xhr.setRequestHeader("Accept", "application/json");
49+
xhr.onerror = function(error) {
50+
console.error(error);
51+
}
52+
xhr.onload = function() {
53+
const result = JSON.parse(xhr.responseText);
54+
console.log('data returned:', result);
55+
alert('Found ' + result.data.allPosts.length + ' posts');
56+
}
57+
const body = JSON.stringify({ query: 'query allPosts { allPosts { id } }' });
58+
xhr.send(body);
6159
});
6260
</script>
6361
</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.GraphQLClientServer = 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: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ if (process.env.NODE_ENV === 'production') {
1818
'process.env.NODE_ENV': JSON.stringify('production'),
1919
})
2020
);
21-
outputFile = libraryName + '.min.js';
21+
outputFile = libraryName + '.[name].min.js';
2222
} else {
23-
outputFile = libraryName + '.js';
23+
outputFile = libraryName + '.[name].js';
2424
}
2525

2626
const config = {
27-
entry: __dirname + '/src/index.js',
27+
entry: {
28+
node: __dirname + '/src/node.js',
29+
client: __dirname + '/src/client.js',
30+
},
2831
devtool: 'source-map',
2932
output: {
3033
path: __dirname + '/lib',

0 commit comments

Comments
 (0)