webpack-vue-cli3解析

安装: npm install -g @vue/cli

新建项目: vue create webpack_vue

进入项目:

启动服务: npm run serve

打包项目: npm run build

vue对webpack做了大量的封装,没有暴露出来webpack的配置信息,当我们需要修改webpack配置的时候,可以在根目录下新建vue.config.js,根据官网进行配置即可。 例如:

module.exports={
    outputDir:'hello'
}

那么当我们运行打包命令的时候就会打包在hello文件夹下,而不是默认的dist;

pages对应原来的entry:

pages: {
    index: {
      // page 的入口
      entry: 'src/index/main.js',
      // 模板来源
      template: 'public/index.html',
      // 在 dist/index.html 的输出
      filename: 'index.html',
      // 当使用 title 选项时,
      // template 中的 title 标签需要是 <title><%= htmlWebpackPlugin.options.title %></title>
      title: 'Index Page',
      // 在这个页面中包含的块,默认情况下会包含
      // 提取出来的通用 chunk 和 vendor chunk。
      chunks: ['chunk-vendors', 'chunk-common', 'index']
    },
    // 当使用只有入口的字符串格式时,
    // 模板会被推导为 `public/subpage.html`
    // 并且如果找不到的话,就回退到 `public/index.html`。
    // 输出文件名会被推导为 `subpage.html`。
    subpage: 'src/subpage/main.js'
}

那么当我们需要做一些配置上没有的事情怎么办呢?例如我们在目录中新建文件夹static,放置静态文件(index.json),

{
    "abc":123
}

这个时候我们启动服务器是不能访问到index.json的,因为没有打包进去,VUE-CLI中给我们提供了一个属性configureWebpack,我们可以写一些原生的webpack代码:

const path=require('path');

module.exports={
    configureWebpack:{
        devServer:{
            contentBase:[path.resolve(__dirname,'static')]
        }
    }
}

以上表示我们可以到static目录下获取资源 启动项目之后访问 http://localhost:8080/index.json 就可以拿到index.json的内容。 当然,上述只是举个栗子,实际vue-cli已经提供了devServer,可以如下配置:

devServer:{
    contentBase:[path.resolve(__dirname,'static')]
}