2017-08-20 2 views
5

この新しいテンプレートを使用するには、アンギュラマテリアルやサードパーティ製のコントロールセットを実際に試そうとすると苦労します。この新しいテンプレートでは、WebpackはTreeShakableとNonTreeShakableに分割されています。さらに、app.moduleはapp.module.shared、app.module.browser、app.module.serverになりました。角のある素材のCore Core 2 Spaテンプレート

これを動作させようと試みたので、app.module.browserで設定されたモジュールでのみ実行されますが、マテリアルタグは処理されません。単純なものを試して、ボタンを試してみてください。私は何の誤りもありませんが、それは動作しません。私はPlunkerでそれらの例に行き、それが生成したものをコピーしました。そして、私は正しいと思っています。

webpackベンダの設定を開始点として含めると、cssとjsがどのようにバンドルされるかが鍵となるようです。

あなたが好きnonTreeShakableModulesに角度材料とCDKを含める必要がTIA

Webpack.vendor 
const path = require('path'); 
const webpack = require('webpack'); 
const ExtractTextPlugin = require('extract-text-webpack-plugin'); 
const merge = require('webpack-merge'); 
const treeShakableModules = [ 
    '@angular/animations', 
    '@angular/common', 
    '@angular/compiler', 
    '@angular/core', 
    '@angular/forms', 
    '@angular/http', 
    '@angular/platform-browser', 
    '@angular/platform-browser-dynamic', 
    '@angular/router', 
    '@angular/material', 
    '@angular/cdk', 
    'zone.js' 

]; 
const nonTreeShakableModules = [ 
    'bootstrap', 
    'jqwidgets-framework', 
    "@angular/material/prebuilt-themes/indigo-pink.css", 
    'bootstrap/dist/css/bootstrap.css', 
    'font-awesome/css/font-awesome.css', 
    'es6-promise', 
    'es6-shim', 
    'event-source-polyfill', 
    'jquery', 
]; 
const allModules = treeShakableModules.concat(nonTreeShakableModules); 

module.exports = (env) => { 
    const extractCSS = new ExtractTextPlugin('vendor.css'); 
    const isDevBuild = !(env && env.prod); 
    const sharedConfig = { 
     stats: { modules: false }, 
     resolve: { extensions: [ '.js' ] }, 
     module: { 
      rules: [ 
       { test: /\.(png|woff|woff2|eot|ttf|svg)(\?|$)/, use: 'url-loader?limit=100000' } 
      ] 
     }, 
     output: { 
      publicPath: 'dist/', 
      filename: '[name].js', 
      library: '[name]_[hash]' 
     }, 
     plugins: [ 
      new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery' }), // Maps these identifiers to the jQuery package (because Bootstrap expects it to be a global variable) 
      new webpack.ContextReplacementPlugin(/\@angular\b.*\b(bundles|linker)/, path.join(__dirname, './ClientApp')), // Workaround for https://github.com/angular/angular/issues/11580 
      new webpack.ContextReplacementPlugin(/angular(\\|\/)core(\\|\/)@angular/, path.join(__dirname, './ClientApp')), // Workaround for https://github.com/angular/angular/issues/14898 
      new webpack.IgnorePlugin(/^vertx$/) // Workaround for https://github.com/stefanpenner/es6-promise/issues/100 
     ] 
    }; 

    const clientBundleConfig = merge(sharedConfig, { 
     entry: { 
      // To keep development builds fast, include all vendor dependencies in the vendor bundle. 
      // But for production builds, leave the tree-shakable ones out so the AOT compiler can produce a smaller bundle. 
      vendor: isDevBuild ? allModules : nonTreeShakableModules 
     }, 
     output: { path: path.join(__dirname, 'wwwroot', 'dist') }, 
     module: { 
      rules: [ 
       { test: /\.css(\?|$)/, use: extractCSS.extract({ use: isDevBuild ? 'css-loader' : 'css-loader?minimize' }) } 
      ] 
     }, 
     plugins: [ 
      extractCSS, 
      new webpack.DllPlugin({ 
       path: path.join(__dirname, 'wwwroot', 'dist', '[name]-manifest.json'), 
       name: '[name]_[hash]' 
      }) 
     ].concat(isDevBuild ? [] : [ 
      new webpack.optimize.UglifyJsPlugin() 
     ]) 
    }); 

    const serverBundleConfig = merge(sharedConfig, { 
     target: 'node', 
     resolve: { mainFields: ['main'] }, 
     entry: { vendor: allModules.concat(['aspnet-prerendering']) }, 
     output: { 
      path: path.join(__dirname, 'ClientApp', 'dist'), 
      libraryTarget: 'commonjs2', 
     }, 
     module: { 
      rules: [ { test: /\.css(\?|$)/, use: ['to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize' ] } ] 
     }, 
     plugins: [ 
      new webpack.DllPlugin({ 
       path: path.join(__dirname, 'ClientApp', 'dist', '[name]-manifest.json'), 
       name: '[name]_[hash]' 
      }) 
     ] 
    }); 

    return [clientBundleConfig, serverBundleConfig]; 
} 
+0

