Skip to content

Commit 2a39b73

Browse files
committed
[WIP] Use webpack to build
- [x] Allow usage of the lib inside a browser using script tag - [x] Allow usage of the binary as before
1 parent c3d6f22 commit 2a39b73

File tree

10 files changed

+607
-436
lines changed

10 files changed

+607
-436
lines changed

.babelrc

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@
2424
"external-helpers",
2525
"transform-runtime"
2626
]
27+
},
28+
"production": {
29+
"presets": [
30+
[
31+
"es2015",
32+
{
33+
"modules": false
34+
}
35+
],
36+
"stage-0"
37+
],
38+
"plugins": [
39+
"external-helpers",
40+
"transform-runtime"
41+
]
2742
}
2843
}
29-
}
44+
}

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@ format: ## Format the source code
2222
@./node_modules/.bin/eslint --fix ./src
2323

2424
run: ## Launch server with example data
25-
@node ./bin/json-graphql-server.js example/data.js
25+
@node ./bin/json-graphql-server.js example/data.js
26+
27+
build:
28+
@NODE_ENV=production ./node_modules/.bin/webpack

bin/json-graphql-server.js

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env node
2-
2+
require('reify');
33
const path = require('path');
44
const express = require('express');
55
const cors = require('cors');

example/index.html

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
<html lang="en">
33
<head></head>
44
<body>
5-
<script src="../lib/index.js"></script>
5+
<button id="button">Load posts</button>
6+
<script src="../lib/json-graphql-server.min.js"></script>
67
<script type="text/javascript">
78
window.addEventListener('load', function() {
89
const data = {
@@ -32,8 +33,9 @@
3233
],
3334
};
3435

35-
var server = JsonGraphqlServer.graphQLClientServer(data);
36-
36+
var server = GraphQLClientServer(data);
37+
});
38+
window.document.getElementById('button').addEventListener('click', function () {
3739
var xhr = new XMLHttpRequest();
3840
xhr.responseType = 'json';
3941
xhr.open("POST", "http://localhost:3000/graphql", true);
@@ -43,9 +45,11 @@
4345
console.error(error);
4446
}
4547
xhr.onload = function() {
46-
console.log('data returned:', JSON.parse(xhr.responseText));
48+
const result = JSON.parse(xhr.responseText);
49+
console.log('data returned:', result);
50+
alert('Found ' + result.data.allPosts.length + ' posts');
4751
}
48-
const body = JSON.stringify({ query: 'query Post($id: ID!) { Post(id: $id) { id } }', variables: { id: 1 } });
52+
const body = JSON.stringify({ query: 'query allPosts { allPosts { id } }' });
4953
xhr.send(body);
5054
});
5155
</script>

