帮助搭建用于同构,即同时能够在客户端和服务端使用的Webpack。javascript
个人Demo:universal-webpack + koa + react + webpack。
官方Democss
目前仅支持Webpack2.html
npm install webpack --save npm install extract-text-webpack-plugin --save
以往咱们会建立一个标准的 webpack.config.js 。
此时,咱们须要建立另外两个配置文件:webpack.config.client.babel.js 和 webpack.config.client.babel.js。以下:java
import { client } from 'universal-webpack/config' import settings from './universal-webpack-settings' import configuration from './webpack.config' export default client(configuration, settings)
代替webpack.config.js来完成客户端的打包。node
import { server } from 'universal-webpack/config' import settings from './universal-webpack-settings' import configuration from './webpack.config' export default server(configuration, settings)
{ "server": { "input": "./source/server.js", "output": "./build/server/server.js" } }
output所对应的文件由input对应的文件使用Webpack根据webpack.config.server.babel.js中的配置生成。react
服务器启动,官方Demo以下:webpack
// express.js import path from 'path' import http from 'http' import express from 'express' import http_proxy from 'http-proxy' // react-router import routes from '../client/routes.js' // Redux import store from '../client/store.js' // The server code must export a function // (`parameters` may contain some miscellaneous library-specific stuff) export default function(parameters) { // Create HTTP server const app = new express() const server = new http.Server(app) // Serve static files app.use(express.static(path.join(__dirname, '..', 'build/assets'))) // Proxy API calls to API server const proxy = http_proxy.createProxyServer({ target: 'http://localhost:xxxx' }) app.use('/api', (req, res) => proxy.web(req, res)) // React application rendering app.use((req, res) => { // Match current URL to the corresponding React page // (can use `react-router`, `redux-router`, `react-router-redux`, etc) react_router_match_url(routes, req.originalUrl).then((error, result) => { if (error) { res.status(500) return res.send('Server error') } // Render React page const page = redux.provide(result, store) res.status(200) res.send('<!doctype html>' + '\n' + ReactDOM.renderToString(<Html>{page}</Html>)) }) }) // Start the HTTP server server.listen() }
但这个文件不是真正的入口文件,须要另外一个文件,咱们须要另外一个文件来启动服务。git
var startServer = require('universal-webpack/server') var settings = require('../universal-webpack-settings') // `configuration.context` and `configuration.output.path` are used var configuration = require('../webpack.config') startServer(configuration, settings)
调用这个入口文件,实质上是调用了经过Webpack打包以后的server.js。github
开发环境的启动命令大体以下:web
webpack-dev-server --hot --inline --config "./webpack.config.client.babel.js" --port XXXX --colors --display-error-details //启动一个webpack-dev-server,真正的Development Server会从这里请求一些静态文件(好比:boundle.js)。 //Universal-webpack不会再服务端释听任何资源,全部资源都在客户端)。 webpack --watch --config "./webpack.config.server.babel.js" --colors --display-error-details //打包服务端代码 nodemon "./source/start-server" --watch "./build/server" 启动服务器
生产环境的启动命令大体以下:
webpack --config "./webpack.config.client.babel.js" --colors --display-error-details webpack --config "./webpack.config.server.babel.js" --colors --display-error-details node "./source/start-server"
返回webpack最终所输出的文件信息:
{ javascript: { main: `/assets/main.785f110e7775ec8322cf.js` }, styles: { main: `/assets/main.785f110e7775ec8322cf.css` } }