jellyfin-web/webpack.common.js

229 lines
7.5 KiB
JavaScript
Raw Normal View History

2020-05-04 10:44:12 +00:00
const path = require('path');
2020-08-15 10:44:52 +00:00
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
2020-08-16 18:24:45 +00:00
const CopyPlugin = require('copy-webpack-plugin');
2020-11-20 04:36:35 +00:00
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
2021-10-05 04:26:00 +00:00
const { DefinePlugin } = require('webpack');
2020-05-29 21:32:45 +00:00
2020-11-21 03:07:37 +00:00
const Assets = [
'native-promise-only/npo.js',
'libarchive.js/dist/worker-bundle.js',
'@jellyfin/libass-wasm/dist/js/subtitles-octopus-worker.js',
'@jellyfin/libass-wasm/dist/js/subtitles-octopus-worker.data',
'@jellyfin/libass-wasm/dist/js/subtitles-octopus-worker.wasm',
'@jellyfin/libass-wasm/dist/js/subtitles-octopus-worker-legacy.js',
'@jellyfin/libass-wasm/dist/js/subtitles-octopus-worker-legacy.data',
'@jellyfin/libass-wasm/dist/js/subtitles-octopus-worker-legacy.js.mem',
2020-11-21 03:07:37 +00:00
'pdfjs-dist/build/pdf.worker.js'
];
const LibarchiveWasm = [
'libarchive.js/dist/wasm-gen/libarchive.js',
'libarchive.js/dist/wasm-gen/libarchive.wasm'
];
const DEV_MODE = process.env.NODE_ENV !== 'production';
const NODE_MODULES_REGEX = /[\\/]node_modules[\\/]/;
const config = {
2020-05-04 10:44:12 +00:00
context: path.resolve(__dirname, 'src'),
2020-11-27 16:17:04 +00:00
target: 'browserslist',
resolve: {
extensions: ['.tsx', '.ts', '.js'],
modules: [
2020-05-04 10:44:12 +00:00
path.resolve(__dirname, 'node_modules')
]
2019-05-25 04:28:06 +00:00
},
2019-09-30 23:58:05 +00:00
plugins: [
2021-10-05 04:26:00 +00:00
new DefinePlugin({
__WEBPACK_SERVE__: JSON.stringify(!!process.env.WEBPACK_SERVE)
}),
2020-08-16 18:24:45 +00:00
new CleanWebpackPlugin(),
2020-11-20 04:36:35 +00:00
new HtmlWebpackPlugin({
filename: 'index.html',
2021-07-14 19:33:30 +00:00
template: 'index.html',
// Append file hashes to bundle urls for cache busting
hash: true
2020-11-20 04:36:35 +00:00
}),
2020-08-16 18:24:45 +00:00
new CopyPlugin({
patterns: [
{
from: 'themes/',
to: 'themes/'
2020-10-22 00:10:40 +00:00
},
{
from: 'assets/**',
globOptions: {
2021-03-06 09:52:49 +00:00
dot: true,
2020-10-22 00:10:40 +00:00
ignore: ['**/css/*']
}
},
{
from: '*.*',
2020-10-22 00:10:40 +00:00
globOptions: {
2021-03-06 09:52:49 +00:00
dot: true,
2020-10-22 00:10:40 +00:00
ignore: ['**.js', '**.html']
}
2020-08-16 18:24:45 +00:00
}
]
2020-11-09 19:20:55 +00:00
}),
2020-11-21 03:07:37 +00:00
new CopyPlugin({
patterns: Assets.map(asset => {
return {
from: path.resolve(__dirname, `./node_modules/${asset}`),
to: path.resolve(__dirname, './dist/libraries')
};
})
}),
new CopyPlugin({
patterns: LibarchiveWasm.map(asset => {
return {
from: path.resolve(__dirname, `./node_modules/${asset}`),
to: path.resolve(__dirname, './dist/libraries/wasm-gen')
};
})
2020-08-16 18:24:45 +00:00
})
2020-08-15 10:44:52 +00:00
],
output: {
2022-09-06 05:45:59 +00:00
filename: '[name].bundle.js',
2021-07-14 19:33:30 +00:00
chunkFilename: '[name].[contenthash].chunk.js',
2021-02-27 09:59:29 +00:00
path: path.resolve(__dirname, 'dist'),
publicPath: ''
2020-10-18 14:29:40 +00:00
},
2022-09-06 05:45:59 +00:00
optimization: {
runtimeChunk: 'single',
splitChunks: {
chunks: 'all',
maxInitialRequests: Infinity,
cacheGroups: {
node_modules: {
test(module) {
return NODE_MODULES_REGEX.test(module.context);
},
2022-09-06 05:45:59 +00:00
name(module) {
// get the name. E.g. node_modules/packageName/not/this/part.js
// or node_modules/packageName
const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
// if "packageName" is a namespace (i.e. @jellyfin) get the namespace + packageName
if (packageName.startsWith('@')) {
const parts = module.context
.substring(module.context.lastIndexOf(packageName))
.split('/');
return `node_modules.${parts[0]}.${parts[1]}`;
}
2022-09-06 05:45:59 +00:00
return `node_modules.${packageName}`;
}
}
}
}
},
2020-10-18 14:29:40 +00:00
module: {
rules: [
2020-10-18 19:37:54 +00:00
{
test: /\.(html)$/,
use: {
loader: 'html-loader'
}
},
{
test: /\.(js|jsx)$/,
2022-10-12 16:18:11 +00:00
exclude: /node_modules[\\/](?!@uupaa[\\/]dynamic-import-polyfill|@remix-run[\\/]router|blurhash|date-fns|dom7|epubjs|flv.js|libarchive.js|marked|react-router|screenfull|ssr-window|swiper)/,
2020-10-18 19:37:54 +00:00
use: [{
2022-09-06 06:18:19 +00:00
loader: 'babel-loader',
options: {
cacheCompression: false,
cacheDirectory: true
}
2020-10-18 19:37:54 +00:00
}]
},
{
test: /\.worker\.ts$/,
exclude: /node_modules/,
use: [
'worker-loader',
'ts-loader'
]
},
{
test: /\.(ts|tsx)$/,
exclude: /node_modules/,
use: [{
loader: 'ts-loader'
}]
},
/* modules that Babel breaks when transforming to ESM */
{
test: /node_modules[\\/](pdfjs-dist|xmldom)[\\/].*\.js$/,
use: [{
loader: 'babel-loader',
options: {
2022-09-06 06:18:19 +00:00
cacheCompression: false,
cacheDirectory: true,
plugins: [
'@babel/transform-modules-umd'
]
}
}]
},
{
test: /\.s[ac]ss$/i,
use: [
DEV_MODE ? 'style-loader' : MiniCssExtractPlugin.loader,
'css-loader',
{
loader: 'postcss-loader',
options: {
2021-06-15 05:05:47 +00:00
postcssOptions: {
config: path.resolve(__dirname, 'postcss.config.js')
}
}
},
'sass-loader'
]
},
2020-10-18 19:37:54 +00:00
{
test: /\.css$/i,
use: [
DEV_MODE ? 'style-loader' : MiniCssExtractPlugin.loader,
2020-10-18 19:37:54 +00:00
'css-loader',
{
loader: 'postcss-loader',
options: {
2021-06-15 05:05:47 +00:00
postcssOptions: {
config: path.resolve(__dirname, 'postcss.config.js')
2020-10-18 19:37:54 +00:00
}
}
}
]
},
{
test: /\.(png|jpg|gif|svg)$/i,
2021-10-23 05:24:14 +00:00
type: 'asset/resource'
2020-10-18 19:37:54 +00:00
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
2021-10-23 05:24:14 +00:00
type: 'asset/resource'
2020-10-18 19:37:54 +00:00
},
{
test: /\.(mp3)$/i,
2021-10-23 05:24:14 +00:00
type: 'asset/resource'
2020-10-18 19:37:54 +00:00
},
2020-10-18 14:29:40 +00:00
{
test: require.resolve('jquery'),
loader: 'expose-loader',
options: {
exposes: ['$', 'jQuery']
}
}
]
2020-08-15 10:44:52 +00:00
}
};
if (!DEV_MODE) {
config.plugins.push(new MiniCssExtractPlugin());
}
module.exports = config;