Skip to content

Commit 9ec0d1f

Browse files
committed
Migrate browser and editor build systems from webpack to Vite
- from webpack, migrate to vite - keep type checking in watch mode [Changes] - any way to remove the need for this [Fragment]? - assess vite logic - migrate `apps/browser` to vite ├── apps │ ├── browser │ │ ├── package.json │ │ ├── scripts │ │ │ ├── build.mjs │ │ │ ├── bump.js │ │ │ ├── create-firefox-build.ts │ │ │ └── create-zip-archives.ts │ │ ├── tsconfig.json │ │ └── vite.config.ts │ └── editor │ ├── package.json │ ├── scripts │ │ └── build.mjs │ ├── src │ │ └── views │ │ └── prompt │ │ └── backend │ │ ├── message-handlers │ │ │ └── handle-open-website.ts │ │ └── utils │ │ └── website-fetcher.ts │ ├── tsconfig.json │ └── vite.config.ts └── tsconfig.json
1 parent cb8107b commit 9ec0d1f

10 files changed

Lines changed: 1819 additions & 1705 deletions

File tree

‎apps/browser/package.json‎

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,27 @@
11
{
22
"name": "browser",
33
"scripts": {
4-
"build": "webpack --mode production && ts-node scripts/create-firefox-build.ts && ts-node scripts/create-zip-archives.ts",
4+
"build": "tsc --noEmit && node scripts/build.mjs && ts-node scripts/create-firefox-build.ts && ts-node scripts/create-zip-archives.ts",
5+
"watch": "node scripts/build.mjs --watch",
6+
"typecheck": "tsc --noEmit",
57
"bump": "node scripts/bump.js"
68
},
79
"devDependencies": {
8-
"@pmmmwh/react-refresh-webpack-plugin": "0.6.1",
9-
"@svgr/webpack": "8.1.0",
1010
"@types/archiver": "6.0.3",
1111
"@types/node": "20.x",
1212
"@types/react": "18.2.60",
1313
"@types/react-dom": "18.2.19",
1414
"@types/webextension-polyfill": "0.12.4",
1515
"archiver": "6.0.1",
1616
"baseline-browser-mapping": "2.11.35",
17-
"clean-webpack-plugin": "4.0.0",
18-
"copy-webpack-plugin": "12.0.2",
19-
"css-loader": "6.8.1",
20-
"mini-css-extract-plugin": "2.9.4",
21-
"react-refresh": "0.17.0",
2217
"sass": "1.93.2",
23-
"sass-loader": "13.3.3",
24-
"style-loader": "3.3.3",
25-
"ts-loader": "9.5.4",
2618
"ts-node": "10.9.2",
27-
"webextension-polyfill": "0.12.0",
28-
"webpack": "5.112.2",
29-
"webpack-cli": "5.1.4",
30-
"webpack-dev-server": "5.2.7"
19+
"vite": "8.3.6",
20+
"webextension-polyfill": "0.12.0"
3121
},
3222
"dependencies": {
3323
"classnames": "2.5.1",
3424
"react": "18.2.0",
35-
"react-dom": "18.2.0",
36-
"tsconfig-paths-webpack-plugin": "4.2.0"
25+
"react-dom": "18.2.0"
3726
}
3827
}

