GithubHelp home page GithubHelp logo

hhy5277 / vue-cli Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vuejs/vue-cli

0.0 2.0 0.0 147 KB

Simple CLI for scaffolding Vue.js projects

License: MIT License

JavaScript 99.79% Vue 0.21%

vue-cli's Introduction

vue-cli Build Status npm package

A simple CLI for scaffolding Vue.js projects.

Installation

Prerequisites: Node.js (>=4.x, 5.x preferred) and Git.

$ npm install -g vue-cli

Usage

$ vue init <template-name> <project-name>

Example:

$ vue init webpack my-project

The above command pulls the template from vuejs-templates/webpack, prompts for some information, and generates the project at ./my-project/.

Official Templates

The purpose of official Vue project templates are to provide opinionated, battery-included development tooling setups so that users can get started with actual app code as fast as possible. However, these templates are un-opinionated in terms of how you structure your app code and what libraries you use in addition to Vue.js.

All official project templates are repos in the vuejs-templates organization. When a new template is added to the organization, you will be able to run vue init <template-name> <project-name> to use that template. You can also run vue list to see all available official templates.

Current available templates include:

  • webpack - A full-featured Webpack + vue-loader setup with hot reload, linting, testing & css extraction.

  • webpack-simple - A simple Webpack + vue-loader setup for quick prototyping.

  • browserify - A full-featured Browserify + vueify setup with hot-reload, linting & unit testing.

  • browserify-simple - A simple Browserify + vueify setup for quick prototyping.

  • simple - The simplest possible Vue setup in a single HTML file

Custom Templates

It's unlikely to make everyone happy with the official templates. You can simply fork an official template and then use it via vue-cli with:

vue init username/repo my-project

Where username/repo is the GitHub repo shorthand for your fork.

The shorthand repo notation is passed to download-git-repo so you can also use things like bitbucket:username/repo for a Bitbucket repo and username/repo#branch for tags or branches.

If you would like to download from a private repository use the --clone flag and the cli will use git clone so your SSH keys are used.

Local Templates

Instead of a GitHub repo, you can also use a template on your local file system:

vue init ~/fs/path/to-custom-template my-project

Writing Custom Templates from Scratch

  • A template repo must have a template directory that holds the template files.

  • A template repo may have a metadata file for the template which can be either a meta.js or meta.json file. It can contain the following fields:

    • prompts: used to collect user options data;

    • filters: used to conditional filter files to render.

    • completeMessage: the message to be displayed to the user when the template has been generated. You can include custom instruction here.

prompts

The prompts field in the metadata file should be an object hash containing prompts for the user. For each entry, the key is the variable name and the value is an Inquirer.js question object. Example:

{
  "prompts": {
    "name": {
      "type": "string",
      "required": true,
      "message": "Project name"
    }
  }
}

After all prompts are finished, all files inside template will be rendered using Handlebars, with the prompt results as the data.

Conditional Prompts

A prompt can be made conditional by adding a when field, which should be a JavaScript expression evaluated with data collected from previous prompts. For example:

{
  "prompts": {
    "lint": {
      "type": "confirm",
      "message": "Use a linter?"
    },
    "lintConfig": {
      "when": "lint",
      "type": "list",
      "message": "Pick a lint config",
      "choices": [
        "standard",
        "airbnb",
        "none"
      ]
    }
  }
}

The prompt for lintConfig will only be triggered when the user answered yes to the lint prompt.

Pre-registered Handlebars Helpers

Two commonly used Handlebars helpers, if_eq and unless_eq are pre-registered:

{{#if_eq lintConfig "airbnb"}};{{/if_eq}}
Custom Handlebars Helpers

You may want to register additional Handlebars helpers using the helpers property in the metadata file. The object key is the helper name:

module.exports = {
  helpers: {
    lowercase: str => str.toLowerCase()
  }
}

Upon registration, they can be used as follows:

{{ lowercase name }}

File filters

The filters field in the metadata file should be an object hash containing file filtering rules. For each entry, the key is a minimatch glob pattern and the value is a JavaScript expression evaluated in the context of prompt answers data. Example:

{
  "filters": {
    "test/**/*": "needTests"
  }
}

Files under test will only be generated if the user answered yes to the prompt for needTests.

Note that the dot option for minimatch is set to true so glob patterns would also match dotfiles by default.

Additional data available in meta.{js,json}

  • destDirName - destination directory name
{
  "completeMessage": "To get started:\n\n  cd {{destDirName}}\n  npm install\n  npm run dev"
}
  • inPlace - generating template into current directory
{
  "completeMessage": "{{#inPlace}}To get started:\n\n  npm install\n  npm run dev.{{else}}To get started:\n\n  cd {{destDirName}}\n  npm install\n  npm run dev.{{/inPlace}}"
}

License

MIT

vue-cli's People

Contributors

allentong avatar chrisvfritz avatar guox191 avatar kazupon avatar ktsn avatar orlandoaleman avatar posva avatar queckezz avatar watilde avatar yyx990803 avatar zhouwenbin avatar zigomir avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.