2020-03-27 20:59:38 +00:00
|
|
|
// Note: You must restart bin/webpack-dev-server for changes to take effect
|
|
|
|
|
2022-10-14 20:31:40 +00:00
|
|
|
import fs from 'fs';
|
|
|
|
import { join, resolve } from 'path';
|
2022-01-10 22:25:06 +00:00
|
|
|
|
2022-10-14 20:31:40 +00:00
|
|
|
import CopyPlugin from 'copy-webpack-plugin';
|
|
|
|
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
|
|
|
|
import HtmlWebpackHarddiskPlugin from 'html-webpack-harddisk-plugin';
|
|
|
|
import HtmlWebpackPlugin from 'html-webpack-plugin';
|
|
|
|
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
|
|
|
|
import webpack, { Configuration } from 'webpack';
|
|
|
|
import AssetsManifestPlugin from 'webpack-assets-manifest';
|
|
|
|
import DeadCodePlugin from 'webpack-deadcode-plugin';
|
2022-01-10 22:25:06 +00:00
|
|
|
|
2022-10-14 20:31:40 +00:00
|
|
|
import { env, settings, output } from './configuration';
|
|
|
|
import rules from './rules';
|
2020-03-27 20:59:38 +00:00
|
|
|
|
2022-11-16 13:32:32 +00:00
|
|
|
const { FE_SUBDIRECTORY, FE_INSTANCE_SOURCE_DIR } = require(join(__dirname, '..', 'app', 'soapbox', 'build-config'));
|
2021-09-03 18:49:18 +00:00
|
|
|
|
2022-10-14 20:31:40 +00:00
|
|
|
/** Return file as string, or return empty string. */
|
|
|
|
const readFile = (filename: string) => {
|
2022-04-20 18:58:22 +00:00
|
|
|
try {
|
|
|
|
return fs.readFileSync(filename, 'utf8');
|
|
|
|
} catch {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-10-14 20:31:40 +00:00
|
|
|
const makeHtmlConfig = (params = {}): HtmlWebpackPlugin.Options => {
|
|
|
|
const defaults: HtmlWebpackPlugin.Options = {
|
2021-09-05 21:42:48 +00:00
|
|
|
template: 'app/index.ejs',
|
|
|
|
chunksSortMode: 'manual',
|
|
|
|
chunks: ['common', 'locale_en', 'application', 'styles'],
|
|
|
|
alwaysWriteToDisk: true,
|
|
|
|
minify: {
|
|
|
|
collapseWhitespace: true,
|
|
|
|
removeComments: false,
|
|
|
|
removeRedundantAttributes: true,
|
|
|
|
removeScriptTypeAttributes: true,
|
|
|
|
removeStyleLinkTypeAttributes: true,
|
|
|
|
useShortDoctype: true,
|
|
|
|
},
|
2022-04-20 18:58:22 +00:00
|
|
|
templateParameters: {
|
|
|
|
snippets: readFile(resolve('custom/snippets.html')),
|
|
|
|
},
|
2022-10-14 20:31:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return Object.assign(defaults, params);
|
2021-08-31 00:17:32 +00:00
|
|
|
};
|
|
|
|
|
2022-10-14 20:31:40 +00:00
|
|
|
const configuration: Configuration = {
|
2021-09-11 00:30:47 +00:00
|
|
|
entry: {
|
2022-04-16 00:54:40 +00:00
|
|
|
application: resolve('app/application.ts'),
|
2021-09-11 00:30:47 +00:00
|
|
|
},
|
2020-03-27 20:59:38 +00:00
|
|
|
|
|
|
|
output: {
|
2022-09-30 17:03:10 +00:00
|
|
|
filename: 'packs/js/[name].js',
|
|
|
|
chunkFilename: 'packs/js/[name].chunk.js',
|
|
|
|
hotUpdateChunkFilename: 'packs/js/[id].hot-update.js',
|
2021-07-21 21:43:41 +00:00
|
|
|
path: output.path,
|
2021-09-05 18:21:39 +00:00
|
|
|
publicPath: join(FE_SUBDIRECTORY, '/'),
|
2020-03-27 20:59:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
optimization: {
|
2021-09-05 21:42:48 +00:00
|
|
|
chunkIds: 'total-size',
|
|
|
|
moduleIds: 'size',
|
2020-03-27 20:59:38 +00:00
|
|
|
runtimeChunk: {
|
|
|
|
name: 'common',
|
|
|
|
},
|
|
|
|
splitChunks: {
|
|
|
|
cacheGroups: {
|
|
|
|
default: false,
|
2021-09-05 21:42:48 +00:00
|
|
|
defaultVendors: false,
|
2020-03-27 20:59:38 +00:00
|
|
|
common: {
|
|
|
|
name: 'common',
|
|
|
|
chunks: 'all',
|
|
|
|
minChunks: 2,
|
|
|
|
minSize: 0,
|
|
|
|
test: /^(?!.*[\\\/]node_modules[\\\/]react-intl[\\\/]).+$/,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
module: {
|
2021-09-09 17:00:42 +00:00
|
|
|
rules,
|
2020-03-27 20:59:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
plugins: [
|
|
|
|
new webpack.EnvironmentPlugin(JSON.parse(JSON.stringify(env))),
|
2021-09-05 21:42:48 +00:00
|
|
|
new webpack.ProvidePlugin({
|
|
|
|
process: 'process/browser',
|
|
|
|
}),
|
2022-04-12 17:38:19 +00:00
|
|
|
new ForkTsCheckerWebpackPlugin({ typescript: { memoryLimit: 8192 } }),
|
2020-03-27 20:59:38 +00:00
|
|
|
new MiniCssExtractPlugin({
|
2021-07-21 21:43:41 +00:00
|
|
|
filename: 'packs/css/[name]-[contenthash:8].css',
|
|
|
|
chunkFilename: 'packs/css/[name]-[contenthash:8].chunk.css',
|
2020-03-27 20:59:38 +00:00
|
|
|
}),
|
2022-10-14 20:31:40 +00:00
|
|
|
// @ts-ignore
|
2020-03-27 20:59:38 +00:00
|
|
|
new AssetsManifestPlugin({
|
|
|
|
integrity: false,
|
|
|
|
entrypoints: true,
|
|
|
|
writeToDisk: true,
|
|
|
|
publicPath: true,
|
|
|
|
}),
|
2022-04-14 20:22:37 +00:00
|
|
|
// https://github.com/MQuy/webpack-deadcode-plugin
|
2022-10-14 20:31:40 +00:00
|
|
|
// @ts-ignore
|
2022-04-14 20:22:37 +00:00
|
|
|
new DeadCodePlugin({
|
|
|
|
patterns: [
|
|
|
|
'app/**/*',
|
|
|
|
],
|
|
|
|
exclude: [
|
|
|
|
'**/*.test.*',
|
|
|
|
'**/__*__/*',
|
|
|
|
'**/(LICENSE|README|COPYING)(.md|.txt)?',
|
|
|
|
// This file is imported with @preval
|
2022-11-15 17:23:36 +00:00
|
|
|
'app/soapbox/features/emoji/emoji-map.json',
|
2022-04-14 20:22:37 +00:00
|
|
|
],
|
|
|
|
}),
|
2021-08-31 00:17:32 +00:00
|
|
|
// https://github.com/jantimon/html-webpack-plugin#options
|
2021-09-05 21:42:48 +00:00
|
|
|
new HtmlWebpackPlugin(makeHtmlConfig()),
|
|
|
|
new HtmlWebpackPlugin(makeHtmlConfig({ filename: '404.html' })),
|
2021-07-21 21:13:24 +00:00
|
|
|
new HtmlWebpackHarddiskPlugin(),
|
2021-04-17 18:30:22 +00:00
|
|
|
new CopyPlugin({
|
|
|
|
patterns: [{
|
2021-09-12 15:51:58 +00:00
|
|
|
from: join(__dirname, '../node_modules/twemoji/assets/svg'),
|
|
|
|
to: join(output.path, 'packs/emoji'),
|
|
|
|
}, {
|
2022-03-21 18:09:01 +00:00
|
|
|
from: join(__dirname, '..', 'app', FE_INSTANCE_SOURCE_DIR),
|
2021-09-03 18:06:04 +00:00
|
|
|
to: join(output.path, 'instance'),
|
2022-03-05 20:38:21 +00:00
|
|
|
}, {
|
|
|
|
from: join(__dirname, '../custom/instance'),
|
|
|
|
to: join(output.path, 'instance'),
|
|
|
|
noErrorOnMissing: true,
|
|
|
|
globOptions: {
|
|
|
|
ignore: ['**/.gitkeep'],
|
|
|
|
},
|
2021-04-17 18:30:22 +00:00
|
|
|
}],
|
|
|
|
options: {
|
|
|
|
concurrency: 100,
|
|
|
|
},
|
|
|
|
}),
|
2020-03-27 20:59:38 +00:00
|
|
|
],
|
|
|
|
|
|
|
|
resolve: {
|
|
|
|
extensions: settings.extensions,
|
|
|
|
modules: [
|
2022-05-09 22:57:15 +00:00
|
|
|
resolve('custom', 'modules'),
|
2020-03-27 20:59:38 +00:00
|
|
|
resolve(settings.source_path),
|
|
|
|
'node_modules',
|
|
|
|
],
|
2021-09-20 20:33:28 +00:00
|
|
|
alias: {
|
2022-08-31 09:35:06 +00:00
|
|
|
'classnames': 'clsx',
|
2021-09-20 22:05:59 +00:00
|
|
|
'icons': resolve('app', 'icons'),
|
2022-03-03 22:00:43 +00:00
|
|
|
'custom': resolve('custom'),
|
2021-09-20 20:33:28 +00:00
|
|
|
},
|
2021-09-05 21:42:48 +00:00
|
|
|
fallback: {
|
|
|
|
path: require.resolve('path-browserify'),
|
|
|
|
util: require.resolve('util'),
|
|
|
|
},
|
2020-03-27 20:59:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
resolveLoader: {
|
|
|
|
modules: ['node_modules'],
|
|
|
|
},
|
|
|
|
};
|
2022-10-14 20:31:40 +00:00
|
|
|
|
|
|
|
export default configuration;
|