In early 2017, a project was established and maintained on Github: Vue Boilerplate Template, to achieve a scaffolding Template for Vue + Webpack out of the box; Its goal and purpose is to provide some reference on how to build medium and large Vue projects based on past experience. This is quite a long time, there are insist on maintenance and update, the main rely on the library, basically keep synchronous upgrade; Documenting this article provides a more comprehensive description of its design and functions as a project Wiki.

Photo from picjumbo.com

About thisVue, Webpack scaffolding template

This is a scaffolding project for developing a single page Web application; We use Vue as the development framework, Webpack as the construction tool, and Element-UI as the UI component library. Vue-router, VUEX, vuE-I18N are also injected to support single page applications, complex state management, and multi-language Settings. In addition, it relies on loDash, DayJS, JS-cookie and other development tool libraries to improve page development efficiency. Of course, in the actual project, you can remove or modify freely according to actual needs. Two demo pages are provided in this project that you can view online.

How to use this scaffold template, such as prerequisites, usage, and demo, is explained in the README and will not be covered here. This scaffolding template was built on vue-CLI when vue-CLI was 2.*; Another is that projects can be used out of the box, such as VUex, ESLint, etc., are introduced by default (the benefits are: clear and complete project Settings; If multiple languages are not required, subtraction can be done on this basis by removing VUE-I18N and related codes; Second, there is no need to write extra code to allow users to selectively introduce dependencies, saving effort and doing more valuable things. Third, this is also a tao advocate, like ESLint, pre-commit, etc. are excellent for team projects, and should be introduced by default); So the entire directory structure looks like this:

├ ─ ─ LICENSE -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- project LICENSE (MIT LICENSE) file ├ ─ ─ build -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- to store the project build documentation ├ ─ ─ the config -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - To store the project build relevant configuration file ├ ─ ─ dist -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- to store the project build output file after ├ ─ ─ index. The ejs -- -- -- -- -- -- -- -- -- -- -- -- -- project initiation document (/ index. HTML) ├ ─ ─ Package - lock. Json - recording NPM modify depend on lock file ├ ─ ─ package. The json -- -- -- -- -- -- -- -- -- -- the NPM package. The json processing details ├ ─ ─ the SRC -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Project application main code │ ├ ─ ─ App. Vue -- -- -- -- -- -- -- -- -- -- - the application's main component │ ├ ─ ─ assets -- -- -- -- -- -- -- -- -- -- -- -- storage resources: Style, images, fonts │ ├ ─ ─ components -- -- -- -- -- -- -- -- project custom common components │ ├ ─ ─ constants -- -- -- -- -- -- -- -- -- project public normally on custom │ ├ ─ ─ filters. The js -- -- -- -- -- -- -- -- project custom vue Filter instructions │ ├ ─ ─ global. Js -- -- -- -- -- -- -- -- - to help share the mian. Js work │ ├ ─ ─ helper -- -- -- -- -- -- -- -- -- -- -- -- project custom auxiliary various tools │ ├ ─ ─ locales -- -- -- -- -- -- -- -- -- -- - Deposit (public) multilingual configuration │ ├ ─ ─ main. Js -- -- -- -- -- -- -- -- -- -- - the population of project code file │ ├ ─ ─ mixins -- -- -- -- -- -- -- -- -- -- -- -- to deposit a mixin code related │ ├ ─ ─ the router -- -- -- -- -- -- -- -- -- -- -- -- storage & To handle the related routing │ ├ ─ ─ store -- -- -- -- -- -- -- -- -- -- -- -- -- storage & handling Vuex related │ └ ─ ─ views -- -- -- -- -- -- -- -- -- -- -- -- -- storage project page code ├ ─ ─ the static -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - static Assets (not webpack processing) ├ ─ ─ the test -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- project of all kinds of test related └ ─ ─ yarn. The lock -- -- -- -- -- -- -- -- -- -- -- -- -- lock file record with yarn modified to rely onCopy the code

Environment setting behind the project

Start with the environment; As we all know, given the history of JavaScript, the experience of writing JavaScript continues to get better as more new syntax and code features are introduced as versions are updated. While some browsers don’t support it very well, the scaffolding already introduces Babel dependencies, and you can rest assured that ES6 and beyond will not cause compatibility problems. Similarly, with CSS, autoprefixer has been introduced and configured so that you can use the new features without worrying about the browser vendor prefix.

Code and submission rule constraints

