-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbuild.js
More file actions
146 lines (132 loc) · 4.67 KB
/
build.js
File metadata and controls
146 lines (132 loc) · 4.67 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
const esbuild = require("esbuild")
const fs = require("fs-extra")
const path = require("path")
const mustache = require("mustache")
const sveltePlugin = require("esbuild-svelte")
const nodeEnv = process.env.NODE_ENV
const browser = process.argv[3] || "chrome"
const production = nodeEnv === "production"
const outputPath = path.join(__dirname, "public", "manifest.json")
const templatePath = path.join(__dirname, "manifest.template.json")
const browserSpecificSettings = {
chrome: {
use_outline_icons: true,
needs_browser_specific_settings: false,
needs_service_worker: true,
},
firefox: {
use_outline_icons: true,
needs_browser_specific_settings: true,
needs_service_worker: false,
},
safari: {
use_outline_icons: false,
needs_browser_specific_settings: false,
needs_service_worker: false,
},
}
const outputFileNames = {
"content.js": { newName: "hotwire_dev_tools_content.js", moveToDistRoot: true },
"popup.js": { newName: "hotwire_dev_tools_popup.js" },
"inject_script.js": { newName: "hotwire_dev_tools_inject_script.js", moveToDistRoot: true },
}
// Dev-only entry points (excluded from production builds)
const devEntryPoints = ["./src/panel/dev.js"]
const baseEntryPoints = ["./src/page/content.js", "./src/popup.js", "./src/background.js", "./src/page/inject_script.js", "./src/panel/panel.js", "./src/panel/register.js", "./src/page/backend.js", "./src/page/proxy.js"]
const esbuildConfig = {
entryPoints: production ? baseEntryPoints : [...baseEntryPoints, ...devEntryPoints],
bundle: true,
minify: production,
sourcemap: !production && browser !== "safari",
target: ["chrome88", "firefox109", "safari15"],
outdir: "./public/dist",
define: {
"process.env.NODE_ENV": `"${nodeEnv}"`,
__IS_CHROME__: JSON.stringify(browser === "chrome"),
__IS_FIREFOX__: JSON.stringify(browser === "firefox"),
__IS_SAFARI__: JSON.stringify(browser === "safari"),
},
conditions: ["svelte", "browser"],
metafile: true,
alias: {
$src: path.resolve(__dirname, "src"),
$uikit: path.resolve(__dirname, "src/uikit"),
$components: path.resolve(__dirname, "src/panel/components"),
$utils: path.resolve(__dirname, "src/utils"),
$lib: path.resolve(__dirname, "src/lib"),
$panel: path.resolve(__dirname, "src/panel"),
$page: path.resolve(__dirname, "src/page"),
},
plugins: [
sveltePlugin({
compilerOptions: { css: "injected" },
}),
{
name: "rename-output-files",
setup(build) {
build.onEnd(async (result) => {
if (result.metafile) {
const outputFiles = Object.keys(result.metafile.outputs)
for (const outputFile of outputFiles) {
const originalName = path.basename(outputFile)
const config = outputFileNames[originalName]
if (config) {
const newName = typeof config === "string" ? config : config.newName
const moveToDistRoot = typeof config === "object" && config.moveToDistRoot
const targetDir = moveToDistRoot ? "./public/dist" : path.dirname(outputFile)
const newPath = path.join(targetDir, newName)
await fs.rename(outputFile, newPath)
console.log(`Renamed ${originalName} to ${newName}`)
}
}
}
})
},
},
],
}
async function generateManifest() {
try {
const template = await fs.readFile(templatePath, "utf8")
const output = mustache.render(template, browserSpecificSettings[browser])
await fs.writeFile(outputPath, output)
console.log(`Generated manifest.json for ${browser}`)
} catch (err) {
console.error("Error generating manifest file:", err)
}
}
async function cleanupDevFiles() {
const devFiles = [
path.join(__dirname, "public", "dev.html"),
path.join(__dirname, "public", "dist", "panel", "dev.js"),
path.join(__dirname, "public", "dist", "panel", "dev.js.map"),
path.join(__dirname, "public", "dist", "panel", "dev.css"),
path.join(__dirname, "public", "dist", "panel", "dev.css.map"),
]
for (const file of devFiles) {
try {
if (await fs.pathExists(file)) {
await fs.remove(file)
console.log(`Removed dev file: ${path.basename(file)}`)
}
} catch (err) {
// Ignore errors if file doesn't exist
}
}
}
const buildAndWatch = async () => {
const context = await esbuild.context({ ...esbuildConfig, logLevel: "info" })
context.watch()
}
async function buildProject() {
await generateManifest()
if (process.argv.includes("--watch")) {
buildAndWatch()
} else {
await esbuild.build(esbuildConfig)
if (production) {
await cleanupDevFiles()
}
}
}
buildProject()