2020-03-24 14:22:29 +01:00
const path = require ( 'path' )
2022-10-22 10:31:34 +02:00
const fs = require ( 'fs' )
2020-03-24 14:22:29 +01:00
const webpack = require ( 'webpack' )
const HtmlWebpackPlugin = require ( 'html-webpack-plugin' )
const VueLoaderPlugin = require ( 'vue-loader/lib/plugin' )
const CopyWebpackPlugin = require ( 'copy-webpack-plugin' )
const MiniCssExtractPlugin = require ( 'mini-css-extract-plugin' )
2022-09-13 03:28:36 +02:00
const JsonMinimizerPlugin = require ( 'json-minimizer-webpack-plugin' )
const CssMinimizerPlugin = require ( 'css-minimizer-webpack-plugin' )
2022-09-24 17:06:50 +02:00
const ProcessLocalesPlugin = require ( './ProcessLocalesPlugin' )
2020-03-24 14:22:29 +01:00
const isDevMode = process . env . NODE _ENV === 'development'
2024-02-19 11:58:59 +01:00
const { version : swiperVersion } = JSON . parse ( fs . readFileSync ( path . join ( _ _dirname , '../node_modules/swiper/package.json' ) ) )
2020-03-24 14:22:29 +01:00
const config = {
name : 'web' ,
mode : process . env . NODE _ENV ,
2022-10-06 16:47:47 +02:00
devtool : isDevMode ? 'eval-cheap-module-source-map' : false ,
2020-03-24 14:22:29 +01:00
entry : {
web : path . join ( _ _dirname , '../src/renderer/main.js' ) ,
} ,
output : {
path : path . join ( _ _dirname , '../dist/web' ) ,
filename : '[name].js' ,
} ,
2023-03-01 01:39:33 +01:00
externals : {
'youtubei.js' : '{}'
} ,
2020-03-24 14:22:29 +01:00
module : {
rules : [
{
2022-07-14 16:43:07 +02:00
test : /\.js$/ ,
2020-03-24 14:22:29 +01:00
use : 'babel-loader' ,
exclude : /node_modules/ ,
} ,
{
test : /\.vue$/ ,
2023-01-25 21:58:51 +01:00
loader : 'vue-loader' ,
options : {
compilerOptions : {
whitespace : 'condense' ,
}
}
2020-03-24 14:22:29 +01:00
} ,
{
2023-01-03 19:19:41 +01:00
test : /\.scss$/ ,
2020-03-24 14:22:29 +01:00
use : [
{
loader : MiniCssExtractPlugin . loader ,
} ,
2022-09-12 02:34:58 +02:00
{
loader : 'css-loader' ,
options : {
esModule : false
}
} ,
2020-04-18 05:17:45 +02:00
{
loader : 'sass-loader' ,
options : {
2023-01-03 19:19:41 +01:00
implementation : require ( 'sass' )
2020-04-18 05:17:45 +02:00
}
} ,
2020-03-24 14:22:29 +01:00
] ,
} ,
{
test : /\.css$/ ,
use : [
{
2022-09-02 15:19:06 +02:00
loader : MiniCssExtractPlugin . loader
2020-03-24 14:22:29 +01:00
} ,
2022-09-12 02:34:58 +02:00
{
loader : 'css-loader' ,
options : {
esModule : false
}
}
2020-03-24 14:22:29 +01:00
] ,
} ,
{
test : /\.html$/ ,
use : 'vue-html-loader' ,
} ,
{
test : /\.(png|jpe?g|gif|tif?f|bmp|webp|svg)(\?.*)?$/ ,
2022-09-12 02:34:58 +02:00
type : 'asset/resource' ,
generator : {
filename : 'imgs/[name][ext]'
}
2020-03-24 14:22:29 +01:00
} ,
{
test : /\.(woff2?|eot|ttf|otf)(\?.*)?$/ ,
2022-09-12 02:34:58 +02:00
type : 'asset/resource' ,
generator : {
filename : 'fonts/[name][ext]'
}
2020-03-24 14:22:29 +01:00
} ,
] ,
} ,
2022-09-13 03:28:36 +02:00
// webpack defaults to only optimising the production builds, so having this here is fine
optimization : {
minimizer : [
'...' , // extend webpack's list instead of overwriting it
2022-09-24 17:06:50 +02:00
new JsonMinimizerPlugin ( {
exclude : /\/locales\/.*\.json/
} ) ,
2022-09-13 03:28:36 +02:00
new CssMinimizerPlugin ( )
]
} ,
2020-03-24 14:22:29 +01:00
node : {
2022-10-06 16:47:47 +02:00
_ _dirname : true ,
2020-03-24 14:22:29 +01:00
_ _filename : isDevMode ,
} ,
plugins : [
2022-09-15 10:59:09 +02:00
new webpack . DefinePlugin ( {
2023-01-13 17:54:05 +01:00
'process.env.IS_ELECTRON' : false ,
2023-04-12 15:39:28 +02:00
'process.env.IS_ELECTRON_MAIN' : false ,
2024-04-12 08:12:50 +02:00
'process.env.SUPPORTS_LOCAL_API' : false ,
2024-02-19 11:58:59 +01:00
'process.env.SWIPER_VERSION' : ` ' ${ swiperVersion } ' ` ,
2023-04-12 15:39:28 +02:00
// video.js' vhs-utils supports both atob() in web browsers and Buffer in node
// As the FreeTube web build only runs in web browsers, we can override their check for atob() here: https://github.com/videojs/vhs-utils/blob/main/src/decode-b64-to-uint8-array.js#L3
// overriding that check means we don't need to include a Buffer polyfill
// https://caniuse.com/atob-btoa
// NOTE FOR THE FUTURE: this override won't work with vite as their define does a find and replace in the code for production builds,
// but uses globals in development builds to save build time, so this would replace the actual atob() function with true if used with vite
// this works in webpack as webpack does a find and replace in the source code for both development and production builds
// https://vitejs.dev/config/shared-options.html#define
// https://webpack.js.org/plugins/define-plugin/
'window.atob' : true
2022-09-15 10:59:09 +02:00
} ) ,
2022-10-06 16:47:47 +02:00
new webpack . ProvidePlugin ( {
2023-04-12 15:39:28 +02:00
process : 'process/browser'
2022-10-06 16:47:47 +02:00
} ) ,
2020-03-24 14:22:29 +01:00
new HtmlWebpackPlugin ( {
excludeChunks : [ 'processTaskWorker' ] ,
filename : 'index.html' ,
2024-03-27 01:27:35 +01:00
template : path . resolve ( _ _dirname , '../src/index.ejs' )
2020-03-24 14:22:29 +01:00
} ) ,
new VueLoaderPlugin ( ) ,
new MiniCssExtractPlugin ( {
2020-11-24 03:53:49 +01:00
filename : isDevMode ? '[name].css' : '[name].[contenthash].css' ,
chunkFilename : isDevMode ? '[id].css' : '[id].[contenthash].css' ,
2023-12-16 14:50:09 +01:00
} ) ,
new CopyWebpackPlugin ( {
patterns : [
{
from : path . join ( _ _dirname , '../node_modules/swiper/modules/{a11y,navigation,pagination}-element.css' ) . replaceAll ( '\\' , '/' ) ,
2024-02-19 11:58:59 +01:00
to : ` swiper- ${ swiperVersion } .css ` ,
2023-12-16 14:50:09 +01:00
context : path . join ( _ _dirname , '../node_modules/swiper/modules' ) ,
transformAll : ( assets ) => {
return Buffer . concat ( assets . map ( asset => asset . data ) )
}
}
]
2023-01-07 02:52:59 +01:00
} )
2020-03-24 14:22:29 +01:00
] ,
resolve : {
alias : {
2023-04-12 15:39:28 +02:00
vue$ : 'vue/dist/vue.runtime.esm.js' ,
// video.js's mpd-parser uses @xmldom/xmldom so that it can support both node and web browsers
// As FreeTube only runs in electron and web browsers, we can use the native DOMParser class, instead of the "polyfill"
// https://caniuse.com/mdn-api_domparser
'@xmldom/xmldom$' : path . resolve ( _ _dirname , '_domParser.js' )
2020-03-24 14:22:29 +01:00
} ,
2022-10-06 16:47:47 +02:00
fallback : {
2023-04-12 15:39:28 +02:00
'fs/promises' : path . resolve ( _ _dirname , '_empty.js' ) ,
2022-10-06 16:47:47 +02:00
path : require . resolve ( 'path-browserify' ) ,
} ,
2022-11-09 13:34:40 +01:00
extensions : [ '.js' , '.vue' ]
2020-03-24 14:22:29 +01:00
} ,
target : 'web' ,
}
2022-10-18 14:50:32 +02:00
const processLocalesPlugin = new ProcessLocalesPlugin ( {
compress : false ,
inputDir : path . join ( _ _dirname , '../static/locales' ) ,
outputDir : 'static/locales' ,
} )
config . plugins . push (
processLocalesPlugin ,
new webpack . DefinePlugin ( {
2022-10-22 10:31:34 +02:00
'process.env.LOCALE_NAMES' : JSON . stringify ( processLocalesPlugin . localeNames ) ,
2023-07-01 16:08:09 +02:00
'process.env.GEOLOCATION_NAMES' : JSON . stringify ( fs . readdirSync ( path . join ( _ _dirname , '..' , 'static' , 'geolocations' ) ) . map ( filename => filename . replace ( '.json' , '' ) ) )
2022-10-18 14:50:32 +02:00
} ) ,
new CopyWebpackPlugin ( {
patterns : [
{
from : path . join ( _ _dirname , '../static/pwabuilder-sw.js' ) ,
to : path . join ( _ _dirname , '../dist/web/pwabuilder-sw.js' ) ,
} ,
{
from : path . join ( _ _dirname , '../static' ) ,
to : path . join ( _ _dirname , '../dist/web/static' ) ,
globOptions : {
dot : true ,
ignore : [ '**/.*' , '**/locales/**' , '**/pwabuilder-sw.js' , '**/dashFiles/**' , '**/storyboards/**' ] ,
2020-05-23 23:29:42 +02:00
} ,
2022-10-18 14:50:32 +02:00
} ,
]
} )
)
2020-03-24 14:22:29 +01:00
module . exports = config