‎apps/browser/scripts/build.mjs‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import * as fs from 'node:fs'
2+
import * as path from 'node:path'
3+
import { fileURLToPath } from 'node:url'
4+
import { build } from 'vite'
5+
6+
const app_dir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
7+
8+
const targets = ['background', 'send-prompt-content-script', 'popup']
9+
10+
const is_watch = process.argv.includes('--watch')
11+
const mode = is_watch ? 'development' : 'production'
12+
process.env.NODE_ENV = mode
13+
14+
const run = async () => {
15+
console.log('Building...')
16+
17+
if (!is_watch) {
18+
fs.rmSync(path.join(app_dir, 'dist'), { recursive: true, force: true })
19+
}
20+
21+
const pending = new Set(targets)
22+
const finish = (target) => {
23+
if (pending.delete(target) && pending.size == 0) {
24+
console.log('Done.')
25+
}
26+
}
27+
28+
for (const target of targets) {
29+
process.env.CWC_TARGET = target
30+
31+
const result = await build({
32+
root: app_dir,
33+
configFile: path.join(app_dir, 'vite.config.ts'),
34+
mode,
35+
logLevel: 'error',
36+
clearScreen: false,
37+
build: { watch: is_watch ? {} : null }
38+
})
39+
40+
if (is_watch) {
41+
result.on('event', (event) => {
42+
if (event.code == 'END' || event.code == 'ERROR') {
43+
finish(target)
44+
}
45+
})
46+
} else {
47+
finish(target)
48+
}
49+
}
50+
}
51+
52+
run().catch((error) => {
53+
console.error(error)
54+
process.exit(1)
55+
})

‎apps/browser/vite.config.ts‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import * as crypto from 'crypto'
2+
import * as fs from 'fs'
3+
import * as path from 'path'
4+
import { defineConfig, type Plugin } from 'vite'
5+
6+
const targets: Record<string, string> = {
7+
'send-prompt-content-script':
8+
'src/content-scripts/send-prompt-content-script/send-prompt-content-script.ts',
9+
background: 'src/background/main.ts',
10+
popup: 'src/views/popup/App.tsx'
11+
}
12+
13+
const root_dir = __dirname
14+
const out_dir = path.resolve(root_dir, 'dist')
15+
const src_dir = path.resolve(root_dir, 'src')
16+
const shared_dir = path.resolve(root_dir, '../../packages/shared/src')
17+
const ui_dir = path.resolve(root_dir, '../../packages/ui/src')
18+
19+
const static_files = [
20+
{ from: 'src/manifest.json', to: 'manifest.json' },
21+
{ from: 'src/icons', to: 'icons' },
22+
{ from: 'src/fonts', to: 'fonts' },
23+
{ from: 'src/views/popup/index.html', to: 'popup.html' },
24+
{ from: 'src/views/popup/index.css', to: 'index.css' }
25+
]
26+
27+
const collect_files = (target_path: string): string[] => {
28+
if (!fs.statSync(target_path).isDirectory()) return [target_path]
29+
return fs
30+
.readdirSync(target_path)
31+
.flatMap((name) => collect_files(path.join(target_path, name)))
32+
}
33+
34+
const copy_static_files = (): Plugin => ({
35+
name: 'copy-static-files',
36+
buildStart() {
37+
for (const { from } of static_files) {
38+
for (const file of collect_files(path.resolve(root_dir, from))) {
39+
this.addWatchFile(file)
40+
}
41+
}
42+
},
43+
writeBundle() {
44+
for (const { from, to } of static_files) {
45+
fs.cpSync(path.resolve(root_dir, from), path.resolve(out_dir, to), {
46+
recursive: true
47+
})
48+
}
49+
}
50+
})
51+
52+
const generate_scoped_name = (local_name: string, filename: string) => {
53+
const file = filename.split('?')[0]
54+
const relative_file = path.relative(root_dir, file).replace(/\\/g, '/')
55+
const module_name = path.basename(file).replace(/\.module\.(scss|css)$/i, '')
56+
const hash = crypto
57+
.createHash('md5')
58+
.update(`${relative_file}${local_name}`)
59+
.digest('hex')
60+
.substring(0, 5)
61+
return `${module_name}__${local_name}__${hash}`
62+
}
63+
64+
export default defineConfig(({ mode }) => {
65+
const target = process.env.CWC_TARGET ?? ''
66+
const is_production = mode == 'production'
67+
68+
if (!Object.hasOwn(targets, target)) {
69+
throw new Error(
70+
`Unknown CWC_TARGET "${target}". Build with "node scripts/build.mjs".`
71+
)
72+
}
73+
74+
return {
75+
publicDir: false,
76+
plugins: target == 'popup' ? [copy_static_files()] : [],
77+
define: {
78+
'process.env.NODE_ENV': JSON.stringify(
79+
is_production ? 'production' : 'development'
80+
)
81+
},
82+
resolve: {
83+
alias: {
84+
'@': src_dir,
85+
'@shared': shared_dir,
86+
'@ui': ui_dir
87+
}
88+
},
89+
oxc: {
90+
jsx: { runtime: 'automatic' }
91+
},
92+
css: {
93+
modules: {
94+
generateScopedName: generate_scoped_name
95+
}
96+
},
97+
build: {
98+
outDir: out_dir,
99+
emptyOutDir: false,
100+
target: 'es2022',
101+
minify: is_production,
102+
sourcemap: !is_production,
103+
reportCompressedSize: false,
104+
lib: {
105+
entry: path.resolve(root_dir, targets[target]),
106+
name: `cwc_${target.replace(/\W/g, '_')}`,
107+
formats: ['iife'],
108+
cssFileName: target
109+
},
110+
rolldownOptions: {
111+
output: {
112+
entryFileNames: `${target}.js`,
113+
codeSplitting: false
114+
}
115+
}
116+
}
117+
}
118+
})

