UNPKG

17 kBMarkdownView Raw
1[![npm version](https://img.shields.io/npm/v/@ionic/app-scripts.svg)](https://www.npmjs.com/package/@ionic/app-scripts)
2[![Circle CI](https://circleci.com/gh/ionic-team/ionic-app-scripts.svg?style=shield&circle-token=:circle-token)](https://circleci.com/gh/ionic-team/ionic-app-scripts)
3
4# Ionic App Scripts
5
6Helper scripts to get [Ionic apps](https://ionicframework.com/) up and running quickly (minus the config overload).
7
8To get the latest `@ionic/app-scripts`, please run:
9
10```
11npm install @ionic/app-scripts@latest --save-dev
12```
13
14### Config Defaults
15
16Out of the box, Ionic starters have been preconfigured with great defaults for building fast apps, including:
17
18- Multi-core processing tasks in parallel for faster builds
19- In-memory file transpiling and bundling
20- Transpiling source code to ES5 JavaScript
21- Ahead of Time (AoT) template compiling
22- Just in Time (JiT) template compiling
23- Template inlining for JiT builds
24- Bundling modules for faster runtime execution
25- Treeshaking unused components and dead-code removal
26- Generating CSS from bundled component Sass files
27- Autoprefixing vendor CSS prefixes
28- Minifying JavaScript files
29- Compressing CSS files
30- Copying `src` static assets to `www`
31- Linting source files
32- Watching source files for live-reloading
33
34Just the bullet list above is a little overwhelming, and each task requires quite a bit of development time just to get started. Ionic App Script's intention is to make it easier to complete common tasks so developers can focus on building their app, rather than building build scripts.
35
36Note that the [Ionic Framework's](https://github.com/ionic-team/ionic) source is made up of modules and can be packaged by any bundler or build process. However, this project's goal is provide simple scripts to make building Ionic apps easier, while also allowing developers to further configure their build process.
37
38
39### npm Scripts
40
41Instead of depending on external task runners, Ionic App Scripts now prefers being executed from [npm scripts](https://docs.npmjs.com/misc/scripts). Ionic's npm scripts come preconfigured in the project's `package.json` file. For example, this is the default setup for npm scripts in each starter:
42
43```
44 "scripts": {
45 "ionic:build": "ionic-app-scripts build",
46 "ionic:serve": "ionic-app-scripts serve"
47 },
48```
49
50To run the `build` script found in the `package.json` `scripts` property, execute:
51
52```
53npm run build
54```
55
56## Environments
57
58You can use Node style `process.env.MY_VAR` syntax directly in your typescript
59and when the application is bundled it'll be replaced with the following order of precedence:
60* If the variable exists in the process environment it will be replaced with that value.
61* If the variable is not defined in the process environment it will be read from a `.env.dev`
62file for dev builds or `.env.prod` file for prod builds which are located in the root of the app
63* If the variable is not defined in either place it will be `undefined`
64
65In order to take advantage of this apps will need a `src/declarations.d.ts` file with the following declaration:
66```typescript
67declare var process: { env: { [key: string]: string | undefined; } };
68```
69
70*Note*: This declaration may conflict if `@types/node` is installed in your project. See [#3541](https://github.com/ionic-team/ionic-cli/issues/3541).
71
72## Custom Configuration
73
74In many cases, the defaults which Ionic provides cover most of the scenarios required by developers; however, Ionic App Scripts does provide multiple ways to configure and override the defaults for each of the various tasks. Note that Ionic will always apply its defaults for any property that was not provided by custom configuration.
75
76[Default Config Files](https://github.com/ionic-team/ionic-app-scripts/tree/master/config)
77
78### package.json Config
79
80Ionic projects use the `package.json` file for configuration. There's a handy [config](https://docs.npmjs.com/misc/config#per-package-config-settings) property which can be used. Below is an example of setting a custom config file using the `config` property in a project's `package.json`.
81
82```
83 "config": {
84 "ionic_cleancss": "./config/cleancss.config.js"
85 },
86```
87
88### Command-line Flags
89
90Remember how we're actually running `ionic-app-scripts` from the `scripts` property of a project's `package.json` file? Well we can also add command-line flags to each script, or make new scripts with these custom flags. For example:
91
92```
93 "scripts": {
94 "build": "ionic-app-scripts build --webpack ./config/webpack.dev.config.js",
95 "minify": "ionic-app-scripts minify --cleancss ./config/cleancss.config.js",
96 },
97```
98
99The same command-line flags can be also applied to `npm run` commands too, such as:
100
101```
102npm run build --webpack ./config/webpack.dev.config.js
103```
104
105
106### Overriding Config Files
107
108| Config File | package.json Config | Cmd-line Flag |
109|-------------|---------------------|-----------------------|
110| CleanCss | `ionic_cleancss` | `--cleancss` or `-e` |
111| Copy | `ionic_copy` | `--copy` or `-y` |
112| Generator | `ionic_generator` | `--generator` or `-g` |
113| NGC | `ionic_ngc` | `--ngc` or `-n` |
114| Sass | `ionic_sass` | `--sass` or `-s` |
115| TSLint | `ionic_tslint` | `--tslint` or `-i` |
116| UglifyJS | `ionic_uglifyjs` | `--uglifyjs` or `-u` |
117| Watch | `ionic_watch` | `--watch` |
118| Webpack | `ionic_webpack` | `--webpack` or `-w` |
119
120
121### Overriding Config Values
122
123| Config Values | package.json Config | Cmd-line Flag | Defaults | Details |
124|-----------------|---------------------|---------------|-----------------|----------------|
125| root directory | `ionic_root_dir` | `--rootDir` | `process.cwd()` | The directory path of the Ionic app |
126| src directory | `ionic_src_dir` | `--srcDir` | `src` | The directory holding the Ionic src code |
127| www directory | `ionic_www_dir` | `--wwwDir` | `www` | The deployable directory containing everything needed to run the app |
128| build directory | `ionic_build_dir` | `--buildDir` | `build` | The build process uses this directory to store generated files, etc |
129| temp directory | `ionic_tmp_dir` | `--tmpDir` | `.tmp` | Temporary directory for writing files for debugging and various build tasks |
130| ionic-angular directory | `ionic_angular_dir` | `--ionicAngularDir` | `ionic-angular` | ionic-angular directory |
131| ionic-angular entry point | `ionic_angular_entry_point` | `--ionicAngularEntryPoint` | `index.js` | entry point file for ionic-angular |
132| source map type | `ionic_source_map_type` | `--sourceMapType` | `source-map` | Chooses the webpack `devtool` option. `eval` and `source-map` are supported |
133| generate source map | `ionic_generate_source_map` | `--generateSourceMap` | `true` | Determines whether to generate a source map or not |
134| tsconfig path | `ionic_ts_config` | `--tsconfig` | `{{rootDir}}/tsconfig.json` | absolute path to tsconfig.json |
135| app entry point | `ionic_app_entry_point` | `--appEntryPoint` | `{{srcDir}}/app/main.ts` | absolute path to app's entrypoint bootstrap file |
136| app ng module path | `ionic_app_ng_module_path` | `--appNgModulePath` | `{{srcDir}}/app/app.module.ts` | absolute path to app's primary `NgModule` |
137| app ng module class | `ionic_app_ng_module_class` | `--appNgModuleClass` | `AppModule` | Exported class name for app's primary `NgModule` |
138| clean before copy | `ionic_clean_before_copy` | `--cleanBeforeCopy` | `false` | clean out existing files before copy task runs |
139| output js file | `ionic_output_js_file_name` | `--outputJsFileName` | `main.js` | name of js file generated in `buildDir` |
140| output css file | `ionic_output_css_file_name` | `--outputCssFileName` | `main.css` | name of css file generated in `buildDir` |
141| bail on lint error | `ionic_bail_on_lint_error` | `--bailOnLintError` | `null` | Set to `true` to make stand-alone lint commands fail with non-zero status code |
142| enable type checking during lint | `ionic_type_check_on_lint` | `--typeCheckOnLint` | `null` | Set to `true` to enable [type checking](https://palantir.github.io/tslint/usage/type-checking) during lint |
143| write AoT files to disk | `ionic_aot_write_to_disk` | `--aotWriteToDisk` | `null` | Set to `true` to write files to disk for debugging |
144| print webpack dependency tree | `ionic_print_webpack_dependency_tree` | `--printWebpackDependencyTree` | `null` | Set to `true` to print out a dependency tree after running Webpack |
145| parse deeplink config | `ionic_parse_deeplinks` | `--parseDeepLinks` | `true` | Parses and extracts data from the `@IonicPage` decorator |
146| convert bundle to ES5 | `ionic_build_to_es5` | `--buildToEs5` | `true` | Convert bundle to ES5 for production deployments |
147| default watch timeout | `ionic_start_watch_timeout` | `--startWatchTimeout` | `3000` | Milliseconds controlling the default watch timeout |
148| choose the polyfill | `ionic_polyfill_name` | `--polyfillName` | `polyfills` | Change with polyfills.modern or polyfills.ng (all options)[https://github.com/driftyco/ionic/tree/master/scripts/polyfill] |
149| enable linting | `ionic_enable_lint` | `--enableLint` | `true` | Set to `false` for skipping the linting after the build |
150
151
152
153
154
155### Ionic Environment Variables
156
157These environment variables are automatically set to [Node's `process.env`](https://nodejs.org/api/process.html#process_process_env) property. These variables can be useful from within custom configuration files, such as custom `webpack.config.js` file.
158
159| Environment Variable | Description |
160|----------------------------|----------------------------------------------------------------------|
161| `IONIC_ENV` | Value can be either `prod` or `dev`. |
162| `IONIC_ROOT_DIR` | The absolute path to the project's root directory. |
163| `IONIC_SRC_DIR` | The absolute path to the app's source directory. |
164| `IONIC_WWW_DIR` | The absolute path to the app's public distribution directory. |
165| `IONIC_BUILD_DIR` | The absolute path to the app's bundled js and css files. |
166| `IONIC_TMP_DIR` | Temp directory for debugging generated/optimized code and various build tasks |
167| `IONIC_NODE_MODULES_DIR` | The absolute path to the `node_modules` directory. |
168| `IONIC_ANGULAR_DIR` | The absolute path to the `ionic-angular` node_module directory. |
169| `IONIC_APP_SCRIPTS_DIR` | The absolute path to the `@ionic/app-scripts` node_module directory. |
170| `IONIC_SOURCE_MAP_TYPE` | The Webpack `devtool` setting. `eval` and `source-map` are supported.|
171| `IONIC_GENERATE_SOURCE_MAP`| Determines whether to generate a sourcemap or not. |
172| `IONIC_TS_CONFIG` | The absolute path to the project's `tsconfig.json` file |
173| `IONIC_APP_ENTRY_POINT` | The absolute path to the project's `main.ts` entry point file |
174| `IONIC_APP_NG_MODULE_PATH` | The absolute path to app's primary `NgModule` |
175| `IONIC_APP_NG_MODULE_CLASS` | The exported class name for app's primary `NgModule` |
176| `IONIC_GLOB_UTIL` | The path to Ionic's `glob-util` script. Used within configs. |
177| `IONIC_CLEAN_BEFORE_COPY` | Attempt to clean existing directories before copying files. |
178| `IONIC_CLOSURE_JAR` | The absolute path ot the closure compiler jar file |
179| `IONIC_OUTPUT_JS_FILE_NAME` | The file name of the generated javascript file |
180| `IONIC_OUTPUT_CSS_FILE_NAME` | The file name of the generated css file |
181| `IONIC_WEBPACK_FACTORY` | The absolute path to Ionic's `webpack-factory` script |
182| `IONIC_WEBPACK_LOADER` | The absolute path to Ionic's custom webpack loader |
183| `IONIC_BAIL_ON_LINT_ERROR` | Boolean determining whether to exit with a non-zero status code on error |
184| `IONIC_TYPE_CHECK_ON_LINT` | Boolean determining whether to type check code during lint or not |
185| `IONIC_AOT_WRITE_TO_DISK` | `--aotWriteToDisk` | `null` | Set to `true` to write files to disk for debugging |
186| `IONIC_PRINT_WEBPACK_DEPENDENCY_TREE` | boolean to print out a dependency tree after running Webpack |
187| `IONIC_PARSE_DEEPLINKS` | boolean to enable parsing the Ionic 3.x deep links API for lazy loading |
188| `IONIC_BUILD_TO_ES5` | boolean to enable converting bundle to ES5 for production deployments |
189| `IONIC_START_WATCH_TIMEOUT` | Milliseconds controlling the default watch timeout |
190
191
192The `process.env.IONIC_ENV` environment variable can be used to test whether it is a `prod` or `dev` build, which automatically gets set by any command. By default the `build` and `serve` tasks produce `dev` builds (a build that does not include Ahead of Time (AoT) compilation or minification). To force a `prod` build you should use the `--prod` command line flag.
193
194`process.env.IONIC_ENV` environment variable is set to `prod` for `--prod` builds, otherwise `dev` for all other builds.
195
196
197## All Available Tasks
198
199These tasks are available within `ionic-app-scripts` and can be added to npm scripts or any Node command.
200
201| Task | Description |
202|------------|-----------------------------------------------------------------------------------------------------|
203| `build` | A complete build of the application. It uses `development` settings by default. Use `--prod` to create an optimized build |
204| `clean` | Empty the `www/build` directory. |
205| `cleancss` | Compress the output CSS with [CleanCss](https://github.com/jakubpawlowicz/clean-css) |
206| `copy` | Run the copy tasks, which by defaults copies the `src/assets/` and `src/index.html` files to `www`. |
207| `lint` | Run the linter against the source `.ts` files, using the `tslint.json` config file at the root. |
208| `minify` | Minifies the output JS bundle and compresses the compiled CSS. |
209| `sass` | Sass compilation of used modules. Bundling must have at least ran once before Sass compilation. |
210| `watch` | Runs watch for dev builds. |
211
212Example NPM Script:
213
214```
215 "scripts": {
216 "minify": "ionic-app-scripts minify"
217 },
218```
219
220## Tips
2211. The Webpack `devtool` setting is driven by the `ionic_source_map_type` variable. It defaults to `source-map` for the best quality source map. Developers can enable significantly faster builds by setting `ionic_source_map_type` to `eval`.
2222. By default, the `lint` command does not exit with a non-zero status code on error. To enable this, pass `--bailOnLintError true` to the command.
223
224```
225"scripts" : {
226 ...
227 "lint": "ionic-app-scripts lint"
228 ...
229}
230```
231
232```
233npm run lint --bailOnLintError true
234```
235
236## The Stack
237
238- [Ionic Framework](https://ionicframework.com/)
239- [TypeScript Compiler](https://www.typescriptlang.org/)
240- [Angular Compiler (NGC)](https://github.com/angular/angular/tree/master/modules/%40angular/compiler-cli)
241- [Webpack Module Bundler](https://webpack.js.org/)
242- Ionic Component Sass
243- [Node Sass](https://www.npmjs.com/package/node-sass)
244- [Autoprefixer](https://github.com/postcss/autoprefixer)
245- [UglifyJS](https://lisperator.net/uglifyjs/)
246- [CleanCss](https://github.com/jakubpawlowicz/clean-css)
247- [TSLint](https://palantir.github.io/tslint/)
248
249## Contributing
250
251We welcome any PRs, issues, and feedback! Please be respectful and follow the [Code of Conduct](https://github.com/ionic-team/ionic/blob/master/CODE_OF_CONDUCT.md).
252
253We use Node 6, and NPM 5 for contributing.
254
255### Publish a Nightly Build
256
2571. Run `npm run build` to generate the `dist` directory
2582. Run `npm run test` to validate the `dist` works
2593. Tick the `package.json` version
2604. Run `npm run nightly` to generate a nightly build on npm
261
262
263
264### Publish a release
265
266Execute the following steps to publish a release:
267
2681. Ensure your branch has been merged into `master`
2692. Run `npm run build` to generate the `dist` directory
2703. Run `npm run test` to validate the `dist` works
2714. Temporarily tick the `package.json` version
2725. Run `npm run changelog` to append the latest additions to the changelog
2736. Manually verify and commit the changelog changes. Often times you'll want to manually add content/instructions
2747. Revert the `package.json` version to the original version
2758. Run `npm version patch` to tick the version and generate a git tag
2769. Run `npm run github-release` to create the github release entry
27710. Run `npm publish` to publish the package to npm
27811. `git push origin master` - push changes to master
\No newline at end of file