「连载四」「初始化项目」前端框架 Vue 入门教程

初始化项目

本项目是基于

@vue/cli(v4.2.3) & webpack(v4) & vuex(v3.1.2) & vue-router(v3.1.6) & ESLint(recommended) & Less(v3.11.1) & Jest & Cypress

按照官方文档,遵循目前项目经验的常用实践,搭建的入门脚手架

Vue CLI 4 可以通过图形化界面初始化项目,并安装 Babel、PWA、Router、Vuex、Less、Linter、Jest、Cypress 等插件,相对命令行更加便捷和高效,命令如下:

<code># 安装 vue 命令行工具
npm install -g @vue/cli /<code>
<code># 生成一个名为 vue-starter 的项目
vue create vue-starter /<code>
<code># 进入 vue-starter 项目根目录
cd vue-starter /<code>
<code># 启动图形化配置界面
vue ui /<code>


以下为我的图形化配置界面的配置:


「连载四」「初始化项目」前端框架 Vue 入门教程


「连载四」「初始化项目」前端框架 Vue 入门教程


「连载四」「初始化项目」前端框架 Vue 入门教程


「连载四」「初始化项目」前端框架 Vue 入门教程


「连载四」「初始化项目」前端框架 Vue 入门教程


「连载四」「初始化项目」前端框架 Vue 入门教程


「连载四」「初始化项目」前端框架 Vue 入门教程


「连载四」「初始化项目」前端框架 Vue 入门教程


「连载四」「初始化项目」前端框架 Vue 入门教程


「连载四」「初始化项目」前端框架 Vue 入门教程


「连载四」「初始化项目」前端框架 Vue 入门教程


自定义配置

虽然使用 vue ui 可以进行可视化配置,但在实际开发中,肯定有需要自定义配置的部分,我的配置如下:

<code>module.exports = {
publicPath: process.env.NODE_ENV === "production" ? "/" : "/",

outputDir: "dist",

assetsDir: "public",

indexPath: "index.html",

filenameHashing: true,

// 当在 multi-page 模式下构建时,webpack 配置会包含不一样的插件 (这时会存在多个 html-webpack-plugin 和 preload-webpack-plugin 的实例)。如果你试图修改这些插件的选项,请确认运行 vue inspect
pages: {
index: {
// page 的入口
entry: "src/index/main.js",
// 模板来源
template: "public/index.html",
// 在 dist/index.html 的输出
filename: "index.html",
// 当使用 title 选项时,
// template 中的 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"
},

// eslint-loader 是否在保存的时候检查
lintOnSave: true,

// 是否使用包含运行时编译器的Vue核心的构建
runtimeCompiler: false,

// 默认情况下 babel-loader 忽略其中的所有文件 node_modules
transpileDependencies: [],

// 生产环境 sourceMap
productionSourceMap: false,

// cors 相关 https://developer.mozilla.org/zh-CN/docs/Web/HTML/CORS_settings_attributes
crossorigin: "use-credentials",
// webpack 配置,键值对象时会合并配置,为方法时会改写配置
// https://cli.vuejs.org/guide/webpack.html#simple-configuration
// eslint-disable-next-line no-unused-vars
configureWebpack: config => {
if (process.env.NODE_ENV === "production") {
// 为生产环境修改配置...
} else {
// 为开发环境修改配置...
}
},

// webpack 链接 API,用于生成和修改 webapck 配置
// https://github.com/mozilla-neutrino/webpack-chain
// eslint-disable-next-line no-unused-vars
chainWebpack: config => {
// 因为是多页面,所以取消 chunks,每个页面只对应一个单独的 JS / CSS
config.optimization.splitChunks({
cacheGroups: {}
});
// 'src/lib' 目录下为外部库文件,不参与 eslint 检测
config.module
.rule("eslint")
.exclude.add("/Users/maybexia/Downloads/FE/community_built-in/src/lib")
.end();
},

// 配置高于 chainWebpack 中关于 css loader 的配置
css: {
// 默认情况下,只有 *.module.[ext] 结尾的文件才会被视作 CSS Modules 模块。设置为 false 后你就可以去掉文件名中的 .module 并将所有的 *.(css|scss|sass|less|styl(us)?) 文件视为 CSS Modules 模块
requireModuleExtension: true,

// 是否使用 css 分离插件 ExtractTextPlugin,采用独立样式文件载入,不采用 /<code>


分享到:


相關文章: