diff --git a/docs/DevServer.html b/docs/DevServer.html index c2a1940..d447060 100644 --- a/docs/DevServer.html +++ b/docs/DevServer.html @@ -96,7 +96,7 @@
style
options
a full system path to a SASS file
devDir
a full system path to a directory in which to put any compiled development resources
port
a port at which to start the dev server
react
false to disable the react hot loader plugin
optional configuration
/* @flow */ +import type {DevServerConfig} from './typedef'; import {SASSCompiler} from './SASSCompiler'; import {watch} from './watch'; import tinylr from 'tiny-lr'; @@ -93,7 +94,13 @@Source: DevServer.js
const LIVERELOAD_PORT = 35729, WEB_PORT = 3000, - cwd = process.cwd(); + cwd = process.cwd(), + {HotModuleReplacementPlugin, NoErrorsPlugin} = webpack, + defaultOptions = { + port: WEB_PORT, + react: true, + contentBase: cwd + }; /** * A lightweight development server that rapidly recompiles the JavaScript and SASS files when they are edited and @@ -106,11 +113,8 @@Source: DevServer.js
* Please install and enable the LiveReload browser extension for the CSS reloading to work. * * @class DevServer - * @param {string} script - a full system path to a JavaScript file - * @param {string} style - a full system path to a SASS file - * @param {string} devDir - a full system path to a directory in which to put any compiled development resources - * @param {number} [port=3000] - a port at which to start the dev server - * @param {boolean} [react=true] - false to disable the react hot loader plugin + * @param {string} script - a full system path to a JavaScript file + * @param {DevServerConfig} [options={}] - optional configuration * @see {@link http://webpack.github.io/docs/webpack-dev-server.html webpack-dev-server} * @see {@link http://gaearon.github.io/react-hot-loader/ React Hot Loader} * @example @@ -120,151 +124,167 @@Source: DevServer.js
* // or - var DevServer = require('webcompiler/lib/DevServer').DevServer; * import {join} from 'path'; * - * const rootDir = join(__dirname, '..'), - * devDir = join(rootDir, 'development'), - * server = new DevServer(join(devDir, 'script.js'), join(devDir, 'app.scss'), devDir); + * const devDir = join(__dirname, '..', 'development'), + * script = join(devDir, 'script.js'), + * style = join(devDir, 'app.scss'); * - * server.run(rootDir); + * new DevServer(script, {style}).run(); * // now navigate to http://localhost:3000 using your favorite browser ( preferably Chrome :) ) */ export class DevServer { /** - * a port at which to start the dev server + * a full system path to a JavaScript file * - * @member {number} port + * @member {string} script * @memberof DevServer * @private * @instance */ - port: number; + script: string; /** - * a LiveReload server + * optional configuration * - * @member {tinylr.Server} lr + * @member {Object} options * @memberof DevServer * @private * @instance */ - lr: tinylr.Server; + options: Object; + + /* eslint-disable require-jsdoc */ + constructor(script: string, options: DevServerConfig = {}) { + /* eslint-enable require-jsdoc */ + this.script = script; + this.options = {...defaultOptions, ...options}; + } /** - * recompiles SASS and notifies LiveReload + * Returns the HTML page layout * - * @method compileSASS * @memberof DevServer * @private * @instance + * @method layout + * @return {string} HTML layout */ - compileSASS: () => void; + layout(): string { + return `<!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="utf-8"> + <meta http-equiv="X-UA-Compatible" content="IE=edge"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <title>Development server - WebCompiler</title> + <link rel="shortcut icon" href="/favicon.ico">${ + this.options.style + ? '\n <link rel="stylesheet" href="/style.css">' + : '' + } + </head> + <body> + <div id="app"></div> + <script src="/script.js" async defer></script> + </body> +</html>`; + } /** - * the Webpack development server + * Returns the JavaScript module loaders array necessary to run the server * - * @member {WebpackDevServer} server * @memberof DevServer * @private * @instance + * @method loaders + * @return {Array<string>} JavaScript module loaders */ - server: WebpackDevServer; + loaders(): Array<string> { + const loaders = []; - /* eslint-disable require-jsdoc */ - constructor(script: string, style: string, devDir: string, port: number = WEB_PORT, react: boolean = true) { - /* eslint-enable require-jsdoc */ - const sass = new SASSCompiler(), - loaders = []; - - if (react) { + if (this.options.react) { loaders.push('react-hot'); } loaders.push('babel'); - this.port = port; - this.lr = tinylr(); - this.compileSASS = sass.fe.bind(sass, style, join(devDir, 'style.css'), () => { - this.lr.changed({body: {files: ['style.css']}}); - }); - this.server = new WebpackDevServer(webpack({ + return loaders; + } + + /** + * Compile SASS and start watching for file changes + * + * @memberof DevServer + * @instance + * @method watchSASS + * @example + * server.watchSASS(); + */ + watchSASS() { + const {style, contentBase} = this.options; + + if (!style) { + return; + } + + const sass = new SASSCompiler(false), + lr = tinylr(), + compileSASS = sass.fe.bind(sass, style, join(contentBase, 'style.css'), () => { + lr.changed({body: {files: ['style.css']}}); + }); + + lr.listen(LIVERELOAD_PORT); + compileSASS(); + watch(cwd, 'scss', compileSASS); + } + + /** + * Starts the Webpack development server + * + * @memberof DevServer + * @instance + * @method watchJS + * @example + * server.watchJS(); + */ + watchJS() { + const {port, contentBase} = this.options; + + const server = new WebpackDevServer(webpack({ cache: {}, debug: true, devtool: 'eval-source-map', entry: [ - `webpack-dev-server/client?http://localhost:${this.port}`, + `webpack-dev-server/client?http://localhost:${port}`, 'webpack/hot/only-dev-server', - script + this.script ], output: { - path: devDir, + path: contentBase, filename: 'script.js', publicPath: '/' }, - plugins: [ - new webpack.HotModuleReplacementPlugin(), - new webpack.NoErrorsPlugin() - ], + plugins: [new HotModuleReplacementPlugin(), new NoErrorsPlugin()], module: { loaders: [{ test: /\.js$/, exclude: /node_modules/, - loaders + loaders: this.loaders() }, { test: /\.json$/, loader: 'json' }] } }), { - contentBase: devDir, + contentBase, publicPath: '/', hot: true, historyApiFallback: true }); - this.server.app.get('*', (req, res) => { - res.send(`<!DOCTYPE html> -<html lang="en"> - <head> - <meta charset="utf-8"> - <meta http-equiv="X-UA-Compatible" content="IE=edge"> - <meta name="viewport" content="width=device-width, initial-scale=1"> - <title>Development server - WebCompiler</title> - <link href="/style.css" rel="stylesheet"> - </head> - <body> - <div id="app"></div> - <script src="/script.js" async defer></script> - </body> -</html>`); + server.app.use((req, res) => { + res.send(this.layout()); }); - } - - /** - * Compile SASS and start watching for file changes - * - * @memberof DevServer - * @instance - * @method watchSASS - * @example - * server.watchSASS(); - */ - watchSASS() { - this.lr.listen(LIVERELOAD_PORT); - this.compileSASS(); - watch(cwd, 'scss', this.compileSASS); - } - - /** - * Starts the Webpack development server - * - * @memberof DevServer - * @instance - * @method watchJS - * @example - * server.watchJS(); - */ - watchJS() { - const port = this.port; - this.server.listen(port, '0.0.0.0', error => { + server.listen(port, '0.0.0.0', error => { if (error) { return console.error(error); } diff --git a/docs/global.html b/docs/global.html index 62c6466..057a08b 100644 --- a/docs/global.html +++ b/docs/global.html @@ -548,6 +548,270 @@Type Definitions
DevServer configuration object
+Name | + + +Type | + + +Argument | + + + +Default | + + +Description | +
---|---|---|---|---|
style |
+
+
+ + + +string + + + + + | + + +
+
+ <optional> + + + + |
+
+
+
+ + + | + + +a full system path to a SASS file |
+
port |
+
+
+ + + +number + + + + + | + + +
+
+ <optional> + + + + |
+
+
+
+ + + 3000 + + | + + +a port at which to start the dev server |
+
react |
+
+
+ + + +boolean + + + + + | + + +
+
+ <optional> + + + + |
+
+
+
+ + + true + + | + + +false to disable the react hot loader plugin |
+
contentBase |
+
+
+ + + +string + + + + + | + + +
+
+ <optional> + + + + |
+
+
+
+ + + CWD + + | + + +a full system path to the server web root |
+