-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
93 lines (87 loc) · 2.27 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
const path = require('path');
const glob = require('glob');
var fs = require('fs');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpackConfig = {
entry: { 'common.js': './src/common.js' },
output: {
filename: '[name]',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{
test: /\.ts$/,
use: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.(png|jpe?g|gif)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 20000,
name: '[name].[ext]',
esModule: false,
},
},
],
},
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
{
test: /\.s[ac]ss$/i,
use: [
'style-loader',
'css-loader',
{
loader: 'sass-loader',
options: {
additionalData: "@import 'global-imports.scss';",
},
},
],
},
],
},
plugins: [],
};
// cf. https://www.key-p.com/blog/staff/archives/107125
glob.sync('**/*.html', { cwd: 'src' }).forEach((htmlName) => {
const baseName = path.basename(htmlName, '.html');
const jsName = baseName + '.js';
const jsPath = path.resolve('src', jsName);
const tsName = baseName + '.ts';
const tsPath = path.resolve('src', tsName);
const scssName = baseName + '.scss';
const scssPath = path.resolve('src', scssName);
const cssName = baseName + '.css';
const cssPath = path.resolve('src', cssName);
let chunks = ['common.js'];
if (fs.existsSync(tsPath)) {
webpackConfig.entry[jsName] = tsPath;
chunks.push(jsName);
} else if (fs.existsSync(jsPath)) {
webpackConfig.entry[jsName] = jsPath;
chunks.push(jsName);
}
if (fs.existsSync(scssPath)) {
webpackConfig.entry[scssName + '.js'] = scssPath;
chunks.push(scssName + '.js');
} else if (fs.existsSync(cssPath)) {
webpackConfig.entry[cssName + '.js'] = cssPath;
chunks.push(cssName + '.js');
}
webpackConfig.plugins.push(
new HtmlWebpackPlugin({
template: path.resolve(__dirname, 'src', htmlName),
filename: htmlName,
inject: 'body',
chunks,
}),
);
});
module.exports = webpackConfig;