forked from WJDDesigns/Ultra-Card
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
193 lines (176 loc) · 6.13 KB
/
webpack.config.js
File metadata and controls
193 lines (176 loc) · 6.13 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
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
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 {
entry: './src/index.ts',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
output: {
filename: `ultra-card.js`,
path: path.resolve(__dirname, 'dist'),
chunkFilename: 'uc-[name].js',
},
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)
{
apply: compiler => {
compiler.hooks.afterEmit.tap('AutoDeployToHA', () => {
const haDeployPath =
process.env.HA_DEPLOY_PATH || '/Volumes/config/www/community/Ultra-Card';
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 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
}
});
}
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`
);
}
});
},
},
],
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,
},
};
};