# 構建和發布
碼農云Admin 致力于提供給程序員**愉悅**的開發體驗。
## 構建
當項目開發完畢,只需要運行一行命令就可以打包你的應用:
~~~bash
$ yarn build
or
$ npm run build
~~~
由于 Ant Design Pro 使用的工具[Vue-cli3](https://cli.vuejs.org/)已經將復雜的流程封裝完畢,構建打包文件只需要一個命令`yarn build`或`npm run build`,構建打包成功之后,會在根目錄生成`dist`文件夾,里面就是構建打包好的文件,通常是`*.js`、`*.css`、`index.html`等靜態文件,也包括了項目根的`public/`下的所有文件。
如果需要自定義構建,比如指定`dist`目錄等,可以通過`/vue.config.js`進行配置,詳情參看:[Vue-cli3 配置](https://cli.vuejs.org/guide/)。
### 前端路由與服務端的結合
Ant Design Pro 使用的`Vue-Router`支持兩種路由方式:`browserHistory`和`hashHistory`可以參考文檔[Vue-Router URL 模式](https://router.vuejs.org/zh/guide/essentials/history-mode.html)。
可以在`src/router/index.js`中進行配置選擇用哪個方式:
~~~javascript
import Vue from 'vue'
import Router from 'vue-router'
import { constantRouterMap } from '@/config/router.config'
Vue.use(Router)
export default new Router({
mode: 'history', // 默認是 history 可以改為 hash
base: process.env.BASE_URL,
scrollBehavior: () => ({ y: 0 }),
routes: constantRouterMap
})
~~~
`hashHistory`使用如`https://cdn.com/#/users/123`這樣的 URL,取井號后面的字符作為路徑。`browserHistory`則直接使用`https://cdn.com/users/123`這樣的 URL。使用`hashHistory`時瀏覽器訪問到的始終都是根目錄下`index.html`。使用`browserHistory`則需要服務器做好處理 URL 的準備,處理應用啟動最初的`/`這樣的請求應該沒問題,但當用戶來回跳轉并在`/users/123`刷新時,服務器就會收到來自`/users/123`的請求,這時你需要配置服務器能處理這個 URL 返回正確的`index.html`。如果你能控制服務端,我們推薦使用`browserHistory`。
### 使用 nginx
nginx 作為最流行的 web 容器之一,配置和使用相當簡單,只要簡單的配置就能擁有高性能和高可用。推薦使用 nginx 托管。示例配置如下:
~~~nginx
server {
listen 80;
# gzip config
gzip on;
gzip_min_length 1k;
gzip_comp_level 9;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
root /usr/share/nginx/html;
location / {
# 用于配合 browserHistory 使用
try_files $uri $uri/ /index.html;
# 如果有資源,建議使用 https + http2,配合按需加載可以獲得更好的體驗
# rewrite ^/(.*)$ https://preview.pro.loacg.com/$1 permanent;
}
location /api {
proxy_pass https://preview.pro.loacg.com;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}
}
server {
# 如果有資源,建議使用 https + http2,配合按需加載可以獲得更好的體驗
listen 443 ssl http2 default_server;
# 證書的公私鑰
ssl_certificate /path/to/public.crt;
ssl_certificate_key /path/to/private.key;
location / {
# 用于配合 browserHistory 使用
try_files $uri $uri/ /index.html;
}
location /api {
proxy_pass https://preview.pro.loacg.com;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}
}
~~~
### 使用 spring boot
Spring Boot 是使用最多的 java 框架,只需要簡單的幾步就可以與 Ant Design Pro 進行整合。
首先運行 build
`$ yarn build`or`$ npm run build`
然后將編譯之后的文件復制到 spring boot 項目的`/src/main/resources/static`目錄下。
重新啟動項目,訪問`http://localhost:8080/`就可以看到效果。
為了方便做整合,最好使用`hash`路由。如果你想使用`browserHistory`,你需要創建一個`controller`,并添加如下代碼:
~~~java
@RequestMapping("/api/**")
public ApiResult api(HttpServletRequest request, HttpServletResponse response){
return apiProxy.proxy(request, reponse);
}
@RequestMapping(value="/**", method=HTTPMethod.GET)
public String index(){
return "index"
}
~~~
> 注意 pro 并沒有提供 java 的 api 接口實現,如果只是為了預覽 demo,可以使用反向代理到`https://preview.pro.loacg.com`。
### 使用 express
[express](http://expressjs.com/)的例子
~~~
app.use(express.static(path.join(__dirname, 'build')));
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
~~~
### 使用 egg
[egg](https://eggjs.org/)的例子
~~~
// controller
exports.index = function* () {
yield this.render('App.jsx', {
context: {
user: this.session.user,
},
});
};
// router
app.get('home', '/*', 'home.index');
~~~
關于路由更多可以參看[Vue-Router 文檔](https://router.vuejs.org/zh/)。