-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathbuild.ts
More file actions
179 lines (159 loc) · 4.88 KB
/
build.ts
File metadata and controls
179 lines (159 loc) · 4.88 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import * as path from 'node:path'
import * as url from 'node:url'
import * as crx from '@crxjs/vite-plugin'
import * as vite from 'vite'
import solid from 'vite-plugin-solid'
import pkg from './package.json' with {type: 'json'}
import main_pkg from 'solid-devtools/package.json' with {type: 'json'}
import {
ICONS_BLUE,
ICONS_GRAY,
} from './src/shared.ts'
const filename = url.fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
const dist_dirname = path.join(dirname, `dist`)
const args = process.argv.slice(2)
type Browser = 'chrome' | 'firefox'
/*
Parse args
*/
let is_dev = false
let browsers: Browser[] = []
for (let arg of args) {
switch (arg) {
case '--watch':
is_dev = true
break
case '--chrome':
case 'chrome':
case '--browser=chrome':
if (!browsers.includes('chrome')) {
browsers.push('chrome')
}
break
case '--firefox':
case 'firefox':
case '--browser=firefox':
if (!browsers.includes('firefox')) {
browsers.push('firefox')
}
break
default:
throw Error(`Unknown arg: "${arg}"`)
}
}
if (is_dev) {
if (browsers.length === 0) {
browsers.push('chrome')
} else if (browsers.length > 1) {
throw Error('Watch mode can only be used with one browser')
}
} else {
if (browsers.length === 0) {
browsers.push('chrome', 'firefox')
}
}
const manifest_version = (() => {
// Convert from Semver (example: 0.1.0-beta6)
const [major, minor, patch, label = '0'] = pkg.version
// can only contain digits, dots, or dash
.replace(/[^\d.-]+/g, '')
// split into version parts
.split(/[.-]/)
return `${major}.${minor}.${patch}.${label}`
})()
type Manifest_Additional_Fields = {
browser_specific_settings?: Record<string, Record<string, string>>
}
for (let browser of browsers) {
let is_chrome = browser === 'chrome'
const manifest: crx.ManifestV3Export & Manifest_Additional_Fields = {
manifest_version: 3,
name: `${is_dev ? '[DEV] ' : ''}Solid Devtools`,
description: 'Chrome Developer Tools extension for debugging SolidJS applications.',
homepage_url: 'https://github.com/thetarnav/solid-devtools',
version: manifest_version,
version_name: is_chrome ? pkg.version : undefined,
browser_specific_settings: is_chrome
? undefined
: {gecko: {id: '{abfd162e-9948-403a-a75c-6e61184e1d47}'}},
author: is_chrome ? {email: 'gthetarnav@gmail.com'} : 'Damian Tarnawski' as any,
minimum_chrome_version: '94',
devtools_page: 'src/devtools.html',
content_scripts: [{
matches: ['*://*/*'],
js: ['src/content.ts'],
run_at: 'document_start',
}],
background: is_chrome
? {
service_worker: 'src/background.ts',
type: 'module',
}
: {
scripts: ['src/background.ts'],
type: 'module',
},
permissions: [],
action: {
default_icon: ICONS_GRAY,
default_title: 'Solid Devtools',
default_popup: 'src/popup.html',
},
icons: ICONS_BLUE,
}
const main_version = JSON.stringify(main_pkg.version.match(/\d+.\d+.\d+/)![0])
const vite_config: vite.InlineConfig = {
root: dirname,
configFile: false,
server: {
port: 3333,
},
resolve: {
conditions: ['browser'],
},
plugins: [
{
name: 'replace-version',
enforce: 'pre',
transform(code, id) {
if (id.includes('solid-devtools')) {
code = code.replace(/import\.meta\.env\.EXPECTED_CLIENT/g, main_version)
}
return code
},
},
solid({dev: false, hot: false}),
crx.crx({
manifest: manifest,
browser: browser,
}),
],
define: {
'import.meta.env.BROWSER': JSON.stringify(browser),
},
build: {
modulePreload: false,
minify: false,
target: 'esnext',
emptyOutDir: !is_dev,
outDir: path.join(dist_dirname, browser),
rollupOptions: {
input: {panel: 'src/panel.html'},
},
},
esbuild: {
dropLabels: [is_dev ? 'PROD' : 'DEV'],
},
optimizeDeps: {
exclude: ['@solid-devtools/debugger'],
},
}
if (is_dev) {
let server = await vite.createServer(vite_config)
await server.listen()
server.printUrls()
} else {
await vite.build(vite_config)
}
}