zoukankan      html  css  js  c++  java
  • 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')]
    }
  • 相关阅读:
    git常用操作命令
    如何编写高质量代码
    Chrome调试工具简单介绍
    使用eclipse+tomcat搭建本地环境
    chrome设置--disable-web-security解决跨域
    利用Maven管理工程项目本地启动报错及解决方案
    用户输入验证【提升篇】
    简单【用户输入验证】
    【消息框】的返回值
    【消息框】的4种显示形式
  • 原文地址:https://www.cnblogs.com/jingouli/p/12336400.html
Copyright © 2011-2022 走看看