Angular 2 + webpackで書きたい

f:id:ryskit:20160807220737j:plainAngular2でwebpackを使いたいのに、Tutorialはsystemjsを使っていて、Githubのangular2-webpack-starterのリポジトリはよく分からない状態だったので、とりあえず自分でなんとか動かせるように設定ファイルを書いてみた。

Angular2もwebpack も初めてなので、書き方が正しいのか分からないですが、設定ファイルを晒します。

package.json

{
  "name": "angular2-webpack",
  "version": "1.0.0",
  "description": "trying angular2 + webpack",
  "scripts": {
    "tsc": "tsc -p . ",
    "start": "webpack-dev-server",
    "webpack": "webpack",
    "build": "npm run tsc && npm run webpack",
    "postinstall": "typings install"
  },
  "dependencies": {
    "@angular/common": "2.0.0-rc.4",
    "@angular/compiler": "2.0.0-rc.4",
    "@angular/core": "2.0.0-rc.4",
    "@angular/forms": "0.2.0",
    "@angular/http": "2.0.0-rc.4",
    "@angular/platform-browser": "2.0.0-rc.4",
    "@angular/platform-browser-dynamic": "2.0.0-rc.4",
    "@angular/router": "3.0.0-beta.1",
    "core-js": "^2.4.0",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.6",
    "zone.js": "0.6.12"
  },
  "devDependencies": {
    "angular2-template-loader": "^0.4.0",
    "css-loader": "^0.23.1",
    "extract-text-webpack-plugin": "^1.0.1",
    "file-loader": "^0.8.5",
    "html-loader": "^0.4.3",
    "html-webpack-plugin": "^2.15.0",
    "jasmine-core": "^2.4.1",
    "null-loader": "^0.1.1",
    "phantomjs-prebuilt": "^2.1.7",
    "raw-loader": "^0.5.1",
    "rimraf": "^2.5.2",
    "style-loader": "^0.13.1",
    "ts-loader": "^0.8.1",
    "typescript": "^1.8.10",
    "typings": "^1.0.4",
    "webpack": "^1.13.0",
    "webpack-dev-server": "^1.14.1",
    "webpack-merge": "^0.14.0"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
  },
  "filesGlob": [
    "./**/*.ts",
    "!./**/*.d.ts",
    "!./node_modules/**/*",
    "./node_modules/typescript/lib/lib.es6.d.ts"
  ],
  "files": [
    "./app/main.ts",
    "./node_modules/typescript/lib/lib.es6.d.ts"
  ]
}

webpack.config.js

var webpack = require("webpack");
var path    = require("path");

module.exports = {
    entry: {
        bundle: ["./app/main.js"]
    },

    output: {
        path: path.resolve(__dirname, "build"),
        publicPath: "/assets/",
        filename: "[name].js"
    },

    resolve: {
        root: __dirname,
        extensions: ['', '.webpack.js', '.web.js', '.js', '.ts']
    },


    module: {
        loaders: [
            {
                test: /\.ts$/,
                loaders: ['ts', 'angular2-template-loader']
            },
            {
                test: /\.html$/,
                loader: 'html'
            }
        ]
    }
};

この設定ファイル書いて、

npm install

実行して、チュートリアルでも始めれば良いと思います。

いろいろ理解が深まったら、もう少し詳しい記事書きますね。 あぁ難しい。。。。