-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesbuild.config.js
More file actions
123 lines (117 loc) · 4.4 KB
/
Copy pathesbuild.config.js
File metadata and controls
123 lines (117 loc) · 4.4 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
const esbuild = require('esbuild');
const { sassPlugin } = require('esbuild-sass-plugin');
const path = require('path');
const fs = require('fs');
const { ManifestJsoncPlugin } = require('./utils/manifest-jsonc-plugin');
const ignorePlugin = require('./utils/ignore-plugin');
const progress = require('esbuild-plugin-progress');
const packageJson = require('../package.json');
// Helper to copy static assets
function copyAssets(src, dest) {
if (!fs.existsSync(src)) return;
fs.readdirSync(src).forEach(file => {
const srcPath = path.join(src, file);
const destPath = path.join(dest, file);
if (fs.statSync(srcPath).isFile()) {
fs.copyFileSync(srcPath, destPath);
}
});
}
// Helper to inject script tag into HTML files
function injectScriptToHtml(srcHtml, destHtml, scriptName, cssName) {
if (!fs.existsSync(srcHtml)) return;
let html = fs.readFileSync(srcHtml, 'utf8');
// Add version comment at the top of the HTML file
const versionComment = `<!-- Version: ${packageJson.version} -->\n`;
html = versionComment + html;
// Insert CSS link before </head> or at start if not found
let cssTag = '';
if (cssName) {
cssTag = `<link rel="stylesheet" href="./${cssName}">`;
if (/<\/head>/i.test(html)) {
html = html.replace(/<\/head>/i, `${cssTag}\n</head>`);
} else {
html = `${cssTag}\n` + html;
}
}
// Insert script before </body> or at end if not found
const scriptTag = `<script type="module" src="./${scriptName}"></script>`;
if (/<\/body>/i.test(html)) {
html = html.replace(/<\/body>/i, `${scriptTag}\n</body>`);
} else {
html += `\n${scriptTag}`;
}
fs.writeFileSync(destHtml, html);
}
// Build entry points
const entryPoints = {
// newtab: 'src/pages/Newtab/index.jsx',
options: 'src/pages/Options/index.jsx',
popup: 'src/pages/Popup/index.jsx',
background: 'src/pages/Background/index.js',
content: 'src/pages/Content/index.js',
devtools: 'src/pages/Devtools/index.js',
panel: 'src/pages/Panel/index.jsx',
'wallet-injection': 'src/lib/browser/wallet-injection.ts',
};
esbuild.build({
entryPoints,
outdir: 'build',
bundle: true,
minify: process.env.NODE_ENV === 'production',
sourcemap: process.env.NODE_ENV !== 'production',
splitting: true,
format: 'esm',
target: ['chrome58', 'firefox57'],
entryNames: '[name].bundle',
loader: {
'.js': 'jsx',
'.jsx': 'jsx',
'.ts': 'ts',
'.tsx': 'tsx',
'.css': 'css',
'.png': 'file',
'.svg': 'file',
'.jpg': 'file',
'.jpeg': 'file',
'.gif': 'file',
'.eot': 'file',
'.otf': 'file',
'.ttf': 'file',
'.woff': 'file',
'.woff2': 'file',
'.html': 'file',
'.xml': 'file',
},
define: {
'process.env.NODE_ENV': `"${process.env.NODE_ENV || 'development'}"`,
'EXTENSION_VERSION': `"${packageJson.version}"`,
},
plugins: [sassPlugin(), progress()],
}).then(() => {
// Copy static assets
copyAssets('src/assets/img', 'build');
copyAssets('src/assets/static', 'build');
copyAssets('src/pages/Content', 'build'); // for content.styles.css
// Inject script tags into HTML files
const htmlScriptMap = [
{ html: 'src/pages/Newtab/index.html', out: 'build/newtab.html', script: 'newtab.bundle.js', css: 'newtab.bundle.css' },
{ html: 'src/pages/Options/index.html', out: 'build/options.html', script: 'options.bundle.js', css: 'options.bundle.css' },
{ html: 'src/pages/Popup/index.html', out: 'build/popup.html', script: 'popup.bundle.js', css: 'popup.bundle.css' },
{ html: 'src/pages/Devtools/index.html', out: 'build/devtools.html', script: 'devtools.bundle.js', css: 'devtools.bundle.css' },
{ html: 'src/pages/Panel/index.html', out: 'build/panel.html', script: 'panel.bundle.js', css: 'panel.bundle.css' },
];
htmlScriptMap.forEach(({ html, out, script, css }) => {
injectScriptToHtml(html, out, script, css);
});
// Copy manifest
if (fs.existsSync('src/manifest.jsonc')) new ManifestJsoncPlugin({
input: 'src/manifest.jsonc',
output: 'build/manifest.json',
packageJson: 'package.json',
}).apply();
//console.log('Build complete!');
}).catch((err) => {
console.error('Build failed:', err);
process.exit(1);
});