기본 콘텐츠로 건너뛰기

angular2 + webpack 개발

angular2 + webpack 개발

Angular2 + webpack

기본 npm package 준비

{ " name ": "angular2-test" , " version ": "1.0.0" , " description ": "" , " main ": "index.js" , " scripts ": { " test ": "echo \"Error: no test specified\" && exit 1" } , " author ": "" , " license ": "ISC" , " dependencies ": { " angular2 ": "^2.0.0-beta.15" , " bluebird ": "^3.3.5" , " core-js ": "^2.2.2" , " lodash ": "^4.11.1" , " reflect-metadata ": "^0.1.3" , " rxjs ": "^5.0.0-beta.6" , " zone.js ": "^0.6.11" } , " devDependencies ": { " awesome-typescript-loader ": "^0.17.0-rc.6" , " copy-webpack-plugin ": "^2.1.1" , " html-webpack-plugin ": "^2.15.0" } }

기본적으로 reflect-metadata , rxjs , zone.js 는 angular2 와 같이 설치되어야지 된다.

npm install rxjs --save npm install reflect-metadata --save npm install zone.js --save

typescript 설정

npm install typings --global

tsd 는 deprecated되었기 때문에 더이상 사용되지 않는다.

tsconfig.json

{ "compilerOptions" : { "target" : "es5" , "module" : "commonjs" , "emitDecoratorMetadata" : true , "experimentalDecorators" : true , "sourceMap" : true }, "exclude" : [ "node_modules" , "typings/main" , "typings/main.d.ts" , "bower_components" ] }

webpack 설정

기본적으로 다음 folder 구조를 따른다. (maven 과 유사)

├── dist ├── src │ ├── app │ └── public ├── package.json ├── tsconfig.json ├── typings.json └── webpack.config.js

src/app : javascript application

: javascript application src/public : html,css,image와 같이 static resource 구성

typescript 를 지원하기 위해서 awesome-typescript-loader 을 설치하고 loader에 다음과 같이 등록한다.

{ test: /\.ts$/ , loader: 'awesome-typescript-loader' , exclude: /node_modules/ }

webpack.config.js

'use strict' ; var webpack = require ( 'webpack' ); var HtmlWebpackPlugin = require ( 'html-webpack-plugin' ); var CopyWebpackPlugin = require ( 'copy-webpack-plugin' ); function buildConfig () { var isProd = false ; const config = { output: { path: __dirname + '/dist' , publicPath: isProd ? '/' : 'http://localhost:8080/' , filename: isProd ? '[name].[hash].js' : '[name].bundle.js' , chunkFilename: isProd ? '[name].[hash].js' : '[name].bundle.js' }, resolve: { extensions: [ '' , '.webpack.js' , '.web.js' , '.ts' , '.js' ], modulesDirectories: [ 'node_modules' ] }, devtool: 'cheap-source-map' , module : { loaders: [ { test: /\.ts$/ , loader: 'awesome-typescript-loader' , exclude: /node_modules/ } ] }, entry: { app: __dirname + '/src/app/app.ts' }, plugins: [ new webpack.NoErrorsPlugin(), new webpack.optimize.DedupePlugin(), new CopyWebpackPlugin([{ from: __dirname + '/src/public' }]), new HtmlWebpackPlugin({ template: './src/public/index.html' , inject: 'body' }) ] }; config.devServer = { contentBase: './dist' , stats: 'minimal' , outputPath: './dist' }; return config; } module .exports = buildConfig();

webpack에서 지정되는 entry point 가 된다. main.ts , bootstrap.ts 등 다양한 파일이름이 있지만, 개인적으로는 app.ts 가 가장 좋은것 같다.

app.ts 에서는 다음 세가지 기능을 한다.

router 의 등록 bootstrap 실행 첫페이지로 direction