これまでにこれを動作させましたか?私が騙せる基本テンプレートがありますか? –

答えて

6

const treeShakableModules = [ 
    '@angular/animations', 
    '@angular/common', 
    '@angular/compiler', 
    '@angular/core', 
    '@angular/forms', 
    '@angular/http', 
    '@angular/platform-browser', 
    '@angular/platform-browser-dynamic', 
    '@angular/router', 
    'zone.js', 
]; 
const nonTreeShakableModules = [ 
    'bootstrap', 
    'bootstrap/dist/css/bootstrap.css', 
    '@angular/material', 
    '@angular/material/prebuilt-themes/indigo-pink.css', 
    '@angular/cdk', 
    'es6-promise', 
    'es6-shim', 
    'event-source-polyfill', 
    'jquery', 
]; 

では、次の2とNPMから角度材料とCDKの両方のモジュールがインストールされていることを確認してくださいコマンド(アニメーションモジュールはオプション)

npm install --save @angular/material @angular/cdk 
npm install --save @angular/animations 

これはpackage.jsonに以下の行を追加する必要があります

"@angular/animations": "https://registry.npmjs.org/@angular/animations/-/animations-4.2.5.tgz", 
"@angular/cdk": "^2.0.0-beta.8", 
"@angular/material": "^2.0.0-beta.8", 

あなたは今WebPACKのCMDまたはPowerShellのコマンドを次のように構築し実行してみてください:

webpack --config webpack.config.vendor.js 

エラー、あなたが存在しない場合app.module.shared.tsで使用するコンポーネントを含めることができます。

// angular material modules 
import { 
    MdAutocompleteModule, 
    MdButtonModule, 
    MdButtonToggleModule, 
    MdCardModule, 
    MdCheckboxModule, 
    MdChipsModule, 
    MdCoreModule, 
    MdDatepickerModule, 
    MdDialogModule, 
    MdExpansionModule, 
    MdGridListModule, 
    MdIconModule, 
    MdInputModule, 
    MdListModule, 
    MdMenuModule, 
    MdNativeDateModule, 
    MdPaginatorModule, 
    MdProgressBarModule, 
    MdProgressSpinnerModule, 
    MdRadioModule, 
    MdRippleModule, 
    MdSelectModule, 
    MdSidenavModule, 
    MdSliderModule, 
    MdSlideToggleModule, 
    MdSnackBarModule, 
    MdSortModule, 
    MdTableModule, 
    MdTabsModule, 
    MdToolbarModule, 
    MdTooltipModule, 
} from '@angular/material'; 
import { CdkTableModule } from '@angular/cdk'; 

それらを@NgModuleのインポートに追加してください。

次の修正が行われるまで、まだ一部のコンポーネントが盗まれています。ページを更新するときにサーバー側のレンダリングを壊すチェックボックスコンポーネントと同じです。しかし、次のリリースで修正される予定です(すでにマスターブランチ上にあります)。

関連する問題