generated from DanWahlin/typescript-fundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
108 lines (104 loc) · 3.39 KB
/
webpack.config.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const path = require('path');
const fs = require('fs');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const excludeDirs = ['css', 'images', 'lib', 'api', '_hello-ts'];
const htmlPlugins = generateHtmlPlugins('./src', excludeDirs);
const entryPoints = generateEntryPoints('./src', 'index.ts', excludeDirs);
module.exports = {
entry: entryPoints,
output: {
filename: '[name]/index.js',
path: path.resolve(__dirname, 'dist'),
},
devtool: 'inline-source-map',
module: {
rules: [
{
test: /\.ts?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.(sa|sc|c)ss$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
},
],
},
resolve: {
extensions: ['.ts', '.js'],
},
plugins: [
new MiniCssExtractPlugin({ filename: '[name]/style.css' }),
new CopyPlugin({
patterns: [
{ from: 'src/images', to: 'images' },
{ from: 'src/api', to: 'api' },
],
}),
new HtmlWebpackPlugin({
template: './index.html',
inject: false,
}),
].concat(htmlPlugins),
devServer: {
contentBase: './dist',
compress: true,
port: 9000,
},
};
// Create dist/templateDir/*.html file for each templateDir found in root
// Only include bundle/chunk associated with HTML file
function generateHtmlPlugins(root, excludeDirs) {
let plugins = [];
const rootDir = fs.readdirSync(path.resolve(__dirname, root));
// Find directories in root folder
rootDir.forEach((templateDir) => {
const stats = fs.lstatSync(path.resolve(__dirname, root, templateDir));
if (stats.isDirectory() && !excludeDirs.includes(templateDir)) {
// Read files in template directory
const dirName = templateDir;
const templateFiles = fs.readdirSync(
path.resolve(__dirname, root, templateDir),
);
templateFiles.forEach((item) => {
// Split names and extension
const parts = item.split('.');
const name = parts[0];
const extension = parts[1];
// If we find an html file then create an HtmlWebpackPlugin
if (extension === 'html') {
// Create new HTMLWebpackPlugin with options
plugins.push(
new HtmlWebpackPlugin({
filename: `${dirName}/index.html`,
template: path.resolve(
__dirname,
`${root}/${templateDir}/${name}.${extension}`,
),
inject: 'body',
// Only include bundle/chunk associated with the templateDir directory
chunks: [`${dirName}`],
}),
);
}
});
}
});
return plugins;
}
// Create an entry point for each directory found in 'root'.
// This will also create a bundle/chunk for each directory
// and place it in the dist/[templateDir] directory.
function generateEntryPoints(root, entryScript, excludeDirs) {
const rootDir = fs.readdirSync(path.resolve(__dirname, root));
let entryPoints = { css: './src/css/style.scss' };
rootDir.forEach((templateDir) => {
const stats = fs.lstatSync(path.resolve(__dirname, root, templateDir));
if (stats.isDirectory() && !excludeDirs.includes(templateDir)) {
entryPoints[templateDir] = `${root}/${templateDir}/${entryScript}`;
}
});
return entryPoints;
}