pixelplanet/webpack.config.client.js

233 lines
5.7 KiB
JavaScript
Raw Normal View History

/**
* webpack config for client files
*/
const fs = require('fs');
const path = require('path');
const process = require('process');
const webpack = require('webpack');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
2022-09-18 00:23:43 +00:00
/*
* make sure we build in root dir
*/
process.chdir(__dirname);
function buildWebpackClientConfig(
development,
analyze,
locale,
extract,
) {
2021-01-29 01:15:33 +00:00
const ttag = {
resolve: {
translations: (locale !== 'default')
? path.resolve('i18n', `${locale}.po`)
2021-01-29 01:15:33 +00:00
: locale,
},
};
if (extract) {
ttag.extract = {
output: path.resolve('i18n', 'template.pot'),
};
}
2021-01-29 01:15:33 +00:00
const babelPlugins = [
['ttag', ttag],
];
return {
name: 'client',
target: 'web',
mode: (development) ? 'development' : 'production',
2022-11-10 13:11:46 +00:00
devtool: (development) ? 'inline-source-map' : false,
2021-01-29 01:15:33 +00:00
entry: {
client:
[path.resolve('src', 'client.js')],
globe:
[path.resolve('src', 'globe.js')],
popup:
[path.resolve('src', 'popup.js')],
2021-01-29 01:15:33 +00:00
},
output: {
path: path.resolve('dist', 'public', 'assets'),
publicPath: '/assets/',
// chunkReason is set if it is a split chunk like vendor or three
filename: (pathData) => (pathData.chunk.chunkReason)
? '[name].[chunkhash:8].js'
: `[name].${locale}.[chunkhash:8].js`,
chunkFilename: `[name].${locale}.[chunkhash:8].js`,
clean: true,
2021-01-29 01:15:33 +00:00
},
resolve: {
alias: {
/*
* have to mock it, because we don't ship ttag itself with the client,
* we have a script for every language
*/
2021-01-29 01:15:33 +00:00
ttag: 'ttag/dist/mock',
/*
* if we don't do that,we might load different versions of three
*/
three: path.resolve('node_modules', 'three'),
2021-01-29 01:15:33 +00:00
},
2022-01-02 20:50:42 +00:00
extensions: ['.js', '.jsx'],
2021-01-29 01:15:33 +00:00
},
module: {
rules: [
{
test: /\.svg$/,
use: [
'babel-loader',
2021-01-29 01:15:33 +00:00
{
loader: 'react-svg-loader',
options: {
svgo: {
plugins: [
{
removeViewBox: false,
},
{
removeDimensions: true,
},
],
},
jsx: false,
},
},
2021-01-29 01:15:33 +00:00
],
},
{
2022-01-01 00:39:01 +00:00
test: /\.(js|jsx)$/,
use: [
{
loader: 'babel-loader',
options: {
plugins: babelPlugins,
},
},
path.resolve('scripts/TtagNonCacheableLoader.js'),
],
2021-01-29 01:15:33 +00:00
include: [
path.resolve('src'),
2022-01-02 20:50:42 +00:00
...['image-q'].map((moduleName) => (
2022-08-16 14:32:59 +00:00
path.resolve('node_modules', moduleName)
)),
2021-01-29 01:15:33 +00:00
],
},
],
},
plugins: [
// Define free variables
// https://webpack.github.io/docs/list-of-plugins.html#defineplugin
new webpack.DefinePlugin({
'process.env.NODE_ENV': development ? '"development"' : '"production"',
'process.env.BROWSER': true,
}),
// Webpack Bundle Analyzer
// https://github.com/th0r/webpack-bundle-analyzer
...analyze ? [new BundleAnalyzerPlugin({ analyzerPort: 8889 })] : [],
],
2021-01-29 01:15:33 +00:00
optimization: {
splitChunks: {
chunks: 'all',
name: false,
cacheGroups: {
default: false,
defaultVendors: false,
/*
* this layout of chunks is also assumed in src/core/assets.js
* client -> client.js + vendor.js
* globe -> globe.js + three.js
*/
2021-01-29 01:15:33 +00:00
vendor: {
name: 'vendor',
chunks: (chunk) => chunk.name.startsWith('client'),
test: /[\\/]node_modules[\\/]/,
},
three: {
name: 'three',
chunks: 'all',
test: /[\\/]node_modules[\\/]three[\\/]/,
},
},
},
},
recordsPath: path.resolve('records.json'),
2021-01-29 21:46:58 +00:00
stats: {
colors: true,
reasons: false,
hash: false,
version: false,
chunkModules: false,
},
2022-01-05 19:40:09 +00:00
cache: (extract) ? false
: {
2022-01-05 19:40:09 +00:00
type: 'filesystem',
},
2021-01-29 01:15:33 +00:00
};
}
function getAllAvailableLocals() {
// return ['default', 'de', 'tr'];
const langDir = path.resolve('i18n');
2021-01-29 21:46:58 +00:00
const langs = fs.readdirSync(langDir)
.filter((e) => (e.endsWith('.po') && !e.startsWith('ssr')))
.map((l) => l.slice(0, -3));
langs.unshift('default');
return langs;
}
2021-01-29 21:46:58 +00:00
/*
* return array of webpack configuartions for all languages
*/
function buildWebpackClientConfigAllLangs() {
const langs = getAllAvailableLocals();
const webpackConfigClient = [];
for (let l = 0; l < langs.length; l += 1) {
const lang = langs[l];
const cfg = buildWebpackClientConfig(false, false, lang, false);
webpackConfigClient.push(cfg);
}
2021-01-29 21:46:58 +00:00
return webpackConfigClient;
}
/*
* Per default get configuration of all packages
* If any argument is given, it will only get one
* ('default' aka english if locale is unset)
*
* @param development If development mode
* @param extract if translatable strings get in i18n templates should
* get updated
* @param locale language get single configuration of specific locale
* @param analyze launch BundleAnalyzerPlugin after build
* @return webpack configuration
*/
module.exports = ({
2022-01-01 00:39:01 +00:00
development, analyze, extract, locale,
}) => {
if (extract || analyze || locale || development) {
return buildWebpackClientConfig(
2022-01-01 00:39:01 +00:00
development, analyze, locale || 'default', extract,
);
}
return buildWebpackClientConfigAllLangs(development);
};
module.exports.buildWebpackClientConfig = buildWebpackClientConfig;
module.exports.getAllAvailableLocals = getAllAvailableLocals;