微信

webpack.dev.conf.js 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. var path = require('path');
  2. var config = require('../config')
  3. var webpack = require('webpack')
  4. var merge = require('webpack-merge')
  5. var utils = require('./utils')
  6. var baseWebpackConfig = require('./webpack.base.conf')
  7. var HtmlWebpackPlugin = require('html-webpack-plugin')
  8. var glob = require('glob')
  9. // add hot-reload related code to entry chunks
  10. Object.keys(baseWebpackConfig.entry).forEach(function (name) {
  11. baseWebpackConfig.entry[name] = ['./build/dev-client'].concat(baseWebpackConfig.entry[name])
  12. })
  13. module.exports = merge(baseWebpackConfig, {
  14. module: {
  15. loaders: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap })
  16. },
  17. // eval-source-map is faster for development
  18. devtool: '#eval-source-map',
  19. plugins: [
  20. new webpack.DefinePlugin({
  21. 'process.env': config.dev.env
  22. }),
  23. // https://github.com/glenjamin/webpack-hot-middleware#installation--usage
  24. new webpack.optimize.OccurenceOrderPlugin(),
  25. new webpack.HotModuleReplacementPlugin(),
  26. new webpack.NoErrorsPlugin()
  27. ]
  28. })
  29. function getEntry(globPath) {
  30. var entries = {},
  31. basename, tmp, pathname;
  32. if (typeof (globPath) != "object") {
  33. globPath = [globPath]
  34. }
  35. globPath.forEach((itemPath) => {
  36. glob.sync(itemPath).forEach(function (entry) {
  37. basename = path.basename(entry, path.extname(entry));
  38. if (entry.split('/').length > 4) {
  39. tmp = entry.split('/').splice(-3);
  40. pathname = tmp.splice(0, 1) + '/' + basename; // 正确输出js和html的路径
  41. entries[pathname] = entry;
  42. } else {
  43. entries[basename] = entry;
  44. }
  45. });
  46. });
  47. return entries;
  48. }
  49. var pages = getEntry(['./src/module/*.html','./src/module/**/*.html']);
  50. for (var pathname in pages) {
  51. // 配置生成的html文件,定义路径等
  52. var conf = {
  53. filename: pathname + '.html',
  54. template: pages[pathname], // 模板路径
  55. inject: true, // js插入位置
  56. // necessary to consistently work with multiple chunks via CommonsChunkPlugin
  57. chunksSortMode: 'dependency'
  58. };
  59. if (pathname in module.exports.entry) {
  60. conf.chunks = ['manifest', 'vendor', pathname];
  61. conf.hash = true;
  62. }
  63. module.exports.plugins.push(new HtmlWebpackPlugin(conf));
  64. }