vbagx/build/webpack.config.js

207 lines
5.1 KiB
JavaScript
Raw Normal View History

2021-04-13 23:46:54 +02:00
const webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const WorkboxPlugin = require('workbox-webpack-plugin');
const path = require('path');
function resolvePath(dir) {
return path.join(__dirname, '..', dir);
}
const env = process.env.NODE_ENV || 'development';
const target = process.env.TARGET || 'web';
const isCordova = target === 'cordova';
const isElectronWatch = process.env.ELECTRON_WATCH || false;
module.exports = {
mode: env,
target: env === "development" ? "web" : "browserslist",
entry: {
app: './src/js/app.js',
},
2021-08-24 22:51:24 +02:00
performance: {
hints: false,
maxEntrypointSize: 400000,
},
2021-04-13 23:46:54 +02:00
output: {
path: resolvePath(isCordova ? (isElectronWatch ? 'cordova/platforms/electron/www' : 'cordova/www') : 'www'),
filename: 'js/[name].js',
chunkFilename: 'js/[name].js',
publicPath: '',
hotUpdateChunkFilename: 'hot/hot-update.js',
hotUpdateMainFilename: 'hot/hot-update.json',
},
resolve: {
extensions: ['.js', '.json'],
alias: {
'@': resolvePath('src'),
},
2021-08-24 22:51:24 +02:00
fallback: { crypto: false },
2021-04-13 23:46:54 +02:00
},
devtool: env === 'production' ? 'source-map' : 'eval',
devServer: {
hot: true,
open: true,
compress: true,
2021-08-24 22:51:24 +02:00
//contentBase: '/www/',
static: '/www/',
allowedHosts: "all",
2021-04-13 23:46:54 +02:00
historyApiFallback: true,
2021-08-24 22:51:24 +02:00
port: 8082,
2021-04-13 23:46:54 +02:00
},
optimization: {
concatenateModules: true,
minimizer: [new TerserPlugin()],
},
module: {
rules: [
{
test: /\.(mjs|js|jsx)$/,
include: [
resolvePath('src'),
],
use: [
{
loader: require.resolve('babel-loader'),
},
]
},
{
test: /\.f7.(html|js)$/,
use: [
'babel-loader',
'framework7-loader',
],
},
{
test: /\.css$/,
use: [
(env === 'development' ? 'style-loader' : {
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: '../'
}
}),
'css-loader',
'postcss-loader',
],
},
{
test: /\.styl(us)?$/,
use: [
(env === 'development' ? 'style-loader' : {
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: '../'
}
}),
'css-loader',
'postcss-loader',
'stylus-loader',
],
},
{
test: /\.less$/,
use: [
(env === 'development' ? 'style-loader' : {
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: '../'
}
}),
'css-loader',
'postcss-loader',
'less-loader',
],
},
{
test: /\.(sa|sc)ss$/,
use: [
(env === 'development' ? 'style-loader' : {
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: '../'
}
}),
'css-loader',
'postcss-loader',
'sass-loader',
],
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
2021-08-24 22:51:24 +02:00
type: "asset/resource",
generator: {
filename: "images/[name][ext]",
}
2021-04-13 23:46:54 +02:00
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac|m4a)(\?.*)?$/,
2021-08-24 22:51:24 +02:00
type: "asset/resource",
generator: {
filename: "media/[name][ext]",
}
2021-04-13 23:46:54 +02:00
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
2021-08-24 22:51:24 +02:00
type: "asset/resource",
generator: {
filename: "fonts/[name][ext]",
}
2021-04-13 23:46:54 +02:00
},
],
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(env),
'process.env.TARGET': JSON.stringify(target),
}),
...(env === 'production' ? [
new CssMinimizerPlugin(),
] : [
// Development only plugins
new webpack.HotModuleReplacementPlugin(),
]),
new HtmlWebpackPlugin({
filename: './index.html',
template: './src/index.html',
inject: true,
minify: env === 'production' ? {
collapseWhitespace: true,
removeComments: true,
removeRedundantAttributes: false,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true
} : false,
}),
new MiniCssExtractPlugin({
filename: 'css/[name].css',
}),
new CopyWebpackPlugin({
patterns: [
{
noErrorOnMissing: true,
from: resolvePath('src/static'),
to: resolvePath(isCordova ? 'cordova/www/static' : 'www/static'),
},
{
noErrorOnMissing: true,
from: resolvePath('src/manifest.json'),
to: resolvePath('www/manifest.json'),
},
],
}),
...(!isCordova ? [
new WorkboxPlugin.InjectManifest({
swSrc: resolvePath('src/service-worker.js'),
})
] : []),
],
};