obs-portal/frontend/webpack.config.js

243 lines
7.7 KiB
JavaScript
Raw Permalink Normal View History

2021-11-27 21:44:03 +00:00
var HtmlWebpackPlugin = require('html-webpack-plugin')
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin')
2021-11-30 19:30:09 +00:00
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
2021-11-27 21:44:03 +00:00
const path = require('path')
2021-11-30 18:50:10 +00:00
const resolveApp = (relativePath) => path.resolve(__dirname, relativePath)
const appSrc = resolveApp('src')
2021-11-30 18:50:10 +00:00
module.exports = function (webpackEnv) {
2021-11-30 19:30:09 +00:00
const isEnvProduction = webpackEnv.production
2021-11-30 18:50:10 +00:00
const isEnvDevelopment = !isEnvProduction
2021-11-27 21:44:03 +00:00
2021-11-30 18:50:10 +00:00
const apiUrl = process.env.API_URL || 'http://localhost:3000'
const baseUrl = process.env.BASE_URL || isEnvDevelopment ? 'http://localhost:3001' : '__BASE_HREF__'
const port = process.env.PORT || 3001
2021-11-27 21:44:03 +00:00
2021-11-30 19:30:09 +00:00
const sourceMap = isEnvDevelopment || Boolean(process.env.GENERATE_SOURCEMAP)
2021-11-30 18:50:10 +00:00
const getStyleLoaders = (modules, withLess) => {
const loaders = [
isEnvDevelopment && require.resolve('style-loader'),
isEnvProduction && {
loader: MiniCssExtractPlugin.loader,
// css is located in `static/css`, use '../../' to locate index.html folder
// in production `paths.publicUrlOrPath` can be a relative path
// options: paths.publicUrlOrPath.startsWith('.')
// ? { publicPath: '../../' }
// : {},
},
{
loader: require.resolve('css-loader'),
options: {
modules,
importLoaders: withLess ? 2 : 1,
sourceMap,
},
},
{
// Options for PostCSS as we reference these options twice
// Adds vendor prefixing based on your specified browser support in
// package.json
loader: require.resolve('postcss-loader'),
options: {
postcssOptions: {
// Necessary for external CSS imports to work
// https://github.com/facebook/create-react-app/issues/2677
ident: 'postcss',
2021-11-27 21:44:03 +00:00
plugins: [
2021-11-30 18:50:10 +00:00
'postcss-flexbugs-fixes',
2021-11-27 21:44:03 +00:00
[
2021-11-30 18:50:10 +00:00
'postcss-preset-env',
2021-11-27 21:44:03 +00:00
{
2021-11-30 18:50:10 +00:00
autoprefixer: {
flexbox: 'no-2009',
},
stage: 3,
2021-11-27 21:44:03 +00:00
},
],
2021-11-30 18:50:10 +00:00
// Adds PostCSS Normalize as the reset css with default options,
// so that it honors browserslist config in package.json
// which in turn let's users customize the target behavior as per their needs.
'postcss-normalize',
2021-11-27 21:44:03 +00:00
],
},
2021-11-30 18:50:10 +00:00
sourceMap,
2021-11-27 21:44:03 +00:00
},
},
2021-11-30 18:50:10 +00:00
].filter(Boolean)
if (withLess) {
loaders.push(
// {
// loader: require.resolve('resolve-url-loader'),
// options: {
// sourceMap,
// root: appSrc,
// },
// },
{
loader: require.resolve('less-loader'),
options: {
lessOptions: {
math: 'always',
2021-11-27 21:44:03 +00:00
},
2021-11-30 18:50:10 +00:00
// sourceMap: true,
2021-11-27 21:44:03 +00:00
},
2021-11-30 18:50:10 +00:00
}
)
}
return loaders
}
return {
entry: './src/index.js',
mode: isEnvProduction ? 'production' : isEnvDevelopment && 'development',
cache: isEnvDevelopment && {type: 'filesystem'},
2021-11-30 19:30:09 +00:00
devtool: sourceMap && (isEnvProduction ? 'source-map' : 'cheap-module-source-map'),
2021-11-30 18:50:10 +00:00
output: {
// The build folder.
path: path.resolve(__dirname, 'build'),
// Add /* filename */ comments to generated require()s in the output.
pathinfo: isEnvDevelopment,
// There will be one main bundle, and one file per asynchronous chunk.
// In development, it does not produce real files.
filename: isEnvProduction ? 'static/js/[name].[contenthash:8].js' : isEnvDevelopment && 'static/js/bundle.js',
// There are also additional JS chunk files if you use code splitting.
chunkFilename: isEnvProduction
? 'static/js/[name].[contenthash:8].chunk.js'
: isEnvDevelopment && 'static/js/[name].chunk.js',
assetModuleFilename: 'static/media/[name].[hash][ext]',
// webpack uses `publicPath` to determine where the app is being served from.
// It requires a trailing slash, or the file assets will get an incorrect path.
// We inferred the "public path" (such as / or /my-project) from homepage.
// publicPath: paths.publicUrlOrPath,
//
// Point sourcemap entries to original disk location (format as URL on Windows)
devtoolModuleFilenameTemplate: isEnvProduction
? (info) => path.relative(appSrc, info.absoluteResourcePath).replace(/\\/g, '/')
: isEnvDevelopment && ((info) => path.resolve(info.absoluteResourcePath).replace(/\\/g, '/')),
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
alias: {
'mapbox-gl': 'maplibre-gl',
'../../theme.config': path.resolve('./src/semantic-ui/theme.config'),
'../../themes/default': 'fomantic-ui-less/themes/default',
},
2021-11-30 18:50:10 +00:00
modules: ['node_modules', 'src'],
},
plugins: [
new HtmlWebpackPlugin({
base: baseUrl,
title: 'OpenBikeSensor Portal',
2021-11-30 18:50:10 +00:00
meta: {
charset: 'utf-8',
viewport: 'width=device-width, initial-scale=1',
'theme-color': '#000000',
description:
'Upload the tracks recorded with your OpenBikeSensor device to this portal and participate in the accumulation of Open Data.',
},
favicon: 'public/favicon.ico',
manifest: 'public/manifest.json',
}),
isEnvDevelopment && new ReactRefreshWebpackPlugin(),
2021-11-30 19:30:09 +00:00
isEnvProduction && new MiniCssExtractPlugin(),
2021-11-30 18:50:10 +00:00
].filter(Boolean),
devServer: {
port,
hot: true,
compress: true,
historyApiFallback: true,
static: {
directory: path.join(__dirname, 'public'),
serveIndex: true,
},
client: {
webSocketURL: `ws://0.0.0.0:${port}/ws`,
},
proxy: {
'/config.json': apiUrl,
2021-11-30 18:50:10 +00:00
'/api': apiUrl,
'/login': apiUrl,
},
},
module: {
rules: [
{
test: /\.(mjs|js|jsx|ts|tsx)$/,
// include: path.resolve('./src'),
exclude: /node_modules/,
use: {
loader: 'babel-loader',
2021-11-27 21:44:03 +00:00
options: {
2021-11-30 18:50:10 +00:00
babelrc: false,
configFile: false,
cacheDirectory: true,
cacheCompression: false,
2021-11-30 19:30:09 +00:00
sourceMaps: sourceMap,
inputSourceMap: sourceMap,
2021-11-30 18:50:10 +00:00
plugins: [
isEnvDevelopment && 'react-refresh/babel',
[
'@babel/plugin-transform-runtime',
{
corejs: false,
regenerator: true,
useESModules: true,
},
],
].filter(Boolean),
presets: [
[
'@babel/preset-react',
{
development: isEnvDevelopment,
runtime: 'automatic',
},
],
'@babel/preset-typescript',
'@babel/preset-env',
],
2021-11-27 21:44:03 +00:00
},
},
2021-11-30 18:50:10 +00:00
},
{
test: /\.css$/i,
use: getStyleLoaders(false),
sideEffects: true,
},
{
test: /\.less$/i,
exclude: /\.module\.less$/i,
use: getStyleLoaders(false, true),
sideEffects: true,
},
{
test: /\.module\.less$/i,
use: getStyleLoaders(true, true),
},
2021-11-30 18:50:10 +00:00
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
type: 'asset/resource',
},
2021-11-30 18:50:10 +00:00
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
},
2021-11-30 18:50:10 +00:00
{
scheme: 'data',
type: 'asset/resource',
},
],
},
}
2021-11-27 21:44:03 +00:00
}