2015-10-23 4 views
11

私はWebpackを学んでいます。私はAngularでAppを作成し、templateCacheを使用して、アプリケーションに必要なものよりも1つのjsファイルですべてのHTMLビューを生成します。それはクールに動作します。しかし、ウェブパックの仕事:Angpack + templateCacheでWebpackを使用するには?

entry: { 
    app: ["bootstrap-webpack!./bootstrap.config.js", './app/app.js'], 
    vendor: ['angular', 'bootstrap', 'angular-ui-router', 'oclazyload'] 
}, 
output: { 
    path: path.join(__dirname, "dist"), 
    filename: '/bundle.js' 
}, 
plugins: [ 
    new webpack.optimize.CommonsChunkPlugin(
     /* chunkName= */ "vendor", /* filename= */ "/vendor.bundle.js"), 

それは私のwebpack設定の一部でした。その結果、私はディレクトリ "dist"を "bundle.js" & & "vendor.bundle.js"とindex.htmlに取得します。その後、私はサーバーを起動し、私のアプリケーションはビューを取得できないと言っています。どうして?私はすべてのビューをバンドルしなければならないことを理解しているので、 "dist"ディレクトリにあるはずです。

答えて

17

私はtemplateCacheを一切使用していません。 html-loaderとテンプレート。

function MyDirective() { 
    return { 
     restrict: "E", 
     replace: true, 
     template: require("./MyDirective.html") 
    }; 
} 

// in your webpack.config.js 
module.exports = { 
    module: { 
     loaders: [ 
      { test: /\.html$/, loaders: ["html"] } 
     ] 
    } 
} 
+0

感謝を働かせた方法です。チームメンバーは今日、さまざまなテンプレートキャッシュローダーを使ってこのアプローチを私に指摘しました。このようなテンプレートを単純にインライン化することができますが、なぜ多くのテンプレートローダーが存在するのか知っていますか? – Jay

+2

彼らはおそらく^^を知らない。 AngularJS 1.0には不必要に複雑なものがたくさんあります。たとえば、依存関係注入のメリットがない場合は、モジュールシステム全体が厄介です –

+1

テンプレートローダーが存在するため、実行時にコンパイルするのではなく、テンプレートをプリコンパイルして実行可能にすることができますこのアプローチが必要です。 –

2

その後半が、同様にこれを共有することがあります。あなたは本当に

ために多分HTMLフラグメントを使用したい場合ここでは3210
<div ng-include="'file.tplc.html'"></div> 

は、私はそれが

var appMod = angular.module('app'); 

appMod.run(function($templateCache) { 

    function requireAll(requireContext) { 
     return requireContext.keys().map(function(val){ 
      return { 
       // tpl will hold the value of your html string because thanks to wepbpack "raw-loader" **important** 
       tpl:requireContext(val), 

       //name is just the filename 
       name : val.split('/').pop() 
      } 
     }); 
    } 

    // here "../" is my app folder root 
    // tplc is the naming convention of the templates 
    let modules = requireAll(require.context("../", true, /\.tplc\.html$/)); 

    modules.map(function (val) { 
     $templateCache.put(val.name, val.tpl); 
    }) 

}); 
+0

説明のためだけです:コードは "webpack"を呼び出すか、アプリケーションを起動することによってトリガーされますか? – user1934212

関連する問題