build.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. const webpack = require('webpack');
  2. const path = require('path');
  3. const merge = require('webpack-merge');
  4. const HtmlWebpackPlugin = require('html-webpack-plugin');
  5. const autoprefixer = require('autoprefixer');
  6. const ExtractTextPlugin = require('extract-text-webpack-plugin');
  7. const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin');
  8. const rm = require('rimraf'); //node环境下rm -rf的命令库
  9. const baseWebpackConfig = require('./webpack.config.js');
  10. const config = require('./config.js');
  11. const webpackConfig = merge(baseWebpackConfig, {
  12. entry: {
  13. // vendor: ['react-router', 'axios', 'prop-types'],
  14. // until: ['codemirror', 'react-codemirror2'],
  15. main: [
  16. //增加对es6 api的支持,如axios里的promise
  17. // 'babel-polyfill',
  18. path.resolve(__dirname, '../src/main.pro.js') // 定义入口文件
  19. ],
  20. },
  21. output: { // 定义出口
  22. publicPath: config.prod.publicPath,
  23. path: config.prod.root,
  24. filename: path.posix.join(config.prod.subDirectory, 'js/[name].[contenthash:5].js'),
  25. chunkFilename: path.posix.join(config.prod.subDirectory, 'js/[name].[chunkhash:5].chunk.js'), //注意这里,用[name]可以自动生成路由名称对应的js文件
  26. },
  27. mode: 'production',
  28. module: {
  29. rules: [{
  30. test: /\.(css|scss)$/,
  31. use: ExtractTextPlugin.extract({
  32. use: [
  33. 'css-loader',
  34. {
  35. loader: 'postcss-loader',
  36. options: {
  37. plugins: function () {
  38. return [autoprefixer({ browsers: ["> 1%","last 2 versions","not ie <= 8"] })];
  39. }
  40. }
  41. },
  42. {
  43. loader: 'sass-loader',
  44. options: {
  45. includePaths: [path.resolve(__dirname, "../node_modules/compass-mixins/lib")]
  46. }
  47. }
  48. ],
  49. fallback: 'style-loader'
  50. })
  51. }]
  52. },
  53. plugins: [
  54. //将js中引入的css分离的插件
  55. new ExtractTextPlugin({
  56. filename: path.posix.join(config.prod.subDirectory, 'css/[name].[chunkhash:5].css'),
  57. // allChunks: false // 指明为false,否则会包括异步加载的 CSS
  58. }),
  59. //压缩提取出的css,并解决ExtractTextPlugin分离出的js重复问题(多个文件引入同一css文件)
  60. new OptimizeCSSPlugin(),
  61. //html模板配置
  62. new HtmlWebpackPlugin({
  63. template: path.resolve(__dirname, '../src/index.html'),
  64. filename: 'index.html', //生成的html的文件名
  65. inject: true, //注入的js文件将会被放在body标签中,当值为'head'时,将被放在head标签中
  66. // hash: true, //为静态资源生成hash值
  67. // minify: { //压缩配置
  68. // removeComments: true, //删除html中的注释代码
  69. // collapseWhitespace: false, //删除html中的空白符
  70. // removeAttributeQuotes: false //删除html元素中属性的引号
  71. // },
  72. // chunksSortMode: 'dependency' //按dependency的顺序引入
  73. }),
  74. ]
  75. });
  76. // 通过node删除旧目录,生成新目录
  77. rm(path.join(config.prod.root, config.prod.subDirectory), err => {
  78. if (err) throw err;
  79. webpack(webpackConfig, function (err, stats) {
  80. if (err) throw err;
  81. process.stdout.write(stats.toString({
  82. colors: true,
  83. modules: false,
  84. children: false,
  85. chunks: false,
  86. chunkModules: false
  87. }) + '\n\n');
  88. console.log(' Build complete.\n');
  89. });
  90. });