123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568 |
- const fs = require('fs');
- const path = require('path');
- const {CleanWebpackPlugin} = require('clean-webpack-plugin');
- const webpack = require('webpack');
- const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
- const MiniCssExtractPlugin = require('mini-css-extract-plugin');
- const CompressionPlugin = require('compression-webpack-plugin');
- const FixStyleOnlyEntriesPlugin = require('webpack-remove-empty-scripts');
- const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
- const IntegrationDocsFetchPlugin = require('./build-utils/integration-docs-fetch-plugin');
- const OptionalLocaleChunkPlugin = require('./build-utils/optional-locale-chunk-plugin');
- const SentryInstrumentation = require('./build-utils/sentry-instrumentation');
- const LastBuiltPlugin = require('./build-utils/last-built-plugin');
- const babelConfig = require('./babel.config');
- const {env} = process;
- const IS_PRODUCTION = env.NODE_ENV === 'production';
- const IS_TEST = env.NODE_ENV === 'test' || env.TEST_SUITE;
- const IS_STORYBOOK = env.STORYBOOK_BUILD === '1';
- const IS_CI = !!env.CI;
- const IS_ACCEPTANCE_TEST = !!env.IS_ACCEPTANCE_TEST;
- const IS_DEPLOY_PREVIEW = !!env.NOW_GITHUB_DEPLOYMENT;
- const IS_UI_DEV_ONLY = !!env.SENTRY_UI_DEV_ONLY;
- const DEV_MODE = !(IS_PRODUCTION || IS_CI);
- const WEBPACK_MODE = IS_PRODUCTION ? 'production' : 'development';
- const SENTRY_BACKEND_PORT = env.SENTRY_BACKEND_PORT;
- const SENTRY_WEBPACK_PROXY_HOST = env.SENTRY_WEBPACK_PROXY_HOST;
- const SENTRY_WEBPACK_PROXY_PORT = env.SENTRY_WEBPACK_PROXY_PORT;
- const FORCE_WEBPACK_DEV_SERVER = !!env.FORCE_WEBPACK_DEV_SERVER;
- const HAS_WEBPACK_DEV_SERVER_CONFIG = SENTRY_BACKEND_PORT && SENTRY_WEBPACK_PROXY_PORT;
- const NO_DEV_SERVER = !!env.NO_DEV_SERVER;
- const TS_FORK_WITH_ESLINT = !!env.TS_FORK_WITH_ESLINT;
- const SHOULD_FORK_TS = DEV_MODE && !env.NO_TS_FORK;
- const SHOULD_HOT_MODULE_RELOAD = DEV_MODE && !!env.SENTRY_UI_HOT_RELOAD;
- const DEPLOY_PREVIEW_CONFIG = IS_DEPLOY_PREVIEW && {
- branch: env.NOW_GITHUB_COMMIT_REF,
- commitSha: env.NOW_GITHUB_COMMIT_SHA,
- githubOrg: env.NOW_GITHUB_COMMIT_ORG,
- githubRepo: env.NOW_GITHUB_COMMIT_REPO,
- };
- const SENTRY_EXPERIMENTAL_SPA =
- !DEPLOY_PREVIEW_CONFIG && !IS_UI_DEV_ONLY ? env.SENTRY_EXPERIMENTAL_SPA : true;
- const staticPrefix = path.join(__dirname, 'src/sentry/static/sentry');
- const distPath = env.SENTRY_STATIC_DIST_PATH || path.join(staticPrefix, 'dist');
- if (env.SENTRY_EXTRACT_TRANSLATIONS === '1') {
- babelConfig.plugins.push([
- 'module:babel-gettext-extractor',
- {
- fileName: 'build/javascript.po',
- baseDirectory: path.join(__dirname, 'src/sentry'),
- functionNames: {
- gettext: ['msgid'],
- ngettext: ['msgid', 'msgid_plural', 'count'],
- gettextComponentTemplate: ['msgid'],
- t: ['msgid'],
- tn: ['msgid', 'msgid_plural', 'count'],
- tct: ['msgid'],
- },
- },
- ]);
- }
- const localeCatalogPath = path.join(
- __dirname,
- 'src',
- 'sentry',
- 'locale',
- 'catalogs.json'
- );
- const localeCatalog = JSON.parse(fs.readFileSync(localeCatalogPath, 'utf8'));
- const localeToLanguage = locale => locale.toLowerCase().replace('_', '-');
- const supportedLocales = localeCatalog.supported_locales;
- const supportedLanguages = supportedLocales.map(localeToLanguage);
- const localeChunkGroups = {};
- supportedLocales
- .filter(l => l !== 'en')
- .forEach(locale => {
- const language = localeToLanguage(locale);
- const group = `locale/${language}`;
-
- const localeGroupTests = [
- new RegExp(`locale\\/${locale}\\/.*\\.po$`),
- new RegExp(`moment\\/locale\\/${language}\\.js$`),
- ];
-
-
-
-
- const groupTest = (module, {chunkGraph}) =>
- localeGroupTests.some(pattern =>
- module.nameForCondition && pattern.test(module.nameForCondition())
- ? true
- : chunkGraph.getModuleChunks(module).some(c => c.name && pattern.test(c.name))
- );
- localeChunkGroups[group] = {
- chunks: 'initial',
- name: group,
- test: groupTest,
- enforce: true,
- };
- });
- const localeRestrictionPlugins = [
- new webpack.ContextReplacementPlugin(
- /sentry-locale$/,
- path.join(__dirname, 'src', 'sentry', 'locale', path.sep),
- true,
- new RegExp(`(${supportedLocales.join('|')})/.*\\.po$`)
- ),
- new webpack.ContextReplacementPlugin(
- /moment\/locale/,
- new RegExp(`(${supportedLanguages.join('|')})\\.js$`)
- ),
- ];
- const cacheGroups = {
- defaultVendors: {
- name: 'vendor',
-
-
-
- test: module =>
- !/platformicons/.test(module.resource) &&
- /[\\/]node_modules[\\/]/.test(module.resource),
- priority: -10,
- enforce: true,
- chunks: 'initial',
- },
- ...localeChunkGroups,
- };
- const babelOptions = {...babelConfig, cacheDirectory: true};
- const babelLoaderConfig = {
- loader: 'babel-loader',
- options: babelOptions,
- };
- let appConfig = {
- mode: WEBPACK_MODE,
- entry: {
-
- app: ['app/utils/statics-setup', 'app'],
-
- sentry: 'less/sentry.less',
-
- select2: 'less/select2.less',
- },
- context: staticPrefix,
- module: {
-
- rules: [
- {
- test: /\.[tj]sx?$/,
- include: [staticPrefix],
- exclude: /(vendor|node_modules|dist)/,
- use: babelLoaderConfig,
- },
- {
- test: /\.po$/,
- use: {
- loader: 'po-catalog-loader',
- options: {
- referenceExtensions: ['.js', '.jsx'],
- domain: 'sentry',
- },
- },
- },
- {
- test: /\.css/,
- use: ['style-loader', 'css-loader'],
- },
- {
- test: /\.less$/,
- include: [staticPrefix],
- use: [MiniCssExtractPlugin.loader, 'css-loader', 'less-loader'],
- },
- {
- test: /\.(woff|woff2|ttf|eot|svg|png|gif|ico|jpg|mp4)($|\?)/,
- use: [
- {
- loader: 'file-loader',
- options: {
-
- esModule: false,
- name: '[folder]/[name].[hash:6].[ext]',
- },
- },
- ],
- },
- ],
- noParse: [
-
- /dist\/jquery\.js/,
- /jed\/jed\.js/,
- /marked\/lib\/marked\.js/,
- /terser\/dist\/bundle\.min\.js/,
- ],
- },
- plugins: [
- new CleanWebpackPlugin(),
-
- new webpack.ProvidePlugin({jQuery: 'jquery'}),
-
- new MiniCssExtractPlugin(),
-
- new webpack.DefinePlugin({
- 'process.env': {
- NODE_ENV: JSON.stringify(env.NODE_ENV),
- IS_ACCEPTANCE_TEST: JSON.stringify(IS_ACCEPTANCE_TEST),
- DEPLOY_PREVIEW_CONFIG: JSON.stringify(DEPLOY_PREVIEW_CONFIG),
- EXPERIMENTAL_SPA: JSON.stringify(SENTRY_EXPERIMENTAL_SPA),
- SPA_DSN: JSON.stringify(env.SENTRY_SPA_DSN),
- },
- }),
-
- new OptionalLocaleChunkPlugin(),
-
- new FixStyleOnlyEntriesPlugin({verbose: false}),
- new SentryInstrumentation(),
- ...(SHOULD_FORK_TS
- ? [
- new ForkTsCheckerWebpackPlugin({
- eslint: TS_FORK_WITH_ESLINT,
- tsconfig: path.resolve(__dirname, './config/tsconfig.build.json'),
- }),
- ]
- : []),
- ...localeRestrictionPlugins,
- ],
- resolve: {
- alias: {
- app: path.join(staticPrefix, 'app'),
- 'sentry-images': path.join(staticPrefix, 'images'),
- 'sentry-fonts': path.join(staticPrefix, 'fonts'),
- '@emotion/styled': path.join(staticPrefix, 'app', 'styled'),
- '@original-emotion/styled': path.join(
- __dirname,
- 'node_modules',
- '@emotion',
- 'styled'
- ),
-
-
- less: path.join(staticPrefix, 'less'),
- 'sentry-test': path.join(__dirname, 'tests', 'js', 'sentry-test'),
- 'sentry-locale': path.join(__dirname, 'src', 'sentry', 'locale'),
- 'ios-device-list': path.join(
- __dirname,
- 'node_modules',
- 'ios-device-list',
- 'dist',
- 'ios-device-list.min.js'
- ),
- },
- fallback: {
- vm: false,
- stream: false,
- crypto: require.resolve('crypto-browserify'),
-
- string_decoder: false,
- },
- modules: ['node_modules'],
- extensions: ['.jsx', '.js', '.json', '.ts', '.tsx', '.less'],
- },
- output: {
- path: distPath,
- filename: '[name].js',
- sourceMapFilename: '[name].js.map',
- },
- optimization: {
- chunkIds: 'named',
- moduleIds: 'named',
- splitChunks: {
-
-
-
- chunks: 'async',
- maxInitialRequests: 5,
- maxAsyncRequests: 7,
- cacheGroups,
- },
-
-
- minimizer: ['...', new CssMinimizerPlugin()],
- },
- devtool: IS_PRODUCTION ? 'source-map' : 'eval-cheap-module-source-map',
- };
- if (IS_TEST || IS_ACCEPTANCE_TEST || IS_STORYBOOK) {
- appConfig.resolve.alias['integration-docs-platforms'] = path.join(
- __dirname,
- 'tests/fixtures/integration-docs/_platforms.json'
- );
- } else {
- const plugin = new IntegrationDocsFetchPlugin({basePath: __dirname});
- appConfig.plugins.push(plugin);
- appConfig.resolve.alias['integration-docs-platforms'] = plugin.modulePath;
- }
- if (IS_ACCEPTANCE_TEST) {
- appConfig.plugins.push(new LastBuiltPlugin({basePath: __dirname}));
- }
- if (
- FORCE_WEBPACK_DEV_SERVER ||
- (HAS_WEBPACK_DEV_SERVER_CONFIG && !NO_DEV_SERVER) ||
- IS_UI_DEV_ONLY
- ) {
- if (SHOULD_HOT_MODULE_RELOAD) {
-
-
-
- const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
- appConfig.plugins.push(new ReactRefreshWebpackPlugin());
- }
- appConfig.devServer = {
- headers: {
- 'Access-Control-Allow-Origin': '*',
- 'Access-Control-Allow-Credentials': 'true',
- },
-
- disableHostCheck: true,
- contentBase: './src/sentry/static/sentry',
- host: SENTRY_WEBPACK_PROXY_HOST,
- hot: true,
-
- hotOnly: true,
- port: SENTRY_WEBPACK_PROXY_PORT,
- stats: 'errors-only',
- overlay: false,
- watchOptions: {
- ignored: ['node_modules'],
- },
- };
- if (!IS_UI_DEV_ONLY) {
-
- const backendAddress = `http://localhost:${SENTRY_BACKEND_PORT}/`;
- const relayAddress = 'http://127.0.0.1:7899';
- appConfig.devServer = {
- ...appConfig.devServer,
- publicPath: '/_webpack',
-
- proxy: {
- '/api/store/**': relayAddress,
- '/api/{1..9}*({0..9})/**': relayAddress,
- '/api/0/relays/outcomes/': relayAddress,
- '!/_webpack': backendAddress,
- },
- before: app =>
- app.use((req, _res, next) => {
- req.url = req.url.replace(/^\/_static\/[^\/]+\/sentry\/dist/, '/_webpack');
- next();
- }),
- };
- }
- }
- if (IS_UI_DEV_ONLY) {
- appConfig.devServer = {
- ...appConfig.devServer,
- compress: true,
- https: true,
- publicPath: '/_assets/',
- proxy: [
- {
- context: ['/api/', '/avatar/', '/organization-avatar/'],
- target: 'https://sentry.io',
- secure: false,
- changeOrigin: true,
- headers: {
- Referer: 'https://sentry.io/',
- },
- },
- ],
- historyApiFallback: {
- rewrites: [{from: /^\/.*$/, to: '/_assets/index.html'}],
- },
- };
- }
- if (IS_UI_DEV_ONLY || IS_DEPLOY_PREVIEW) {
- appConfig.output.publicPath = '/_assets/';
-
- const HtmlWebpackPlugin = require('html-webpack-plugin');
- appConfig.plugins.push(
- new HtmlWebpackPlugin({
-
- ...(IS_UI_DEV_ONLY
- ? {devServer: `https://localhost:${SENTRY_WEBPACK_PROXY_PORT}`}
- : {}),
- favicon: path.resolve(staticPrefix, 'images', 'favicon_dev.png'),
- template: path.resolve(staticPrefix, 'index.ejs'),
- mobile: true,
- title: 'Sentry',
- })
- );
- }
- const minificationPlugins = [
-
-
-
- new CompressionPlugin({
- algorithm: 'gzip',
- test: /\.(js|map|css|svg|html|txt|ico|eot|ttf)$/,
- }),
-
-
- ];
- if (IS_PRODUCTION) {
-
- minificationPlugins.forEach(function (plugin) {
- appConfig.plugins.push(plugin);
- });
- }
- if (env.MEASURE) {
- const SpeedMeasurePlugin = require('speed-measure-webpack-plugin');
- const smp = new SpeedMeasurePlugin();
- appConfig = smp.wrap(appConfig);
- }
- module.exports = appConfig;
|