sourceMap for Less

打包后的less/css文件以 style 标签引入,为了区分样式属于那个文件,需要把css文件分开,以 link 标签引入,目前没有实现,因为 minicssplugin 是把所有css文件合并成一个,他不能和 style-loader 同时存在,只能在production环境中使用。sourceMap for less 能够解决。在浏览器中可以看到 less 文件。

  1. rules: [
  2. {
  3. test: /.less$/,
  4. use: [
  5. 'style-loader',
  6. {
  7. loader: 'css-loader',
  8. options: {
  9. sourceMap: true,
  10. }
  11. },
  12. {
  13. loader: 'less-loader',
  14. options: { sourceMap: true, strictMath: true, strictUnits: true }
  15. }
  16. ]
  17. }
  18. ]

获取webpack打包环境

webpack.config.js

  1. module.exports = (env, argv) => {
  2. // argv.mode
  3. }

打包前删除 dist

  1. let CleanWebpackPlugin = require('clean-webpack-plugin')
  2. module.exports = {
  3. // ...
  4. plugins: [
  5. new CleanWebpackPlugin(['dist'])
  6. ]
  7. }

devServer.proxy

慕课网-webpack-devServer

changeOrigin=true 开启跨域;需要转发的网站是支持 https 的,那么需要增加secure=false,来防止转发失败。

  1. module.exports = {
  2. //...
  3. devServer: {
  4. proxy: {
  5. '/api': {
  6. target: 'http://baidu.com',
  7. pathRewrite: {'^/api': ''},
  8. secure: false,
  9. changeOrigin: true
  10. }
  11. }
  12. }
  13. };

当前配置只能代理json接口的数据,对于html文件,还是使用打包后 dist 文件夹中文件,那么我们使用bypass来实现这个需求。

  1. module.exports = {
  2. //...
  3. devServer: {
  4. proxy: {
  5. '/api': {
  6. target: 'http://baidu.com',
  7. pathRewrite: {'^/api': ''},
  8. bypass(req, res, proxyOptions) {
  9. // 判断请求头中的 accept 值
  10. if (req.headers.accept.indexOf('html') !== -1) {
  11. console.log('Skipping proxy for browser request.');
  12. // 返回的是 contentBase 的路径
  13. return '/index.html';
  14. }
  15. }
  16. }
  17. }
  18. }
  19. };

代理一个域名下的两个地址,可以使用 context。

  1. module.exports = {
  2. //...
  3. devServer: {
  4. proxy: [
  5. {
  6. context: ['/auth', '/api'],
  7. target: 'http://baidu.com'
  8. }
  9. ]
  10. }
  11. };

dev-server 使用了 http-proxy-middleware , http-proxy-middleware 基于 http-proxy 实现的,可以查看 http-proxy 的源码和文档:https://github.com/nodejitsu/… 。

更多的参数设置:

  1. target:要使用url模块解析的url字符串
  2. forward:要使用url模块解析的url字符串
  3. agent:要传递给http(s).request的对象(请参阅Node的https代理和http代理对象)
  4. ssl:要传递给https.createServer()的对象
  5. ws:true / false,是否代理websockets
  6. xfwd:true / false,添加x-forward标头
  7. secure:true / false,是否验证SSL Certs
  8. toProxy:true / false,传递绝对URL作为路径(对代理代理很有用)
  9. prependPath:true / false,默认值:true - 指定是否要将目标的路径添加到代理路径
  10. ignorePath:true / false,默认值:false - 指定是否要忽略传入请求的代理路径(注意:如果需要,您必须附加/手动)。
  11. localAddress:要为传出连接绑定的本地接口字符串
  12. changeOrigin:true / false,默认值:false - 将主机标头的原点更改为目标URL

开发环境 chunkhash

  • hash:每次编译 Compilation 对象的 hash,全局一致,跟单次编译有关,跟单个文件无关,不推荐使用;
  • chunkhash:chunk 的 hash,根据不同的 chunk 及其包含的模块计算出来的 hash,chunk 中包含的任意模块发生变化,则 chunkhash 发生变化,推荐使用;
  • contenthash:CSS 文件特有的 hash 值,是根据 CSS 文件内容计算出来的,CSS 发生变化则其值发生变化,推荐 CSS 导出中使用。

在开发环境,使用热更新不能同时使用 [chunkhash] 或 [contenthash] ,需要注释掉热更新相关内容,或者根据环境设置 filname。

  1. ERROR in chunk main [entry]
  2. [name].[chunkhash].js
  3. Cannot use [chunkhash] or [contenthash] for chunk in '[name].[chunkhash].js'
  4. (use [hash] instead)

The solution was to change the filename depending on whether the mode is production or development: stackoverflow

  1. {
  2. filename: mode === 'production' ? '[name].[chunkhash].js' : '[name].[hash].js'
  3. }

保存文件即检测

在开发环境使用 webpack-dev-server 监测文件的改动,保存文件时重新打包,对 js 和 vue 文件增加 eslint-loader ,可以在打包文件前做代码检查。

  1. urles: [
  2. {
  3. // 优先处理
  4. enfore: 'pre',
  5. test: /\.(vue|jsx?)$/,
  6. exclude: /node_modules/,
  7. loader: 'eslint-loader',
  8. options: {
  9. // 自动修复
  10. fix: true
  11. }
  12. }
  13. ]

不过每次保存,不只是检测保存的这个文件,而是所有范围内的文件。