server.build.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. const webpack = require('webpack');
  2. const path = require('path');
  3. const config = require('./config.js');
  4. const rm = require('rimraf'); //node环境下rm -rf的命令库
  5. const fs = require('fs')
  6. const nodeModules = {}
  7. fs.readdirSync('node_modules')
  8. .filter(function (x) {
  9. return [ '.bin' ].indexOf(x) === -1
  10. })
  11. .forEach(function (mod) {
  12. nodeModules[ mod ] = 'commonjs ' + mod
  13. })
  14. const webpackConfig = {
  15. mode: 'production',
  16. target: 'node',
  17. node: {
  18. __filename: true,
  19. __dirname: true,
  20. },
  21. externals: nodeModules,
  22. entry: {
  23. app: [
  24. //增加对es6 api的支持,如axios里的promise
  25. // 'babel-polyfill',
  26. path.resolve(__dirname, '../app.js') // 定义入口文件
  27. ],
  28. },
  29. output: { // 定义出口
  30. publicPath: config.prod.publicPath,
  31. path: config.prod.root,
  32. filename: '[name].js',
  33. },
  34. resolve: { // 指定可以被 import 的文件后缀
  35. extensions: ['.js'],
  36. // alias: { // 配置常用路径
  37. // src: path.resolve(__dirname, '../src'),
  38. // views: 'src/views',
  39. // }
  40. },
  41. module: {
  42. rules: [{
  43. test: /\.(js|jsx)$/,
  44. loader: 'babel-loader',
  45. include: [path.resolve(__dirname, '../src'), path.resolve(__dirname, '../server')],
  46. }, {
  47. test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
  48. loader: 'url-loader',
  49. options: {
  50. limit: 10000,
  51. }
  52. }, {
  53. test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
  54. loader: 'url-loader',
  55. options: {
  56. limit: 10000,
  57. }
  58. }, {
  59. test: /\.(css|scss)$/,
  60. use: [
  61. 'css-loader/locals',
  62. 'sass-loader',
  63. ]
  64. }]
  65. },
  66. };
  67. // 通过node删除旧目录,生成新目录
  68. rm(path.join(config.prod.root, 'app.js'), err => {
  69. if (err) throw err;
  70. webpack(webpackConfig, function (err, stats) {
  71. if (err) throw err;
  72. process.stdout.write(stats.toString({
  73. colors: true,
  74. modules: false,
  75. children: false,
  76. chunks: false,
  77. chunkModules: false
  78. }) + '\n\n');
  79. console.log(' Build complete.\n');
  80. });
  81. });