-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins.ts
69 lines (64 loc) · 1.78 KB
/
plugins.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { ProvidePlugin } from "webpack";
import CopyWebpackPlugin from 'copy-webpack-plugin'
import HtmlWebpackPlugin from 'html-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import NodePolyfillPlugin from "node-polyfill-webpack-plugin";
import fs from 'fs';
/**
* allows to use node with webpack 5
*/
const nodePolyfillPlugin = new NodePolyfillPlugin();
/**
* Plugin allows to specify aliases for paths only in webpack
* and then will insert it in webpack
*/
// const tsconfigPathsPlugin = new TsconfigPathsPlugin();
/**
* make content of lib accessible from any module
*/
const providePlugin = new ProvidePlugin({ _: 'lodash' });
/**
* Plugin inserts css into html files
*/
const miniCssExtractPlugin = new MiniCssExtractPlugin();
/**
* Plugin copies files from folder
* to dist/[folderName] on webpack build
*/
const copyWebpackPlugin = new CopyWebpackPlugin({
patterns: [{
from: './src/assets',
to: './assets'
},
{
from: './src/assets/icon',
to: 'favicon.ico'
}]
});
const htmlFilesNames = getHtmlFilesNames();
const htmlPlugins = getHtmlPlugins(htmlFilesNames);
const webpackPlugins = [
miniCssExtractPlugin,
copyWebpackPlugin,
nodePolyfillPlugin,
providePlugin,
...htmlPlugins];
/**
* Plugin allows to create object of html
* files which will be populated with corresponding js files
* @property {inject} - defines where and if javascript files should be injected in html
*/
function getHtmlPlugins(filesName: string[]) {
return filesName.map(filename => {
return new HtmlWebpackPlugin({
template: `./src/html/${filename}`,
inject: true,
chunks: [filename.replace(/\.[^/.]+$/, "")],
filename: filename,
});
});
}
function getHtmlFilesNames(): string[] {
return fs.readdirSync('./src/html');
}
export default webpackPlugins;