最近使用springboot, vue, webpack开发了一款SPA应用,这里主要记录开发+调试+打包的过程及作法。
使用的技术
gradle
springboot
vue.js
webpackcss
下面是开发时的项目结构目录,主要分红后端项目与前端项目,后端主要用于编写服务器逻辑,前端则是编写页面和存放静态资源。html
-- springboot-vue-webpack-examples | |-- server | | | |-- src // 源码 | |-- build // 构建输出目录 | |-- ui | | | |-- build-scripts // vue 构建脚本 | |-- config // vue 配置文件 | |-- src // 源码 | |-- static // 静态资源 | |-- build // 构建输出目录
ui项目经过vue init webpack ui
命令建立使用的是vue提供的模板,之因此使用该模板由于里面已经配置好了调试与生产打包方式,咱们不须要作什么更改就能够直接使用。前端
默认在vue的模板中构建脚本是放置在build
目录中,这里将构建脚本移置build-scripts
目录中,主要是由于gradle
的默认构建输出目录是build
为了减小配置而修改了vue构建脚本的目录,我感受这是最简单方便的,由于好比咱们在使用代码版本控制器的时候build
目录会被忽略而不提交至服务器,我不想去修改太多的配置。将vue构建脚本移动后须要修改几个点,以下图vue
ui项目的gradle
脚本配置,最终咱们打包发布都是直接经过gradle
命令,因此须要将node
构建集成到gradle
中。node
project(':ui') { apply plugin: 'com.moowork.node' task cnpmInstall(type: NpmTask) { group = 'node' args = ['install', '--registry=http://registry.cnpmjs.org'] } task buildUI(type: NpmTask, dependsOn: cnpmInstall) { group = 'node' args = ['run', 'build'] } jar.dependsOn buildUI task runDev(type: NpmTask, dependsOn: cnpmInstall) { group = 'node' args = ['run', 'dev'] } }
cnpmInstall
该命令主要用于依赖安装,之因此须要这个命令主要是由于咱们的网络环境不太好,将镜像设置为国内的下载依赖比较稳定。buildUI
调用package.json
中的命令构建ui。runDev
能够经过gradlew :ui:runDev
命令启动ui。webpack
ui/config/index.js
配置修改git
// see http://vuejs-templates.github.io/webpack for documentation. var path = require('path') var assetsRoot = path.resolve(__dirname, '../build/resources/main/ui') module.exports = { build: { env: require('./prod.env'), index: path.resolve(assetsRoot, 'index.html'), assetsRoot: assetsRoot, assetsSubDirectory: 'static', assetsPublicPath: '/', productionSourceMap: true, // Gzip off by default as many popular static hosts such as // Surge or Netlify already gzip all static assets for you. // Before setting to `true`, make sure to: // npm install --save-dev compression-webpack-plugin productionGzip: false, productionGzipExtensions: ['js', 'css'] }, dev: { env: require('./dev.env'), port: 3000, assetsSubDirectory: 'static', assetsPublicPath: '/', proxyTable: { '/api/**': 'http://localhost:8080' }, // CSS Sourcemaps off by default because relative paths are "buggy" // with this option, according to the CSS-Loader README // (https://github.com/webpack/css-loader#sourcemaps) // In our experience, they generally work as expected, // just be aware of this issue when enabling this option. cssSourceMap: false } }
assetsRoot
将webpack构建的资源输出至build/resources/main/ui
目录,gradle
打包会将ui
加入至classpath
中,spring
查找静态资源有ui
目录区分比较方便。proxyTable
增长代理配置将/api/**
URL下的全部请求转发至服务后端即springboot
启动的服务,这样作的目的是为了方便debug
,在vue webpack中已经配置好了hot-dev
,在开发中咱们修改了前端js
或者vue
不须要从新构建或重启应用前端就能响应。因此在开发中咱们启动服务后的访问入口是http://localhost:3000
其实3000
是express dev-server
的端口,这里也体现了上面为何要配置proxyTable
,当咱们从dev-server
做为访问入口时与后端服务并非同一个,存在跨域问题,可是经过代理能够避免这个问题,同时这并不会对生产环境形成影响,当咱们发布项目以后能够直接经过springboot
服务做为访问入口,由于在生产环境中咱们不须要hot-reload
功能。github
服务端WEB配置web
@Configuration @RestController public class WebConfiguration extends WebMvcConfigurerAdapter { @Value("classpath:/ui/index.html") private Resource indexHtml; @Bean @Order(Ordered.HIGHEST_PRECEDENCE) public CharacterEncodingFilter characterEncodingFilter() { CharacterEncodingFilter filter = new CharacterEncodingFilter("UTF-8", true); return filter; } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**").addResourceLocations("classpath:/ui/"); } @Bean public ServletRegistrationBean apiV1ServletBean(WebApplicationContext wac) { DispatcherServlet servlet = new DispatcherServlet(wac); ServletRegistrationBean bean = new ServletRegistrationBean(servlet, "/api/v1/*"); bean.setName("api-v1"); return bean; } @RequestMapping("/") public Object index() { return ResponseEntity.ok().body(indexHtml); } }
addResourceHandlers
增长静态资源访问路径。apiV1ServletBean
之因此增长一个servlet
配置,是为了与静态资源区分,后端服务都是经过restful
接口交互而静态资源是经过/
根目录的方式访问。index
根目录返回index.html
。spring
至此基础配置就差很少是这样打包发布就能够直接经过gradlew build
。发布时咱们无需修改任何配置及代码,与开发环境是一致。而在开发环境中咱们也保留良好的开发,及调试环境。
注:运行时不须要nodejs
环境