package.json

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
"version": "1.0.2",
44
"main": "src/index",
55
"repository": "git@github.com:marmelab/json-graphql-server.git",
6-
"authors": ["François Zaninotto", "Gildas Garcia"],
7-
"files": ["*.md", "src", "bin"],
6+
"authors": [
7+
"François Zaninotto",
8+
"Gildas Garcia"
9+
],
10+
"files": [
11+
"*.md",
12+
"src",
13+
"bin"
14+
],
815
"license": "MIT",
916
"scripts": {
1017
"format": "make format",
@@ -14,14 +21,18 @@
1421
"server": "make run"
1522
},
1623
"lint-staged": {
17-
"src/**/*.js": ["eslint --fix", "git add"]
24+
"src/**/*.js": [
25+
"eslint --fix",
26+
"git add"
27+
]
1828
},
1929
"devDependencies": {
2030
"@types/jest": "~19.2.4",
2131
"babel-cli": "~6.24.1",
2232
"babel-core": "~6.25.0",
2333
"babel-eslint": "~7.2.3",
2434
"babel-jest": "~20.0.3",
35+
"babel-loader": "7.1.2",
2536
"babel-plugin-add-module-exports": "^0.2.1",
2637
"babel-plugin-external-helpers": "~6.22.0",
2738
"babel-plugin-transform-runtime": "~6.23.0",
@@ -36,15 +47,8 @@
3647
"jest": "~20.0.4",
3748
"lint-staged": "~3.4.1",
3849
"prettier": "~1.5.2",
39-
"rollup": "~0.43.0",
40-
"rollup-plugin-babel": "~2.7.1",
41-
"rollup-plugin-commonjs": "~8.0.2",
42-
"rollup-plugin-json": "~2.3.0",
43-
"rollup-plugin-node-builtins": "~2.1.2",
44-
"rollup-plugin-node-globals": "~1.1.0",
45-
"rollup-plugin-node-resolve": "~3.0.0",
46-
"rollup-watch": "~4.0.0",
47-
"supertest": "~3.0.0"
50+
"supertest": "~3.0.0",
51+
"webpack": "~3.10.0"
4852
},
4953
"dependencies": {
5054
"apollo-client": "~1.2.0",

rollup.config.js

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

src/graphQLClientServer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { MockHttpServer } from './mockHttpRequest';
22
import { graphql } from 'graphql';
3-
import getSchemaFromData from './introspection/getSchemaFromData';
3+
import schemaBuilder from './schemaBuilder';
44

55
/**
66
* Starts a GraphQL Server in your browser: intercepts every call to http://localhost:3000/graphql
@@ -42,7 +42,7 @@ import getSchemaFromData from './introspection/getSchemaFromData';
4242
* GraphQLClientServer(data, 'http://localhost:8080/api/graphql');
4343
*/
4444
export default function(data, url = 'http://localhost:3000/graphql') {
45-
const schema = getSchemaFromData(data);
45+
const schema = schemaBuilder(data);
4646

4747
const server = new MockHttpServer(req => {
4848
if (!req.url.startsWith(url)) {

src/index.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
//export { default as createApolloClient } from './createApolloClient';
2-
require('reify');
1+
import GraphQLClientServer from './graphQLClientServer';
2+
export { default as createApolloClient } from './createApolloClient';
3+
export { default as jsonGraphqlExpress } from './jsonGraphqlExpress';
4+
export const graphQLClientServer = GraphQLClientServer;
35

4-
//const graphQLClientServer = require('./graphQLClientServer').default;
5-
const jsonGraphqlExpress = require('./jsonGraphqlExpress').default;
6-
7-
module.exports = {
8-
//graphQLClientServer,
9-
jsonGraphqlExpress,
10-
};
6+
if (typeof window !== 'undefined') {
7+
window.GraphQLClientServer = GraphQLClientServer;
8+
}

webpack.config.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const path = require('path');
2+
const webpack = require('webpack');
3+
const UglifyJsPlugin = webpack.optimize.UglifyJsPlugin;
4+
5+
let libraryName = 'json-graphql-server';
6+
7+
let plugins = [], outputFile;
8+
9+
if (process.env.NODE_ENV === 'production') {
10+
plugins.push(new UglifyJsPlugin({
11+
minimize: true,
12+
sourceMap: true,
13+
}));
14+
outputFile = libraryName + '.min.js';
15+
} else {
16+
outputFile = libraryName + '.js';
17+
}
18+
19+
const config = {
20+
entry: __dirname + '/src/index.js',
21+
devtool: 'source-map',
22+
output: {
23+
path: __dirname + '/lib',
24+
filename: outputFile,
25+
library: libraryName,
26+
libraryTarget: 'umd',
27+
umdNamedDefine: true
28+
},
29+
module: {
30+
rules: [
31+
{
32+
test: /(\.jsx|\.js)$/,
33+
loader: 'babel-loader',
34+
exclude: /(node_modules|bower_components)/
35+
}
36+
]
37+
},
38+
resolve: {
39+
modules: [path.resolve('./node_modules'), path.resolve('./src')],
40+
extensions: ['.json', '.js']
41+
},
42+
plugins: plugins
43+
};
44+
45+
module.exports = config;

0 commit comments

Comments
 (0)