2020-07-08 09:45:23 +02:00
|
|
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
2020-06-02 11:39:53 +02:00
|
|
|
/* global require, module, process, __dirname */
|
2020-03-11 22:32:20 +01:00
|
|
|
const path = require('path')
|
|
|
|
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
|
2020-06-02 11:39:53 +02:00
|
|
|
const HtmlWebpackPlugin = require('html-webpack-plugin')
|
|
|
|
const HtmlWebpackHarddiskPlugin = require('html-webpack-harddisk-plugin')
|
|
|
|
|
|
|
|
const webpackDevServerHost = process.env.WEBPACK_HOT_RELOAD_HOST || '127.0.0.1'
|
2020-06-04 13:45:24 +02:00
|
|
|
|
|
|
|
// main = app
|
2020-10-15 11:42:42 +02:00
|
|
|
// toolbar = toolbar
|
2020-07-23 10:41:59 +02:00
|
|
|
// shared_dashboard = publicly available dashboard
|
2020-10-15 11:42:42 +02:00
|
|
|
module.exports = () => [createEntry('main'), createEntry('toolbar'), createEntry('shared_dashboard')]
|
2020-06-04 13:45:24 +02:00
|
|
|
|
|
|
|
function createEntry(entry) {
|
|
|
|
return {
|
|
|
|
name: entry,
|
|
|
|
mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
|
|
|
|
devtool: process.env.NODE_ENV === 'production' ? 'source-map' : 'inline-source-map',
|
|
|
|
entry: {
|
|
|
|
[entry]:
|
|
|
|
entry === 'main'
|
2020-07-08 09:45:23 +02:00
|
|
|
? './frontend/src/index.tsx'
|
2020-06-04 13:45:24 +02:00
|
|
|
: entry === 'toolbar'
|
2020-07-30 19:16:59 +02:00
|
|
|
? './frontend/src/toolbar/index.tsx'
|
2020-07-23 10:41:59 +02:00
|
|
|
: entry === 'shared_dashboard'
|
|
|
|
? './frontend/src/scenes/dashboard/SharedDashboard.js'
|
2020-06-04 13:45:24 +02:00
|
|
|
: null,
|
2020-04-14 21:41:26 +02:00
|
|
|
},
|
2020-06-04 13:45:24 +02:00
|
|
|
watchOptions: {
|
|
|
|
ignored: /node_modules/,
|
|
|
|
},
|
|
|
|
output: {
|
|
|
|
path: path.resolve(__dirname, 'frontend', 'dist'),
|
|
|
|
filename: '[name].js',
|
|
|
|
chunkFilename: '[name].[contenthash].js',
|
|
|
|
publicPath:
|
2020-06-19 11:03:21 +02:00
|
|
|
process.env.NODE_ENV === 'production'
|
|
|
|
? '/static/'
|
2020-07-28 22:23:44 +02:00
|
|
|
: process.env.JS_URL
|
|
|
|
? `${process.env.JS_URL}${process.env.JS_URL.endsWith('/') ? '' : '/'}static/`
|
2020-06-19 11:03:21 +02:00
|
|
|
: process.env.IS_PORTER
|
|
|
|
? `https://${process.env.PORTER_WEBPACK_HOST}/static/`
|
|
|
|
: `http${process.env.LOCAL_HTTPS ? 's' : ''}://${webpackDevServerHost}:8234/static/`,
|
2020-06-04 13:45:24 +02:00
|
|
|
},
|
|
|
|
resolve: {
|
2020-07-08 09:45:23 +02:00
|
|
|
extensions: ['.js', '.ts', '.tsx'],
|
2020-06-04 13:45:24 +02:00
|
|
|
alias: {
|
|
|
|
'~': path.resolve(__dirname, 'frontend', 'src'),
|
|
|
|
lib: path.resolve(__dirname, 'frontend', 'src', 'lib'),
|
|
|
|
scenes: path.resolve(__dirname, 'frontend', 'src', 'scenes'),
|
2020-08-14 17:02:57 +02:00
|
|
|
types: path.resolve(__dirname, 'frontend', 'types'),
|
2020-06-04 13:45:24 +02:00
|
|
|
...(process.env.NODE_ENV !== 'production'
|
|
|
|
? {
|
|
|
|
'react-dom': '@hot-loader/react-dom',
|
|
|
|
}
|
|
|
|
: {}),
|
2020-03-11 22:37:10 +01:00
|
|
|
},
|
2020-06-04 13:45:24 +02:00
|
|
|
},
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
2020-07-08 09:45:23 +02:00
|
|
|
test: /\.[jt]sx?$/,
|
2020-06-04 13:45:24 +02:00
|
|
|
exclude: /(node_modules)/,
|
|
|
|
use: {
|
|
|
|
loader: 'babel-loader',
|
2020-03-11 22:37:10 +01:00
|
|
|
},
|
2020-06-04 13:45:24 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// Apply rule for .sass, .scss or .css files
|
|
|
|
test: /\.(sa|sc|c)ss$/,
|
|
|
|
|
|
|
|
// Set loaders to transform files.
|
|
|
|
// Loaders are applying from right to left(!)
|
|
|
|
// The first loader will be applied after others
|
|
|
|
use: [
|
2020-07-23 10:41:59 +02:00
|
|
|
entry === 'main' || entry === 'shared_dashboard'
|
2020-06-04 13:45:24 +02:00
|
|
|
? {
|
|
|
|
// After all CSS loaders we use plugin to do his work.
|
|
|
|
// It gets all transformed CSS and extracts it into separate
|
|
|
|
// single bundled file
|
|
|
|
loader: MiniCssExtractPlugin.loader,
|
|
|
|
}
|
|
|
|
: entry === 'toolbar'
|
|
|
|
? {
|
|
|
|
loader: 'style-loader',
|
|
|
|
options: {
|
|
|
|
insert: function insertAtTop(element) {
|
|
|
|
// tunnel behind the shadow root
|
|
|
|
if (window.__PHGTLB_ADD_STYLES__) {
|
|
|
|
window.__PHGTLB_ADD_STYLES__(element)
|
|
|
|
} else {
|
|
|
|
if (!window.__PHGTLB_STYLES__) {
|
|
|
|
window.__PHGTLB_STYLES__ = []
|
|
|
|
}
|
|
|
|
window.__PHGTLB_STYLES__.push(element)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
: null,
|
|
|
|
{
|
|
|
|
// This loader resolves url() and @imports inside CSS
|
|
|
|
loader: 'css-loader',
|
2020-03-11 22:32:20 +01:00
|
|
|
},
|
2020-06-04 13:45:24 +02:00
|
|
|
{
|
|
|
|
// Then we apply postCSS fixes like autoprefixer and minifying
|
|
|
|
loader: 'postcss-loader',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// First we transform SASS to standard CSS
|
|
|
|
loader: 'sass-loader',
|
|
|
|
options: {
|
|
|
|
implementation: require('sass'),
|
|
|
|
},
|
|
|
|
},
|
2020-07-03 14:37:07 +02:00
|
|
|
].filter((a) => a),
|
2020-06-04 13:45:24 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// Now we apply rule for images
|
|
|
|
test: /\.(png|jpe?g|gif|svg)$/,
|
|
|
|
use: [
|
|
|
|
{
|
|
|
|
// Using file-loader for these files
|
|
|
|
loader: 'file-loader',
|
2020-03-11 22:37:10 +01:00
|
|
|
|
2020-06-04 13:45:24 +02:00
|
|
|
// In options we can set different things like format
|
|
|
|
// and directory to save
|
|
|
|
options: {
|
|
|
|
name: '[name].[contenthash].[ext]',
|
|
|
|
outputPath: 'images',
|
|
|
|
},
|
2020-03-11 22:32:20 +01:00
|
|
|
},
|
2020-06-04 13:45:24 +02:00
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Apply rule for fonts files
|
|
|
|
test: /\.(woff|woff2|ttf|otf|eot)$/,
|
|
|
|
use: [
|
|
|
|
{
|
|
|
|
// Using file-loader too
|
|
|
|
loader: 'file-loader',
|
|
|
|
options: {
|
|
|
|
name: '[name].[contenthash].[ext]',
|
|
|
|
outputPath: 'fonts',
|
|
|
|
},
|
2020-03-11 22:32:20 +01:00
|
|
|
},
|
2020-06-04 13:45:24 +02:00
|
|
|
],
|
|
|
|
},
|
2020-10-13 15:44:56 +02:00
|
|
|
{
|
|
|
|
// Apply rule for sound files
|
|
|
|
test: /\.(mp3)$/,
|
|
|
|
use: [
|
|
|
|
{
|
|
|
|
// Using file-loader too
|
|
|
|
loader: 'file-loader',
|
|
|
|
options: {
|
|
|
|
name: '[name].[contenthash].[ext]',
|
|
|
|
outputPath: 'sounds',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2020-06-04 13:45:24 +02:00
|
|
|
],
|
|
|
|
},
|
|
|
|
devServer: {
|
|
|
|
contentBase: path.join(__dirname, 'frontend', 'dist'),
|
|
|
|
hot: true,
|
|
|
|
host: webpackDevServerHost,
|
|
|
|
port: 8234,
|
2020-07-10 13:31:35 +02:00
|
|
|
stats: 'minimal',
|
2020-07-28 22:23:44 +02:00
|
|
|
disableHostCheck: !!process.env.LOCAL_HTTPS,
|
|
|
|
public: process.env.JS_URL
|
|
|
|
? new URL(process.env.JS_URL).host
|
|
|
|
: process.env.IS_PORTER
|
|
|
|
? `${process.env.PORTER_WEBPACK_HOST}`
|
|
|
|
: `${webpackDevServerHost}:8234`,
|
2020-06-19 11:03:21 +02:00
|
|
|
allowedHosts: process.env.IS_PORTER
|
|
|
|
? [`${process.env.PORTER_WEBPACK_HOST}`, `${process.env.PORTER_SERVER_HOST}`]
|
|
|
|
: [],
|
2020-06-04 13:45:24 +02:00
|
|
|
headers: {
|
|
|
|
'Access-Control-Allow-Origin': '*',
|
|
|
|
'Access-Control-Allow-Headers': '*',
|
2020-03-11 22:32:20 +01:00
|
|
|
},
|
2020-06-02 11:39:53 +02:00
|
|
|
},
|
2020-07-10 13:31:35 +02:00
|
|
|
plugins: [
|
|
|
|
// common plugins for all entrypoints
|
|
|
|
].concat(
|
2020-06-04 13:45:24 +02:00
|
|
|
entry === 'main'
|
|
|
|
? [
|
2020-07-10 13:31:35 +02:00
|
|
|
// other bundles include the css in js via style-loader
|
2020-06-04 13:45:24 +02:00
|
|
|
new MiniCssExtractPlugin({
|
|
|
|
filename: '[name].css',
|
2020-07-10 13:31:35 +02:00
|
|
|
ignoreOrder: true,
|
2020-06-04 13:45:24 +02:00
|
|
|
}),
|
2020-07-10 13:31:35 +02:00
|
|
|
// we need these only once per build
|
2020-06-04 13:45:24 +02:00
|
|
|
new HtmlWebpackPlugin({
|
|
|
|
alwaysWriteToDisk: true,
|
|
|
|
title: 'PostHog',
|
|
|
|
template: path.join(__dirname, 'frontend', 'src', 'index.html'),
|
|
|
|
}),
|
2020-07-23 10:41:59 +02:00
|
|
|
|
2020-06-04 13:45:24 +02:00
|
|
|
new HtmlWebpackPlugin({
|
|
|
|
alwaysWriteToDisk: true,
|
|
|
|
title: 'PostHog',
|
|
|
|
filename: 'layout.html',
|
|
|
|
inject: false,
|
|
|
|
template: path.join(__dirname, 'frontend', 'src', 'layout.ejs'),
|
|
|
|
}),
|
|
|
|
new HtmlWebpackHarddiskPlugin(),
|
|
|
|
]
|
2020-07-23 10:41:59 +02:00
|
|
|
: entry === 'shared_dashboard'
|
|
|
|
? [
|
|
|
|
new MiniCssExtractPlugin({
|
|
|
|
filename: '[name].css',
|
2020-07-28 22:23:44 +02:00
|
|
|
ignoreOrder: true,
|
2020-07-23 10:41:59 +02:00
|
|
|
}),
|
|
|
|
new HtmlWebpackPlugin({
|
|
|
|
alwaysWriteToDisk: true,
|
|
|
|
title: 'PostHog',
|
|
|
|
filename: 'shared_dashboard.html',
|
|
|
|
template: path.join(__dirname, 'frontend', 'src', 'shared_dashboard.ejs'),
|
|
|
|
}),
|
|
|
|
new HtmlWebpackHarddiskPlugin(),
|
|
|
|
]
|
2020-07-10 13:31:35 +02:00
|
|
|
: []
|
|
|
|
),
|
2020-06-04 13:45:24 +02:00
|
|
|
}
|
2020-03-11 22:32:20 +01:00
|
|
|
}
|