/
dev.ts
149 lines (135 loc) · 3.76 KB
/
dev.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import * as p from '@clack/prompts';
import { config } from 'dotenv';
import webpack from 'webpack';
import WebpackDevServer from 'webpack-dev-server';
import type { BuildFlags } from '../config/index';
import { projectRoot } from '../config/index';
import { watchI18N } from '../util/i18n';
import { createWebpackConfig } from '../webpack/webpack.config';
const flags: BuildFlags = {
distribution: 'browser',
mode: 'development',
channel: 'canary',
coverage: process.env.COVERAGE === 'true',
localBlockSuite: undefined,
};
const files = ['.env', '.env.local'];
for (const file of files) {
if (existsSync(join(projectRoot, file))) {
config({
path: join(projectRoot, file),
});
console.log(`${file} loaded`);
break;
}
}
const buildFlags = process.argv.includes('--static')
? { ...flags, debugBlockSuite: false }
: ((await p.group(
{
distribution: () =>
p.select({
message: 'Distribution',
options: [
{
value: 'browser',
},
{
value: 'desktop',
},
],
initialValue: 'browser',
}),
mode: () =>
p.select({
message: 'Mode',
options: [
{
value: 'development',
},
{
value: 'production',
},
],
initialValue: 'development',
}),
channel: () =>
p.select({
message: 'Channel',
options: [
{
value: 'canary',
},
{
value: 'beta',
},
{
value: 'stable',
},
],
initialValue: 'canary',
}),
coverage: () =>
p.confirm({
message: 'Enable coverage',
initialValue: process.env.COVERAGE === 'true',
}),
debugBlockSuite: () =>
p.confirm({
message: 'Debug blocksuite locally?',
initialValue: false,
}),
},
{
onCancel: () => {
p.cancel('Operation cancelled.');
process.exit(0);
},
}
)) as BuildFlags & { debugBlockSuite: boolean });
flags.distribution = buildFlags.distribution;
flags.mode = buildFlags.mode;
flags.channel = buildFlags.channel;
flags.coverage = buildFlags.coverage;
flags.entry = undefined;
const cwd =
flags.distribution === 'browser'
? join(projectRoot, 'packages', 'frontend', 'web')
: join(projectRoot, 'packages', 'frontend', 'electron');
if (flags.distribution === 'desktop') {
flags.entry = join(cwd, 'renderer', 'index.tsx');
}
if (buildFlags.debugBlockSuite) {
const { config } = await import('dotenv');
const envLocal = config({
path: join(cwd, '.env.local'),
});
const localBlockSuite = await p.text({
message: 'local blocksuite PATH',
initialValue: envLocal.error
? undefined
: envLocal.parsed?.LOCAL_BLOCK_SUITE,
});
if (typeof localBlockSuite !== 'string') {
throw new Error('local blocksuite PATH is required');
}
if (!existsSync(localBlockSuite)) {
throw new Error(`local blocksuite not found: ${localBlockSuite}`);
}
flags.localBlockSuite = localBlockSuite;
}
watchI18N();
try {
// @ts-expect-error no types
await import('@affine/templates/build-edgeless');
const config = createWebpackConfig(cwd, flags);
const compiler = webpack(config);
// Start webpack
const devServer = new WebpackDevServer(config.devServer, compiler);
await devServer.start();
} catch (error) {
console.error('Error during build:', error);
process.exit(1);
}