genMultiEntry.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. const fs = require('fs')
  2. const path = require('path')
  3. const HtmlWebpackPlugin = require('html-webpack-plugin')
  4. const pagesPath = path.resolve(__dirname, '../src/pages')
  5. const entryPath = path.resolve(__dirname, '../src/entries')
  6. const pagesFiles = fs.readdirSync(pagesPath, 'utf-8')
  7. const matchNameReg = /(.*)\.\w+$/
  8. const mkFile = path => {
  9. return fs.closeSync(fs.openSync(path, 'w'))
  10. }
  11. const genHtmlWebpackPlugin = (files) => {
  12. return files.map(fileName => {
  13. const chunkName = fileName.match(matchNameReg)[1]
  14. return new HtmlWebpackPlugin({
  15. template: path.join(pagesPath, fileName),
  16. filename: fileName,
  17. chunks: [chunkName],
  18. minify: false
  19. })
  20. })
  21. }
  22. const genEntry = (entries) => {
  23. let entry = {}
  24. entries.forEach(entryName => {
  25. let _name = entryName.match(matchNameReg)[1]
  26. const matchReg = new RegExp('^' + _name + '.js$')
  27. const entryFile = fs.readdirSync(entryPath).find(f => matchReg.test(f))
  28. if(entryFile) {
  29. entry[_name] = path.join(entryPath, entryFile)
  30. } else {
  31. const defaultPath = path.join(entryPath, _name + '.js')
  32. mkFile(defaultPath)
  33. entry[_name] = defaultPath
  34. }
  35. })
  36. return entry
  37. }
  38. module.exports.HTMLWebpackPlugins = genHtmlWebpackPlugin(pagesFiles)
  39. module.exports.entry = genEntry(pagesFiles)