-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathwebpack.config.js
More file actions
258 lines (238 loc) · 9.11 KB
/
webpack.config.js
File metadata and controls
258 lines (238 loc) · 9.11 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const fs = require('fs');
const webpack = require('webpack');
// Extract version from version.ts file
function getVersion() {
try {
const versionFile = fs.readFileSync(path.resolve(__dirname, 'src/version.ts'), 'utf8');
const versionMatch = versionFile.match(/VERSION\s*=\s*['"]([^'"]+)['"]/);
if (versionMatch && versionMatch[1]) {
return versionMatch[1];
}
} catch (e) {
console.error('Error reading version:', e);
}
return 'unknown';
}
const version = getVersion();
console.log(`Building Ultra Card version: ${version}`);
// Generate the version.js file with the extracted version
function generateVersionJs() {
const content = `/**
* Ultra Card Version
* v${version}
*
* This file is auto-generated from src/version.ts
* DO NOT MODIFY DIRECTLY
*/
let version = "undefined";
function setVersion(value) {
version = value;
}
// Set default version (will be overridden by card)
setVersion('${version}');
export { version, setVersion };`;
fs.writeFileSync(path.resolve(__dirname, 'dist/version.js'), content);
console.log(`Generated version.js with version ${version}`);
}
// Generate the version file before webpack starts
generateVersionJs();
module.exports = (env, argv) => {
const isProduction = argv.mode === 'production';
return {
devtool: isProduction ? 'hidden-source-map' : 'eval-source-map',
entry: {
'ultra-card': './src/index.ts',
'ultra-card-panel': './src/panels/ultra-card-dashboard.ts',
},
module: {
rules: [
{
test: /\.tsx?$/,
use: {
loader: 'ts-loader',
options: { transpileOnly: true },
},
exclude: /node_modules/,
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'dist'),
chunkFilename: 'uc-[name].js',
},
optimization: {
usedExports: true,
},
plugins: [
new CopyWebpackPlugin({
patterns: [
{
from: path.resolve(__dirname, 'src/assets'),
to: path.resolve(__dirname, 'dist/assets'),
noErrorOnMissing: true,
},
{
from: path.resolve(__dirname, 'src/assets'),
to: path.resolve(__dirname, 'assets'),
noErrorOnMissing: true,
},
// Copy individual assets to root for HACS serving
{
from: path.resolve(__dirname, 'src/assets/Ultra.jpg'),
to: path.resolve(__dirname, 'Ultra.jpg'),
noErrorOnMissing: true,
},
],
}),
// Define environment variables
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(isProduction ? 'production' : 'development'),
'process.env.VERSION': JSON.stringify(version),
}),
// Generate a debug info file that contains version info
{
apply: compiler => {
compiler.hooks.afterEmit.tap('GenerateVersionInfo', () => {
// Create a debug info file
const debugContent = `// Ultra Card Debug Info
// Version: ${version}
// Build Date: ${new Date().toISOString()}
// Build Mode: ${isProduction ? 'production' : 'development'}
`;
fs.writeFileSync(path.resolve(__dirname, 'dist/debug-info.js'), debugContent);
console.log(`Created debug info file for version ${version}`);
});
},
},
// Auto-deploy to Home Assistant on build (for development). Skipped when SKIP_HA_DEPLOY=1 (e.g. build:deploy uses deploy.js instead).
{
apply: compiler => {
compiler.hooks.afterEmit.tap('AutoDeployToHA', () => {
if (process.env.SKIP_HA_DEPLOY === '1') {
return;
}
const haDeployPath =
process.env.HA_DEPLOY_PATH || '/Volumes/config/www/community/Ultra-Card';
const panelIntegrationPath =
process.env.HA_PANEL_DEPLOY_PATH ||
'/Volumes/config/custom_components/ultra_card_pro_cloud/www';
const sourceFile = path.resolve(__dirname, 'dist/ultra-card.js');
const targetFile = path.join(haDeployPath, 'ultra-card.js');
// Only deploy if the HA config directory exists (volume is mounted)
if (fs.existsSync(haDeployPath)) {
try {
fs.copyFileSync(sourceFile, targetFile);
// Also copy the license file if it exists
const licenseSource = path.resolve(__dirname, 'dist/ultra-card.js.LICENSE.txt');
if (fs.existsSync(licenseSource)) {
fs.copyFileSync(
licenseSource,
path.join(haDeployPath, 'ultra-card.js.LICENSE.txt')
);
}
// Copy panel bundle for Ultra Card Hub
const panelSource = path.resolve(__dirname, 'dist/ultra-card-panel.js');
if (fs.existsSync(panelSource)) {
fs.copyFileSync(panelSource, path.join(haDeployPath, 'ultra-card-panel.js'));
}
const panelLicense = path.resolve(__dirname, 'dist/ultra-card-panel.js.LICENSE.txt');
if (fs.existsSync(panelLicense)) {
fs.copyFileSync(panelLicense, path.join(haDeployPath, 'ultra-card-panel.js.LICENSE.txt'));
}
// Copy assets folder if it exists
const assetsSource = path.resolve(__dirname, 'dist/assets');
const assetsTarget = path.join(haDeployPath, 'assets');
if (fs.existsSync(assetsSource)) {
if (!fs.existsSync(assetsTarget)) {
fs.mkdirSync(assetsTarget, { recursive: true });
}
const assetFiles = fs.readdirSync(assetsSource);
assetFiles.forEach(file => {
// Skip .DS_Store and other hidden files
if (file.startsWith('.')) return;
try {
fs.copyFileSync(path.join(assetsSource, file), path.join(assetsTarget, file));
} catch (e) {
// Ignore individual file copy errors
}
});
}
// Copy emitted lazy chunks so manifest-first module loaders can resolve in HA.
const distRootFiles = fs.readdirSync(path.resolve(__dirname, 'dist'));
distRootFiles
.filter(
file =>
file.startsWith('uc-') && (file.endsWith('.js') || file.endsWith('.js.LICENSE.txt'))
)
.forEach(file => {
fs.copyFileSync(
path.resolve(__dirname, 'dist', file),
path.join(haDeployPath, file)
);
});
if (fs.existsSync(panelIntegrationPath)) {
if (fs.existsSync(panelSource)) {
fs.copyFileSync(panelSource, path.join(panelIntegrationPath, 'ultra-card-panel.js'));
}
if (fs.existsSync(panelLicense)) {
fs.copyFileSync(
panelLicense,
path.join(panelIntegrationPath, 'ultra-card-panel.js.LICENSE.txt')
);
}
distRootFiles
.filter(
file =>
file.startsWith('uc-') && (file.endsWith('.js') || file.endsWith('.js.LICENSE.txt'))
)
.forEach(file => {
fs.copyFileSync(
path.resolve(__dirname, 'dist', file),
path.join(panelIntegrationPath, file)
);
});
}
console.log(`\x1b[32m✓ Auto-deployed to HA: ${haDeployPath}\x1b[0m`);
console.log(
`\x1b[36m Refresh browser (F5) to see changes - no HA restart needed!\x1b[0m`
);
} catch (err) {
console.log(`\x1b[33m⚠ Could not auto-deploy: ${err.message}\x1b[0m`);
}
} else {
console.log(
`\x1b[90m HA deploy path not found (${haDeployPath}) - skipping auto-deploy\x1b[0m`
);
}
});
},
},
...(process.env.ANALYZE ? [new BundleAnalyzerPlugin({ analyzerMode: 'static', openAnalyzer: false, reportFilename: 'bundle-report.html' })] : []),
],
performance: {
hints: isProduction ? 'warning' : false,
maxAssetSize: 2 * 1024 * 1024, // 2MB - catch bundle regressions
maxEntrypointSize: 2 * 1024 * 1024,
},
devServer: {
static: {
directory: path.join(__dirname, 'dist'),
},
compress: true,
port: 8080,
hot: true,
open: true,
},
};
};