最近又整回vue去了,不说了,还是脚手架先整吧。
假设你已经通过vue create vue-base
有了vue-base 项目

然后我看到

1
2
3
4
5
"scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },

  

 

这里执行的是vue-cli-service

开心😄又来活儿了

我好奇它的webpack基础配置是什么?就是我本地项目文件不做任何vue.config.js的配置,它能实现什么?

vue-cli-service 其实执行的是 node_modules/.bin/vue-cli-service

最终是执行了@vue/cli-service

1
2
const Service = require('../lib/Service')
const service = new Service(process.env.VUE_CLI_CONTEXT || process.cwd())

  

 

 

这里看到首先去new Service,这里初始化配置
然后执行了

1
2
3
4
service.run(command, args, rawArgv).catch(err => {
  error(err)
  process.exit(1)
})

  

 

run函数里面先执行了this.init 把默认配置初始化好
然后执行了command 这里以server为例,去执行了 lib/commands下面对应的serve.js文件

可以看到这里面也有一些配置,覆盖了之前的初始化的配置
lib/commands/serve.js

1
2
3
4
5
const defaults = {
  host: '0.0.0.0',
  port: 8080,
  https: false
}

  

 

如何更改vue-cli-service里面默认的端口号呢?
把启动命令更改为vue-cli-service serve --port 7001

后续可以看到在这里把初始化的很多参数加到 webpack配置里面来
Service.js文件里面

loadUserOptions 这个方法里面获取了用户的vue.config.js命名的文件信息或者是package.json 里面 vue字段的信息

关于配置 Service.js这里
const { defaults, validate } = require(’./options’)
options.js
export default {}
出来一份配置

说一下这个文件里面引入的@vue/cli-shared-utils 就像一个校验一样,这里处理的很棒!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const builtInPlugins = [
      './commands/serve',
      './commands/build',
      './commands/inspect',
      './commands/help',
      // config plugins are order sensitive
      './config/base',
      './config/css',
      './config/prod',
      './config/app'
    ].map(idToPlugin)
    if (inlinePlugins) {
      plugins = useBuiltIn !== false
        ? builtInPlugins.concat(inlinePlugins)
        : inlinePlugins
    } else {
      const projectPlugins = Object.keys(this.pkg.devDependencies || {})
        .concat(Object.keys(this.pkg.dependencies || {}))
        .filter(isPlugin)
        .map(id => {
          if (
            this.pkg.optionalDependencies &&
            id in this.pkg.optionalDependencies
          ) {
            let apply = () => {}
            try {
              apply = require(id)
            } catch (e) {
              warn(`Optional dependency ${id} is not installed.`)
            }
 
            return { id, apply }
          } else {
            return idToPlugin(id)
          }
        })
      plugins = builtInPlugins.concat(projectPlugins)
    }

  

 

里面有一个这个对象,配置各个命令和配置,看起来是做了prod和base的区分配置 后面是做了一个合并

prod.js里面看到
首先是通过webpack-chain 链接的各个配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module.exports = (api, options) => {
  api.chainWebpack(webpackConfig => {
    if (process.env.NODE_ENV === 'production') {
     production 环境
      webpackConfig
        .mode('production')   //瞧这里设置了mode
        .devtool(options.productionSourceMap ? 'source-map' : false)
     
      // keep module.id stable when vendor modules does not change
      webpackConfig
        .plugin('hash-module-ids')
          .use(require('webpack/lib/HashedModuleIdsPlugin'), [{
            hashDigest: 'hex'
          }])
 
      // disable optimization during tests to speed things up
      if (process.env.VUE_CLI_TEST) {
        webpackConfig.optimization.minimize(false)
      }
    }
  })
}

  

 

在base.js

然后你就看到了这么些的默认配置

1
2
3
4
5
6
7
8
9
10
webpackConfig
      .mode('development')
      .context(api.service.context)
      .entry('app')
        .add('./src/main.js')
        .end()
      .output
        .path(api.resolve(options.outputDir))
        .filename(isLegacyBundle ? '[name]-legacy.js' : '[name].js')
        .publicPath(options.publicPath)

  

基础的entry ,output

1
2
3
4
5
6
7
8
alias
        .set('@', api.resolve('src'))
        .set(
          'vue$',
          options.runtimeCompiler
            ? 'vue/dist/vue.esm.js'
            : 'vue/dist/vue.runtime.esm.js'
        )

  

 

配置了别名 @指向项目src目录
在这里插入图片描述
以下是在 vue 2的项目中,一旦换成 vue 3的项目就没了,所以放在了try catch中
在这里插入图片描述
这里可以看到对, .vue文件启用了cache-loader,缓存,启用了vue-loader 这个来转换vue文件

继续往下处理了images、svg、media、fonts文件

很意外的看到了关于.pug后缀文件的处理,可能是其它包里面的?这里没有仔细去看了

最后
在这里插入图片描述
增加了terser默认的内置压缩工具包,做压缩
这里的配置有一个terserOptions.js文件

更改的

https://cli.vuejs.org/zh/config/#vue-config-js

posted on   ygunoil  阅读(3513)  评论(1编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示