0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-22 08:40:03 +01:00
posthog/webpack.config.js

223 lines
8.7 KiB
JavaScript
Raw Normal View History

/* global require, module, process, __dirname */
2020-03-11 22:32:20 +01:00
const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const HtmlWebpackHarddiskPlugin = require('html-webpack-harddisk-plugin')
const AntdDayjsWebpackPlugin = require('antd-dayjs-webpack-plugin')
2023-06-12 08:53:54 +02:00
const MonacoWebpackPlugin = require('monaco-editor-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
2020-06-04 13:45:24 +02:00
function createEntry(entry) {
const commonLoadersForSassAndLess = [
2021-05-31 23:58:28 +02:00
{
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',
},
]
2020-06-04 13:45:24 +02:00
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',
2020-06-04 13:45:24 +02:00
entry: {
[entry]: entry === 'main' || entry === 'cypress' ? './frontend/src/index.tsx' : null,
},
2020-06-04 13:45:24 +02:00
watchOptions: {
ignored: /node_modules/,
},
output: {
path: path.resolve(__dirname, 'frontend', 'dist'),
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/`,
2020-06-04 13:45:24 +02:00
},
resolve: {
Bundle our frontend via esbuild (#6758) * esbuild package * almost get esbuild working * fix react-virtualized imports * add splitting * fix funny import reorder bug * fix squeakAudio referring to itself * write index.html file * fix some bad imports * update antd paths * remove raw-loader usage, it didn't work anyway * refactor and copy public * build app and toolbar * get toolbar working, but without styles * make toolbar and its styles work * shared dashboards * clean frontend build before rebuilding * add watch mode * reorder tasks * revert js url * incremental builds of app with debounced chokidar watching * common build/watch script * improve logs * watch during firrst build * fix toolbar url * fix wrongly exported scene * create sceneProxyLogic to untangle sceneLogic from all bundles * disconnect sceneLogic and refactor setPageTitle * live reloading server * rename utils file * only wait for /static * fix encoding * simplify * add missing dayjs plugins * fix pathless logics * simplify options * add jsx for webapck * slight delay to catch changes * a type is a type * fix build * esbuild in start * funnelLogic path * include all files with a "." (so .mjs, etc) in /frontend/ to docker * rename to "utils.mjs", make "build.mjs" executable * improve erroring * revert some needless changes * more reverts * change some scripts * remove setuff * clarify function * make "--host 0.0.0.0" work * fix import order issue in webpack * remove webpack css inlining for toolbar to simplify config * make toolbar with external styles work in storybook * move live server injection into django * fix undefined bug * simplify setup to work with injection directly in http://localhost:8000 (no proxying needed on :8234) * add comments * Fix `fse` usage I was getting this otherwise: $ node frontend/build.mjs file:///Users/twixes/Developer/posthog/frontend/utils.mjs:46 fse.copySync(srcDir, destDir, { overwrite: true }, function (err) { ^ TypeError: fse.copySync is not a function at copyPublicFolder (file:///Users/twixes/Developer/posthog/frontend/utils.mjs:46:9) at file:///Users/twixes/Developer/posthog/frontend/build.mjs:5:1 at ModuleJob.run (internal/modules/esm/module_job.js:146:23) at async Loader.import (internal/modules/esm/loader.js:165:24) at async Object.loadESM (internal/process/esm_loader.js:68:5) * Mock `process` for VFile used by ReactMarkdown I was getting this otherwise: core.js:55 Uncaught ReferenceError: process is not defined at new VFile (core.js:55) at VFile (core.js:49) at Function.parse (index.js:273) at ReactMarkdown2 (react-markdown.js:42) at renderWithHooks (react-dom.development.js:14803) at mountIndeterminateComponent (react-dom.development.js:17482) at beginWork (react-dom.development.js:18596) at HTMLUnknownElement.callCallback2 (react-dom.development.js:188) at Object.invokeGuardedCallbackDev (react-dom.development.js:237) at invokeGuardedCallback (react-dom.development.js:292) * Mock `process.env` for VFile used by ReactMarkdown I was getting this otherwise: platform.ts:73 Uncaught TypeError: Cannot read properties of undefined (reading 'ENABLE_VSCODE_BROWSER_CODE_LOADING') at platform.ts:73 at platform.ts:79 at Function.r._invokeFactory (loader.js:1118) at r.complete (loader.js:1128) at r._onModuleComplete (loader.js:1754) at r._resolve (loader.js:1714) at r.defineModule (loader.js:1357) at _ (loader.js:1804) at numbers.ts:10 at fake:1 * pass the heavy appScenes to sceneLogic through props via App.tsx * remove sceneProxyLogic * remove exported variables * fix sceneLogic test Co-authored-by: Michael Matloka <dev@twixes.com>
2021-11-03 09:50:24 +01:00
extensions: ['.js', '.jsx', '.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'),
feat(frontend): build `@posthog/lemon-ui` and `@posthog/apps-common` (#10120) * fix dayjs * fix timeouts (we're not strictly speaking running in nodejs) * export unexported type * consolidate on a single FormInstance * no need to rename * fuse * forminstance 2 * locationChanged * BuiltLogic * remove Type.ts exception * fix duh * playing with common apps * playing with common apps * fix some scss deprecations :shrug: * revert * move packages/apps-common * remove compiled json file * build apps-common before other packages * mkdirp, build types before running * build types before the rest * move imports json to packages folder * mark some packages as external, saving 200kb * revert nonsense * remove ant dep * make app source editor types optional for speed * move dev server reloads to utils.mjs * remove webpack start scripts (unused) * refactor build/utils to support various dirs * apps package * revert some stuff * yarn * go back to the old location and commit packages.json in * commit it in * out of scope * fix bad imports * fix some postcss mess * move to @posthog/apps-common * add more stuff to apps * include that in packages.json * simple cjs build * export everything * simplify * make separate package for lemonade * more lemonade * fix jest imports * fix jest imports * yarn as well * src * write types before building * rename lemon-ui, part 1 * add readmes * rename to lemon-ui, part 2 * this app source editor tsd is committed into the repo, so we don't need it before a build
2022-06-10 17:03:06 +02:00
'@posthog/apps-common': path.resolve(__dirname, 'frontend', '@posthog', 'apps-common', 'src'),
'@posthog/lemon-ui': path.resolve(__dirname, 'frontend', '@posthog', 'lemon-ui', 'src'),
'@posthog/ee/exports': [
path.resolve(__dirname, 'ee', 'frontend', 'exports'),
path.resolve(__dirname, 'frontend', '@posthog', 'ee', 'exports'),
],
storybook: path.resolve(__dirname, '.storybook'),
types: path.resolve(__dirname, 'frontend', 'types'),
public: path.resolve(__dirname, 'frontend', 'public'),
cypress: path.resolve(__dirname, 'cypress'),
process: 'process/browser',
2020-03-11 22:37:10 +01:00
},
2020-06-04 13:45:24 +02:00
},
module: {
rules: [
{
test: /\.[jt]sx?$/,
exclude: /(node_modules)/,
2020-06-04 13:45:24 +02:00
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: [
...commonLoadersForSassAndLess,
2020-06-04 13:45:24 +02:00
{
// First we transform SASS to standard CSS
loader: 'sass-loader',
options: {
implementation: require('sass'),
},
},
].filter((a) => a),
2020-06-04 13:45:24 +02:00
},
{
// 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,
},
},
},
],
},
2020-06-04 13:45:24 +02:00
{
// Now we apply rule for images
test: /\.(png|jpe?g|gif|svg|lottie)$/,
2020-06-04 13:45:24 +02:00
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
],
},
Command palette (#1819) * add hotkey base * add platform check * Rename CommandBox to CommandPalette * Add styled-components * Restructure CommandPalette and add Esc handling * Update utils.js * add search box * Add logic types * Implement base logic * Fix scroll prevention * Update logic and add goto command base with Fuse fuzzy serach * ux hotkey * Implement command (de)registration * minor fix * base UI * error message & hints * command groups UI * Add commands search * Make commands work * Prepare palette for fuzzy search and add basic labeling * ui adjustments * ui * Improve colors * switch to global command * Optimize resolution * global go to commands * abstract item selection logic to command palette * Update casing and types * Add conditional useCommands * Improve results * keyboard nav * keyboard nav fix * removed double fuse (synonyms broken) * Update style * Fix highlighting * Remove extra style * conditional go to commands * add mouse hover handling and rename functions * remove mac check * add more commands * added insights stub pages * Add command components * refactor logic * Update styling * Remove extraneous `input` * load custom dashboards to command palette * Add input styling * Add write icon * Refactor results * minor adjustments * add person search * remove papercups fully * fix input indexing * fix result executing empty * Add command grouping and improve navigation * add urls * make logic explicit * fix error with hadnler * Remove redundant border-top * add trend functions * personal api key stub * Add command palette toggle button * Fix things * fix personal api keys * copy api key to clipboard * Add toggle text * Improve palette UX and perform refactoring * Improve UX and add palette usage reporting * tests refactor * filter person list * refactor api person tests * add squeak * deprecate by_distinct_id & by_email person endpoints * Optimize squeak * fix typegen error * use new filters in frontend * Optimize squeak * key identifier refactor * fix mypy * removed unused code * Make custom command UI more coherent * Add calculator to palette * Use equal sign * Make palette button nicer * Add lodash back officially and show palette suggestions lodash is still used in a few places, but it was not in package.json. The reason this was working was that lodash is a dependency of some other depedencies, but this was fragile. It's still not ideal to use this, but at least this is now not a hack the way it was. * Remove isHint * Optimize graph time range command * Move command results grouping to Kea logic * Fix result focus autoshift * Improve palette result focusing * Adjust for window.posthog being optional * add test for third-party person filters * Remove styled-components in favor of .scss files * Remove redundant container class * Use insect (sic!) squeak instead of pig squeak * Show only unique palette results * Fix palette overlay * Add powerful command building protocol CommandFlow * Fix minor issues * Always show scope when flow active * Use custom label icon * Add feedback sharing command * block command input from being captured in screen recording at least until we can figure out how to capture this info in a privacy-preserving way * Hide palette button on narrow screens * Improve responsiveness * Fix palette feedback sending * Fix Esc handling * Add Message Sent info * Fix Message Sent info * Fix dashboard creation and null name handling * Rename Cy tests to JS convention * Add basic Cypress test * Address feedback Co-authored-by: Eric <eeoneric@gmail.com> Co-authored-by: Paolo D'Amico <paolodamico@users.noreply.github.com>
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',
},
},
],
},
2023-06-12 08:53:54 +02:00
// probably only need this because we're using webpack v4
{
test: /monaco-editor\/.*\.m?js/,
loader: 'babel-loader',
},
2020-06-04 13:45:24 +02:00
],
},
// 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: [
2023-06-12 08:53:54 +02:00
new MonacoWebpackPlugin(),
new AntdDayjsWebpackPlugin(),
// common plugins for all entrypoints
].concat(
2020-06-04 13:45:24 +02:00
entry === 'main'
? [
// 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-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(),
]
: entry === 'cypress'
? [
new HtmlWebpackHarddiskPlugin(),
new webpack.ProvidePlugin({
process: 'process/browser',
}),
]
: []
),
2020-06-04 13:45:24 +02:00
}
2020-03-11 22:32:20 +01:00
}
// main = app
module.exports = () => [createEntry('main')]
module.exports.createEntry = createEntry