# Hero
**Repository Path**: dianrong/Hero
## Basic Information
- **Project Name**: Hero
- **Description**: HERO 一个更好的移动跨平台开发方案
hero移动开发方案源起于点融网LB业务部门的一次无心的尝试,在点融的黑帮文化中发展出来的一个业界领先的开发方案。
- **Primary Language**: JavaScript
- **License**: BSD-3-Clause
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 60
- **Forks**: 13
- **Created**: 2017-03-17
- **Last Updated**: 2024-05-29
## Categories & Tags
**Categories**: develop-tools
**Tags**: None
## README
# **HERO 一个更好的移动跨平台开发方案**
hero移动开发方案源起于点融网LB业务部门的一次大胆的尝试,在点融的黑帮文化中发展出来的一个业界领先的开发方案。
## Hero 框架图

# 快速开始hero-cli
Create Hero apps with no build configuration.
* [Getting Started](#getting-started) – How to create a new app.
* [User Guide](#user-guide) – How to develop apps bootstrapped with Hero App.
Hero App works on Android, iOS, and Modem browser.
If something doesn’t work please [file an issue](https://github.com/hero-mobile/hero-cli/issues/new).
## Quick Overview
```sh
npm install -g hero-mobile/hero-cli
hero init my-app
cd my-app/
npm install
```
Once the installation is done, you can run some commands inside the project folder:
* `npm start` Start the application.
* `npm run build` When you’re ready to deploy to production, create a minified bundle with this command.
Run `npm start` and then open [http://localhost:3000/index.html](http://localhost:3000/index.html) to see your app.
### Get Started Immediately
hero-cli using [Webpack](http://webpack.github.io/) to build the boudle for deployment while you don't need to install or configure them.
They are preconfigured and hidden so that you can focus on the code. Just create a project, and you’re good to go.
## Getting Started
### Installation
Install it once globally:
```sh
npm install -g hero-mobile/hero-cli
```
**You’ll need to have Node >= 4 on your machine**.
**We strongly recommend to use Node >= 6 and npm >= 3 for faster installation speed and better disk usage.** You can use [nvm](https://github.com/creationix/nvm#usage) to easily switch Node versions between different projects.
### Creating an App
To create a new app, run:
```sh
hero init my-app
cd my-app
```
It will create a directory called `my-app` inside the current folder.
Inside that directory, it will generate the initial project structure and then you can run command `npm install` to install the dependencies manually:
```
├── public
│ ├── ...
│ └── favicon.ico
├── src
│ ├── ...
│ ├── environments
│ │ ├── environment-dev.js
│ │ └── environment-prod.js
│ ├── index.html
│ └── index.js
├── .babelrc
├── .editorconfig
├── .eslintrc
├── .gitattributes
├── .gitignore
├── .hero-cli.json
├── package.json
└── README.md
```
For the project to build, **these files must exist with exact filenames**:
* `src/index.html` is the entry page;
* `src/index.js` is the JavaScript entry point.
* `.hero-cli.json` is the configuration file for hero-cli build, it tell hero loads which configuration when you run command `hero start -e dev` or `hero build -e prod`(which is invoked by `npm start` or `npm build`) according to the value of `-e` parameter. For more build options please refer to [Build Scripts](#build-scripts).
You can delete or rename the other files.
* `public` assets like images inside this folder will **copied into the build folder untouched**. It will not be processed by Webpack.
* `src` For faster rebuilds, only files inside this folder are processed by Webpack. You need to **put any JS and CSS files inside this folder**, or Webpack won’t see them.
* `src/environments` where your configurations exists(these file path configured in file `.hero-cli.json`, you can change it later) and you can access the configuration values in JavaScript or HTML code. See [Adding Custom Environment Variables](#adding-custom-environment-variables).
You may curious about where is the `pages/start.html`. Yes, it's generated by hero-cli. See [Generate HTML](#generate-html)
## User Guide
* [Generate HTML](#generate-html)
* [Adding Custom Environment Variables](#adding-custom-environment-variables)
* [Referencing Environment Variables in the JavaScript](#referencing-environment-variables-in-the-javascript)
* [Referencing Environment Variables in the HTML](#referencing-environment-variables-in-the-html)
* [Adding Temporary Environment Variables In Your Shell](#adding-temporary-environment-variables-in-your-shell)
* [Adding Development Environment Variables via `.hero-cli.json`](#adding-development-environment-variables-via-hero-clijson)
* [Proxying API Requests in Development](#proxying-api-requests-in-development)
* [Build Scripts](#build-scripts)
* [`hero start`](#hero-start)
* [`hero build`](#hero-build)
### Generate HTML
Any JS file meet the following 2 conditions will treat as JavaScript entry point.
* Declaration of `class` exists in the JS file.
* `class` marked by [Decorator](https://github.com/wycats/javascript-decorators/blob/master/README.md) `@Entry` from [hero-cli/decorator](https://github.com/hero-mobile/hero-cli/blob/master/decorator.js).
Which would cause a HTML file generated using Webpack plugin [html-webpack-plugin](https://www.npmjs.com/package/html-webpack-plugin):
* Options specified in `@Entry(options)` will passed to `html-webpack-plugin` transparently.
* Destination of generated HTML file will keep the file path structure of the Javascript entry, or you can override it using the option `filename` provided by `html-webpack-plugin`.
* Generated HTML file can access the [Custom Environment Variables](#adding-custom-environment-variables).
Example:
Below JavaScript file `src/pages/start.js` will generate a HTML file access by URL `/pages/start.html`, that's why we can visit [http://localhost:3000/pages/start.html](http://localhost:3000/pages/start.html).
```javascript
// content of file: src/pages/start.js
import { Entry } from 'hero-cli/decorator';
// class marked by @Entry
// will generate HTML accessed by URL /pages/start.html
// Equal to
// @Entry({
// filename: 'pages/start.html'
// })
//
@Entry()
export class DecoratePage {
sayHello(data){
console.log('Hello Hero!')
}
}
```
### Adding Custom Environment Variables
Your project can consume variables declared in your environment as if they were declared locally in your JS files. By default you will have any environment variables starting with `HERO_APP_`. These environment variables can be useful for consuming sensitive data that lives outside of version control.
**The environment variables are embedded during the build time**.
#### Referencing Environment Variables in the JavaScript
These environment variables will be defined for you on `process.env`.
For example, having an environment variable named `HERO_APP_SECRET_CODE` will be exposed in your JS as `process.env.HERO_APP_SECRET_CODE`.
```javascript
console.log('Send Request with Token: '+ process.env.HERO_APP_SECRET_CODE);
```
There is also exist two special built-in environment variable called `NODE_ENV` and `HOME_PAGE`.
You can read it from `process.env.NODE_ENV`. When you run `hero start`, it is always equal to `'development'`, when you run `hero build` to make a production bundle, it is always equal to `'production'`. **You cannot override `NODE_ENV` manually**. This prevents developers from accidentally deploying a slow development build to production.
Having access to the `NODE_ENV` is also useful for performing actions conditionally:
```javascript
if (process.env.NODE_ENV !== 'production') {
analytics.disable();
}
```
You can read variable `process.env.HOME_PAGE`, which value is equal to attribute `homepage` in file `.hero-cli.json`. This is useful for [Building for Relative Paths](#building-for-relative-paths).
#### Referencing Environment Variables in the HTML
For example, let’s define a variable `HERO_APP_WEBSITE_NAME` with value `Welcome Hero`, and you can access it like this:
```html