pixelplanet/webpack.config.client.babel.js

226 lines
5.4 KiB
JavaScript
Raw Normal View History

/**
*/
2021-01-29 21:46:58 +00:00
import fs from 'fs';
import path from 'path';
import webpack from 'webpack';
import AssetsPlugin from 'assets-webpack-plugin';
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
2021-01-29 01:15:33 +00:00
/*
* Emit a file with assets paths
*/
const assetPlugin = new AssetsPlugin({
2022-01-03 12:59:40 +00:00
path: path.resolve(__dirname, 'dist'),
2021-01-29 01:15:33 +00:00
filename: 'assets.json',
update: true,
2021-01-29 01:15:33 +00:00
entrypoints: true,
prettyPrint: true,
});
2021-01-29 21:46:58 +00:00
export function buildWebpackClientConfig(
development,
analyze,
locale,
extract,
) {
2021-01-29 01:15:33 +00:00
const ttag = {
resolve: {
translations: (locale !== 'default')
? path.resolve(__dirname, 'i18n', `${locale}.po`)
: locale,
},
};
if (extract) {
ttag.extract = {
output: path.resolve(__dirname, 'i18n', 'template.pot'),
};
}
2021-01-29 01:15:33 +00:00
const babelPlugins = [
['ttag', ttag],
];
// cache invalidates if .po file changed
const buildDependencies = {
config: [__filename],
}
if (locale !== 'default') {
buildDependencies.i18n = [ttag.resolve.translations];
}
2021-01-29 01:15:33 +00:00
return {
name: 'client',
target: 'web',
context: __dirname,
mode: (development) ? 'development' : 'production',
2022-01-01 00:39:01 +00:00
devtool: (development) ? 'eval' : false,
2021-01-29 01:15:33 +00:00
entry: {
[(locale !== 'default') ? `client-${locale}` : 'client']:
[path.resolve(__dirname, 'src', 'client.js')],
2021-01-29 01:15:33 +00:00
[(locale !== 'default') ? `globe-${locale}` : 'globe']:
[path.resolve(__dirname, 'src', 'globe.js')],
2021-01-29 01:15:33 +00:00
},
output: {
2022-01-03 12:59:40 +00:00
path: path.resolve(__dirname, 'dist', 'public', 'assets'),
2021-01-29 01:15:33 +00:00
publicPath: '/assets/',
filename: '[name].[chunkhash:8].js',
chunkFilename: (locale !== 'default')
? `[name]-${locale}.[chunkhash:8].js`
: '[name].[chunkhash:8].js',
},
resolve: {
alias: {
ttag: 'ttag/dist/mock',
},
2022-01-02 20:50:42 +00:00
extensions: ['.js', '.jsx'],
2021-01-29 01:15:33 +00:00
},
module: {
rules: [
{
test: /\.svg$/,
use: [
{
loader: 'babel-loader',
},
{
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)$/,
2021-01-29 01:15:33 +00:00
loader: 'babel-loader',
include: [
path.resolve(__dirname, 'src'),
2022-01-02 20:50:42 +00:00
...['image-q'].map((moduleName) => (
path.resolve(__dirname + '/node_modules/' + moduleName)
))
2021-01-29 01:15:33 +00:00
],
options: {
plugins: babelPlugins,
},
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,
}),
assetPlugin,
// 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,
vendor: {
name: 'vendor',
chunks: (chunk) => chunk.name.startsWith('client'),
test: /[\\/]node_modules[\\/]/,
},
three: {
name: 'three',
chunks: 'all',
test: /[\\/]node_modules[\\/]three[\\/]/,
},
},
},
},
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
: {
type: 'filesystem',
name: (development) ? `${locale}-dev` : locale,
buildDependencies,
},
2021-01-29 01:15:33 +00:00
};
}
export function getAllAvailableLocals() {
2021-01-29 21:46:58 +00:00
const langDir = path.resolve(__dirname, 'i18n');
const langs = fs.readdirSync(langDir)
.filter((e) => (e.endsWith('.po') && !e.startsWith('ssr')))
.map((l) => l.slice(0, -3));
langs.push('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
*/
export default ({
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);
};