开发服务

  1. easywebpack 整个体系提供的开发构建服务是基于 koa 实现的,用于支持各种类型项目(服务端渲染,前端渲染,静态页面,本地内存编译等),实现了 内存编译,热更新,发布编译,[代理](https://webpack.js.org/configuration/dev-server/#devserver-proxy),[historyApiFallback](https://webpack.js.org/configuration/dev-server/#devserver-historyapifallback)等功能,其中代理和historyApiFallback配置只在纯前端项目起作用。具体实现请见 [webpack-tool ](https://github.com/easy-team/webpack-tool)插件。 easywebpack-cli 和 [egg-webpack](https://github.com/easy-team/egg-webpack) 都依赖该插件。

纯前端渲染项目

//${root}/webpack.config.js
module.exports = {
 entry:{
    index: './src/index.js'
  },
  module:{
    rules:[]
  }
  plugins: [],
  devServer: { 
    writeToDisk: true,
    open: 'http://127.0.0.1:8888',
    // https://webpack.js.org/configuration/dev-server/#devserver-before
    before: app => { 
      // register koa middleware
      app.use(async (ctx, next) => {
        // 开发时, webapck 内存没有,需要直接读取文件
        console.log(ctx.url, ctx.path);
        if(ctx.url === '/index.html'){
          const filepath = path.join(__dirname, 'dist', ctx.url);
          ctx.body = fs.readFileSync(filepath, 'utf-8');
        } else {
          await next();
        }
      });
    },
    // https://webpack.js.org/configuration/dev-server/#devserver-after
    after: app => {
      // register koa middleware
    },
    // https://webpack.js.org/configuration/dev-server/#devserver-proxy
    proxy: {
      '/api': {
        changeOrigin: true,
        target: 'http://localhost:3000',
        pathRewrite: {'/api' : '/api/v2'}
      }
    },
    // https://webpack.js.org/configuration/dev-server/#devserver-historyapifallback
    historyApiFallback: {
      index: '/app.html' // 默认 index.html
    }
  }
}

服务端渲染项目

Egg 服务端渲染项目时,devServer 配置是无需的,即使配置了也无效。代理和historyApiFallback通过项目的 Node 服务处理。