AFFiNE/apps/web/next.config.mjs

215 lines
5.5 KiB
JavaScript
Raw Normal View History

2023-03-02 05:13:45 +03:00
// @ts-check
import { createRequire } from 'node:module';
2023-02-19 11:40:39 +03:00
import path from 'node:path';
import { PerfseePlugin } from '@perfsee/webpack';
2023-03-28 22:06:16 +03:00
import { withSentryConfig } from '@sentry/nextjs';
import SentryWebpackPlugin from '@sentry/webpack-plugin';
2023-02-19 11:40:39 +03:00
import debugLocal from 'next-debug-local';
import preset from './preset.config.mjs';
import { getCommitHash, getGitVersion } from './scripts/gitInfo.mjs';
2023-02-19 11:40:39 +03:00
const require = createRequire(import.meta.url);
const { createVanillaExtractPlugin } = require('@vanilla-extract/next-plugin');
const withVanillaExtract = createVanillaExtractPlugin();
console.info('Runtime Preset', preset);
const enableDebugLocal = path.isAbsolute(process.env.LOCAL_BLOCK_SUITE ?? '');
if (enableDebugLocal) {
console.info('Debugging local blocksuite');
}
2023-04-14 02:20:41 +03:00
if (process.env.COVERAGE === 'true') {
console.info('Enable coverage report');
}
2023-01-07 12:01:10 +03:00
const profileTarget = {
ac: '100.85.73.88:12001',
2023-02-09 10:24:25 +03:00
dev: '100.84.105.99:11001',
test: '100.84.105.99:11001',
stage: '',
2023-04-12 17:11:47 +03:00
prod: 'https://app.affine.pro',
2023-01-07 12:01:10 +03:00
local: '127.0.0.1:3000',
};
const getRedirectConfig = profile => {
const target = profileTarget[profile || 'dev'] || profileTarget['dev'];
return [
[
{ source: '/api/:path*', destination: `http://${target}/api/:path*` },
{
source: '/collaboration/:path*',
destination: `http://${target}/collaboration/:path*`,
},
],
target,
2023-02-10 15:41:01 +03:00
profile || 'dev',
2023-01-07 12:01:10 +03:00
];
};
2022-09-22 10:39:53 +03:00
/** @type {import('next').NextConfig} */
2022-11-03 09:53:44 +03:00
const nextConfig = {
2023-03-28 22:06:16 +03:00
sentry: {
hideSourceMaps: true,
},
productionBrowserSourceMaps: true,
compiler: {
2023-03-04 10:36:20 +03:00
styledComponents: true,
removeConsole: {
exclude: ['error', 'log', 'warn', 'info'],
},
emotion: {
sourceMap: true,
},
},
images: {
unoptimized: true,
},
experimental: {
swcPlugins: [
2023-03-08 11:29:50 +03:00
process.env.COVERAGE === 'true' && ['swc-plugin-coverage-instrument', {}],
2023-03-20 10:05:02 +03:00
// ['@swc-jotai/debug-label', {}],
// ['@swc-jotai/react-refresh', {}],
2023-03-08 11:29:50 +03:00
].filter(Boolean),
},
reactStrictMode: true,
2023-02-17 05:43:52 +03:00
transpilePackages: [
'@affine/component',
'@affine/i18n',
2023-03-02 05:13:45 +03:00
'@affine/debug',
2023-03-02 04:26:55 +03:00
'@affine/env',
2023-03-11 08:15:19 +03:00
'@affine/templates',
2023-04-04 02:40:07 +03:00
'@toeverything/hooks',
'@affine/workspace',
'@affine/jotai',
'@toeverything/y-indexeddb',
2023-02-17 05:43:52 +03:00
],
publicRuntimeConfig: {
PROJECT_NAME: process.env.npm_package_name ?? 'AFFiNE',
BUILD_DATE: new Date().toISOString(),
gitVersion: getGitVersion(),
hash: getCommitHash(),
serverAPI:
profileTarget[process.env.API_SERVER_PROFILE || 'dev'] ??
profileTarget.dev,
editorVersion: require('./package.json').dependencies['@blocksuite/editor'],
...preset,
},
2023-03-02 11:49:33 +03:00
webpack: (config, { dev, isServer }) => {
2022-12-30 14:42:01 +03:00
config.experiments = { ...config.experiments, topLevelAwait: true };
config.module.rules.push({
test: /\.md$/i,
loader: 'raw-loader',
});
config.resolve.alias['yjs'] = require.resolve('yjs');
if (!isServer && !dev && process.env.PERFSEE_TOKEN) {
2023-03-02 11:49:33 +03:00
config.devtool = 'hidden-nosources-source-map';
const perfsee = new PerfseePlugin({
project: 'affine-toeverything',
});
if (Array.isArray(config.plugins)) {
config.plugins.push(perfsee);
} else {
config.plugins = [perfsee];
}
}
if (
process.env.SENTRY_AUTH_TOKEN &&
process.env.SENTRY_ORG &&
process.env.SENTRY_PROJECT
) {
config.plugins.push(
new SentryWebpackPlugin({
include: '.next',
ignore: ['node_modules', 'cypress', 'test'],
urlPrefix: '~/_next',
})
);
}
2023-03-02 11:49:33 +03:00
return config;
},
rewrites: async () => {
2023-02-10 15:41:01 +03:00
const [profile, target, desc] = getRedirectConfig(
process.env.API_SERVER_PROFILE
2023-01-07 12:01:10 +03:00
);
console.info(`API request proxy to [${desc} Server]: ` + target);
2023-01-07 12:01:10 +03:00
return profile;
2022-11-03 13:50:27 +03:00
},
2023-02-14 09:51:05 +03:00
basePath: process.env.NEXT_BASE_PATH,
2023-04-13 16:37:50 +03:00
assetPrefix: process.env.NEXT_ASSET_PREFIX,
2023-03-03 05:00:56 +03:00
pageExtensions: [...(preset.enableDebugPage ? ['tsx', 'dev.tsx'] : ['tsx'])],
2022-11-03 09:53:44 +03:00
};
2022-09-22 10:39:53 +03:00
const baseDir = process.env.LOCAL_BLOCK_SUITE ?? '/';
2023-02-19 11:40:39 +03:00
const withDebugLocal = debugLocal(
{
'@blocksuite/editor': path.resolve(baseDir, 'packages', 'editor'),
'@blocksuite/blocks/models': path.resolve(
baseDir,
'packages',
'blocks',
'src',
'models'
),
'@blocksuite/blocks/std': path.resolve(
baseDir,
'packages',
'blocks',
'src',
'std'
),
'@blocksuite/blocks/content-parser': path.resolve(
baseDir,
'packages',
'blocks',
'src',
'content-parser'
),
'@blocksuite/blocks': path.resolve(baseDir, 'packages', 'blocks'),
'@blocksuite/store': path.resolve(baseDir, 'packages', 'store'),
},
{
enable: enableDebugLocal,
}
);
2023-02-16 19:51:32 +03:00
const detectFirebaseConfig = () => {
if (!process.env.NEXT_PUBLIC_FIREBASE_API_KEY) {
console.warn(
'NEXT_PUBLIC_FIREBASE_API_KEY not found, affine cloud feature will be disabled.'
);
2023-02-16 19:51:32 +03:00
} else {
console.info('NEXT_PUBLIC_FIREBASE_API_KEY found.');
2023-02-16 19:51:32 +03:00
}
};
detectFirebaseConfig();
2023-01-03 06:43:59 +03:00
2023-03-28 22:06:16 +03:00
let config = withDebugLocal(nextConfig);
if (process.env.SENTRY_AUTH_TOKEN) {
config = withSentryConfig(config, {
silent: true,
});
} else {
console.log(
'SENTRY_AUTH_TOKEN not found, Sentry monitoring feature will be disabled.'
);
delete config.sentry;
2023-03-28 22:06:16 +03:00
}
2023-04-04 02:40:07 +03:00
if (process.env.PERFSEE_TOKEN) {
console.info('perfsee token found.');
} else {
console.warn(
'perfsee token not found. performance monitoring will be disabled.'
);
}
export default withVanillaExtract(config);