ampcast/webpack.config.js

189 lines
7.1 KiB
JavaScript
Raw Normal View History

2021-07-20 09:29:42 +08:00
const {resolve} = require('path');
const webpack = require('webpack');
2024-04-12 08:14:21 +08:00
const dotenv = require('dotenv');
2021-07-20 09:29:42 +08:00
const ESLintPlugin = require('eslint-webpack-plugin');
2021-07-19 08:47:37 +08:00
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
2023-11-15 08:11:50 +08:00
const CopyPlugin = require('copy-webpack-plugin');
2021-07-20 09:29:42 +08:00
const packageJson = require('./package.json');
2021-07-19 08:47:37 +08:00
2024-04-12 08:14:21 +08:00
module.exports = (args) => {
const {mode = 'production', target = 'pwa'} = args;
2021-07-20 09:29:42 +08:00
const __dev__ = mode === 'development';
2024-04-17 06:56:57 +08:00
const wwwDir = resolve(__dirname, __dev__ ? 'www-dev' : 'app/www');
2024-05-03 00:45:03 +08:00
// Use a local `.env` file (if it exists) associated with the target environment.
2024-06-16 00:50:55 +08:00
dotenv.config({path: __dev__ || mode === 'docker' ? './.env' : `./.env.${target}`});
2024-04-17 06:56:57 +08:00
const env = process.env;
2021-07-19 08:47:37 +08:00
2021-07-20 09:29:42 +08:00
return {
2024-04-12 08:14:21 +08:00
mode,
2022-12-29 03:21:10 +08:00
entry: {
2024-09-18 20:43:04 +08:00
'lib/music-metadata': 'music-metadata',
'lib/tidal-player': '@tidal-music/player',
2022-12-29 16:54:35 +08:00
'lib/unidecode': 'unidecode',
2022-12-29 03:21:10 +08:00
'lib/vendors': [
2023-12-18 01:31:50 +08:00
'colorjs.io',
'colorthief',
2023-08-10 22:04:04 +08:00
'd3-array',
'd3-interpolate',
'd3-scale',
2022-12-29 03:21:10 +08:00
'detect-browser',
2022-12-29 16:54:35 +08:00
'dexie',
2022-12-29 03:21:10 +08:00
'fullscreen-api-polyfill',
2023-08-10 22:04:04 +08:00
'jsfft',
2024-06-22 23:22:48 +08:00
'idb-keyval',
'is-electron',
2022-12-29 03:21:10 +08:00
'md5',
2022-12-29 16:54:35 +08:00
'react',
'react-dom',
2022-12-29 03:21:10 +08:00
'react-error-boundary',
'spotify-web-api-js',
2023-08-10 22:04:04 +08:00
'string-score',
2022-12-29 03:21:10 +08:00
'youtube-player',
2024-08-01 01:43:12 +08:00
// These are always included in `bundle.js`. Tree-shaking?
2024-06-22 23:22:48 +08:00
// '@ctrl/tinycolor',
// 'rxjs',
2022-12-29 03:21:10 +08:00
],
bundle: {
import: './src/index.tsx',
2024-06-22 23:22:48 +08:00
dependOn: ['lib/unidecode', 'lib/vendors'],
2022-12-29 03:21:10 +08:00
},
2024-09-21 08:43:10 +08:00
'lib/media-services': {
import: './src/services/mediaServices/all.ts',
dependOn: ['bundle'],
2024-08-01 01:43:12 +08:00
},
'lib/visualizers': {
import: './src/services/visualizer/visualizers.ts',
2024-09-21 08:43:10 +08:00
dependOn: ['bundle'],
2024-08-01 01:43:12 +08:00
},
2022-12-29 03:21:10 +08:00
},
2021-07-20 09:29:42 +08:00
output: {
2022-12-29 03:21:10 +08:00
chunkFilename: 'lib/[name].js',
2024-04-17 06:56:57 +08:00
path: `${wwwDir}/v${packageJson.version}`,
2021-07-20 09:29:42 +08:00
},
2022-12-29 03:21:10 +08:00
optimization: {
runtimeChunk: 'single',
2024-08-01 01:43:12 +08:00
splitChunks: {
cacheGroups: {
2024-09-18 20:43:04 +08:00
// Concatenate CSS output into one file.
// The order is dependent on the order of the JS imports above.
2024-08-01 01:43:12 +08:00
styles: {
name: 'styles',
type: 'css/mini-extract',
chunks: 'all',
enforce: true,
},
2024-09-18 20:43:04 +08:00
// Prevent imported libraries from splitting.
'lib/music-metadata': {
name: 'lib/music-metadata',
test: /[\\/]node_modules[\\/](music\-metadata)[\\/]/,
chunks: 'all',
enforce: true,
},
'lib/tidal-player': {
name: 'lib/tidal-player',
test: /[\\/]node_modules[\\/](@tidal\-music[\\/]player)[\\/]/,
chunks: 'all',
enforce: true,
},
2024-08-01 01:43:12 +08:00
},
},
2022-12-29 03:21:10 +08:00
},
2021-07-20 09:29:42 +08:00
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.s?css$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
},
{
2024-10-25 05:08:42 +08:00
loader: 'postcss-loader',
2021-07-20 09:29:42 +08:00
},
2022-11-20 23:57:10 +08:00
{
2024-10-25 05:08:42 +08:00
loader: 'sass-loader',
2022-11-20 23:57:10 +08:00
},
2021-07-20 09:29:42 +08:00
],
},
2022-08-27 02:39:31 +08:00
{
test: /\.frag$/,
type: 'asset/source',
},
2021-07-20 09:29:42 +08:00
],
},
plugins: [
new ESLintPlugin({
extensions: ['.tsx', '.ts'],
}),
new MiniCssExtractPlugin({
2024-08-01 01:43:12 +08:00
filename: 'bundle.css',
2021-07-20 09:29:42 +08:00
}),
new webpack.DefinePlugin({
__dev__,
2024-04-17 06:56:57 +08:00
__app_name__: JSON.stringify(packageJson.name || ''),
__app_version__: JSON.stringify(packageJson.version || ''),
__app_contact__: JSON.stringify(packageJson.author.email || ''),
__am_dev_token__: JSON.stringify(env.AM_DEV_TOKEN || ''),
__lf_api_key__: JSON.stringify(env.LF_API_KEY || ''),
__lf_api_secret__: JSON.stringify(env.LF_API_SECRET || ''),
__sp_client_id__: JSON.stringify(env.SP_CLIENT_ID || ''),
2024-09-18 20:43:04 +08:00
__td_client_id__: JSON.stringify(env.TD_CLIENT_ID || ''),
2024-04-17 06:56:57 +08:00
__yt_client_id__: JSON.stringify(env.YT_CLIENT_ID || ''),
2024-04-12 08:14:21 +08:00
__spotify_disabled__: env.SPOTIFY_DISABLED === 'true',
2024-09-18 20:43:04 +08:00
__tidal_disabled__: true, // env.TIDAL_DISABLED === 'true',
2024-04-12 08:14:21 +08:00
__youtube_disabled__: env.YOUTUBE_DISABLED === 'true',
__single_streaming_service__: env.SINGLE_STREAMING_SERVICE === 'true',
2021-07-20 09:29:42 +08:00
}),
2023-11-15 08:11:50 +08:00
new CopyPlugin({
patterns: [
{
from: './src/html/index.html',
to: wwwDir,
transform(content) {
2024-05-03 00:45:03 +08:00
return String(content).replace(/%version%/g, `v${packageJson.version}`);
2023-11-15 08:11:50 +08:00
},
},
],
2023-07-06 05:02:45 +08:00
}),
2024-04-17 06:56:57 +08:00
__dev__
2023-11-15 08:11:50 +08:00
? undefined
: new CopyPlugin({
patterns: [
{
from: './src/service-worker.js',
to: wwwDir,
transform(content) {
return String(content)
.replace('%appName%', packageJson.name)
2024-04-12 08:14:21 +08:00
.replace('%appVersion%', packageJson.version)
.replace('%timeStamp%', Date.now());
2023-11-15 08:11:50 +08:00
},
},
],
}),
,
2021-07-19 08:47:37 +08:00
],
2021-07-20 09:29:42 +08:00
resolve: {
alias: {
assets: resolve(__dirname, 'src/assets/'),
components: resolve(__dirname, 'src/components/'),
hooks: resolve(__dirname, 'src/hooks/'),
services: resolve(__dirname, 'src/services/'),
styles: resolve(__dirname, 'src/styles/'),
types: resolve(__dirname, 'src/types/'),
utils: resolve(__dirname, 'src/utils/'),
2023-07-06 05:02:45 +08:00
libs: resolve(__dirname, 'libs/'),
2021-07-20 09:29:42 +08:00
},
extensions: ['.tsx', '.ts', '.js'],
},
stats: {
builtAt: true,
},
};
};