This is critical for code design and writing; Because: Code is written to people first, then to machines. When designing your code, it’s worth the time and effort to make it readable. However, asking everyone to voluntarily maintain a uniform code style is no less difficult than trying to achieve greatness on their own. So ESLint is a dependency and definitely a must for team projects; Inject it into Git’s pre-commit hook to enforce a uniform code style. At this time, there is the possibility of eternal transmission. The same is true for code submissions. It’s not a good habit to submit meaningless messages in the hope of a quick fix. Because when you look back, you’ll realize that everything that went wrong happened at the right time. Fortunately, for these tasks, this scaffolding has been done for you; Of course, you can adjust it to your team’s overall preferences.

The global method provided

For code writing efficiency, is particularly important; After all: “The world’s martial arts, only fast can not be broken; Even if it does not show martial arts, speed determines the distance between you and me “; More seriously, the idea is that only by completing requirements quickly can you have time to learn more or refine on key points. Therefore, common tools, plug-ins, and methods have been added to the Vue global instance in this scaffold for easy invocation. For example, by adding to vue. prototype, or global mixins (minxns/ Globalmixin.js) and filters (filters.js);

import _ from '@helper/lodash.js'
import { $apis, $utils } from '@helper'

Vue.prototype.$_ = _
Vue.prototype.$apis = $apis
Vue.prototype.$utils = $utils
Copy the code

Instead of repeatedly importing this.$_. Debounce, you can use this.$_. For the $sign, it is a personal preference to know that it is from the global; Of course, you can modify the notation to your liking. You can see that in the Helper folder, there is a special file called lodash.js to optimize the use of LoDash and to make it easier to use. This is described in detail in the introduction of using modularity as much as possible in Webpack Packaging Optimization volume. Similarly, you can repackage the various libraries you introduce so that they can be easily called in a project, and can be modified and replaced uniformly without changing where they are called. It is also worth mentioning the handling of HTTP requests.

More elegant handling of Http requests

Here is an ancient example of handling Http requests; It may seem extreme and even weird, but it’s still a common play (using the current popular MVVM framework; Even Query.Ajax supports chained calls); However, there are some hardworking developers who take the trouble to write similar code, which is fine in itself; But when handed over to someone else to maintain, or to read, it can cause harm.

$.ajax({ type: "GET", url: "xxx/getYyyContent", data: { username: $("#username").val() }, dataType: "json", success: function (data) { const textContent = data.data.textContent $('#text-content').html(textContent) }, error: Function (error) {// Do something to handle error}, complete: function (error) {// Do something to handle error});Copy the code

Avenue to simple, excellent development process, must be easy to write and maintain! This scaffold references the class libraries AXIos and Q, and encapsulates them simply to handle Http request correlation; Enable it to support chain calls, and at the same time, unified processing of returned data, simplify returned content, so that the final result can be obtained as simple as possible, see Helper /ajax.js. In addition, it is recommended that developers store interfaces in a unified folder based on functional module planning, such as Helper /apis; So clear, easy to call, and for multi-person collaborative development, and do not respond to each other, reduce unnecessary conflicts. Similar to the good use of configuration, table driven method of programming, should live to learn and use, throughout; For a more detailed description, see The Basics of Using Vue beautifully. If Http requests are handled this way, they can be modified to look like this as shown in the annoying example above:

const params = { username: this.form.username } this.$apis.xxx.getYyyContent(params).then(result => { this.textContent = result.textContent }).catch(error => {// Do something to handle error}).fin(() => {// Do additional processing whether the request succeeds or fails})Copy the code

Considerate Router configuration

Using the MVVM framework + * -Router to create single-page applications has become mainstream. In this scaffold, vue-Router has been added and processed; Router /routes/demo.js; Feel free to add routing and page mapping to enrich your application. At the same time, you can also configure various page information (such as: title, whether permission verification is required, etc.). For routing configuration, the more intimate configuration is to deal with the navigation bar (/ sidebar) related sections.

In modern Web applications with separate front and back ends, navigation bar configuration is generally combined by the front and back ends: the back end is responsible for providing relevant configuration data, and the front end is in charge of data and page mapping; In order to save workload, in-depth copy of front-end route configuration, recursive traversal removes items that do not need to be displayed in the navigation bar, and then merges back-end data to assemble a background configurable application navigation list without changing the front-end. Of course, you can add more Settings based on your business needs to make your application a better access and maintenance experience. For example, to avoid requesting navigation data every time you refresh the interface, make persistent storage (Eg: localStorage) and so on.

Perfect Webpack packaging optimization

One of the highlights of the scaffolding is that it keeps updating and optimizing the mainstream dependency libraries, such as Webpack, Element-UI, etc. Using WebPack to build Web applications, how to make it present a good build speed, optimize the package volume/file number after the build, improve its running efficiency, is every front-end developer should know. In this respect, also made a lot of learning and trying, and some experience recorded in: Webpack packaging optimization volume & Webpack packaging optimization speed. For its configuration scheme, see Vue Webpack Config; Of course, welcome to put forward your valuable suggestions to help further improve it. Also worth mentioning is the balance between the size of the build package and the number of files: avoid packages that are too large or too small; Webpack also provides plugins to help with this, such as LimitChunkCountPlugin and MinChunkSizePlugin. In view of the HTTP working mechanism, it is a good choice to build js files as few as possible, but not large (100KB ~ 500KB) on the basis of not destroying the on-demand loading. Of course, this refers to the situation that the server is enabled with GIP compression; If the front end is responsible for turning on gzip compression, the upper limit should be lower. In addition, considering optimization of projects over HTTP/2 is another dimension of the solution, which is not discussed here.

Support for progressive Web applications

Progressive Web Applications (PWA ~ Progressive Web App) : It exists as a new way to deliver amazing user experiences on the Web. It offers incredible advantages for building high-quality Progressive Web applications that can easily delight users, increase engagement, and increase conversion times. So this has become a must-do for modern Web applications. Vuejs-templates/PWA is selected for PWA support, which uses the sw-Precache-webpack-plugin to help generate service-worker.js. Due to some limitations of serviceWorker itself, this template does not introduce this feature by default. Uncomment this line of code to turn this feature on. For more information on this section, see related submissions & statements related to Optimizing Web Performance, Progressive, Accessibility, and Best Practices. It is recommended that you use the Lighthouse Google Browsing extension to keep score of your Web application to detect potential optimizations in your project.

One caveat: You can search globally for the ~ @change @~ keyword, which is the tag for mutable Settings; You can modify this part as required.

Scaffolding to provide the default command

As you know, NPM (yarn also) allows you to define script commands using the scripts field in package.json files. It supports wildcards, variables, hooks, external arguments, concurrent & asynchronous execution, and more. Therefore, you can use NPM Script to create your own efficient workflow. In this scaffolding, the default is simply to provide simple commands that you can use in your own scripts in conjunction with your favorite tools, such as gulp, bash, and so on, to shape your own efficient workflow. The following is a brief description of the default commands:

NPM (yarn) command Functional description
yarn start Run the project to develop the environment, yesyarn run devThe shortcut version of the
yarn run build:dll buildvendor.dll.js, on the basis ofwebpack.dll.conf.js
yarn run build Build the project to produce the environment
yarn run jarvis runwebpack-jarvis(very smart browser-based Webpack dashboard) inhttp://localhost:1337/
yarn run analyz Visualize the package being built inhttp://localhost:8888/
yarn run preview Perform a local preview of the project to the production environment inhttp://localhost:3000/
yarn run pretest A simple “forecast” of the packages built in the production environment: inhttp://localhost:3000/

Use this scaffold for the project

  • “Beautiful City Chain | NICE LINKS” : an open platform designed to gather excellent websites from all over the world and explore the wider world of the Internet; Here, you can easily discover, learn and share more useful or interesting things.

Which direction the project needs to be optimized

  • Improve unit test correlation; At the code level, good unit testing helps to improve the quality of the code, so that the project quality and subsequent maintenance can be better. Consider updating this section with reference to Vue Test Utils in a later release.
  • SEO optimization related; This is a question that should be considered by all sides; But at the project design level, it can be consideredServer side renderingTo greatly improve SEO; However, this involves more adjustments than other solutions for projects that are already in the development stage.”A Chain of Cities | NICE LINKS”Is based onprerenderIn thenginxLayer, pre-render for the browser to optimize SEO, now looks ok. The alternative isPrerender SPA PluginIn theWebpackPrerender static HTML content when building a project. A compromise will be considered for this area in future releases.

Write the epilogue at the end

For sharing, your generous tolerance and suggestions are the key to promoting both of us to the next level. Output text, always more hard than writing code; After paying a lot of knocking, we hope to get your valuable opinions and suggestions, so that the scaffolding can be further, to benefit more people. Finally, I recommend you to experience your latest work ~ “Beautiful City Chain | NICE LINKS” : love to create, infatuated for you, HOPE you will like it.

@2018-05-13 in Shenzhen. Last Modify: 2018-05-20


Articles you may be interested in (/ useful) :

  • How to write a beautiful Vue
  • Webpack package optimization for speed
  • Webpack package optimized volume section
  • Npm VS Yarn Memo sheet
  • “Lead” the most complete front-end resource collection
  • Updated version of the front-end resources tutorial
  • The grass at night – Front notes
  • Replacement of the Experienced front end “Posture” (I)
  • Wall crack recommended article collection