总结

当我们把所有东西放在一起时,我们完整的webpack.config.js文件看起来像这样:

  1. 'use strict';
  2. const path = require("path");
  3. const webpack = require('webpack');
  4. const HtmlWebpackPlugin = require('html-webpack-plugin');
  5. const basePlugins = [
  6. new webpack.optimize.CommonsChunkPlugin('vendor', '[name].[hash].bundle.js'),
  7. new HtmlWebpackPlugin({
  8. template: './src/index.html',
  9. inject: 'body',
  10. minify: false
  11. })
  12. ];
  13. const envPlugins = {
  14. production: [
  15. new webpack.optimize.UglifyJsPlugin({
  16. compress: {
  17. warnings: false
  18. }
  19. })
  20. ],
  21. development: []
  22. };
  23. const plugins = basePlugins.concat(envPlugins[process.env.NODE_ENV] || []);
  24. module.exports = {
  25. entry: {
  26. app: './src/index.ts',
  27. vendor: [
  28. '@angular/core',
  29. '@angular/compiler',
  30. '@angular/common',
  31. '@angular/http',
  32. '@angular/platform-browser',
  33. '@angular/platform-browser-dynamic',
  34. '@angular/router',
  35. 'es6-shim',
  36. 'redux',
  37. 'redux-thunk',
  38. 'redux-logger',
  39. 'reflect-metadata',
  40. 'ng2-redux',
  41. 'zone.js',
  42. ]
  43. },
  44. output: {
  45. path: path.resolve(__dirname, 'dist'),
  46. filename: '[name].[hash].js',
  47. publicPath: "/",
  48. sourceMapFilename: '[name].[hash].js.map'
  49. },
  50. devtool: 'source-map',
  51. resolve: {
  52. extensions: ['.webpack.js', '.web.js', '.ts', '.js']
  53. },
  54. plugins: plugins,
  55. module: {
  56. rules: [
  57. { test: /\.ts$/, loader: 'tslint' },
  58. { test: /\.ts$/, loader: 'ts', exclude: /node_modules/ },
  59. { test: /\.html$/, loader: 'raw' },
  60. { test: /\.css$/, loader: 'style!css?sourceMap' },
  61. { test: /\.svg/, loader: 'url' },
  62. { test: /\.eot/, loader: 'url' },
  63. { test: /\.woff/, loader: 'url' },
  64. { test: /\.woff2/, loader: 'url' },
  65. { test: /\.ttf/, loader: 'url' },
  66. ],
  67. noParse: [ /zone\.js\/dist\/.+/, /angular2\/bundles\/.+/ ]
  68. }
  69. }

更进一步

Webpack也做类似热部署和代码优化,我们没有覆盖。有关更多信息,您可以查看官方文档。源代码也可以在Github上获得。