ampcast/webpack.config.js

168 lines
6.0 KiB
JavaScript
Raw Normal View History

2021-07-20 09:29:42 +08:00
const {resolve} = require('path');
const webpack = require('webpack');
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');
2022-11-13 09:48:34 +08:00
const devKeys = require('../credentials/keys-dev.json');
const prodKeys = require('../credentials/keys-prod.json');
2024-02-09 01:54:31 +08:00
const electronKeys = require('../credentials/keys-electron.json');
2021-07-20 09:29:42 +08:00
const packageJson = require('./package.json');
2021-07-19 08:47:37 +08:00
2021-07-20 09:29:42 +08:00
module.exports = (env) => {
2024-02-09 01:54:31 +08:00
const {mode = 'production', target = 'web'} = env;
2021-07-20 09:29:42 +08:00
const __dev__ = mode === 'development';
2024-02-09 01:54:31 +08:00
const __electron__ = target === 'electron';
2023-08-10 22:04:04 +08:00
const wwwDir = resolve(__dirname, __dev__ ? 'www-dev' : __electron__ ? 'www-electron' : 'www');
2024-02-09 01:54:31 +08:00
const keys = __dev__ ? devKeys : __electron__ ? electronKeys : prodKeys;
2021-07-19 08:47:37 +08:00
2021-07-20 09:29:42 +08:00
return {
2022-12-29 03:21:10 +08:00
entry: {
2022-12-29 16:54:35 +08:00
'lib/butterchurn': 'butterchurn',
'lib/unidecode': 'unidecode',
2022-12-29 03:21:10 +08:00
'lib/vendors': [
2023-08-10 22:04:04 +08:00
'@ctrl/tinycolor',
2022-12-29 03:21:10 +08:00
'audiomotion-analyzer',
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',
2023-07-06 05:02:45 +08:00
'libs/dialog-polyfill',
2022-12-29 03:21:10 +08:00
'fullscreen-api-polyfill',
2023-08-10 22:04:04 +08:00
'jsfft',
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',
],
bundle: {
import: './src/index.tsx',
dependOn: ['lib/butterchurn', 'lib/unidecode', 'lib/vendors'],
},
},
2021-07-20 09:29:42 +08:00
output: {
2022-12-29 03:21:10 +08:00
chunkFilename: 'lib/[name].js',
2023-11-15 08:11:50 +08:00
path: __electron__ ? wwwDir : `${wwwDir}/v${packageJson.version}`,
2021-07-20 09:29:42 +08:00
},
2022-12-29 03:21:10 +08:00
optimization: {
runtimeChunk: 'single',
},
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',
},
{
loader: 'sass-loader',
},
2022-11-20 23:57:10 +08:00
{
loader: 'postcss-loader',
},
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({
filename: 'bundle.css',
}),
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
}),
new webpack.ProvidePlugin({
process: 'process/browser',
}),
new webpack.DefinePlugin({
__dev__,
__app_name__: JSON.stringify(packageJson.name),
__app_version__: JSON.stringify(packageJson.version),
2022-10-25 07:11:17 +08:00
__app_contact__: JSON.stringify(packageJson.author.email),
2022-11-13 09:48:34 +08:00
__am_dev_token__: JSON.stringify(keys.am_dev_token),
__lf_api_key__: JSON.stringify(keys.lf_api_key),
__lf_api_secret__: JSON.stringify(keys.lf_api_secret),
__sp_client_id__: JSON.stringify(keys.sp_client_id),
__yt_api_key__: JSON.stringify(keys.yt_api_key),
__yt_client_id__: JSON.stringify(keys.yt_client_id),
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) {
return String(content).replace(
/%version%/g,
__electron__ ? '' : `/v${packageJson.version}`
);
},
},
],
2023-07-06 05:02:45 +08:00
}),
2023-11-15 08:11:50 +08:00
__dev__ || __electron__
? undefined
: new CopyPlugin({
patterns: [
{
from: './src/html/privacy-policy.html',
to: wwwDir,
},
],
}),
__dev__ || __electron__
? undefined
: new CopyPlugin({
patterns: [
{
from: './src/service-worker.js',
to: wwwDir,
transform(content) {
return String(content)
.replace('%appName%', packageJson.name)
.replace('%appVersion%', packageJson.version);
},
},
],
}),
,
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,
},
mode,
};
};