|
| 1 | +# Moving your project to Angular CLI |
| 2 | + |
| 3 | +The easiest way to move an existing project to Angular CLI is to copy your |
| 4 | +application files into a new, empty CLI project. |
| 5 | + |
| 6 | +Let's see how to do it step by step. |
| 7 | +Here we use a project made with the [official QuickStart](https://github.com/angular/quickstart) |
| 8 | +as an example, but you should be able to adjust these instructions to other setups. |
| 9 | + |
| 10 | +Note for Windows users: we show unix commands here like `cp -r` to copy and `rm -rf` to delete files. |
| 11 | +Windows does not have these commands so use Explorer instead. |
| 12 | + |
| 13 | +Start with preparing your existing project folder. We'll refer to it as `awesome-app`. |
| 14 | +- commit and push your existing changes. |
| 15 | +- clean your folder from temporary files and ignored files using `git clean -fdx`. |
| 16 | +- rename your project folder to `old-awesome-app`. |
| 17 | + |
| 18 | +Now make a new project on the same parent folder as `old-awesome-app` using Angular CLI. |
| 19 | +- Verify you have the [Angular CLI prerequisites](https://github.com/angular/angular-cli#prerequisites). |
| 20 | +- Install the CLI globally: `npm install -g @angular/cli`. |
| 21 | +- Make a new app: `ng new awesome-app`. |
| 22 | +- Move into the folder: `cd awesome-app`. |
| 23 | +- Test your app works: `ng serve --open`. |
| 24 | + |
| 25 | +Copy over your app files. |
| 26 | +- Remove the existing app: `rm -rf src/app src/styles.css src/index.html e2e`. |
| 27 | +- Copy `src/app/`, `src/index.html`, `src/styles.css` and `e2e/` from your old app. |
| 28 | +If you don't have a `src/` folder then these files and folders should be |
| 29 | +at the root of the old project instead. |
| 30 | +``` |
| 31 | +cp -r ../old-awesome-app/src/app ./src/app |
| 32 | +cp ../old-awesome-app/src/index.html ./src/index.html |
| 33 | +cp ../old-awesome-app/src/styles.css ./src/styles.css |
| 34 | +cp -r ../old-awesome-app/e2e ./e2e/ |
| 35 | +``` |
| 36 | +- Don't copy `../old-awesome-app/src/main.ts`. Instead compare it to the new `./src/main.ts` |
| 37 | +and manually copy any extra code the old one has. |
| 38 | +- Compare `../old-awesome-app/package.json` to the new `./package.json` and add in your |
| 39 | +third party libraries and `@types/*` packages, project descriptions and any other fields. |
| 40 | +- Run `npm install` to install any packages you added. |
| 41 | +- Copy over any other files your app needs like images into `src/assets`. |
| 42 | +Adjust paths on your app to use this folder e.g. `<img src='assets/my-image.jpg>`. |
| 43 | + |
| 44 | +There are a few adjustments you need to do to use the CLI build system. |
| 45 | +- Change any absolute paths you have for `templateUrl`, `styleUrls` or lazy loaded NgModules to |
| 46 | +relative paths instead. |
| 47 | +- Polyfills are listed in `./src/polyfills.ts` so remove `core-js` and `zone.js` from `index.html`. |
| 48 | +- SystemJS is not needed anymore, so remove it from `index.html` as well. |
| 49 | +- Instead of using `<script>` and `<link>` tags directly in `index.html`, use |
| 50 | +`angular-cli.json` instead. |
| 51 | + - Look for the `styles` array in `angular-cli.json` and add in any CSS files you have in |
| 52 | +`src/index.html`. Use a relative path from `./src/`. |
| 53 | + - Do the same for any remaining script tags as well, using the `scripts` array instead. |
| 54 | + |
| 55 | +The final step is to copy your git history so you can continue working without losing anything: |
| 56 | +- Copy over the git folder: `cp -r ../old-awesome-app/.git .git` |
| 57 | +- Commit and push your changes as normal. |
| 58 | + |
| 59 | +You can now delete `../old-awesome-app`, and you're done! |
| 60 | + |
| 61 | +The CLI runs static analysis on your code to ensure it's AOT ready, so you might run into a few |
| 62 | +new compilation errors that weren't there before. |
| 63 | +Check out this [handy list of AOT Do's and Dont's](https://github.com/rangle/angular-2-aot-sandbox#aot-dos-and-donts) |
| 64 | +if you get any unfamiliar errors. |
0 commit comments