2023-04-25 02:53:36 +03:00
|
|
|
import { resolve } from 'node:path';
|
|
|
|
|
|
|
|
import { fileURLToPath } from 'url';
|
|
|
|
|
|
|
|
export const root = fileURLToPath(new URL('..', import.meta.url));
|
|
|
|
export const NODE_MAJOR_VERSION = 18;
|
2023-03-16 17:58:21 +03:00
|
|
|
|
2023-05-09 10:30:01 +03:00
|
|
|
// hard-coded for now:
|
|
|
|
// fixme(xp): report error if app is not running on DEV_SERVER_URL
|
|
|
|
const DEV_SERVER_URL = process.env.DEV_SERVER_URL;
|
|
|
|
|
|
|
|
/** @type 'production' | 'development'' */
|
|
|
|
const mode = (process.env.NODE_ENV = process.env.NODE_ENV || 'development');
|
|
|
|
|
2023-03-28 02:03:11 +03:00
|
|
|
const nativeNodeModulesPlugin = {
|
|
|
|
name: 'native-node-modules',
|
|
|
|
setup(build) {
|
|
|
|
// Mark native Node.js modules as external
|
|
|
|
build.onResolve({ filter: /\.node$/, namespace: 'file' }, args => {
|
|
|
|
return { path: args.path, external: true };
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-04-11 21:42:36 +03:00
|
|
|
// List of env that will be replaced by esbuild
|
|
|
|
const ENV_MACROS = ['AFFINE_GOOGLE_CLIENT_ID', 'AFFINE_GOOGLE_CLIENT_SECRET'];
|
2023-03-16 17:58:21 +03:00
|
|
|
|
2023-04-11 21:42:36 +03:00
|
|
|
/** @return {{main: import('esbuild').BuildOptions, preload: import('esbuild').BuildOptions}} */
|
2023-04-25 02:53:36 +03:00
|
|
|
export const config = () => {
|
2023-05-09 10:30:01 +03:00
|
|
|
const define = Object.fromEntries([
|
|
|
|
...ENV_MACROS.map(key => [
|
2023-04-11 21:42:36 +03:00
|
|
|
'process.env.' + key,
|
|
|
|
JSON.stringify(process.env[key] ?? ''),
|
2023-05-09 10:30:01 +03:00
|
|
|
]),
|
|
|
|
['process.env.NODE_ENV', `"${mode}"`],
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (DEV_SERVER_URL) {
|
|
|
|
define['process.env.DEV_SERVER_URL'] = `"${DEV_SERVER_URL}"`;
|
|
|
|
}
|
|
|
|
|
2023-04-11 21:42:36 +03:00
|
|
|
return {
|
|
|
|
main: {
|
2023-05-09 10:30:01 +03:00
|
|
|
entryPoints: [
|
|
|
|
resolve(root, './layers/main/src/index.ts'),
|
|
|
|
resolve(root, './layers/main/src/exposed.ts'),
|
|
|
|
],
|
2023-04-25 02:53:36 +03:00
|
|
|
outdir: resolve(root, './dist/layers/main'),
|
2023-04-11 21:42:36 +03:00
|
|
|
bundle: true,
|
2023-04-18 12:50:29 +03:00
|
|
|
target: `node${NODE_MAJOR_VERSION}`,
|
2023-04-11 21:42:36 +03:00
|
|
|
platform: 'node',
|
2023-05-09 10:30:01 +03:00
|
|
|
external: ['electron', 'yjs', 'better-sqlite3', 'electron-updater'],
|
2023-04-11 21:42:36 +03:00
|
|
|
plugins: [nativeNodeModulesPlugin],
|
|
|
|
define: define,
|
2023-05-09 10:30:01 +03:00
|
|
|
format: 'cjs',
|
2023-04-11 21:42:36 +03:00
|
|
|
},
|
|
|
|
preload: {
|
2023-04-25 02:53:36 +03:00
|
|
|
entryPoints: [resolve(root, './layers/preload/src/index.ts')],
|
|
|
|
outdir: resolve(root, './dist/layers/preload'),
|
2023-04-11 21:42:36 +03:00
|
|
|
bundle: true,
|
2023-04-18 12:50:29 +03:00
|
|
|
target: `node${NODE_MAJOR_VERSION}`,
|
2023-04-11 21:42:36 +03:00
|
|
|
platform: 'node',
|
2023-05-09 10:30:01 +03:00
|
|
|
external: ['electron', '../main/exposed-meta'],
|
|
|
|
plugins: [nativeNodeModulesPlugin],
|
2023-04-11 21:42:36 +03:00
|
|
|
define: define,
|
|
|
|
},
|
|
|
|
};
|
2023-03-16 17:58:21 +03:00
|
|
|
};
|