diff2html/webpack.website.ts

151 lines
3.9 KiB
TypeScript
Raw Permalink Normal View History

2019-12-29 22:31:32 +00:00
import path from 'path';
2019-12-29 22:31:32 +00:00
import webpack from 'webpack';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import CopyWebpackPlugin from 'copy-webpack-plugin';
2019-12-29 22:31:32 +00:00
const pages = ['index', 'demo'];
2021-04-08 13:55:58 +00:00
type Plugin = ((this: webpack.Compiler, compiler: webpack.Compiler) => void) | webpack.WebpackPluginInstance;
function plugins(page: string): Plugin[] {
2020-08-15 13:40:09 +00:00
return [
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[id].css',
}),
new HtmlWebpackPlugin({
hash: true,
inject: true,
title: `${page} page`,
filename: `${page}.html`,
template: `./website/templates/pages/${page}/${page}.handlebars`,
minify: {
html5: true,
collapseWhitespace: true,
caseSensitive: true,
removeEmptyElements: false,
removeComments: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
},
}),
new CopyWebpackPlugin({
patterns: [
{ from: 'website/favicon.ico', to: 'favicon.ico' },
{ from: 'website/robots.txt', to: 'robots.txt' },
{ from: 'website/sitemap.xml', to: 'sitemap.xml' },
],
}),
];
}
const config: webpack.Configuration[] = pages.map(page => {
return {
entry: {
2019-12-29 22:31:32 +00:00
[page]: `./website/templates/pages/${page}/${page}.ts`,
},
output: {
2019-12-29 22:31:32 +00:00
path: path.resolve(__dirname, './docs'),
},
resolve: {
2019-12-29 22:31:32 +00:00
extensions: ['.tsx', '.ts', '.jsx', '.js'],
},
module: {
rules: [
{
test: /\.tsx?$/,
2019-12-29 22:31:32 +00:00
use: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.(html)$/,
use: {
2019-12-29 22:31:32 +00:00
loader: 'html-loader',
options: {
2019-12-29 22:31:32 +00:00
attrs: ['img:src'],
},
},
},
{
test: /\.handlebars$/,
loader: 'handlebars-loader',
options: {
inlineRequires: '/images/',
precompileOptions: {
knownHelpersOnly: false,
2019-12-29 22:31:32 +00:00
},
helperDirs: [path.join(__dirname, 'website/templates/helpers')],
partialDirs: [path.join(__dirname, 'website/templates')],
},
},
{
test: /\.(gif|png|jpe?g|webp)$/i,
use: [
{
2019-12-29 22:31:32 +00:00
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]',
outputPath: 'images',
esModule: false,
2019-12-29 22:31:32 +00:00
},
},
{
2019-12-29 22:31:32 +00:00
loader: 'image-webpack-loader',
options: {
mozjpeg: {
progressive: true,
2019-12-29 22:31:32 +00:00
quality: 65,
},
optipng: {
2019-12-29 22:31:32 +00:00
enabled: true,
},
pngquant: {
quality: [0.65, 0.9],
2019-12-29 22:31:32 +00:00
speed: 4,
},
gifsicle: {
2019-12-29 22:31:32 +00:00
interlaced: false,
},
webp: {
2019-12-29 22:31:32 +00:00
quality: 75,
},
},
},
],
},
{
test: /\.(css)$/,
use: [MiniCssExtractPlugin.loader, { loader: 'css-loader', options: { importLoaders: 1 } }, 'postcss-loader'],
},
{
test: /\.woff(2)?(\?v=\d\.\d\.\d)?$/,
use: [
{
loader: 'url-loader',
options: {
limit: 1000,
mimetype: 'application/font-woff',
},
},
],
},
{
test: /\.(ttf|eot|svg)(\?v=\d\.\d\.\d)?$/,
loader: 'file-loader',
},
2019-12-29 22:31:32 +00:00
],
},
2020-08-15 13:40:09 +00:00
plugins: plugins(page),
};
});
export default config;