lbry-desktop/web/webpack.config.js

185 lines
5 KiB
JavaScript
Raw Permalink Normal View History

2021-07-28 22:10:19 +02:00
const { WEBPACK_WEB_PORT, LBRY_WEB_API, BRANDED_SITE } = require('../config.js');
2019-03-05 05:46:57 +01:00
const path = require('path');
const fs = require('fs');
2019-03-05 05:46:57 +01:00
const merge = require('webpack-merge');
2019-11-07 20:39:22 +01:00
const baseConfig = require('../webpack.base.config.js');
const serviceWorkerConfig = require('./webpack.sw.config.js');
2019-03-05 05:46:57 +01:00
const CopyWebpackPlugin = require('copy-webpack-plugin');
2020-08-19 23:09:30 +02:00
const WriteFilePlugin = require('write-file-webpack-plugin');
2019-09-26 17:06:01 +02:00
const { DefinePlugin, ProvidePlugin } = require('webpack');
2020-02-05 04:46:00 +01:00
const SentryWebpackPlugin = require('@sentry/webpack-plugin');
2020-08-19 23:09:30 +02:00
const { getJsBundleId } = require('./bundle-id.js');
2021-06-02 23:03:09 +02:00
const { insertToHead, buildHead } = require('./src/html');
2020-08-07 18:17:29 +02:00
const { insertVariableXml, getOpenSearchXml } = require('./src/xml');
2019-03-05 05:46:57 +01:00
const CUSTOM_ROOT = path.resolve(__dirname, '../custom/');
2019-11-07 20:39:22 +01:00
const STATIC_ROOT = path.resolve(__dirname, '../static/');
2019-11-11 16:22:57 +01:00
const UI_ROOT = path.resolve(__dirname, '../ui/');
2019-03-05 05:46:57 +01:00
const DIST_ROOT = path.resolve(__dirname, 'dist/');
2021-06-04 20:58:32 +02:00
const WEB_STATIC_ROOT = path.resolve(__dirname, 'static/');
2019-11-07 20:39:22 +01:00
const WEB_PLATFORM_ROOT = __dirname;
2020-02-05 04:46:00 +01:00
const isProduction = process.env.NODE_ENV === 'production';
const hasSentryToken = process.env.SENTRY_AUTH_TOKEN !== undefined;
2020-08-19 23:09:30 +02:00
const jsBundleId = getJsBundleId();
2020-02-05 04:46:00 +01:00
2021-05-30 14:33:42 +02:00
// copy static files to dist folder
const copyWebpackCommands = [
{
from: `${STATIC_ROOT}/index-web.html`,
to: `${DIST_ROOT}/index.html`,
2021-05-30 14:33:42 +02:00
// add javascript script to index.html, generate/insert metatags
2020-07-08 19:02:18 +02:00
transform(content, path) {
2021-06-02 23:03:09 +02:00
return insertToHead(content.toString(), buildHead());
2020-07-08 19:02:18 +02:00
},
2020-08-19 23:09:30 +02:00
force: true,
},
2020-08-07 18:17:29 +02:00
{
from: `${STATIC_ROOT}/opensearch.xml`,
to: `${DIST_ROOT}/opensearch.xml`,
transform(content, path) {
return insertVariableXml(content.toString(), getOpenSearchXml());
},
2020-08-19 23:09:30 +02:00
force: true,
2020-08-07 18:17:29 +02:00
},
2020-10-07 03:59:53 +02:00
{
from: `${STATIC_ROOT}/robots.txt`,
to: `${DIST_ROOT}/robots.txt`,
force: true,
},
{
from: `${STATIC_ROOT}/img/favicon.png`,
to: `${DIST_ROOT}/public/favicon.png`,
2020-08-19 23:09:30 +02:00
force: true,
},
{
from: `${STATIC_ROOT}/img/favicon-spaceman.png`,
to: `${DIST_ROOT}/public/favicon-spaceman.png`,
force: true,
},
{
from: `${STATIC_ROOT}/img/v2-og.png`,
to: `${DIST_ROOT}/public/v2-og.png`,
},
{
from: `${STATIC_ROOT}/font/`,
to: `${DIST_ROOT}/public/font/`,
},
2021-05-25 19:40:48 +02:00
{
2021-06-04 20:58:32 +02:00
from: `${WEB_STATIC_ROOT}/pwa/`,
2021-05-25 19:40:48 +02:00
to: `${DIST_ROOT}/public/pwa/`,
},
];
const CUSTOM_OG_PATH = `${CUSTOM_ROOT}/v2-og.png`;
if (fs.existsSync(CUSTOM_OG_PATH)) {
copyWebpackCommands.push({
from: CUSTOM_OG_PATH,
to: `${DIST_ROOT}/public/v2-og.png`,
2020-08-04 00:25:19 +02:00
force: true,
});
}
// clear the dist folder of existing js files before compilation
let regex = /^.*\.(json|js|map)$/;
// only run on nonprod environments to avoid side effects on prod
if (!isProduction) {
const path = `${DIST_ROOT}/public/`;
if (fs.existsSync(path)) {
fs.readdirSync(path)
.filter((f) => regex.test(f))
.map((f) => fs.unlinkSync(path + f));
}
}
const ROBOTS_TXT_PATH = `${CUSTOM_ROOT}/robots.txt`;
if (fs.existsSync(ROBOTS_TXT_PATH)) {
copyWebpackCommands.push({
from: ROBOTS_TXT_PATH,
to: `${DIST_ROOT}/robots.txt`,
force: true,
});
}
2020-02-05 04:46:00 +01:00
let plugins = [
2020-08-19 23:09:30 +02:00
new WriteFilePlugin(),
new CopyWebpackPlugin(copyWebpackCommands),
2020-02-05 04:46:00 +01:00
new DefinePlugin({
IS_WEB: JSON.stringify(true),
'process.env.SDK_API_URL': JSON.stringify(process.env.SDK_API_URL || LBRY_WEB_API),
2020-02-05 04:46:00 +01:00
}),
new ProvidePlugin({
__: ['i18n.js', '__'],
}),
];
if (isProduction && hasSentryToken) {
plugins.push(
new SentryWebpackPlugin({
include: './dist',
ignoreFile: '.sentrycliignore',
ignore: ['node_modules', 'webpack.config.js'],
configFile: 'sentry.properties',
})
);
}
2019-03-05 05:46:57 +01:00
const webConfig = {
target: 'web',
entry: {
2020-08-19 23:09:30 +02:00
[`ui-${jsBundleId}`]: '../ui/index.jsx',
2019-03-05 05:46:57 +01:00
},
output: {
2019-03-05 18:54:11 +01:00
filename: '[name].js',
2020-02-05 04:46:00 +01:00
path: path.join(__dirname, 'dist/public/'),
publicPath: '/public/',
2021-06-11 08:06:29 +02:00
chunkFilename: '[name]-[chunkhash].js',
2019-03-05 05:46:57 +01:00
},
devServer: {
port: WEBPACK_WEB_PORT,
2019-12-13 22:46:28 +01:00
contentBase: path.join(__dirname, 'dist'),
2021-05-25 19:40:48 +02:00
disableHostCheck: true, // to allow debugging with ngrok
},
2019-03-05 05:46:57 +01:00
module: {
rules: [
{
2019-11-07 20:39:22 +01:00
loader: 'babel-loader',
test: /\.jsx?$/,
options: {
2019-11-21 22:48:19 +01:00
presets: ['@babel/env', '@babel/react', '@babel/flow'],
plugins: [
'@babel/plugin-proposal-optional-chaining',
'@babel/plugin-proposal-object-rest-spread',
'@babel/plugin-proposal-class-properties',
],
2019-11-07 20:39:22 +01:00
},
},
{
loader: 'preprocess-loader',
test: /\.jsx?$/,
exclude: /node_modules/,
options: {
TARGET: 'web',
2021-07-28 22:10:19 +02:00
BRANDED_SITE: BRANDED_SITE,
2019-11-07 20:39:22 +01:00
ppOptions: {
type: 'js',
2019-03-05 05:46:57 +01:00
},
2019-11-07 20:39:22 +01:00
},
2019-03-05 05:46:57 +01:00
},
],
},
resolve: {
2019-11-11 16:22:57 +01:00
modules: [UI_ROOT, __dirname],
2019-03-05 05:46:57 +01:00
alias: {
// lbryinc: '../extras/lbryinc',
$web: WEB_PLATFORM_ROOT,
$ui: UI_ROOT,
2019-11-07 20:39:22 +01:00
electron: `${WEB_PLATFORM_ROOT}/stubs/electron.js`,
fs: `${WEB_PLATFORM_ROOT}/stubs/fs.js`,
2019-03-05 05:46:57 +01:00
},
},
2020-02-05 04:46:00 +01:00
plugins,
2019-03-05 05:46:57 +01:00
};
module.exports = [merge(baseConfig, webConfig), serviceWorkerConfig];