genMultiEntry.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. const fs = require('fs')
  2. const path = require('path')
  3. const HtmlWebpackPlugin = require('html-webpack-plugin')
  4. const mkFile = require('./mkFile.js')
  5. const pagesPath = path.resolve(__dirname, '../src/pages')
  6. const entryPath = path.resolve(__dirname, '../src/js')
  7. const pagesFiles = fs.readdirSync(pagesPath, 'utf-8')
  8. const matchNameReg = /\w+(?=\.)/
  9. const genHtmlWebpackPlugin = (files) => {
  10. return files.map(fileName => {
  11. const chunkName = fileName.match(matchNameReg)[0]
  12. const entryFilePath = path.join(entryPath, chunkName + '.js')
  13. if (!fs.existsSync(entryFilePath)) {
  14. mkFile(path.join(entryFilePath))
  15. }
  16. return new HtmlWebpackPlugin({
  17. template: path.join(pagesPath, fileName),
  18. filename: fileName,
  19. chunks: ['commons', chunkName]
  20. })
  21. })
  22. }
  23. const genEntry = (entries) => {
  24. let entry = {}
  25. entries.forEach(entryName => {
  26. let _name = entryName.match(matchNameReg)[0]
  27. entry[_name] = path.join(entryPath, _name + '.js')
  28. })
  29. return entry
  30. }
  31. module.exports.HTMLWebpackPlugins = genHtmlWebpackPlugin(pagesFiles)
  32. module.exports.entry = genEntry(pagesFiles)