‎apps/browser/webpack.config.ts‎

Lines changed: 0 additions & 86 deletions
This file was deleted.

‎apps/editor/package.json‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
"scripts": {
77
"build": "npx vsce package --no-dependencies",
88
"vscode:prepublish": "rimraf out && npm run compile",
9-
"compile": "webpack --config ./webpack.config.ts --mode production",
10-
"watch": "webpack --config ./webpack.config.ts --watch --mode development",
9+
"compile": "node scripts/build.mjs",
10+
"watch": "node scripts/build.mjs --watch",
11+
"typecheck": "tsc --noEmit",
1112
"bump": "node scripts/bump.js",
1213
"test": "jest",
1314
"test:watch": "jest --watch"
@@ -1367,33 +1368,24 @@
13671368
"@types/turndown": "5.0.5",
13681369
"@types/vscode": "1.96.0",
13691370
"@types/ws": "8.18.1",
1370-
"clean-webpack-plugin": "4.0.0",
1371-
"copy-webpack-plugin": "12.0.2",
1372-
"css-loader": "6.5.1",
1373-
"esbuild-loader": "4.2.2",
1374-
"fork-ts-checker-webpack-plugin": "9.0.2",
1375-
"mini-css-extract-plugin": "2.9.2",
13761371
"sass-embedded": "1.89.2",
1377-
"sass-loader": "12.6.0",
1378-
"style-loader": "3.3.4",
13791372
"ts-node": "10.9.2",
13801373
"typed-css-modules": "0.9.1",
1381-
"webpack": "5.112.2",
1382-
"webpack-cli": "6.0.1"
1374+
"vite": "8.3.6",
1375+
"vite-plugin-svgr": "5.2.0"
13831376
},
13841377
"dependencies": {
13851378
"@joplin/turndown": "4.0.81",
13861379
"@joplin/turndown-plugin-gfm": "1.0.63",
13871380
"@mozilla/readability": "0.6.0",
1388-
"@svgr/webpack": "8.1.0",
13891381
"@vscode/codicons": "0.0.46-38",
13901382
"axios": "1.21.0",
13911383
"classnames": "2.5.1",
13921384
"dayjs": "1.11.10",
13931385
"diff": "8.0.4",
13941386
"dompurify": "3.4.17",
13951387
"dotenv": "16.4.5",
1396-
"glob": "11.1.0",
1388+
"glob": "14.0.4",
13971389
"he": "1.2.0",
13981390
"ignore": "6.0.2",
13991391
"jsdom": "24.0.0",

0 commit comments

Comments
 (0)