/// import 'core-js/es6'; import 'core-js/es7/reflect'; import 'zone.js/dist/zone'; import { bootstrap } from 'angular2/platform/browser'; import { HTTP_PROVIDERS } from 'angular2/http'; import { enableProdMode } from 'angular2/core'; import { Component } from 'angular2/core'; import { provide } from 'angular2/core'; import { Router, RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, LocationStrategy, Location, HashLocationStrategy } from 'angular2/router'; import { HeroService } from './hero.service'; import { DashboardComponent } from './dashboard.component'; import { HeroesComponent, HeroesRouterInfo } from './heroes.component'; import { HeroDetailComponent } from './hero-detail.component'; @RouteConfig([ { path: '/heroes', name: 'Heroes', component: HeroesComponent, }, HeroesRouterInfo, { path: '/dashboard', name: 'Dashboard', component: DashboardComponent, useAsDefault: true }, { path: '/detail/:id', name: 'HeroDetail', component: HeroDetailComponent }, ]) @Component({ selector: 'my-app', template: ` {{title}} Dashboard Heroes `, styleUrls: ['app.component.css'], directives: [ROUTER_DIRECTIVES] }) class AppComponent { title: string; router: Router; location: Location; constructor(router: Router, location: Location) { this.title = 'This is Title'; this.router = router; this.location = location; } } // enableProdMode(); bootstrap(AppComponent, [ HTTP_PROVIDERS, ROUTER_PROVIDERS, provide(LocationStrategy, { useClass: HashLocationStrategy }), HeroService ]).catch(err => console.error(err));

위 app.ts 의 경우에는 router가 추가 되고, 여러 서비스 Provider들이 추가되는 것 이외에는 큰 차이가 없을것같다. 기본적으로 기존 angular 의 app.js 와 동일한 기능을 갖게 된다.

lodash 추가

typings install lodash --ambient --save

from http://netframework.tistory.com/449 by ccl(S)

댓글

이 블로그의 인기 게시물

[Debugging] AngularJS2 - Can't bind to 'ngModel' since it isn't a...

[Debugging] AngularJS2 - Can't bind to 'ngModel' since it isn't a... - 좋아요 하이 .. !! Angular2 로 개발을 하다가 아래와 같은 에러를 만났다. 흠 .. 이게 뭘까 열심히 구글링을 해봤다. 간단한 내용이다. 모듈을 추가해주기만 하면 된다. app.module.ts를 열어보자. 여기에다가 FormsModule 과 ReactiveFormsModule을 추가해주면 문제가 해결된다 ! 다들 즐거운 코딩하자. from http://devkingdom.tistory.com/106 by ccl(A) rewrite - 2020-03-18 00:54:15

[aws] deploy Angular app with s3 | AWS S3로 angular 앱 배포하기

[aws] deploy Angular app with s3 | AWS S3로 angular 앱 배포하기 angular project를 빌드한다 ng build --prod 그러면 dist 폴더가 생긴다. dist 폴더 안에 있는 아이들을 사용한다. 아마존 s3 콘솔로 이동 https://s3.console.aws.amazon.com/s3/home?region=ap-northeast-2 새로운 Bucket 을 생성한다(Create bucket). 버킷 이름은 고유하게 짓는다. 버킷 생성후 properties tab > static website hosting을 클릭한다. index document는 index.html은 쓴다. properties > static website hosting Permission tab 에서 권한을 수정한다. overview tab 에서 필요한 파일 업로드 dist 폴더 안에 있는 파일들을 업로드 한다. bucket policy 설정 properties > static website hosting > endpoint 클릭하면 서버에 올라간 앱을 확인 할 수 있다 일단 angular 앱을 올리긴 했는데.. 이걸로는 아무것도 할 수 었다. django로 만든 서버를 올리고 database를 연결하고 그것을 지금 이 angular 앱과 연결해야한다. 아직 어떻게 해야 할지는 모르겠음 계속 삽질 중. 그래도 angular app 하나 올라갔는데 재밌네 from http://paigeblog.tistory.com/18 by ccl(A) rewrite - 2020-03-25 16:20:22

Angular2 시작하기

Angular2 시작하기 Angular2 시작하기 1. NodeJS 설치 - https://nodejs.org/ko/ NodeJS 공식 홈페이지 접속하여 Node를 다운 후 설치. 2. NPM 을 통한 Angular-Cli 설치 Window Command 를 통하여 npm install -g @angular/cli 명령어를 실행한다. 위와 같이 정상적으로 angular-cli 가 설치되었다면 Project 가 위치할 폴더를 생성. 참고 사이트 : https://cli.angular.io/ 3. Angular-cli 를 통한 Angular Project 생성. Window Command 를 통한 ng new [폴더명] 위와 같이 Angular 기본 프로젝트가 생성됨. 해당 프로젝트로 폴더로 이동하여 ng serve 명령어 실행 Node 를 통해 Angular 프로젝트 실행. http://localhost:4200 접속하게되면 위와같이 Angular 프로젝트 실행된다. 앞으로 Angular2 의 개념들을 포스팅하면서 Spring-Boot , Spring Project의 Angular-cli 를 이용하여 ng build 하여 포팅하는 글을 올리겠습니다. from http://overclassbysin.tistory.com/3 by ccl(A) rewrite - 2020-03-07 07:55:13