187 |
188 |
189 |
204 |
205 | ```
206 | 5. 说明
207 | - 模块中需要使用 `namespaced: true` 标记
208 | - 根节点状态可以直接在 `index.js` 编写
209 | - 能不用this.$store 最好不要使用,保持页面整洁
210 |
211 | 6. `eventbus.js` 主要用于简单的非父子组件交互通信使用
212 | > 使用方法
213 | ```
214 | this.$eventbus.on(event: string | Array, fn: Function)
215 | this.$eventbus.emit(event: string, data: Object)
216 | ```
217 | #### 目前正在使用的企业
218 |
219 |
--------------------------------------------------------------------------------
/circle.yml:
--------------------------------------------------------------------------------
1 | machine:
2 | node:
3 | version: stable
4 |
5 | dependencies:
6 | pre:
7 | - sudo sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list'
8 | - sudo apt-get update
9 | - sudo apt-get install google-chrome-stable
10 |
11 | test:
12 | override:
13 | - bash test.sh
14 |
--------------------------------------------------------------------------------
/deploy-docs.sh:
--------------------------------------------------------------------------------
1 | cd docs
2 | rm -rf _book
3 | gitbook build
4 | cd _book
5 | git init
6 | git add -A
7 | git commit -m 'update book'
8 | git push -f git@github.com:vuejs-templates/webpack.git master:gh-pages
9 |
--------------------------------------------------------------------------------
/docs/README.md:
--------------------------------------------------------------------------------
1 | # Introduction
2 |
3 | This boilerplate is targeted towards large, serious projects and assumes you are somewhat familiar with Webpack and `vue-loader`. Make sure to also read [`vue-loader`'s documentation](https://vue-loader.vuejs.org/) for common workflow recipes.
4 |
5 | If you just want to try out `vue-loader` or whip out a quick prototype, use the [webpack-simple](https://github.com/vuejs-templates/webpack-simple) template instead.
6 |
7 | ## Quickstart
8 |
9 | To use this template, scaffold a project with [vue-cli](https://github.com/vuejs/vue-cli). **It is recommended to use npm 3+ for a more efficient dependency tree.**
10 |
11 | ``` bash
12 | $ npm install -g vue-cli
13 | $ vue init webpack my-project
14 | $ cd my-project
15 | $ npm install
16 | $ npm run dev
17 | ```
18 |
--------------------------------------------------------------------------------
/docs/SUMMARY.md:
--------------------------------------------------------------------------------
1 | # Summary
2 |
3 | - [Project Structure](structure.md)
4 | - [Build Commands](commands.md)
5 | - [Linter Configuration](linter.md)
6 | - [Pre-Processors](pre-processors.md)
7 | - [Handling Static Assets](static.md)
8 | - [Environment Variables](env.md)
9 | - [Integrate with Backend Framework](backend.md)
10 | - [API Proxying During Development](proxy.md)
11 | - [Unit Testing](unit.md)
12 | - [End-to-end Testing](e2e.md)
13 | - [Prerendering for SEO](prerender.md)
14 |
--------------------------------------------------------------------------------
/docs/backend.md:
--------------------------------------------------------------------------------
1 | # Integrating with Backend Framework
2 |
3 | If you are building a purely-static app (one that is deployed separately from the backend API), then you probably don't even need to edit `config/index.js`. However, if you want to integrate this template with an existing backend framework, e.g. Rails/Django/Laravel, which comes with their own project structures, you can edit `config/index.js` to directly generate front-end assets into your backend project.
4 |
5 | Let's take a look at the default `config/index.js`:
6 |
7 | ``` js
8 | // config/index.js
9 | 'use strict'
10 | const path = require('path')
11 |
12 | module.exports = {
13 | build: {
14 | index: path.resolve(__dirname, 'dist/index.html'),
15 | assetsRoot: path.resolve(__dirname, 'dist'),
16 | assetsSubDirectory: 'static',
17 | assetsPublicPath: '/',
18 | productionSourceMap: true
19 | },
20 | dev: {
21 | port: 8080,
22 | proxyTable: {}
23 | }
24 | }
25 | ```
26 |
27 | Inside the `build` section, we have the following options:
28 |
29 | ### `build.index`
30 |
31 | > Must be an absolute path on your local file system.
32 |
33 | This is where the `index.html` (with injected asset URLs) will be generated.
34 |
35 | If you are using this template with a backend-framework, you can edit `index.html` accordingly and point this path to a view file rendered by your backend app, e.g. `app/views/layouts/application.html.erb` for a Rails app, or `resources/views/index.blade.php` for a Laravel app.
36 |
37 | ### `build.assetsRoot`
38 |
39 | > Must be an absolute path on your local file system.
40 |
41 | This should point to the root directory that contains all the static assets for your app. For example, `public/` for both Rails/Laravel.
42 |
43 | ### `build.assetsSubDirectory`
44 |
45 | Nest webpack-generated assets under this directory in `build.assetsRoot`, so that they are not mixed with other files you may have in `build.assetsRoot`. For example, if `build.assetsRoot` is `/path/to/dist`, and `build.assetsSubDirectory` is `static`, then all Webpack assets will be generated in `path/to/dist/static`.
46 |
47 | This directory will be cleaned before each build, so it should only contain assets generated by the build.
48 |
49 | Files inside `static/` will be copied into this directory as-is during build. This means if you change this prefix, all your absolute URLs referencing files in `static/` will also need to be changed. See [Handling Static Assets](static.md) for more details.
50 |
51 | ### `build.assetsPublicPath`
52 |
53 | This should be the URL path where your `build.assetsRoot` will be served from over HTTP. In most cases, this will be root (`/`). Only change this if your backend framework serves static assets with a path prefix. Internally, this is passed to Webpack as `output.publicPath`.
54 |
55 | ### `build.productionSourceMap`
56 |
57 | Whether to generate source maps for production build.
58 |
59 | ### `dev.port`
60 |
61 | Specify the port for the dev server to listen to.
62 |
63 | ### `dev.proxyTable`
64 |
65 | Define proxy rules for the dev server. See [API Proxying During Development](proxy.md) for more details.
66 |
--------------------------------------------------------------------------------
/docs/commands.md:
--------------------------------------------------------------------------------
1 | # Build Commands
2 |
3 | All build commands are executed via [NPM Scripts](https://docs.npmjs.com/misc/scripts).
4 |
5 | ### `npm run dev`
6 |
7 | > Starts a Node.js local development server. See [API Proxying During Development](proxy.md) for more details.
8 |
9 | - Webpack + `vue-loader` for single file Vue components.
10 | - State preserving hot-reload
11 | - State preserving compilation error overlay
12 | - Lint-on-save with ESLint
13 | - Source maps
14 |
15 | ### `npm run build`
16 |
17 | > Build assets for production. See [Integrating with Backend Framework](backend.md) for more details.
18 |
19 | - JavaScript minified with [UglifyJS](https://github.com/mishoo/UglifyJS2).
20 | - HTML minified with [html-minifier](https://github.com/kangax/html-minifier).
21 | - CSS across all components extracted into a single file and minified with [cssnano](https://github.com/ben-eb/cssnano).
22 | - All static assets compiled with version hashes for efficient long-term caching, and a production `index.html` is auto-generated with proper URLs to these generated assets.
23 |
24 | ### `npm run unit`
25 |
26 | > Run unit tests in PhantomJS with [Karma](https://karma-runner.github.io/). See [Unit Testing](unit.md) for more details.
27 |
28 | - Supports ES2015+ in test files.
29 | - Supports all webpack loaders.
30 | - Easy [mock injection](http://vuejs.github.io/vue-loader/en/workflow/testing-with-mocks.html).
31 |
32 | ### `npm run e2e`
33 |
34 | > Run end-to-end tests with [Nightwatch](http://nightwatchjs.org/). See [End-to-end Testing](e2e.md) for more details.
35 |
36 | - Run tests in multiple browsers in parallel.
37 | - Works with one command out of the box:
38 | - Selenium and chromedriver dependencies automatically handled.
39 | - Automatically spawns the Selenium server.
40 |
--------------------------------------------------------------------------------
/docs/e2e.md:
--------------------------------------------------------------------------------
1 | # End-to-end Testing
2 |
3 | This boilerplate uses [Nightwatch.js](http://nightwatchjs.org) for e2e tests. Nightwatch.js is a highly integrated e2e test runner built on top of Selenium. This boilerplate comes with Selenium server and chromedriver binaries pre-configured for you, so you don't have to mess with these yourself.
4 |
5 | Let's take a look at the files in the `test/e2e` directory:
6 |
7 | - `runner.js`
8 |
9 | A Node.js script that starts the dev server, and then launches Nightwatch to run tests against it. This is the script that will run when you run `npm run e2e`.
10 |
11 | - `nightwatch.conf.js`
12 |
13 | Nightwatch configuration file. See [Nightwatch's docs on configuration](http://nightwatchjs.org/guide#settings-file) for more details.
14 |
15 | - `custom-assertions/`
16 |
17 | Custom assertions that can be used in Nightwatch tests. See [Nightwatch's docs on writing custom assertions](http://nightwatchjs.org/guide#writing-custom-assertions) for more details.
18 |
19 | - `specs/`
20 |
21 | Your actual tests! See [Nightwatch's docs on writing tests](http://nightwatchjs.org/guide#writing-tests) and [API reference](http://nightwatchjs.org/api) for more details.
22 |
23 | ### Running Tests in More Browsers
24 |
25 | To configure which browsers to run the tests in, add an entry under "test_settings" in [`test/e2e/nightwatch.conf.js`](https://github.com/vuejs-templates/webpack/blob/master/template/test/e2e/nightwatch.conf.js#L17-L39) , and also the `--env` flag in [`test/e2e/runner.js`](https://github.com/vuejs-templates/webpack/blob/master/template/test/e2e/runner.js#L15). If you wish to configure remote testing on services like SauceLabs, you can either make the Nightwatch config conditional based on environment variables, or use a separate config file altogether. Consult [Nightwatch's docs on Selenium](http://nightwatchjs.org/guide#selenium-settings) for more details.
26 |
--------------------------------------------------------------------------------
/docs/env.md:
--------------------------------------------------------------------------------
1 | # Environment Variables
2 |
3 | Sometimes it is practical to have different config values according to the environment that the application is running in.
4 |
5 | As an example:
6 |
7 | ```js
8 | // config/prod.env.js
9 | module.exports = {
10 | NODE_ENV: '"production"',
11 | DEBUG_MODE: false,
12 | API_KEY: '"..."' // this is shared between all environments
13 | }
14 |
15 | // config/dev.env.js
16 | module.exports = merge(prodEnv, {
17 | NODE_ENV: '"development"',
18 | DEBUG_MODE: true // this overrides the DEBUG_MODE value of prod.env
19 | })
20 |
21 | // config/test.env.js
22 | module.exports = merge(devEnv, {
23 | NODE_ENV: '"testing"'
24 | })
25 | ```
26 |
27 | > **Note:** string variables need to be wrapped into single and double quotes `'"..."'`
28 |
29 | So, the environment variables are:
30 | - Production
31 | - NODE_ENV = 'production',
32 | - DEBUG_MODE = false,
33 | - API_KEY = '...'
34 | - Development
35 | - NODE_ENV = 'development',
36 | - DEBUG_MODE = true,
37 | - API_KEY = '...'
38 | - Testing
39 | - NODE_ENV = 'testing',
40 | - DEBUG_MODE = true,
41 | - API_KEY = '...'
42 |
43 | As we can see, `test.env` inherits the `dev.env` and the `dev.env` inherits the `prod.env`.
44 |
45 | ### Usage
46 |
47 | It is simple to use the environment variables in your code. For example:
48 |
49 | ```js
50 | Vue.config.productionTip = process.env.NODE_ENV === 'production'
51 | ```
52 |
--------------------------------------------------------------------------------
/docs/linter.md:
--------------------------------------------------------------------------------
1 | # Linter Configuration
2 |
3 | This boilerplate uses [ESLint](https://eslint.org/) as the linter, and uses the [Standard](https://github.com/feross/standard/blob/master/RULES.md) preset with some small customizations.
4 |
5 | If you are not happy with the default linting rules, you have several options:
6 |
7 | 1. Overwrite individual rules in `.eslintrc.js`. For example, you can add the following rule to enforce semicolons instead of omitting them:
8 |
9 | ``` js
10 | // .eslintrc.js
11 | "semi": [2, "always"]
12 | ```
13 |
14 | 2. Pick a different ESLint preset when generating the project, for example [eslint-config-airbnb](https://github.com/airbnb/javascript/tree/master/packages/eslint-config-airbnb).
15 |
16 | 3. Pick "none" for ESLint preset when generating the project and define your own rules. See [ESLint documentation](https://eslint.org/docs/rules/) for more details.
17 |
--------------------------------------------------------------------------------
/docs/pre-processors.md:
--------------------------------------------------------------------------------
1 | # Pre-Processors
2 |
3 | This boilerplate has pre-configured CSS extraction for most popular CSS pre-processors including LESS, SASS, Stylus, and PostCSS. To use a pre-processor, all you need to do is installing the appropriate webpack loader for it. For example, to use SASS:
4 |
5 | ``` bash
6 | npm install sass-loader node-sass --save-dev
7 | ```
8 |
9 | Note you also need to install `node-sass` because `sass-loader` depends on it as a peer dependency.
10 |
11 | ### Using Pre-Processors inside Components
12 |
13 | Once installed, you can use the pre-processors inside your `*.vue` components using the `lang` attribute on `
19 | ```
20 |
21 | ### A note on SASS syntax
22 |
23 | - `lang="scss"` corresponds to the CSS-superset syntax (with curly braces and semicolons).
24 | - `lang="sass"` corresponds to the indentation-based syntax.
25 |
26 | ### PostCSS
27 |
28 | Styles in `*.vue` files are piped through PostCSS by default, so you don't need to use a specific loader for it. You can simply add PostCSS plugins you want to use in `build/webpack.base.conf.js` under the `vue` block:
29 |
30 | ``` js
31 | // build/webpack.base.conf.js
32 | module.exports = {
33 | // ...
34 | vue: {
35 | postcss: [/* your plugins */]
36 | }
37 | }
38 | ```
39 |
40 | See [vue-loader's related documentation](http://vuejs.github.io/vue-loader/en/features/postcss.html) for more details.
41 |
42 | ### Standalone CSS Files
43 |
44 | To ensure consistent extraction and processing, it is recommended to import global, standalone style files from your root `App.vue` component, for example:
45 |
46 | ``` html
47 |
48 |
49 | ```
50 |
51 | Note you should probably only do this for the styles written by yourself for your application. For existing libraries e.g. Bootstrap or Semantic UI, you can place them inside `/static` and reference them directly in `index.html`. This avoids extra build time and also is better for browser caching. (See [Static Asset Handling](static.md))
52 |
--------------------------------------------------------------------------------
/docs/prerender.md:
--------------------------------------------------------------------------------
1 | # Prerendering for SEO
2 |
3 | If you want to prerender routes that will not significantly change once pushed to production, use this Webpack plugin: [prerender-spa-plugin](https://www.npmjs.com/package/prerender-spa-plugin), which has been tested for use with Vue. For pages that _do_ frequently change, [Prerender.io](https://prerender.io/) and [Netlify](https://www.netlify.com/pricing) both offer plans for regularly re-prerendering your content for search engines.
4 |
5 | ## Using `prerender-spa-plugin`
6 |
7 | 1. Install it as a dev dependency:
8 |
9 | ```bash
10 | npm install --save-dev prerender-spa-plugin
11 | ```
12 |
13 | 2. Require it in **build/webpack.prod.conf.js**:
14 |
15 | ```js
16 | // This line should go at the top of the file where other 'imports' live in
17 | const PrerenderSpaPlugin = require('prerender-spa-plugin')
18 | ```
19 |
20 | 3. Configure it in the `plugins` array (also in **build/webpack.prod.conf.js**):
21 |
22 | ```js
23 | new PrerenderSpaPlugin(
24 | // Path to compiled app
25 | path.join(__dirname, '../dist'),
26 | // List of endpoints you wish to prerender
27 | [ '/' ]
28 | )
29 | ```
30 |
31 | If you also wanted to prerender `/about` and `/contact`, then that array would be `[ '/', '/about', '/contact' ]`.
32 |
33 | 4. Enable history mode for `vue-router`:
34 | ```js
35 | const router = new VueRouter({
36 | mode: 'history',
37 | routes: [...]
38 | })
39 | ```
40 |
--------------------------------------------------------------------------------
/docs/proxy.md:
--------------------------------------------------------------------------------
1 | # API Proxying During Development
2 |
3 | When integrating this boilerplate with an existing backend, a common need is to access the backend API when using the dev server. To achieve that, we can run the dev server and the API backend side-by-side (or remotely), and let the dev server proxy all API requests to the actual backend.
4 |
5 | To configure the proxy rules, edit `dev.proxyTable` option in `config/index.js`. The dev server is using [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware) for proxying, so you should refer to its docs for detailed usage. But here's a simple example:
6 |
7 | ``` js
8 | // config/index.js
9 | module.exports = {
10 | // ...
11 | dev: {
12 | proxyTable: {
13 | // proxy all requests starting with /api to jsonplaceholder
14 | '/api': {
15 | target: 'http://jsonplaceholder.typicode.com',
16 | changeOrigin: true,
17 | pathRewrite: {
18 | '^/api': ''
19 | }
20 | }
21 | }
22 | }
23 | }
24 | ```
25 |
26 | The above example will proxy the request `/api/posts/1` to `http://jsonplaceholder.typicode.com/posts/1`.
27 |
28 | ## URL Matching
29 |
30 | In addition to static urls you can also use glob patterns to match URLs, e.g. `/api/**`. See [Context Matching](https://github.com/chimurai/http-proxy-middleware#context-matching) for more details. In addition, you can provide a `filter` option that can be a custom function to determine whether a request should be proxied:
31 |
32 | ``` js
33 | proxyTable: {
34 | '**': {
35 | target: 'http://jsonplaceholder.typicode.com',
36 | filter: function (pathname, req) {
37 | return pathname.match('^/api') && req.method === 'GET'
38 | }
39 | }
40 | }
41 | ```
42 |
--------------------------------------------------------------------------------
/docs/static.md:
--------------------------------------------------------------------------------
1 | # Handling Static Assets
2 |
3 | You will notice in the project structure we have two directories for static assets: `src/assets` and `static/`. What is the difference between them?
4 |
5 | ### Webpacked Assets
6 |
7 | To answer this question, we first need to understand how Webpack deals with static assets. In `*.vue` components, all your templates and CSS are parsed by `vue-html-loader` and `css-loader` to look for asset URLs. For example, in `` and `background: url(./logo.png)`, `"./logo.png"` is a relative asset path and will be **resolved by Webpack as a module dependency**.
8 |
9 | Because `logo.png` is not JavaScript, when treated as a module dependency, we need to use `url-loader` and `file-loader` to process it. This boilerplate has already configured these loaders for you, so you basically get features such as filename fingerprinting and conditional base64 inlining for free, while being able to use relative/module paths without worrying about deployment.
10 |
11 | Since these assets may be inlined/copied/renamed during build, they are essentially part of your source code. This is why it is recommended to place Webpack-processed static assets inside `/src`, along side other source files. In fact, you don't even have to put them all in `/src/assets`: you can organize them based on the module/component using them. For example, you can put each component in its own directory, with its static assets right next to it.
12 |
13 | ### Asset Resolving Rules
14 |
15 | - **Relative URLs**, e.g. `./assets/logo.png` will be interpreted as a module dependency. They will be replaced with an auto-generated URL based on your Webpack output configuration.
16 |
17 | - **Non-prefixed URLs**, e.g. `assets/logo.png` will be treated the same as the relative URLs and translated into `./assets/logo.png`.
18 |
19 | - **URLs prefixed with `~`** are treated as a module request, similar to `require('some-module/image.png')`. You need to use this prefix if you want to leverage Webpack's module resolving configurations. For example if you have a resolve alias for `assets`, you need to use `` to ensure that alias is respected.
20 |
21 | - **Root-relative URLs**, e.g. `/assets/logo.png` are not processed at all.
22 |
23 | ### Getting Asset Paths in JavaScript
24 |
25 | In order for Webpack to return the correct asset paths, you need to use `require('./relative/path/to/file.jpg')`, which will get processed by `file-loader` and returns the resolved URL. For example:
26 |
27 | ``` js
28 | computed: {
29 | background () {
30 | return require('./bgs/' + this.id + '.jpg')
31 | }
32 | }
33 | ```
34 |
35 | **Note the above example will include every image under `./bgs/` in the final build.** This is because Webpack cannot guess which of them will be used at runtime, so it includes them all.
36 |
37 | ### "Real" Static Assets
38 |
39 | In comparison, files in `static/` are not processed by Webpack at all: they are directly copied to their final destination as-is, with the same filename. You must reference these files using absolute paths, which is determined by joining `build.assetsPublicPath` and `build.assetsSubDirectory` in `config.js`.
40 |
41 | As an example, with the following default values:
42 |
43 | ``` js
44 | // config/index.js
45 | module.exports = {
46 | // ...
47 | build: {
48 | assetsPublicPath: '/',
49 | assetsSubDirectory: 'static'
50 | }
51 | }
52 | ```
53 |
54 | Any file placed in `static/` should be referenced using the absolute URL `/static/[filename]`. If you change `assetSubDirectory` to `assets`, then these URLs will need to be changed to `/assets/[filename]`.
55 |
56 | We will learn more about the config file in the section about [backend integration](backend.md).
57 |
--------------------------------------------------------------------------------
/docs/structure.md:
--------------------------------------------------------------------------------
1 | # Project Structure
2 |
3 | ``` bash
4 | .
5 | ├── build/ # webpack config files
6 | │ └── ...
7 | ├── config/
8 | │ ├── index.js # main project config
9 | │ └── ...
10 | ├── src/
11 | │ ├── main.js # app entry file
12 | │ ├── App.vue # main app component
13 | │ ├── components/ # ui components
14 | │ │ └── ...
15 | │ └── assets/ # module assets (processed by webpack)
16 | │ └── ...
17 | ├── static/ # pure static assets (directly copied)
18 | ├── test/
19 | │ └── unit/ # unit tests
20 | │ │ ├── specs/ # test spec files
21 | │ │ ├── index.js # test build entry file
22 | │ │ └── karma.conf.js # test runner config file
23 | │ └── e2e/ # e2e tests
24 | │ │ ├── specs/ # test spec files
25 | │ │ ├── custom-assertions/ # custom assertions for e2e tests
26 | │ │ ├── runner.js # test runner script
27 | │ │ └── nightwatch.conf.js # test runner config file
28 | ├── .babelrc # babel config
29 | ├── .postcssrc.js # postcss config
30 | ├── .eslintrc.js # eslint config
31 | ├── .editorconfig # editor config
32 | ├── index.html # index.html template
33 | └── package.json # build scripts and dependencies
34 | ```
35 |
36 | ### `build/`
37 |
38 | This directory holds the actual configurations for both the development server and the production webpack build. Normally you don't need to touch these files unless you want to customize Webpack loaders, in which case you should probably look at `build/webpack.base.conf.js`.
39 |
40 | ### `config/index.js`
41 |
42 | This is the main configuration file that exposes some of the most common configuration options for the build setup. See [API Proxying During Development](proxy.md) and [Integrating with Backend Framework](backend.md) for more details.
43 |
44 | ### `src/`
45 |
46 | This is where most of your application code will live in. How to structure everything inside this directory is largely up to you; if you are using Vuex, you can consult the [recommendations for Vuex applications](http://vuex.vuejs.org/en/structure.html).
47 |
48 | ### `static/`
49 |
50 | This directory is an escape hatch for static assets that you do not want to process with Webpack. They will be directly copied into the same directory where webpack-built assets are generated.
51 |
52 | See [Handling Static Assets](static.md) for more details.
53 |
54 | ### `test/unit`
55 |
56 | Contains unit test related files. See [Unit Testing](unit.md) for more details.
57 |
58 | ### `test/e2e`
59 |
60 | Contains e2e test related files. See [End-to-end Testing](e2e.md) for more details.
61 |
62 | ### `index.html`
63 |
64 | This is the **template** `index.html` for our single page application. During development and builds, Webpack will generate assets, and the URLs for those generated assets will be automatically injected into this template to render the final HTML.
65 |
66 | ### `package.json`
67 |
68 | The NPM package meta file that contains all the build dependencies and [build commands](commands.md).
69 |
--------------------------------------------------------------------------------
/docs/unit.md:
--------------------------------------------------------------------------------
1 | # Unit Testing
2 |
3 | An overview of the tools used by this boilerplate for unit testing:
4 |
5 | - [Karma](https://karma-runner.github.io/): the test runner that launches browsers, runs the tests and reports the results to us.
6 | - [karma-webpack](https://github.com/webpack/karma-webpack): the plugin for Karma that bundles our tests using Webpack.
7 | - [Mocha](https://mochajs.org/): the test framework that we write test specs with.
8 | - [Chai](http://chaijs.com/): test assertion library that provides better assertion syntax.
9 | - [Sinon](http://sinonjs.org/): test utility library that provides spies, stubs and mocks.
10 |
11 | Chai and Sinon are integrated using [karma-sinon-chai](https://github.com/kmees/karma-sinon-chai), so all Chai interfaces (`should`, `expect`, `assert`) and `sinon` are globally available in test files.
12 |
13 | And the files:
14 |
15 | - `index.js`
16 |
17 | This is the entry file used by `karma-webpack` to bundle all the test code and source code (for coverage purposes). You can ignore it for the most part.
18 |
19 | - `specs/`
20 |
21 | This directory is where you write your actual tests. You can use full ES2015+ and all supported Webpack loaders in your tests.
22 |
23 | - `karma.conf.js`
24 |
25 | This is the Karma configuration file. See [Karma docs](https://karma-runner.github.io/) for more details.
26 |
27 | ## Running Tests in More Browsers
28 |
29 | You can run the tests in multiple real browsers by installing more [karma launchers](https://karma-runner.github.io/1.0/config/browsers.html) and adjusting the `browsers` field in `test/unit/karma.conf.js`.
30 |
31 | ## Mocking Dependencies
32 |
33 | This boilerplate comes with [inject-loader](https://github.com/plasticine/inject-loader) installed by default. For usage with `*.vue` components, see [vue-loader docs on testing with mocks](http://vue-loader.vuejs.org/en/workflow/testing-with-mocks.html).
34 |
--------------------------------------------------------------------------------
/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {{ name }}
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/meta.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | "helpers": {
3 | "if_or": function (v1, v2, options) {
4 | if (v1 || v2) {
5 | return options.fn(this);
6 | }
7 |
8 | return options.inverse(this);
9 | }
10 | },
11 | "prompts": {
12 | "name": {
13 | "type": "string",
14 | "required": true,
15 | "message": "项目名称"
16 | },
17 | "description": {
18 | "type": "string",
19 | "required": false,
20 | "message": "项目描述",
21 | "default": "基于Vue.js的前端项目"
22 | },
23 | "author": {
24 | "type": "string",
25 | "message": "作者"
26 | },
27 | "build": {
28 | "type": "list",
29 | "message": "Vue 编译",
30 | "choices": [
31 | {
32 | "name": "运行时+编译器:推荐大多数用户使用",
33 | "value": "standalone",
34 | "short": "standalone"
35 | },
36 | {
37 | "name": "运行时版本:约6KB的打包机最小+ gzip,但模板(或任何Vue特定的HTML)只允许在.vue文件中 - 渲染功能需要在其他地方",
38 | "value": "runtime",
39 | "short": "runtime"
40 | }
41 | ]
42 | },
43 | "router": {
44 | "type": "confirm",
45 | "message": "安装 路由?"
46 | },
47 | "lint": {
48 | "type": "confirm",
49 | "message": "使用 ESLint 规范 你的代码?"
50 | },
51 | "lintConfig": {
52 | "when": "lint",
53 | "type": "list",
54 | "message": "选择一个默认ESLint标准",
55 | "choices": [
56 | {
57 | "name": "Standard (https://github.com/standard/standard)",
58 | "value": "standard",
59 | "short": "Standard"
60 | },
61 | {
62 | "name": "Airbnb (https://github.com/airbnb/javascript)",
63 | "value": "airbnb",
64 | "short": "Airbnb"
65 | },
66 | {
67 | "name": "none (自定义配置)",
68 | "value": "none",
69 | "short": "none"
70 | }
71 | ]
72 | },
73 | "vuesocket": {
74 | "type": "confirm",
75 | "message": "集成vue-socket.io开发响应式项目?"
76 | },
77 | "socketio": {
78 | "type": "string",
79 | "message": "如果你选择集成vue-socket.io请输入你的远程服务端地址否则请忽略此项:"
80 | },
81 | "unit": {
82 | "type": "confirm",
83 | "message": "安装测试框架 Karma + Mocha?"
84 | },
85 | "e2e": {
86 | "type": "confirm",
87 | "message": "使用Nightwatch进行端到端测试?"
88 | }
89 | },
90 | "filters": {
91 | ".eslintrc.js": "lint",
92 | ".eslintignore": "lint",
93 | "config/test.env.js": "unit || e2e",
94 | "test/unit/**/*": "unit",
95 | "build/webpack.test.conf.js": "unit",
96 | "test/e2e/**/*": "e2e",
97 | "src/router/**/*": "router"
98 | },
99 | "completeMessage": "友情提示: 编程有害身体,为了你和家人的健康,请到野外编程\n\n {{^inPlace}}cd {{destDirName}}\n {{/inPlace}}npm install\n npm run dev\n\n如有疑问请联系QQ:121625933"
100 | };
101 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "vue-cli-template-webpack",
3 | "version": "2.0.0",
4 | "license": "MIT",
5 | "description": "A full-featured Webpack setup with hot-reload, lint-on-save, unit testing & css extraction.",
6 | "scripts": {
7 | "docs": "cd docs && gitbook serve",
8 | "docs:deploy": "bash ./deploy-docs.sh"
9 | },
10 | "devDependencies": {
11 | "vue-cli": "^2.8.1"
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/template/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": [
3 | ["env", {
4 | "modules": false,
5 | "targets": {
6 | "browsers": ["> 1%", "last 2 versions", "not ie <= 8"]
7 | }
8 | }],
9 | "stage-2"
10 | ],
11 | "plugins": ["transform-runtime"],
12 | "env": {
13 | "test": {
14 | "presets": ["env", "stage-2"],
15 | "plugins": ["istanbul"]
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/template/.editorconfig:
--------------------------------------------------------------------------------
1 | root = true
2 |
3 | [*]
4 | charset = utf-8
5 | indent_style = space
6 | indent_size = 2
7 | end_of_line = lf
8 | insert_final_newline = true
9 | trim_trailing_whitespace = true
10 |
--------------------------------------------------------------------------------
/template/.eslintignore:
--------------------------------------------------------------------------------
1 | /build/
2 | /config/
3 | /dist/
4 | /*.js
5 | {{#unit}}
6 | /test/unit/coverage/
7 | {{/unit}}
8 |
--------------------------------------------------------------------------------
/template/.eslintrc.js:
--------------------------------------------------------------------------------
1 | // https://eslint.org/docs/user-guide/configuring
2 |
3 | module.exports = {
4 | root: true,
5 | parser: 'babel-eslint',
6 | parserOptions: {
7 | sourceType: 'module'
8 | },
9 | env: {
10 | browser: true,
11 | },
12 | {{#if_eq lintConfig "standard"}}
13 | // https://github.com/standard/standard/blob/master/docs/RULES-en.md
14 | extends: 'standard',
15 | {{/if_eq}}
16 | {{#if_eq lintConfig "airbnb"}}
17 | extends: 'airbnb-base',
18 | {{/if_eq}}
19 | // required to lint *.vue files
20 | plugins: [
21 | 'html'
22 | ],
23 | {{#if_eq lintConfig "airbnb"}}
24 | // check if imports actually resolve
25 | 'settings': {
26 | 'import/resolver': {
27 | 'webpack': {
28 | 'config': 'build/webpack.base.conf.js'
29 | }
30 | }
31 | },
32 | {{/if_eq}}
33 | // add your custom rules here
34 | 'rules': {
35 | {{#if_eq lintConfig "standard"}}
36 | // allow paren-less arrow functions
37 | 'arrow-parens': 0,
38 | // allow async-await
39 | 'generator-star-spacing': 0,
40 | {{/if_eq}}
41 | {{#if_eq lintConfig "airbnb"}}
42 | // don't require .vue extension when importing
43 | 'import/extensions': ['error', 'always', {
44 | 'js': 'never',
45 | 'vue': 'never'
46 | }],
47 | // allow optionalDependencies
48 | 'import/no-extraneous-dependencies': ['error', {
49 | 'optionalDependencies': ['test/unit/index.js']
50 | }],
51 | {{/if_eq}}
52 | // allow debugger during development
53 | 'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/template/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | node_modules/
3 | /dist/
4 | npm-debug.log*
5 | yarn-debug.log*
6 | yarn-error.log*
7 | {{#unit}}
8 | /test/unit/coverage/
9 | {{/unit}}
10 | {{#e2e}}
11 | /test/e2e/reports/
12 | selenium-debug.log
13 | {{/e2e}}
14 |
15 | # Editor directories and files
16 | .idea
17 | .vscode
18 | *.suo
19 | *.ntvs*
20 | *.njsproj
21 | *.sln
22 |
--------------------------------------------------------------------------------
/template/.postcssrc.js:
--------------------------------------------------------------------------------
1 | // https://github.com/michael-ciniawsky/postcss-load-config
2 |
3 | module.exports = {
4 | "plugins": {
5 | // to edit target browsers: use "browserslist" field in package.json
6 | "autoprefixer": {}
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/template/README.md:
--------------------------------------------------------------------------------
1 | ## 编译
2 |
3 | ``` bash
4 | # 安装依赖
5 | npm install
6 |
7 | # 热重载 localhost:8080
8 | npm run dev
9 |
10 | # 生产模式(压缩)
11 | npm run build or npm run pro
12 |
13 | # 构建生产并查看捆绑分析器报告
14 | npm run build --report or npm run pro --report
15 | {{#unit}}
16 | # 运行单元测试
17 | npm run unit
18 | {{/unit}}
19 | {{#e2e}}
20 |
21 | # 运行e2e测试
22 | npm run e2e
23 | {{/e2e}}
24 | {{#if_or unit e2e}}
25 |
26 | # 运行所有测试
27 | npm test
28 | {{/if_or}}
29 | ```
30 | # 项目说明
31 |
32 | ## 组件相关文件夹说明 (components)
33 | > 该文件夹建议只写组件
34 |
35 | `怎么判断是组件?`
36 | >是不是可以复用,原则上可以复用的需要抽离成组件,不能复用无需组件化.
37 |
38 |
39 | ## 配置相关文件夹说明 (config)
40 |
41 | ### `api.conf.js`
42 | - 功能:
43 | > 集中管理各个业务模块的后端请求地址,便于排错和集中管理
44 | - 用法:
45 | > 每个模块都导出一个本模块的地址对象即可
46 | ```
47 | export const demo(模块名称) = {
48 | test: '/api/test' //只需要写相对路径路径
49 | }
50 | ```
51 |
52 | ### `service.conf.js`
53 | - 功能: 按照项目的需求初始化一些服务的配置参数,例如请求`http://127.0.0.1/api/test`,则根路径(`baseUrl`)为`http://127.0.0.1`,这样在`api.conf.js`只需要写相对路径,其中```baseUrl```为必须参数
54 | >
55 |
56 | ### `apistore.js`
57 | - 功能:
58 | > 作为所有业务请求的入口,将所有的http相关的处理从页面组件中剥离,在服务模块中集中处理完复杂逻辑等操作,减少页面臃肿程度.
59 | - 页面调用方法:(我们将所有的服务模块全部包装到this.$api中,以便识别服务)
60 | ```
61 | this.$api.user.adduser({name:'halower'})
62 | ```
63 | - 依赖文件:
64 | > `api.conf.js`、 `fetch(文件夹)`
65 |
66 | ### `baseapi.js`
67 | - 功能:
68 | > 作为所有服务对象的基类,包含了请求地址代理,统一的请求入口(`get,post` ect.)
69 |
70 | ### `routestore.js`
71 | - 功能:
72 | > 集中的路由管理中心,将各个业务模块中的路由统一整理合并
73 | - 用法:
74 | 1. 导入每个业务模块的路由单元
75 | ```
76 | import demo from '@/pages/demo/z-routes
77 | ```
78 | 2. 添加业务路由
79 | ```
80 | routes = routes.concat(demo)
81 | ```
82 | ## 指令相关文件夹说明 (directives)
83 |
84 | 功能
85 | 全局指令处理,只需要在此处使用Vue.directive即可扩展项目的指令集
86 |
87 | ## 过滤器相关文件夹说明 (filters)
88 | - 功能
89 | > 全局过滤器处理,只需要在此处扩展项目的过滤器集
90 | `Vue2.0中Vue作者和很多开发人员针对是否有必要保留过滤器曾在github上做过大量讨论,但是在目前的版本中依然支持过滤器,所以vbp也将其暂时引入进来`
91 |
92 | ## 业务服务相关文件夹说明 (fetch)
93 | - 功能
94 | > 将繁琐的数据处理和关联性的逻辑操作从页面中剥离,降低服务与页面的耦合
95 | - 用法
96 | 1. 引入服务基类并继承它
97 | 2. 方法中的url路径需要使用到`/config/api.conf.js` 的模块,由于已经使用代理处理过,因此只需要使用`this.业务模块名.请求地址`的模式即可以获取真实请求路径
98 | ```
99 | import BaseApiController from '@/config/baseapi'
100 |
101 | /**
102 | * @class 演示业务功能
103 | */
104 | class DemoApiController extends BaseApiController {
105 | /*
106 | * @method 这里我们只做演示
107 | */
108 | test () {
109 | // 假设这里有很复杂的前端逻辑
110 | return this.get([this.demo.test], {}).then(res => {
111 | return res.data
112 | })
113 | }
114 | }
115 | export default new DemoApiController()
116 | ```
117 | - 依赖文件:
118 | > `api.conf.js`
119 |
120 | ## 业务页面相关文件夹 (pages)
121 | - 功能
122 | > 编写各个业务模块的页面,每个页面都可以独立配置自己的自己独立的业务路由
123 | - 约定
124 | >
125 | 1. 建议路由按照业务模块层级命名,可以有效地避免非规则的命名给我们造成的路由冲突的困扰
126 | 2. 每个业务模块下都使用z-routes.js命名路由的配置文件,这样做仅仅是统一的将路由文件至于 该文件夹的最底下,当然你可以不这么做。
127 | - 用法
128 | > 在配置完路由后,请按照config中的说明文档,将路由导入 routestore.js
129 |
130 | ## 状态相关管理文件夹 (store)
131 | - 功能
132 | > 统一管理项目状态,在复杂的交互项目中可以启用该模块,可以减少事件的不便性
133 |
134 | - 使用方法
135 | >
136 | 1. `mutation-types.js` 在这里进行mutation命名的统一管理
137 | ```
138 | export const CHANGE_NAME = 'CHANGE_NAME' // 测试更改模块名称
139 | ```
140 | 在各个模块中可以使用如下方式引用,使用 ` [types.CHANGE_NAME]` 方式获取
141 |
142 | ```
143 | import * as types from '@/store/mutation-types'
144 | ```
145 |
146 | 2. 在`modules`文件夹中编写对应的模块,可以引用`fetch`文件夹中的服务在actions中处理业务
147 | 3. 写完模块状态管理后,在`index.js`中引入相应模块
148 | ```
149 | modules: {
150 | test
151 | }
152 | ```
153 | 4. 页面使用
154 | ```
155 |
156 |