0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-21 21:49:51 +01:00
posthog/webpack.config.js
Marius Andra 6bb8a6a570
feat(apps): frontend apps (#9831)
* create plugin source model

* edit source via plugin_source model

* deprecate "source"

* test plugin source updates

* add support for index.ts and index.js

* refactor plugin loading, support plugin sources from db

* fix source code in tests

* remove transpilation code

* reload plugin after saving

* store defaults in the db instead of persisting in form

* remove fields that don't exist

* feat(apps): transpile frontend.tsx

* update timestamp even if no local fields changed

* yarn.lock

* feat(apps): load apps with frontend.tsx in the frontend

* fix import

* reload apps, show if transpiling

* remove unused fields

* commit suggestion

* rename to PluginSourceFile

* rename to PluginSourceFile

* remove dead code

* cleaner types

* PluginSourceFile

* fix nag

* fix types

* one more merge conflict

* transpile when adding frontend

* fix test

* edit app from sidebar

* sneaky edit button

* fix code feedback

* add comments

* make it safer to call

* convert to upsert

* convert to upsert

* comment on the null

* revert plugin server changes

* get source from new model behind the scenes

* simplify

* revert accidental changes

* revert accidental changes

* remove defaults

* sync the old source field

* use the source model in the plugin server

* cache the source from pluginsourcefile

* test that getPluginSource gets data from the database

* safer null check that doesn't override `{}` in the db with `null`

* clarify origins

* use enum

* fix error from migration 0233 ([] is falsy in python!)

* fix merge

* error as null

* use the explicit "source__index_ts" field

* cache transpiled code

* remove unused type

* cleaner query

* remove extra line

* fix test

* improved error handling and run code through prettier

* new packages

* loading state in source editor

* kinder error

* sync plugin state if pluginsLogic, retry loading

* update snapshot after kea-forms update

* remove "updateAppConfig"

* add a query

* add examples to editor

* refactor sidebar

* add more lemons

* simplify app config

* rename action and add todo

* some helpful comments

* create appConfig if enabling a plugin and none was yet provided in django's app context

* re-enable app only if enabled

* fix last bug

* add back onInit and move types around

* show apps section header only if any link in the list

* remove dead code

* fix few errors, only allow editing of source apps

* stringify json

* completely bail out of apps if the flag is off

* Update snapshots

* fix broken link

* Update snapshots

* use new icon for "apps" now that it's not "plugins" any longer

* Update snapshots

* Update snapshots

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
2022-05-20 18:14:58 +02:00

225 lines
8.7 KiB
JavaScript

/* eslint-disable @typescript-eslint/no-var-requires */
/* global require, module, process, __dirname */
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const HtmlWebpackHarddiskPlugin = require('html-webpack-harddisk-plugin')
const AntdDayjsWebpackPlugin = require('antd-dayjs-webpack-plugin')
const webpackDevServerHost = process.env.WEBPACK_HOT_RELOAD_HOST || '127.0.0.1'
const webpackDevServerFrontendAddr = webpackDevServerHost === '0.0.0.0' ? '127.0.0.1' : webpackDevServerHost
function createEntry(entry) {
const commonLoadersForSassAndLess = [
{
loader: 'style-loader',
},
{
// This loader resolves url() and @imports inside CSS
loader: 'css-loader',
},
{
// Then we apply postCSS fixes like autoprefixer and minifying
loader: 'postcss-loader',
},
]
return {
name: entry,
mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
devtool:
process.env.GENERATE_SOURCEMAP === 'false'
? false
: process.env.NODE_ENV === 'production'
? 'source-map'
: 'inline-source-map',
entry: {
[entry]:
entry === 'main' || entry === 'cypress'
? './frontend/src/index.tsx'
: entry === 'toolbar'
? './frontend/src/toolbar/index.tsx'
: entry === 'shared_dashboard'
? './frontend/src/scenes/dashboard/SharedDashboard.tsx'
: null,
},
watchOptions: {
ignored: /node_modules/,
},
output: {
path: path.resolve(__dirname, 'frontend', 'dist'),
filename: '[name].js',
chunkFilename: '[name].[contenthash].js',
publicPath: process.env.JS_URL
? `${process.env.JS_URL}${process.env.JS_URL.endsWith('/') ? '' : '/'}static/`
: process.env.NODE_ENV === 'production'
? '/static/'
: `http${process.env.LOCAL_HTTPS ? 's' : ''}://${webpackDevServerFrontendAddr}:8234/static/`,
},
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
alias: {
'~': path.resolve(__dirname, 'frontend', 'src'),
lib: path.resolve(__dirname, 'frontend', 'src', 'lib'),
scenes: path.resolve(__dirname, 'frontend', 'src', 'scenes'),
packages: path.resolve(__dirname, 'frontend', 'src', 'packages'),
storybook: path.resolve(__dirname, '.storybook'),
types: path.resolve(__dirname, 'frontend', 'types'),
public: path.resolve(__dirname, 'frontend', 'public'),
cypress: path.resolve(__dirname, 'cypress'),
},
},
module: {
rules: [
{
test: /\.[jt]sx?$/,
exclude: /(node_modules)/,
use: {
loader: 'babel-loader',
},
},
{
// 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: [
...commonLoadersForSassAndLess,
{
// First we transform SASS to standard CSS
loader: 'sass-loader',
options: {
implementation: require('sass'),
},
},
].filter((a) => a),
},
{
// Apply rule for less files (used to import and override AntD)
test: /\.(less)$/,
use: [
...commonLoadersForSassAndLess,
{
loader: 'less-loader', // compiles Less to CSS
options: {
lessOptions: {
javascriptEnabled: true,
},
},
},
],
},
{
// Now we apply rule for images
test: /\.(png|jpe?g|gif|svg)$/,
use: [
{
// Using file-loader for these files
loader: 'file-loader',
// In options we can set different things like format
// and directory to save
options: {
name: '[name].[contenthash].[ext]',
outputPath: 'images',
},
},
],
},
{
// 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',
},
},
],
},
{
// Apply rule for sound files
test: /\.(mp3)$/,
use: [
{
// Using file-loader too
loader: 'file-loader',
options: {
name: '[name].[contenthash].[ext]',
outputPath: 'sounds',
},
},
],
},
],
},
// add devServer config only to 'main' entry
...(entry === 'main'
? {
devServer: {
contentBase: path.join(__dirname, 'frontend', 'dist'),
hot: true,
host: webpackDevServerHost,
port: 8234,
stats: 'minimal',
disableHostCheck: !!process.env.LOCAL_HTTPS,
public: process.env.JS_URL
? new URL(process.env.JS_URL).host
: `${webpackDevServerFrontendAddr}:8234`,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': '*',
},
},
}
: {}),
plugins: [
new AntdDayjsWebpackPlugin(),
// common plugins for all entrypoints
].concat(
entry === 'main'
? [
// we need these only once per build
new HtmlWebpackPlugin({
alwaysWriteToDisk: true,
title: 'PostHog',
template: path.join(__dirname, 'frontend', 'src', 'index.html'),
}),
new HtmlWebpackPlugin({
alwaysWriteToDisk: true,
title: 'PostHog',
filename: 'layout.html',
inject: false,
template: path.join(__dirname, 'frontend', 'src', 'layout.ejs'),
}),
new HtmlWebpackHarddiskPlugin(),
]
: entry === 'shared_dashboard'
? [
new HtmlWebpackPlugin({
alwaysWriteToDisk: true,
title: 'PostHog',
filename: 'shared_dashboard.html',
template: path.join(__dirname, 'frontend', 'src', 'shared_dashboard.ejs'),
}),
new HtmlWebpackHarddiskPlugin(),
]
: entry === 'cypress'
? [new HtmlWebpackHarddiskPlugin()]
: []
),
}
}
// main = app
// toolbar = toolbar
// shared_dashboard = publicly available dashboard
module.exports = () => [createEntry('main'), createEntry('toolbar'), createEntry('shared_dashboard')]
module.exports.createEntry = createEntry