11const path = require ( 'path' ) ;
2- const fs = require ( 'fs' ) ;
3-
42const webpack = require ( 'webpack' ) ;
53const MiniCssExtractPlugin = require ( 'mini-css-extract-plugin' ) ;
6- const HtmlWebpackPlugin = require ( 'html-webpack-plugin' ) ;
74const { CleanWebpackPlugin } = require ( 'clean-webpack-plugin' ) ;
85const CopyWebpackPlugin = require ( 'copy-webpack-plugin' ) ;
96const ImageminPlugin = require ( 'imagemin-webpack-plugin' ) . default ;
@@ -12,35 +9,52 @@ const TerserPlugin = require('terser-webpack-plugin');
129const OptimizeCSSAssetsPlugin = require ( 'optimize-css-assets-webpack-plugin' ) ;
1310const PreloadWebpackPlugin = require ( 'preload-webpack-plugin' ) ;
1411const SpriteLoaderPlugin = require ( 'svg-sprite-loader/plugin' ) ;
12+ const generateHtmlPlugins = require ( './helpers/generateHtmlPlugins' ) ;
1513
14+ /**
15+ * Получает root директорию проекта.
16+ * @type {string }
17+ */
18+ const projectRootDir = process . cwd ( ) ;
19+ /**
20+ * Текущая версия проекта из package.json
21+ * @type {string }
22+ */
23+ const version = require ( '../package.json' ) . version ;
24+ /**
25+ * Кастомный title для сайта
26+ * @type {string }
27+ */
28+ const title = 'Create HTML Boilerplate' ;
29+ /**
30+ * Директория с шаблонами
31+ * @type {string }
32+ */
33+ const templatesPath = path . join ( projectRootDir , 'source' , 'html' , 'views' ) ;
34+ /**
35+ * Базовый конфиг для сайта
36+ * templatesPath – путь для HTML шаблонов (обязательный параметр)
37+ * options – любые данные которые необходимы в шаблоне (опциональный параметр)
38+ * @type {{templatesPath: string, options: {[key]: string | number | any }} }
39+ */
40+ const config = {
41+ templatesPath,
42+ options : {
43+ version,
44+ title,
45+ } ,
46+ } ;
47+ /**
48+ * Определяет в каком режиме работает сборка – девелопмент или продакшн.
49+ * @type {boolean }
50+ */
1651const isProd = process . argv . indexOf ( '-p' ) !== - 1 ;
17-
18- function generateHtmlPlugins ( templateDir ) {
19- const templateFiles = fs . readdirSync ( path . resolve ( __dirname , templateDir ) ) ;
20- const htmlFiles = templateFiles . filter ( templateFile => {
21- const parts = templateFile . split ( '.' ) ;
22- return parts [ 1 ] === 'html' ;
23- } ) ;
24-
25- return htmlFiles . map ( htmlFile => {
26- const parts = htmlFile . split ( '.' ) ;
27- const name = parts [ 0 ] ;
28- const extension = parts [ 1 ] ;
29-
30- return new HtmlWebpackPlugin ( {
31- filename : `${ name } .html` ,
32- template : path . resolve ( __dirname , `${ templateDir } /${ name } .${ extension } ` ) ,
33- inject : true ,
34- } ) ;
35- } ) ;
36- }
37-
38- const htmlPlugins = generateHtmlPlugins ( '../source/html/views' ) ;
52+ const htmlPlugins = generateHtmlPlugins ( config ) ;
3953
4054module . exports = {
4155 resolve : {
4256 alias : {
43- source : '../ source' ,
57+ source : path . join ( '..' , ' source') ,
4458 } ,
4559 } ,
4660 mode : isProd ? 'production' : 'development' ,
@@ -51,7 +65,7 @@ module.exports = {
5165 } ,
5266 devtool : 'inline-source-map' ,
5367 devServer : {
54- contentBase : path . join ( __dirname , 'source/' ) ,
68+ contentBase : path . join ( projectRootDir , 'source/' ) ,
5569 port : 9001 ,
5670 hot : true ,
5771 compress : true ,
@@ -104,9 +118,6 @@ module.exports = {
104118 new MiniCssExtractPlugin ( {
105119 filename : 'css/[name].min.css' ,
106120 } ) ,
107- new HtmlWebpackPlugin ( {
108- template : 'source/html/views/index.html' ,
109- } ) ,
110121 new PreloadWebpackPlugin ( {
111122 rel : 'preload' ,
112123 include : 'allChunks' , // or 'initial', or 'allAssets'
0 commit comments