Angular2.0於2016年9月上線,我於9月入坑。
入坑以來,一直讓我很困惑的問題
1、angular-cli是個什么鬼東西?
2、為什么我們自己的資源文件還沒寫什么,就有起碼50多個js文件加載出來,頁面啟動速度蝸牛級別
3、ng build打包出來的文件,未壓縮
4、ng build --prod打包出來的文件,未壓縮,而且缺少js文件
5、部署后,如何替換客戶端緩存文件,也就是文件版本如何更換
6、文件配置文件在哪里?
諸如此類問題,需要以下條件:
angular-cli :Beta.14版本 (ng --version)
node :Beta.4 or higher
npm: Beta.3 or higher
具體方法參見angular-cli在github上的原文:https://github.com/angular/angular-cli/wiki/Upgrading-from-Beta.10-to-Beta.14;
我這里只介紹根據原文我所做的具體操作:
1、卸載掉舊版cli,下載最新版
npm uninstall -g angular-cli npm cache clean npm install -g angular-cli@latest
2、使用新版cli,new一個新項目出來
ng new migration-project
cd migration-project
3、將新項目中的src目錄備份
mv src/ src.webpacktemplate/
4、將原項目中的src,移入新項目中
mv ${OLD_PATH}/src src
5、將原項目中的e2e目錄,移入新項目中
mv ${OLD_PATH}/e2e e2e
6、刪掉新項目中不需要的文件
rm src/system-config.ts rm src/typings.d.ts
7、將備份的src中這些文件拷貝出來
cp src.webpacktemplate/polyfills.ts src/ cp src.webpacktemplate/styles.css src/ cp src.webpacktemplate/test.ts src/ cp src.webpacktemplate/tsconfig.json src/ cp src.webpacktemplate/typings.d.ts src/
8、將備份中的src.webpacktemplate/main.ts文件,拷貝到新項目中,注意和你原項目中的main.ts做對比,保留你的代碼
9、如上操作src.webpacktemplate/index.html文件
10、將舊目錄的環境配置文件拷入,注意文件名
mv ${OLD_PATH}/config/environment.dev.ts src/environments/environment.ts
mv ${OLD_PATH}/config/environment.prod.ts src/environments/environment.prod.ts
此處原文標注:
If you have any custom environments don't forget to move them too.
Environments are now listed in the angular-cli.json. Make sure those files matches the files on your disk. More importantly, because they're part of your application, their paths are relative to your main.ts
.
11、npm install --save ${LIBRARY}
12、刪除掉所有的 moduleId: module.id,在webpack中,module.id 是number,但是在angular中期望是一個string
13、把你所有的靜態資源文件,圖片放入新項目中的src/assets目錄,別忘了改代碼里的引用路徑
cp -R ${OLD_PATH}/public/ src/assets/
14、
Finally, if you're using SASS or LESS, you need to rename your styleUrls
in all your files. Webpack understands LESS and SASS so you can use styleUrls: ['my-component.scss']
in your component and it will be transcompiled automatically.
15、可以入坑了,ng serve運行一下,改掉所有出錯的地方,如果沒有出錯,就可以刪掉src.webpacktemplate/目錄了
16、刪掉新項目的.git配置,把原項目中的.git文件復制過來
rm -rf .git/
cp -R ${OLD_PATH}/.git .
17、ng serve起項目,ng build --prod打包也成功了!
打包出來的東西少很多了!!

研究得出,它的配置文件是這個: