From 0f9f8ff494d89319d5c43c6b7bd829c494bdb784 Mon Sep 17 00:00:00 2001 From: Niklas von Hertzen Date: Sun, 3 Sep 2017 21:25:06 +0800 Subject: [PATCH] Add dev server script --- karma.js | 26 ++++++++++++++++++++++++-- package.json | 4 +++- tests/server.js | 41 +++++++++++++++++++++++++++++++++++++++++ tests/testrunner.js | 7 ++++--- 4 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 tests/server.js diff --git a/karma.js b/karma.js index 4da50d3..7cfb47d 100644 --- a/karma.js +++ b/karma.js @@ -13,9 +13,31 @@ const bodyParser = require('body-parser'); const cors = require('cors'); const filenamifyUrl = require('filenamify-url'); +const CORS_PORT = 8081; +const corsApp = express(); +corsApp.use(cors()); +corsApp.use('/', express.static(path.resolve(__dirname))); +corsApp.use((error, req, res, next) => { + console.error(error); + next(); +}); + +process.on('uncaughtException', (err) => { + if(err.errno === 'EADDRINUSE') { + console.warn(err); + } else { + console.log(err); + process.exit(1); + } +}); + +corsApp.listen(CORS_PORT, () => { + console.log(`CORS server running on port ${CORS_PORT}`); +}); + const app = express(); app.use(cors()); -app.use(function(req, res, next) { +app.use((req, res, next) => { // IE9 doesn't set headers for cross-domain ajax requests if(typeof(req.headers['content-type']) === 'undefined'){ req.headers['content-type'] = "application/json"; @@ -81,7 +103,7 @@ app.use((error, req, res, next) => { next(); }); -const listener = app.listen(8081, () => { +const listener = app.listen(8000, () => { server.start(); }); diff --git a/package.json b/package.json index 41d62e9..9e08dc2 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "prettier": "1.5.3", "promise-polyfill": "6.0.2", "rimraf": "2.6.1", + "serve-index": "1.9.0", "slash": "1.0.0", "webpack": "3.4.1" }, @@ -64,7 +65,8 @@ "lint": "eslint src/**", "test": "npm run flow && npm run lint && npm run karma", "karma": "node karma", - "watch": "webpack --progress --colors --watch" + "watch": "webpack --progress --colors --watch", + "start": "node tests/server" }, "homepage": "https://html2canvas.hertzen.com", "license": "MIT", diff --git a/tests/server.js b/tests/server.js new file mode 100644 index 0000000..52d0fcb --- /dev/null +++ b/tests/server.js @@ -0,0 +1,41 @@ +const express = require('express'); +const cors = require('cors'); +const path = require('path'); +const fs = require('fs'); +const webpack = require('webpack'); +const config = require('../webpack.config'); +const serveIndex = require('serve-index'); + +const PORT = 8080; +const CORS_PORT = 8081; + +const app = express(); +app.use('/', serveIndex(path.resolve(__dirname, '../'), {icons: true})); +app.use('/', express.static(path.resolve(__dirname, '../'))); +app.listen(PORT, () => { + console.log(`Server running on port ${PORT}`); +}); + +const corsApp = express(); +corsApp.use(cors()); +corsApp.use('/', express.static(path.resolve(__dirname, '../'))); +corsApp.listen(CORS_PORT, () => { + console.log(`CORS server running on port ${CORS_PORT}`); +}); + +const compiler = webpack(config); +compiler.watch( + { + aggregateTimeout: 300 // wait so long for more changes + }, + (err, stats) => { + console.error(err); + + console.log( + stats.toString({ + chunks: false, // Makes the build much quieter + colors: true + }) + ); + } +); diff --git a/tests/testrunner.js b/tests/testrunner.js index 67ff291..4037289 100644 --- a/tests/testrunner.js +++ b/tests/testrunner.js @@ -117,7 +117,8 @@ const assertPath = (result, expected, desc) => { return testContainer.contentWindow .html2canvas(testContainer.contentWindow.document.documentElement, { removeContainer: true, - backgroundColor: '#ffffff' + backgroundColor: '#ffffff', + ...(testContainer.contentWindow.h2cOptions || {}) }) .then(canvas => { try { @@ -388,7 +389,7 @@ const assertPath = (result, expected, desc) => { version: platform.version } }), - 'http://localhost:8081/screenshot/chunk' + 'http://localhost:8000/screenshot/chunk' ) ) ); @@ -407,7 +408,7 @@ const assertPath = (result, expected, desc) => { version: platform.version } }), - 'http://localhost:8081/screenshot' + 'http://localhost:8000/screenshot' ); } });